Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles.

Slides:



Advertisements
Similar presentations
Greedy Algorithms Pasi Fränti Greedy algorithm 1.Coin problem 2.Minimum spanning tree 3.Generalized knapsack problem 4.Traveling salesman problem.
Advertisements

1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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 Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
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.
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
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.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
EMIS 8374 Optimal Trees updated 25 April slide 1 Minimum Spanning Tree (MST) Input –A (simple) graph G = (V,E) –Edge cost c ij for each edge e 
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Minimum weight spanning trees
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Introduction to Graph Theory
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
MST – KRUSKAL UNIT IV. Disjoint-Set Union Problem Want a data structure to support disjoint sets – Collection of disjoint sets S = {S i }, S i ∩ S j =
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Minimum- Spanning Trees
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),
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Introduction to Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Lecture 22 Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Data Structures & Algorithms Graphs
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
Minimum Spanning Tree.
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Algorithms and Data Structures Lecture XII
Minimum Spanning Trees
Minimum-Cost Spanning Tree
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
Kruskal’s Minimum Spanning Tree Algorithm
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles

Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles no cycle  vertex of degree  1 |V|-1 edges  vertex of degree  1 connected  no vertex of degree 0 (unless |V|=1)

no cycle  vertex of degree  1 Supose not, i.e., all vertices of degree  2, yet no cycle. Let v 1,...,v t be the longest path v t has 2-neighbors, one different from v t-1. Why cannot take v 1,...,v t,u ? Cycle. Contradiction.

|V|-1 edges  vertex of degree  1 Suppose all degrees  2. Then |E|=(1/2)  deg(v)  |V| Contradiction. Done. vVvV

connected  no vertex of degree 0 (unless |V|=1)

Tree connected graph with no cycle connected graph with |V|-1 edges graph with |V|-1 edges and no cycles no cycle  vertex of degree  1 |V|-1 edges  vertex of degree  1 connected  no vertex of degree 0 (unless |V|=1) 

connected graph with no cycle connected graph with |V|-1 edges  Induction on |V|. Base case |V|=1. Let G be connected with no cycles. Then G has vertex v of degree 1. Let G’ = G – v. Then G’ is connected and has no cycle. By IH G’ has |V|-2 edges and hence G has |V|-1 edges. 

connected graph with no cycle connected graph with |V|-1 edges  Induction on |V|. Base case |V|=1. Let G be connected |V|-1 edges. Then G has vertex v of degree 1. Let G’ = G – v. Then G’ is connected and has |V’|-1 edges. By IH G’ has no cycle. Hence G has no cycle. 

connected graph with no cycle connected graph with |V|-1 edges graph with |V|-1 edges and no cycles  Assume |V|-1 edges and no cycles. Let |G 1 |,...,|G k | be the connected components. Then |E_i| = |V_i| - 1, hence |E| = |V| - k. Thus k = 1.

Spanning trees

How many spanning trees ?

Spanning trees How many spanning trees ? 4

Spanning trees How many spanning trees ?

Spanning trees How many spanning trees ? 8

Minimum weight spanning trees

Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle

Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle K = MST output by Kruskal T = optimal MST Is K = T ?

Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle K = MST output by Kruskal T = optimal MST Is K = T ? no – e.g. if all edge-weights are the same  many optima

Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle K = MST output by Kruskal T = optimal MST Assume all edge weights are different. Then K=T. (In particular, unique optimum)

Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle G connected, e in a cycle  G-e connected K = MST output by Kruskal T = optimal MST

Kruskal’s algorithm e the edge of the smallest weight in K-T. Consider T+e. G connected, e in a cycle  G-e connected K = MST output by Kruskal T = optimal MST

Kruskal’s algorithm e the edge of the smallest weight in K-T. Consider T+e. Case 1: all edgeweights in C smaller that w e Case 2: one edgeweight in C larger that w e

Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle Need to maintain components. Find-Set(u) Union(u,v)

Union-Find S[1..n] for i  1 to n do S[i]  i Find-Set(u) return S[u] Union(u,v) a  S[u] for i  1 to n do if S[i]=a then S[i]  S[v]

Union-Find S[1..n] for i  1 to n do S[i]  i Find-Set(u) return S[u] Union(u,v) a  S[u] for i  1 to n do if S[i]=a then S[i]  S[v] O(1) O(n)

Kruskal’s algorithm Find-Set(u) Union(u,v) O(1) O(n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle

Kruskal’s algorithm Find-Set(u) Union(u,v) O(1) O(n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle O(E log E + V^2)

Union-Find 2 S[1..n] for i  1 to n do S[i]  i Find-Set(u) while (S[u]  u) do u  S[u] S[u] Union(u,v) S[Find-Set(u)]  Find-Set(v) n=|V| u v

Union-Find 2 S[1..n] for i  1 to n do S[i]  i Find-Set(u) while (S[u]  u) do u  S[u] S[u] Union(u,v) S[Find-Set(u)]  Find-Set(v) n=|V| u v

Union-Find 2 S[1..n] for i  1 to n do S[i]  i Find-Set(u) while (S[u]  u) do u  S[u] S[u] Union(u,v) S[Find-Set(u)]  Find-Set(v) n=|V| O(n)

Union-Find 2 S[1..n] for i  1 to n do S[i]  i Find-Set(u) while (S[u]  u) do u  S[u] S[u] Union(u,v) u  Find-Set(u); v  Find-Set(v); if D[u]<D[v] then S[u]  v if D[u]>D[v] then S[v]  u if D[u]=D[v] then S[u]  v; D[v]++ n=|V| O(log n)

Kruskal’s algorithm Find-Set(u) Union(u,v) O(log n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle

Kruskal’s algorithm Find-Set(u) Union(u,v) O(log n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle O(E log E + (E+V)log V)

Kruskal’s algorithm log E  log V 2 = 2 log V = O(log V) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle O(E log E + (E+V)log V) O((E+V) log V)

Minimum weight spanning trees

Prim’s algorithm 1) S  {1} 2) add the cheapest edge {u,v} such that u  S and v  S C S  S  {v} (until S=V)

Minimum weight spanning trees 1) S  {1} 2) add the cheapest edge {u,v} such that u  S and v  S C S  S  {v} (until S=V) P = MST output by Prim T = optimal MST Is P = T ? assume all the edgeweights different

Minimum weight spanning trees P = MST output by Prim T = optimal MST P = T assuming all the edgeweights different v 1,v 2,...,v n order added to S by Prim smallest i such that an edge e  E connecting S={v 1,...,v i } to S C different in T than in Prim (f)

Minimum weight spanning trees smallest i such that an edge e  E connecting S={v 1,...,v i } to S C different in T than in Prim (f) e SCSC S T

Minimum weight spanning trees smallest i such that an edge e  E connecting S={v 1,...,v i } to S C different in T than in Prim (f) e f SCSC S T+f

Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C and C[j] is minimal S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j

Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C and C[j] is minimal S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j O(n)

Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C and C[j] is minimal S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j O(n) O(V 2 + E) = O(V 2 )

Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C and C[j] is minimal S  S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]  w[{j,u}]; P[u]  j O(log n) O((E+V)log V) using heap

Minimum weight spanning trees Kruskal O( (E+V) log V) Prim O( (E+V) log V)

Minimum weight spanning trees Kruskal O( (E+V) log V) Prim O( (E+V) log V) can be made O(E + V log V) using Fibonacci heaps if edges already sorted then O(E log * V)