1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
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.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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:
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
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.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
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.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
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
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
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|
Chapter 22: Elementary Graph Algorithms
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.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

1 Chapter 22 Elementary Graph Algorithms

2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix Graph search –Breadth-first search (BFS) –Depth-first search (DFS) Topological sort Strongly connected components Undirected Graph Directed Graph

3 Representation of Graphs Adjacency list:  (V+E) –Preferred for sparse graph |E| << |V| 2 –Adj[u] contains all the vertices v such that there is an edge (u, v)  E –Weighted graph: w(u, v) is stored with vertex v in Adj[u] –No quick way to determine if a given edge is present in the graph Adjacency matrix:  (V 2 ) –Preferred for dense graph –Symmetry for undirected graph –Weighted graph: store w(u, v) in the (u, v) entry –Easy to determine if a given edge is present in the graph

4 Representation For A Undirected Graph

5 Representation For A Directed Graph 1 bit per entry

6 Breadth-First Search (BFS) Graph search: given a source vertex s, explores the edges of G to discover every vertex that is reachable from s –Compute the distance (smallest number of edges) from s to each reachable vertex –Produce a breadth-first tree with root s that contains all reachable vertices –Compute the shortest path from s to each reachable vertex BFS discovers all vertices at distance k from s before discovering any vertices at distance k+1

7 Data Structure for BFS Adjacency list color[u] for each vertex –WHITE if u has not been discovered –BLACK if u and all its adjacent vertices have been discovered –GRAY if u has been discovered, but has some adjacent white vertices Frontier between discovered and undiscovered vertices d[u] for the distance from s to u  [u] for predecessor of u FIFO queue Q to manage the set of gray vertices –Q stores all the gray vertices

8

9 Another Example of DFS

10 Initialization Set up s and initialize Q Explore all the vertices adjacent to u and update d,  and Q

11 Analysis of BFS O(V+E) –Each vertex is en-queued (O(1)) at most once  O(V) –Each adjacency list is scanned at most once  O(E) Print out the vertices on a shortest path from s to v

12 PRINT-PATH Illustration (1)PRINT-PATH(G, s, y) (2)PRINT-PATH(G, s, x) y (3)PRINT-PATH(G, s, w) x y (4)PRINT-PATH(G, s, s) w x y (5)s w x y

13 Depth-First Search (DFS) DFS: search deeper in the graph whenever possible –Edges are explored out of the most recently discovered vertex v that still has unexplored edges leaving it –When all of v’s edges have been explored (finished), the search backtracks to explore edges leaving the vertex from which v was discovered –This process continues until we have discovered all the vertices that are reachable from the original source vertex –If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source –The entire process is repeated until all vertices are discovered DFS will create a forest of DFS-trees

14 Data Structure for DFS Adjacency list color[u] for each vertex –WHITE if u has not been discovered –GRAY if u is discovered but not finished –BLACK if u is finished Timestamps: 1  d[u] < f[u]  2|V| –d[u] records when u is first discovered (and grayed) –f[u] records when the search finishes examining u’s adjacency list (and blacken u)  [u] for predecessor of u

15

16

17 Properties of DFS Time complexity:  (V+E) –Loops on lines 1-3 and 5-7 of DFS:  (V) –DFS-VISIT Called exactly once for each vertex Loops on lines 4-7 for a vertex v: |Adj[v]| Total time = DFS results in a forest of trees Discovery and finishing times have parenthesis structure –Theorem 22.7

18

19 Classification of Edges Tree edges are edges in the DFS forest. Edge (u, v) is a tree edge if it was first discovered by exploring edge (u, v). –v is WHITE Back edges are those edges (u, v) connecting a vertex u to an ancestor v in a DFS tree. Self-loops, which may occur in directed graphs, are considered to be back edges. –v is GRAY Forward edges are those non-tree edges (u, v) containing a vertex u to a descendant v in a DFS tree –v is BLACK and d[u] < d[v] Cross edges are all other edges. They can go between vertices in the same tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different DFS trees. –v is BLACK and d[u] > d[v] In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge. (Theorem 22.10)

20 Topological Sort A topological sort of a directed acyclic graph (DAG) is a linear order of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering –If the graph is not acyclic, then no linear ordering is possible. –A topological sort can be viewed as an ordering of its vertices along a horizontal line so that all directed edges go from left to right DAG are used in many applications to indicate precedence among events

21

22 Topological Sort  (V+E)

23 Lemma DAG is acyclic if and only if DFS of G yields no back edges –  Suppose that there is a back edge (u, v). Then vertex v is an ancestor of vertex u in the depth-first forest. There is thus a path from v to u in G, and the back edge (u, v) completes a cycle –  Suppose that G contains a cycle c. We show that a DFS of G yields a back edge. Let v be the first vertex to be discovered in c, and let (u, v) be the preceding edge in c. At time d[v], the vertices of c form a path of white vertices from v to u. By the white-path theorem (Theorem 22.9), vertex u becomes a descendant of v in the depth- first forest. Therefore, (u, v) is a back edge.

24 Theorem TOPOLOGICAL-SORT(G) produces a topological sort of a directed acyclic graph G –Suppose that DFS is run on a given DAG G to determine finishing times for its vertices. It suffices to show that for any pair of distinct vertices u, v, if there is an edge in G from u to v, then f[v] < f[u]. The linear ordering is corresponding to finishing time ordering –Consider any edge (u, v) explored by DFS(G). When this edge is explored, v cannot be gray (otherwise, (u, v) will be a back edge). Therefore v must be either white or black If v is white, v becomes a descendant of u, f[v] < [u] (ex. pants & shoes) If v is black, it has already been finished, so that f[v] has already been set  f[v] < f[u] (ex. belt & jacket)