Introduction to Algorithms Graph Algorithms

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
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.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
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
Graphs.
CSCE 411 Design and Analysis of Algorithms Set 2: Brute Force & Exhaustive Search Dr. J. Michael Moore Fall 2014 CSCE 411, Fall 2014: Set 2 1 Based primarily.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Chapter 22: Elementary Graph Algorithms
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
 2004 SDU 1 Lecture5-Strongly Connected Components.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Data Structures and Algorithm Analysis Lecture 5
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Lecture 16 Strongly Connected Components
Topological Sort (an application of DFS)
CSCE 411 Design and Analysis of Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Lectures on Graph Algorithms: searching, testing and sorting
Richard Anderson Autumn 2016 Lecture 5
Topological Sort (an application of DFS)
Richard Anderson Winter 2009 Lecture 6
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis

Bipartiteness Graph G = (V,E) is bipartite iff it can be partitioned into two sets of nodes A and B such that each edge has one end in A and the other end in B Alternatively: Graph G = (V,E) is bipartite iff all its cycles have even length Graph G = (V,E) is bipartite iff nodes can be coloured using two colours Question: given a graph G, how to test if the graph is bipartite? Note: graphs without cycles (trees) are bipartite bipartite: non bipartite

Testing bipartiteness Method: use BFS search tree Recall: BFS is a rooted spanning tree. Algorithm: Run BFS search and colour all nodes in odd layers red, others blue Go through all edges in adjacency list and check if each of them has two different colours at its ends - if so then G is bipartite, otherwise it is not We use the following alternative definitions in the analysis: Graph G = (V,E) is bipartite iff all its cycles have even length, or Graph G = (V,E) is bipartite iff it has no odd cycle bipartite non bipartite

Topological Sort Want to “sort” or linearize a directed acyclic graph (DAG). A B D C E A B C D E

Topological Sort Performed on a DAG. Linear ordering of the vertices of G such that if (u, v)  E, then u appears before v. Topological-Sort (G) call DFS(G) to compute finishing times f [v] for all v  V as each vertex is finished, insert it onto the front of a linked list return the linked list of vertices Time: (V + E).

Example A B D 1/ C E Linked List:

Example A B D 1/ 2/ C E Linked List:

Example A B D 1/ 2/3 C E Linked List: 2/3 E

Example A B D 1/4 2/3 C E Linked List: 1/4 2/3 D E

Example A B D 5/ 1/4 2/3 C E Linked List: 1/4 2/3 D E

Example A B D 5/ 1/4 6/ 2/3 C E Linked List: 1/4 2/3 D E

Example A B D 5/ 1/4 6/7 2/3 C E Linked List: 6/7 1/4 2/3 C D E

Example A B D 5/8 1/4 6/7 2/3 C E Linked List: 5/8 6/7 1/4 2/3 B C D E

Example A B D 9/ 5/8 1/4 6/7 2/3 C E Linked List: 5/8 6/7 1/4 2/3 B C

Example 9/10 9/10 A B D 5/8 1/4 6/7 2/3 C E Linked List: 5/8 6/7 1/4

Precedence Example Tasks that have to be done to eat breakfast: get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat. Certain events must happen in a certain order (ex: get bowl before pouring milk) For other events, it doesn't matter (ex: get bowl and get spoon)

Precedence Example get glass get bowl pour juice pour cereal get spoon pour milk eat breakfast Order: glass, juice, bowl, cereal, milk, spoon, eat.

Precedence Example Topological Sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 eat milk spoon juice cereal glass bowl consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat

Precedence Example What if we started with juice? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 eat milk glass spoon juice cereal bowl consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat

Correctness Proof Show if (u, v)  E, then f [v] < f [u]. When we explore (u, v), what are their colors? Note, u is gray – we are exploring it Is v gray? No, because then v would be an ancestor of u.  (u, v) is a back edge.  a cycle (dag has no back edges). Is v white? Then v becomes descendant of u. By parenthesis theorem, d[u] < d[v] < f [v] < f [u]. Is v black? Then v is already finished. Since we’re exploring (u, v), we have not yet finished u. Therefore, f [v] < f [u].

Strongly Connected Components Consider a directed graph. A strongly connected component (SCC) of the graph is a maximal set of nodes with a (directed) path between every pair of nodes. If a path from u to v exists in the SCC, then a path from v to u also exists. Problem: Find all the SCCs of the graph.

Uses of SCC’s Packaging software modules Construct directed graph of which modules call which other modules A SCC is a set of mutually interacting modules Pack together those in the same SCC

SCC Example h f a e g c b d four SCCs

Main Idea of SCC Algorithm DFS tells us which nodes are reachable from the roots of the individual trees Also need information in the other direction: is the root reachable from its descendants? Run DFS again on the transpose graph (reverse the directions of the edges)

SCC Algorithm Input: directed graph G = (V,E) call DFS(G) to compute finishing times compute GT // transpose graph call DFS(GT), considering nodes in decreasing order of finishing times each tree from Step 3 is a separate SCC of G

SCC Algorithm Example h f a e g c b d input graph - run DFS

After Step 1 fin(c) fin(d) fin(b) fin(e) fin(a) fin(h) fin(g) fin(f) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 a b c d e g h f Order of nodes for Step 3: f, g, h, a, e, b, d, c

After Step 2 h f a e g c b d transposed input graph - run DFS with specified order of nodes

After Step 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 f h g a b d e c SCCs are {f,h,g} and {a,e} and {b,c} and {d}.

Run Time of SCC Algorithm Step 1: O(V+E) to run DFS Step 2: O(V+E) to construct transpose graph, assuming adjacency list rep. Adjacency matrix is O(1) time w/ wrapper. Step 3: O(V+E) to run DFS again Step 4: O(V) to output result Total: O(V+E)

Component Graph GSCC = (VSCC, ESCC). VSCC has one vertex for each SCC in G. ESCC has an edge if there’s an edge between the corresponding SCC’s in G. {a,e} {f,h,g} {d} GSCC based on example graph from before {b,c}

Component Graph Facts Claim: GSCC is a directed acyclic graph. Suppose there is a cycle in GSCC such that component Ci is reachable from component Cj and vice versa. Then Ci and Cj would not be separate SCCs. Lemma: If there is an edge in GSCC from component C' to component C, then f(C') > f(C). Consider any component C during Step 1 (running DFS on G) Let d(C) be earliest discovery time of any node in C Let f(C) be latest finishing time of any node in C