David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.

Slides:



Advertisements
Similar presentations
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Advertisements

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.
Graphs – Depth First Search ORD DFW SFO LAX
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
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.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
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/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.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
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)
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Chapter 22 Elementary Graph Algorithms
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
CS6045: Advanced Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree

David Luebke 2 10/1/2015 Review: Breadth-First Search BFS(G, s) { initialize vertices; Q = {s};// Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } v->p represents parent in tree v->d represents depth in tree

David Luebke 3 10/1/2015 Review: Depth-First Search l Depth-first search is another strategy for exploring a graph n Explore “deeper” in the graph whenever possible n Edges are explored out of the most recently discovered vertex v that still has unexplored edges n When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

David Luebke 4 10/1/2015 Review: DFS Code DFS(G) { for each vertex u  G->V { u->color = WHITE; } time = 0; for each vertex u  G->V { if (u->color == WHITE) DFS_Visit(u); } DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] { if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; time = time+1; u->f = time; }

David Luebke 5 10/1/2015 DFS Example 1 | | | | | | | | source vertex d f

David Luebke 6 10/1/2015 DFS Example 1 | | | | | | 2 | | source vertex d f

David Luebke 7 10/1/2015 DFS Example 1 | | | | |3 | 2 | | source vertex d f

David Luebke 8 10/1/2015 DFS Example 1 | | | | |3 | 4 2 | | source vertex d f

David Luebke 9 10/1/2015 DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f

David Luebke 10 10/1/2015 DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f

David Luebke 11 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

David Luebke 12 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

David Luebke 13 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the grey vertices? What do they represent?

David Luebke 14 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 15 10/1/2015 DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 16 10/1/2015 DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 17 10/1/2015 DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 18 10/1/2015 DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 19 10/1/2015 DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 20 10/1/2015 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 21 10/1/2015 Review: Depth-First Sort Analysis l Running time: O(V+E) l Show by amortized analysis n “Charge” the exploration of edge to the edge: u Each loop in DFS_Visit can be attributed to an edge in the graph u Runs once/edge if directed graph, twice if undirected u Thus loop will run in O(E) time, algorithm O(V+E) ] Considered linear for graph, b/c adj list requires O(V+E) storage n Important to be comfortable with this kind of reasoning and analysis

David Luebke 22 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex u The tree edges form a spanning forest u Can tree edges form cycles? Why or why not?

David Luebke 23 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor u Encounter a grey vertex (grey to grey)

David Luebke 24 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent u Not a tree edge, though u From grey node to black node

David Luebke 25 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent n Cross edge: between a tree or subtrees u From a grey node to a black node

David Luebke 26 10/1/2015 Review: DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edgesCross edges

David Luebke 27 10/1/2015 Review: Kinds Of Edges l Thm: If G is undirected, a DFS produces only tree and back edges l Thm: An undirected graph is acyclic iff a DFS yields no back edges n If acyclic, no back edges n If no back edges, acyclic u No back edges implies only tree edges (Why?) u Only tree edges implies we have a tree or a forest u Which by definition is acyclic l Thus, can run DFS to find cycles

David Luebke 28 10/1/2015 DFS And Cycles l Running time: O(V+E) l We can actually determine if cycles exist in O(V) time: n In an undirected acyclic forest, |E|  |V| - 1 n So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way n Why not just test if |E| <|V| and answer the question in constant time?

David Luebke 29 10/1/2015 Directed Acyclic Graphs l A directed acyclic graph or DAG is a directed graph with no directed cycles:

David Luebke 30 10/1/2015 DFS and DAGs l Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: n Forward: if G is acyclic, will be no back edges u Trivial: a back edge implies a cycle n Backward: if no back edges, G is acyclic u Argue contrapositive: G has a cycle   a back edge ] Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle ] When v discovered, whole cycle is white ] Must visit everything reachable from v before returning from DFS-Visit() ] So path from u  v is grey  grey, thus (u, v) is a back edge

David Luebke 31 10/1/2015 Topological Sort l Topological sort of a DAG: n Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G l Real-world example: getting dressed

David Luebke 32 10/1/2015 Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket

David Luebke 33 10/1/2015 Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket SocksUnderwearPantsShoesWatchShirtBeltTieJacket

David Luebke 34 10/1/2015 Topological Sort Algorithm Topological-Sort() { Run DFS When a vertex is finished, output it Vertices are output in reverse topological order } l Time: O(V+E) l Correctness: Want to prove that (u,v)  G  u  f > v  f

David Luebke 35 10/1/2015 Correctness of Topological Sort l Claim: (u,v)  G  u  f > v  f n When (u,v) is explored, u is grey u v = grey  (u,v) is back edge. Contradiction (Why?) u v = white  v becomes descendent of u  v  f < u  f (since must finish v before backtracking and finishing u) u v = black  v already finished  v  f < u  f

David Luebke 36 10/1/2015 Minimum Spanning Tree l Problem: given a connected, undirected, weighted graph:

David Luebke 37 10/1/2015 Minimum Spanning Tree l Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

David Luebke 38 10/1/2015 Minimum Spanning Tree l Which edges form the minimum spanning tree (MST) of the below graph? HBC GED F A

David Luebke 39 10/1/2015 Minimum Spanning Tree l Answer: HBC GED F A

David Luebke 40 10/1/2015 Minimum Spanning Tree l MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees n Let T be an MST of G with an edge (u,v) in the middle n Removing (u,v) partitions T into two trees T 1 and T 2 n Claim: T 1 is an MST of G 1 = (V 1,E 1 ), and T 2 is an MST of G 2 = (V 2,E 2 ) (Do V 1 and V 2 share vertices? Why?) n Proof: w(T) = w(u,v) + w(T 1 ) + w(T 2 ) (There can’t be a better tree than T 1 or T 2, or T would be suboptimal)

David Luebke 41 10/1/2015 Minimum Spanning Tree l Thm: n Let T be MST of G, and let A  T be subtree of T n Let (u,v) be min-weight edge connecting A to V-A n Then (u,v)  T

David Luebke 42 10/1/2015 Minimum Spanning Tree l Thm: n Let T be MST of G, and let A  T be subtree of T n Let (u,v) be min-weight edge connecting A to V-A n Then (u,v)  T l Proof: in book (see Thm 24.1)

David Luebke 43 10/1/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

David Luebke 44 10/1/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph

David Luebke 45 10/1/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time?

David Luebke 46 10/1/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

David Luebke 47 10/1/2015 The End