Professional Documents
Culture Documents
Greedy Strategy: Algorithm: Design & Analysis
Greedy Strategy: Algorithm: Design & Analysis
Articulation Points and Biconnectedness Biconnected Component Algorithm Analysis of the Algorithm
Greedy Strategy
Greedy Strategy
Optimizing by Greedy
[candidates] A finite set of coins, of 1, 5, 10 and 25 units, with enough number for each value [constraints] Pay an exact amount by a selected set of coins [optimization] a smallest possible number of coins in the selected set For each selection, choose the highest-valued coin as possible.
If the available coins are of 1,5,12 units, and we have to pay 15 units totally, then the smallest set of coins is {5,5,5}, but not {12,1,1,1} However, the correctness of greedy strategy on the case of {1,5,10,25} is not straightly seen.
Greedy Strategy
Constructing the final setgreedy( greedy( setcandidate) candidate) set set solution by expanding the setS=; S=; set partial solution step by step, n o whilef not solution (S)and and f in each of which a selection while not solution (S) ocandidate n o g is made from a set of i candidate n t i a z candidates, with the choice ad select i locally locally optimizingxx r select optimizing m t i made must be: : y t t y from candidate; p i from candidate; e l o i [feasible] it has to satisfy K the l b i a s candidate=candidate-{ x }; c problems constraints candidate=candidate-{ x }; a o e l f [locally optimal] it has to be iffeasible feasible ( x )then thenS=S S=S { x }; if ( x ) { x }; d n the best local choice among all a ifsolution solution (S)then thenreturn returnS S feasible choices on the step if (S) [irrevocable] the candidate elsereturn return(no (nosolution) solution) else
selected can never be deselected on subsequent steps
34 25 33 18 I 21 H J 28 17 53 G B 27 21 36 26 C
A weighted graph
The nearest neighbor of vertex I is H The nearest neighbor of shaded subset of vertex is G
D 22 33 29 25 16 I 21 18 21 F 25 J 28 17 53 G
1 1
1 1
DFS tree
Prims algorithm:
Difficult selecting: best local optimization means no cycle and small weight under limitation. Easy checking: doing nothing
Kruskals algorithm:
c (0)
c (1)
c (2)
c (3)
0.Let Letaabe bethe thestarting startingvertex, vertex,selecting selectingedges edgesone oneby byone onein inoriginal originalgraph graph 0. 1.Merging Mergingaaand andccinto intoa({a,c}), a({a,c}),selecting selecting(a,c) (a,c) 1. 2.Merging Merginga aand andbbinto intoa({a,c,b}), a({a,c,b}),selecting selecting(c,b) (c,b) 2. 3.Merging Merginga aand andd dinto intoa({a,c,b,d}), a({a,c,b,d}),selecting selecting(a,d) (a,d)or or(d,b) (d,b) 3. Ending,as asonly onlyone onevertex vertexleft left Ending,
B
4 3
G I
2 4
F
6
H
8
2 2
Greedy Greedystrategy: strategy: For Foreach eachset setof offringe fringe vertex, vertex,select selectthe theedge edge with withthe theminimal minimal weight, weight,that thatis, is,local local optimal. optimal.
A spanning tree T of a connected, weighted graph has MST property if and only if for any non-tree edge uv, T{uv} contain a cycle in which uv is one of the maximum-weight edge. All the spanning trees having MST property have the same weight.
uv-path in T1
u
Must have same weight
edge exchange
wi wi+1 not in T2
wi
wi+1
edge uv in T2 but not in T1 , with minimum weight among all different edges
a new spanning tree: same weight as T1, less different edges from that of T2
In a connected, weighted graph G=(V,E,W), a tree T is a minimum spanning tree if and only if T has the MST property. Proof
For a minimum spanning tree T, if it doesnt has MST property. So, there is a non-tree edge uv, and T{uv} contain an edge xy with weight larger than that of uv. Substituting uv for xy results a spanning tree with less weight than T. Contradiction. As claimed above, any minimum spanning tree has the MST property. Since T has MST property, it has the same weight as any minimum spanning tree, i.e. T is a minimum spanning tree as well.
Let Tk be the tree constructed after the kth step of Prims algorithm is executed, then Tk has the MST property in Gk, the subgraph of G induced by vertices of Tk. wa+1 wb-1 assumed first and Tk-1 wa u1(w1)
edge added in Tk
wb ui(wp)
Note: w(uiv)w(u1v), and if wa added earlier than wb, then wawa+1 and wb-1wb added later than any edges in u1wa-path, and v as well
v,
Create the set and update it after each vertex is selected (deleting the vertex having been selected and inserting new fringe vertices) Easy to decide the vertex with highest priority Changing the priority of the vertices (decreasing key).
B
4
F
6
I
2
4
1
H
8
2 2
Greedy Greedystrategy: strategy: For Foreach eachset setof offringe fringe vertex, vertex,select selectthe theedge edge with withthe theminimal minimal weight, weight,that thatis, is,local local optimal. optimal.
So,
Implementing priority queue using heap, we can get (n2+m)
B
4 3
G I
2 4
F
6
H
8
2 2
Also AlsoGreedy Greedystrategy: strategy: From Fromthe theset setof ofedges edges not notyet yetincluded includedin inthe the partially partiallybuilt builtMST, MST, select selectthe theedge edgewith with the theminimal minimalweight, weight, that thatis, is,local localoptimal, optimal, in inanother anothersense. sense.
How to know an insertion of edge will result in a cycle efficiently? For correctness: the two endpoints of the selected edge can not be in the same connected components. For the efficiency: connected components are implemented as dynamic equivalence classes using union-find.
F=; while (isEmpty(pq) == false) vwEdge = getMin(pq); deleteMin(pq); int vSet = find(sets, vwEdge.from); int wSet = find(sets, vwEdge.to); if (vSet wSet) Add vwEdge to F; union(sets, vSet, wSet) return
For a correct MST, each edge in the graph should be examined at least once. So, the lower bound is (m)
Note: Note: Theshortest shortest[0, [0,3]3]The pathdoesnt doesntcontain contain path theshortest shortestedge edge the leavings, s,the theedge edge leaving [0,1] [0,1]
2
3 5 1
8 6
s 0
4 4
7 3
6 7
4
4 3
6
Home Assignment
pp.416-: