November 22, 20011 Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University

Slides:



Advertisements
Similar presentations
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Advertisements

1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
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.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
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.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
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)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Topological Sort Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Algorithms and Data Structures Lecture XIII
CS 3343: Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Algorithms and Data Structures Lecture XIII
Lecture 12 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Algorithms Searching in a Graph.
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University

November 22, This Lecture Finish up Topological Sort Weighted Graphs Minimum Spanning Trees Greedy Choice Theorem Kruskal’s Algorithm Prim’s Algorithm

November 22, Directed Acyclic Graphs A DAG is a directed graph with no cycles Often used to indicate precedences among events, i.e., event a must happen before b An example would be a parallel code execution Inducing a total order can be done using Topological Sorting

November 22, DAG Theorem A directed graph G is acyclic iff a DFS of G yields no back edges Proof suppose there is a back edge (u,v); v is an ancestor of u in DFS forest. Thus, there is a path from v to u in G and (u,v) completes the cycle suppose there is a cycle c; let v be the first vertex in c to be discovered and u is a predecessor of v in c. Upon discovering v the whole cycle from v to u is white We must visit all nodes reachable on this white path before return DFS-Visit(v), i.e., vertex u becomes a descendant of v Thus, (u,v) is a back edge

November 22, Topological Sort Sorting of a directed acyclic graph (DAG) A topological sort of a DAG is a linear ordering of all its vertices sucht that for any edge (u,v) in the DAG, u appears before v in the ordering The following algorithm topologically sorts a DAG The linked lists comprises a total ordering Topological-Sort(G) 1) call DFS(G) to compute finishing times f[v] for each vertex v 2) as each vertex is finished, insert it onto the front of a linked list 3) return the linked list of vertices

November 22, Topological Sort Example Precedence relations: an edge from x to y means one must be done with x before one can do y Intuition: can schedule task only when all of its subtasks have been scheduled

November 22, Topological Sort Running time depth-first search: O(V+E) time insert each of the |V| vertices to the front of the linked list: O(1) per insertion Thus the total running time is O(V+E)

November 22, Topological Sort Correctness Claim: for a DAG, an edge When (u,v) explored, u is gray. We can distinguish three cases v = gray  (u,v) = back edge (cycle, contradiction) v = white  v becomes descendant of u  v will be finished before u  f[v] < f[u] v = black  v is already finished  f[v] < f[u] The definition of topological sort is satisfied

November 22, Spanning Tree A spanning tree of G is a subgraph which is a tree contains all vertices of G

November 22, Minimum Spanning Trees Undirected, connected graph G = (V,E) Weight function W: E  R (assigning cost or length or other values to edges) Spanning tree: tree that connects all the vertices (above?) Minimum spanning tree: tree that connects all the vertices and minimizes

November 22, Optimal Substructure MST T Removing the edge (u,v) partitions T into T 1 and T 2 We claim that T 1 is the MST of G 1 =(V 1,E 1 ), the subgraph of G induced by vertices in T 1 Also, T 2 is the MST of G 2 T1T1 T2T2

November 22, Greedy Choice Greedy choice property: locally optimal (greedy) choice yields a globally optimal solution Theorem Let G=(V, E), and let S  V and let (u,v) be min-weight edge in G connecting S to V – S Then (u,v)  T – some MST of G

November 22, Greedy Choice (2) Proof suppose (u,v)  T look at path from u to v in T swap (x, y) – the first edge on path from u to v in T that crosses from S to V – S this improves T – contradiction (T supposed to be MST) u v x y S V-S

November 22, Generic MST Algorithm Generic-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3 Find an edge (u,v) that is safe for A 4 A  A  {(u,v)} 5 return A Generic-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3 Find an edge (u,v) that is safe for A 4 A  A  {(u,v)} 5 return A Safe edge – edge that does not destroy A’s property MoreSpecific-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3.1 Make a cut (S, V-S) of G that respects A 3.2 Take the min-weight edge (u,v) connecting S to V-S 4 A  A  {(u,v)} 5 return A MoreSpecific-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3.1 Make a cut (S, V-S) of G that respects A 3.2 Take the min-weight edge (u,v) connecting S to V-S 4 A  A  {(u,v)} 5 return A

November 22, Prim-Jarnik Algorithm Vertex based algorithm Grows one tree T, one vertex at a time A cloud covering the portion of T already computed Label the vertices v outside the cloud with key[v] – the minimum weigth of an edge connecting v to a vertex in the cloud, key[v] = , if no such edge exists

November 22, MST-Prim(G,w,r) 01 Q  V[G] // Q – vertices out of T 02 for each u  Q 03 key[u]   04 key[r]  0 05  [r]  NIL 06 while Q   do 07 u  ExtractMin(Q) // making u part of T 08 for each v  Adj[u] do 09 if v  Q and w(u,v) < key[v] then 10  [v]  u 11 key[v]  w(u,v) Prim-Jarnik Algorithm (2) updating keys

November 22, Prim Example

November 22, Prim Example (2)

November 22, Prim Example (3)

November 22, Priority Queues A priority queue is a data structure for maintaining a set S of elements, each with an associated value called key We need PQ to support the following operations BuildPQ(S) – initializes PQ to contain elements of S ExtractMin(S) returns and removes the element of S with the smallest key ModifyKey(S,x,newkey) – changes the key of x in S A binary heap can be used to implement a PQ BuildPQ – O(n) ExtractMin and ModifyKey – O(lg n)

November 22, Prim’s Running Time Time = |V|T(ExtractMin) + O(E)T(ModifyKey) Time = O(V lgV + E lgV) = O(E lgV) QT(ExtractMin)T(DecreaseKey)Total arrayO(V)O(V)O(1)O(V 2 ) binary heapO(lg V) O(E lgV ) Fibonacci heap O(lg V)O(1) amortizedO(V lgV +E )

November 22, Kruskal's Algorithm Edge based algorithm Add the edges one at a time, in increasing weight order The algorithm maintains A – a forest of trees. An edge is accepted it if connects vertices of distinct trees We need a data structure that maintains a partition, i.e.,a collection of disjoint sets MakeSet(S,x): S  S  {{x}} Union(S i,S j ): S  S – {S i,S j }  {S i  S j } FindSet(S, x): returns unique S i  S, where x  S i

November 22, Kruskal's Algorithm The algorithm adds the cheapest edge that connects two trees of the forest MST-Kruskal(G,w) 01 A   02 for each vertex v  V[G] do 03 Make-Set(v) 04 sort the edges of E by non-decreasing weight w 05 for each edge (u,v)  E, in order by non- decreasing weight do 06 if Find-Set(u)  Find-Set(v) then 07 A  A  {(u,v)} 08 Union(u,v) 09 return A

November 22, Kruskal Example

November 22, Kruskal Example (2)

November 22, Kruskal Example (3)

November 22, Kruskal Example (4)

November 22, Disjoint Sets as Lists Each set – a list of elements identified by the first element, all elements in the list point to the first element Union – add a smaller list to a larger one FindSet: O(1), Union(u,v): O(min{|C(u)|, C(v)}) 123 ABC  4  123 ABC  4

November 22, Kruskal Running Time Initialization O(V) time Sorting the edges  (E lg E) =  (E lg V) (why?) O(E) calls to FindSet Union costs Let t(v) – the number of times v is moved to a new cluster Each time a vertex is moved to a new cluster the size of the cluster containing the vertex at least doubles: t(v)  log V Total time spent doing Union Total time: O(E lg V)

November 22, Next Lecture Shortest Paths in Weighted Graphs