Weighted Graphs • Weighted graphs: graphs for which each edge has an Minimum Spanning Trees associated weight w(u, v), a real number value CISC4080, Computer Algorithms w: E → R, weight function CIS, Fordham Univ. • Graph representation • Adjacency list: store w(u,v) along with vertex v in u’s adjacency list • Adjacency matrix: store w(u, v) at location (u, v) in the matrix (instead of 1) Instructor: X. Zhang 2 Spanning Tree Cost of Spanning Tree • cost of a spanning tree T : sum of edge weights for all edges in • Given a connected, undirected, weighted graph G = ( V, E ) , a spanning tree spanning tree is a subgraph G ′ = ( V , E ′ ) with E ′ ⊆ E that is w ( T ) = ∑ ( u,v ) ∈ T w ( u,v ) acyclic and connects all vertices together. w(T 1 )=1+3+4+5+6=19 • E’ includes edges G=(V,E) highlighted (by wider lines) • How many w(T 2 )=1+2+4+5+4=16 different spanning trees are there for G? 4 3
Problem: Finding MST Cost of Spanning Tree • Given: Connected, undirected, weighted graph, G • A minimum spanning tree (MST) is a spanning tree of minimum • Find: Minimum spanning tree, T cost. • For a given G, there might be multiple MSTs (all with same cost) Acyclic subset of edges( E ) that G connects all vertices of G . Such problems are optimization problems : there are multiple viable solutions (here, spanning trees), we want to find best (lowest This spanning tree has cost lower than or cost, best perf) one. equal to any other spanning tree of G. Applications: w(T 2 )=1+2+4+5+4=16 – Communication networks 5 6 – Circuit design • Can you come up another MST for G? – Layout of highway systems MST: Greedy Algorithms Pick edge with lowest cost • A problem solving strategy • other strategies: brute force, divide and conquer, Step 2 randomization, dynamic programming, … Step 1 • Greedy algorithms/strategies for solving optimization problem • build up a solution step by step Step 3.2 Step 3.1 • (local optimization) in each step always choose the option that offers best immediate benefits (a myopic approach) • not worrying about global optimization… Add E A,B Adding e A,D will introduce cycle, • Performance of greedy algorithms: so go to next one… • Sometimes yield optimal solution, sometimes yield Step 5 suboptimal (i.e., not optimal) Step 4 • Sometimes: we can bound difference from optimal… 7 Add E A,B 8 Add E A,B
Kruskal’s Algorithm Minimum Spanning Trees // G=(V,E) is connected, undirected, weighted // return Te: a subset of E that forms MST MST (V,E): N = |V| // # of nodes Te={ } // set of tree edge Order E by weights //spanning tree needs N-1 edges for i=1 to N-1 find next lightest edge, e (next element in E) if adding e into the tree does not introduce cycle Implementation detail: add e into Te * Maintain sets of nodes that are connected by tree edges return Te * find(u): return the set that u belongs to * find(u)=find(v) means u, v belongs to same group (i.e., u and v => This is Kruskal’s algorithm are already connected) 9 Prim algorithm for MST Kruskal algorithm for MST / / G=(V,E) is connected, undirected, weighted // G=(V,E) is connected, undirected, weighted // return E t : set of tree edges in MST // return Te: a subset of E that forms MST MST_Prim (V,E): 17. Repeat for N times 1. N = |V| // # of nodes 18. //add lowest cost node to tree MST (V,E): 2. Q = empty priority queue of nodes 19. u = Q.dequeue() N = |V| // # of nodes 3. 20. add u to V t 4. //start from a random node s Te={ } // set of tree edge 21. add edge (prev[u], u) into E t 5. s a node from V ← 22. 6. cost[s] = 0; Q.enqueue (s) Order E by weights 23. //update all other nodes cost 7. V t = {s} //spanning tree needs N-1 edges 24. for each node v in V-V t 8. for i=1 to N-1 25. if w(u,v) < cost[v] 9. //initialize all other nodes find next lightest edge, e (next element in E) 26. cost[v] = w(u,v) 10. for each none u in V-V t , 27. prev[v] = u if adding e into the tree does not introduce cycle 11. cost[u] = w(u,s) if (u,s) E ∈ 28. end of Repeat loop add e into Te 12. = ∞ if (u,s) ∉ E 29. return Te 30. return E t 13. prev[u] = s if (u,s) ∈ E 14. Q. enqueue (u) Add one edge in each step: need to check for cycle 15. Prim algorithm: grow the tree from one node, in each step, 16. 11 12 connect one more node into the tree.
Prim Algorithm for MST: Prim Algorithm for MST: start from a node Connect a node with lowest cost Step 2 to N: grow the tree to Step 1: pick node D (an arbitrary node) u = node with lowest cost to connect to connect a node with lowest cost tree cost[u]: current cost of connect u to the tree add edge (pred(u), u) into tree prev[u]: connect to which node in the tree //update cost and prev // init cost, pred all each none tree node v: for each none tree node u, if w(v,u)<cost[v] cost[u] = w(u,D) if (u,D) is an edge cost[v]=w(v,u) = inf if (u,D) is not an edge prev[v]=u cost=2 cost=1 cost=2 cost=2 cost=3 cost=inf prev=D cost=inf prev=C prev=D cost=3 prev=D cost=inf prev=D prev=D cost=4 cost=6 cost=4 cost=4 cost=6 prev=D cost=4 prev=D prev=C prev=D prev=D prev=D Currently, min cost to connect F 13 Currently, min cost to connect F 14 Currently, min cost to connect F to the tree is 6, via connecting to D to the tree is 4, via connecting to C to the tree is 6, via connecting to D Prim Algorithm: Summary Implementation Step 2 to N: grow the tree to connect a node with lowest cost • Minimal Spanning Tree Problem u <- the node with lowest cost • application: finding the lowest cost way to connect a set of Discussion: add edge (pred(u), u) into tree nodes //update cost and prev How to quickly find the node all each none tree node v: • an example of optimization problem if w(v,u)<cost[v] with lowest cost? • Greedy algorithm cost[v]=w(v,u) prev[v]=u Recall the cost of nodes will be • myopic, choose what’s looks to be the best option at current cost=1 cost=2 updated … cost=inf step. prev=C prev=D • sometimes lead to optimal solution, sometimes not Priority queue implemented • Two MST algorithms using heap cost=4 • Kruscal: grow the tree by adding one edge a time (choose the prev=D cost=4 edge with lowest weight and does not introduce cycle) prev=C • Prim: grow the tree by adding one node a time, choosing the Add A and (A,C) into the tree, no 15 updates … node with lowest cost to connect to current (sub)-tree 16
Recommend
More recommend