Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Greedy Strategy

Algorithm : Design & Analysis [14]

In the last class


Undirected and Symmetric Digraph UDF Search Skeleton Biconnected Components


Articulation Points and Biconnectedness Biconnected Component Algorithm Analysis of the Algorithm

Greedy Strategy

Optimization Problem MST Problem


Prims Algorithm Kruskals Algorithm Dijstras Algorithm

Single-Source Shortest Path Problem

Greedy Strategy

Optimizing by Greedy

Coin Change Problem

[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.

Solution by greedy strategy

Greedy Fails Sometimes

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

Weighted Graph and MST


B 27 A B 27 A 34 H 25 18 17 G I 21 26 C 36 33 21 28 J 53 29 42 D 21 25 22 E 16 F A 34 H 21 36 26 C 42 29 D 21 25 E 22 16 F

ASpanning SpanningTree: Tree:W W (T )=257 A (T )=257

34 25 33 18 I 21 H J 28 17 53 G B 27 21 36 26 C

AMST: MST:W W (T )=190 A (T )=190


42 E

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

Graph Traversal and MST


There are cases that graph traversal tree cannot be minimum spanning tree, with the vertices explored in any order.
All other edges with weight 5

1 1

1 1

DFS tree

BFS tree in any ordering of vertex

Greedy Algorithms for MST

Prims algorithm:

Difficult selecting: best local optimization means no cycle and small weight under limitation. Easy checking: doing nothing

Kruskals algorithm:

Easy selecting: smallest in primitive meaning Difficult checking: no cycle

Merging Two Vertices


v0 v1 v5 v4 v6 v3 v3 v4 v6 v3 v2 v5 v4 v6 v0 v2 v5 v0

Constructing a Spanning Tree


a b a b a b a b

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,

Prims Algorithm for MST


A
7 3 1 2 6

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.

edges included in the MST

Minimum Spanning Tree Property

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

MST Property and Minimum Spanning Tree

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.

Correctness of Prims Algorithm

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)

last edges with larger weight than w(uiv), resulting contradictions.

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,

added in Tk to form a cycle, added in Tk only these need be considered

Key Issue in Implementation

Maintaining the set of fringe vertices

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).

The choice: priority queue

Implementing Prims Algorithm


Main Procedure
primMST(G,n) Initialize the priority queue pq as empty; Select vertex s to start the tree; Set its candidate edge to (-1,s,0); Updating the Queue insert(pq,s,0); updateFringe(pq,G,v) while (pq is not empty) For all vertices w adjcent to v //2m loops v=getMin(pq); deleteMin(pq); newWgt=w(v,w); add the candidate edge of v to the tree; if w.status is unseen then updateFringe(pq,G,v); Set its candidate edge to (v,w,newWgt); return insert(pq,w,newWgt) else getMin(pq) always be the if newWgt<getPriorty(pq,w) vertex with the smallest Revise its candidate edge to (v,w,newWgt); key in the fringe set. decreaseKey(pq,w,newWgt) return

ADT operation executions: insert, getMin, deleteMin: n times decreaseKey: m times

Prims Algorithm for MST


A
7 2 3 1 6 G 3

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.

edges included in the MST

Complexity of Prims Algorithm

Operations on ADT priority queue: (for a graph with n vertices


and m edges)

insert: n getMin: n deleteMin: n decreasKey: m (appears in 2m loops, but execute at most m)


T(n,m) = O(nT(getMin)+nT(deleteMin+insert)+mT(decreaseKey))

So,
Implementing priority queue using heap, we can get (n2+m)

Kruskals Algorithm for MST


A
7 3 1 2 6

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.

edges included in the MST

Key Issue in Implementation

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.

Kruskals Algorithm: the Procedure


kruskalMST(G,n,F) //outline int count;


Build a minimizing priority queue, pq, of edges of G, prioritized by weight. Initialize a Union-Find structure, sets, in which each vertex of G is in its own set.

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

Simply Simplysorting, sorting,the the cost (( m log m )) costwill willbe be m log m

Prim vs. Kruskal

Lower bound for MST

For a correct MST, each edge in the graph should be examined at least once. So, the lower bound is (m)

(n2+m) and (mlogm), which is better?

Generally speaking, depends on the density of edge of the graph.

Single Source Shortest Paths


The single source Red labels on each vertex is the length of the shortest path from s to the vertex.
1 7 6 2 8 1 s 0 4 4 4 5 2 7 3 2 5 6 3 4 9 6 2 3 1 3

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]

Dijstras Algorithm: an Example


1
7 1 2 8 2

2
3 5 1

8 6

s 0
4 4

7 3

6 7
4

4 3
6

Home Assignment

pp.416-:

8.7-8.9 8.14-15 8.25

You might also like