The greedy method is a strategy to solve some optimization problems

advertisement
The greedy method is a strategy to solve some optimization problems. Let us suppose
that we can solve a problem by a sequence of decisions. The greedy method employs
the following approach: In each stage, our decision is a locally optimal one. For some
problem, as we shall see, these locally optimal solutions will finally add up to a
globally optimal solution.
Let us now describe the spirit of greedy method by an example. Consider the case
where we are given a set of n numbers and we are asked to pick out k numbers such
that the sum of these k numbers is the largest, among all possible ways of picking out
these k numbers.
To solve this problem, one may test all possible ways of picking k numbers out of
these n numbers. That is , of course, a foolish way of solving this problem because we
may simply pick out the k largest numbers and these k largest numbers must
constitute our solution. Or, we may say that our algorithm to solve this problem is as
follow:
For i=1 to k do
Pick out the largest number and delete this number from the input.
Endfor
The above algorithm is a typical greedy method. At each stage, the largest number is
selected.
The Kruskal’s Method to Find a Minimal S panning Tree
One of the famous problems which can be solved by the greedy method is the
minimal spanning tree problem. Minimal spanning trees can be defined on either or
on a graph. For Kruskal’s method, minimal spanning trees are defined on graphs.
Definition: Let G=(V,E) be a weighted connected undirected graph where V represent
the set of vertices and E represents the set of edges. A spanning tree of G is an
undirected tree S =(V,T) where T is a subset of E. The total weight of a spanning tree
is the sum of all weights of T. A minimal spanning tree of G is a spanning tree of G
with the smallest total weight.
Kruskal’s Minimal Spanning Trees Algorithm
Input: A weighted, connected and undirected graph G=(V,E).
Output:A minimal spanning tree for G.
T=Ø
While T contains less than n-1 edges do
Begin
Choose an edge (v,w) from E of the smallest weight
Delete (v,w) from E
If (the adding of (v,w) to T does not create a cycle in T) then
Add (v,w) to T
Else
Discard (v,w)
End
Kruskal’s method to construct a minimal spanning tree can be briefly described as
follows:
(1)Select the edge with the smallest weight edge from the set of edges.
This constitutes the initial partially constructed subgraph which will later be
develoved into a minimal spanning tree.
(2)Add the next smallest weight edge to this partially construct graph if this will
not cause a cycle to be found. Discard the selected edge if otherwise.
(3)Terminate if the spanning tree contains n-1 edges. Otherwise, Go to (2).
Next, we shall introduce an algorithm independently discovered by Dijkstra and
Prim. Prim’s algorithm builds a minimal spanning tree step by step. At any moment,
let X denote the set of vertices contained in the partially constructed minimal
spanning tree. Let Y=V-X. The next edge (u,v) to be added is an edge between X and
Y (u∈X and v∈Y) with the smallest weight. The next edge added will be (u,v) and
after this edge is added, v will be added to X and deleted from Y. An important in
Prim’s method is that we can start with any vertex, which is quite convenient.
The Basic Prim’s Algorithm to Find a Minimal Spanning Tree
Input: A weighted, connected and undirected graph G=(V,E).
Output: A minimal spanning tree.
Step 1. Let x be any vertex in V. Let X={x} and Y=V-{x}.
Step 2. Select an edge (u,v) from E such that u∈X,v∈Y and (u,v) has the
smallest weight among edges between X and Y.
Step 3. Connect u to v. Let X=X∪{v} and Y=Y-{v}.
Step 4. If Y=Ø, terminate and the resulting tree is a minimal spanning tree. Otherwise,
Go to Step 2.
Introduction to the Design and Analysis of Algorithms-------by R. C. T. Lee et al.
Download