Download presentation
Presentation is loading. Please wait.
Published byEzra Carpenter Modified over 9 years ago
1
Nattee Niparnan
2
Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long run result
3
Example of Greedy Algorithm Rational Knapsack Step You have to select items to be put into your sack Greedy Simply pick the one having highest price/weight ratio We know that this approach does not work in the case of 0-1 knapsack
5
Minimal Spanning Tree Given an undirected connected graph With weighted edges Find a subgraph of that graph That is connected Having smallest sum of edges’ weights
6
Example graph MST
7
Observation MST should not have a cycle Why? Removing edge in a cycle does not destroy the connectivity problem So why bother having an edge in a cycle in the MST
8
Property of Trees A tree with N nodes has N – 1 edges A connected graph having |V| - 1 = |E| is a tree
9
MST Problem Input An undirected connected graph Weighted edges Output A set of edges that constitute the MST of the given graph (notice that all nodes must be in the tree, so we don’t need to select them)
10
Kruskal’s Algorithm Idea We need |V|- 1 edges Simply pick them At each step, just select one edge having smallest value That does not cause a cycle
11
Example ACE BDF
12
ACE BDF 1
13
ACE BDF 1 1
14
ACE BDF 1 1 1
15
ACE BDF 1 1 1 2
16
ACE BDF 1 1 1 2 3
17
Cut Property Why Kruskal’s works? It is because of the “cut property” Suppose a set of edges X are part of a MST of G = (V,E), pick any subset of nodes S for which X does not cross between S and V – S, and let e be the lightest edge across this partition. Then X U {e} is part of some MST
19
Cut Property
20
Implementing Kruskal’s Need something to check the connected component of the graph We could do CC problem But that takes too much time Try some data structure that represent “disjoint set” It should be able to makeset(x)create a set containing just x find(x)find a set where x is a member union(x,y)union a set find(x) and a set find(y)
21
Implementing Kruskal’s procedure kruskal(G;w) //Input: A connected undirected graph G = (V,E) with edge weights w e //Output: A minimum spanning tree defined by the edges X for all u V : makeset(u) X = {} Sort the edges E by weight for all edges (u,v) E, in increasing order of weight: if find(u) != find(v): add edge (u,v) to X union(u, v)
22
Analysis What is O of kruskal? It sorts the edges It needs |V| makeset 2|E| find |V| - 1 union What is the eventual complexity?
23
Prim’s Algorithm Instead of selecting the “minimal” edge of all edges that does not create a cycle Select the “minimal” edge of all edges that connects to the original graph
24
Prim’s Implementation procedure kruskal(G;w) //Input: A connected undirected graph G = (V,E) with edge weights w e //Output: A minimum spanning tree defined by the array prev for all u V : cost(u) = 1 prev(u) = nil Pick any initial node u 0 cost(u 0 ) = 0 H = makequeue(V ) (priority queue, using cost-values as keys) while H is not empty: v = deletemin(H) for each (v,z) E: if cost(z) > w(v,z): cost(z) = w(v,z) prev(z) = v
25
Example ACE BDF
26
ACE BDF 1
27
ACE BDF 1 2
28
ACE BDF 1 1 2
29
ACE BDF 1 1 2 3
30
ACE BDF 1 1 1 2 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.