Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greed is good. (Some of the time)
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
© 2004 Goodrich, Tamassia Minimum Spanning Trees1 Minimum Spanning Trees (§ 13.7) Spanning subgraph Subgraph of a graph G containing all the vertices of.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Design and Analysis of Algorithms Minimum Spanning trees
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Greedy Algorithms Make choice based on immediate rewards rather than looking ahead to see the optimum In many cases this is effective as the look ahead.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Union-find Algorithm Presented by Michael Cassarino.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Minimum Spanning Trees Problem Description Why Compute MST? MST in Unweighted Graphs MST in Weighted Graphs –Kruskal’s Algorithm –Prim’s Algorithm 1.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Graph Search Applications, Minimum Spanning Tree
Greedy Algorithms General principle of greedy algorithm
Introduction to Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Lecture 22 Minimum Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Three Graph Algorithms
Short paths and spanning trees
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
Minimum Spanning Trees
Spanning Trees.
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Tree.
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

Nattee Niparnan

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

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

Minimal Spanning Tree Given an undirected connected graph With weighted edges Find a subgraph of that graph connected Having smallest sum of edges’ weights

Example graph MST

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

Property of Trees A tree with N nodes has N – 1 edges A connected graph having |V| - 1 = |E| is a tree

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)

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

Example ACE BDF

ACE BDF 1

ACE BDF 1 1

ACE BDF 1 1 1

ACE BDF

ACE BDF

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

Cut Property

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)

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)

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

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)

Store Set as a Tree A set of {1, 8, 7, 3}

Store Set as a Tree A set of {1, 8, 7, 3}

Data Array for parent P 2

Data Array for parent P 0 union(6,0) 2

Data Array for parent P 0 union(4,6) 2

Data Array for parent P 0 union(6,1) 2

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; }

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 }

Analysis find(x) O(N) union(x,y)O(N) Can we do better? Keep each tree short

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

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]

Example ACE BDF

ACE BDF 1

ACE BDF 1 2

ACE BDF 1 1 2

ACE BDF

ACE BDF