Download presentation
Presentation is loading. Please wait.
Published byAndrew Bridges Modified over 8 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 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 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 weighted graph 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? Depends on implementation of the set
23
Disjoint Set Data Structure There are N elements Each must be in exactly one set We can union two sets Operation makeset(x)create a set containing just x find(x)find a set where x is a member (return int) If x and y are in the same set, find(x) must equal to find(y) union(x,y)union a set find(x) and a set find(y)
24
Store Set as a Tree A set of {1, 8, 7, 3} 1 8 7 3
25
Store Set as a Tree A set of {1, 8, 7, 3} 1 8 73
26
Data Array for parent 0127456119 1 8 7 3 4 5 6 9 0 P 2
27
Data Array for parent 6127456119 1 8 7 3 4 5 6 9 P 0 union(6,0) 2
28
Data Array for parent 6127454119 1 8 7 3 4 5 6 9 P 0 union(4,6) 2
29
Data Array for parent 6127154119 1 8 7 3 45 6 9 P 0 union(6,1) 2
30
findset(x) int findset(P, x) // Input: P = parent array, x item to find // Output: the index of set of x { while(p[x] != x) x = p[x]; return x; }
31
union(x,y) int findset(P, x, y) // Input: P = parent array, x,y element of // set to be unioned { s1 = find(x) s2 = find(y) p[s1] = s2 }
32
Analysis find(x) O(N) union(x,y)O(N) Can we do better? Keep each tree short
33
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
34
Prim’s Implementation procedure prim(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 v V : cost[v] = 1 prev[v] = 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,u) E if cost[u] > w[v,u] cost[u] = w[v,u] prev[u] = v H.decreasekey(u,cost[u]) // change the value of u to cost[u]
35
Example ACE BDF
36
ACE BDF 1
37
ACE BDF 1 2
38
ACE BDF 1 1 2
39
ACE BDF 1 1 2 3
40
ACE BDF 1 1 1 2 3
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.