Chapter 22: Elementary Graph Algorithms

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.
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
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 Breadth First Search & Depth First Search by Shailendra Upadhye.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
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?
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
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
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.
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.
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.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
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 Comp 122, Fall 2004.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
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.
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.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Elementary Graph Algorithms
Graph Algorithms – 2 DAGs Topological order
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
Basic Graph Algorithms
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first search (BFS) depth-first search(DFS) topological sort strongly connected components

Notation Graph G(V,E) is a data structure defined by a set of vertices V a set of eges E |V| = # of vertices |E| = # of edges Usually omit | | in asymptotic notation Q(V, E) means Q(|V|, |E|)

Adj = adjacency-list representation of G(V,E) is an array of |V| lists Adj[u] contains all vertices adjacent to vertex u For a directed graph, edge (uv) represented by v in Adj[u].  sum of lengths of adjacency lists = |E| For an undirected graph, edge (u,v) appears as v in Adj[u] and as u in Adj[v]. sum of lengths of adjacency lists = 2|E| Memory requirement is Q(V+E) A weighted graph has a function w(u,v) defined on the domain E. The weight of edge (u,v) can be stored as property of vertex v in Adj[u].

adjacency-matrix representation: Number the vertices 1,2,...|V| Define |V| x |V| matrix A such that aij = 1 if (i, j)  E, aij = 0 otherwise For a weighted graph, set aij = w(i,j) if (i, j)  E Disadvantage is requires Q(V2) memory regardless of |E| Advantage is speed of determining if edge (u,v) is in graph For an undirected graph A is symmetric A sparse graphs mean |E| << |V|2 adjacency-list representation preferred (saves space) A dense graph means |E| ~ |V|2 adjacency-matrix representation preferred (speed)

Graph representations 1 1

Breadth-first search: Given G(V,E) and a source vertex s, Breadth-first search does the following: (1) finds every vertex v reachable from s (2) calculates distance (minimum number of edges) between s and v (3) produces a Breadth-first tree with s as the root and every reachable vertex as a node The path from s to v in the Breadth-first tree corresponds to the shortest path in G. “Breadth-first” finds all vertices at distance k from s before searching for any vertices at distance k+1.

BFS(G,s) pseudo code for each u  V(G) – {s} do color[u]  white, d[u]  , p[u]  NIL (Initialization: color all vertices white for “undiscovered” set distance from s as infinite, set predecessor in Breadth-first tree to NIL) color[s]  gray, d[s]  0, p[s]  NIL (initialize s as gray for “discovered” i.e. reachable from s A gray vertex has been discovered but its adjacency list has not been searched for links to other vertices. After adjacency list is searched, color is changed to black) Q  0, Enqueue(Q,s) (A “first-in first-out” queue holds a list of the current gray vertices Gray vertices with the smallest distance from s are processed first Resolve ambiguity by some rule like alphabetical order)

BFS(G,s) algorithm continued while Q  0 do u  Dequeue(Q) for each v  Adj[u] do if color[v] = white then color[v] = gray d[v]d[u] + 1 p[v] u Enqueue(Q,v) color[u]  black (adj list searched) Runtime analysis: initialization requires O(V). each vertex is discovered at most once  queue operations require O(V). searching adjacency lists requires O(E)  total runtime O(V+E)

Example of BFS p596

Predecessor sub-graph Gp(Vp,Ep) Predecessor of v: p(v) is the vertex in whose adjacency list v was discovered Predecessor sub-graph Gp(Vp,Ep) Vp = {v  V : p[v]  NIL}  {s} Ep = {(p[v],v) : v  Vp - {s}} Predecessor sub-graph of BFS is a single tree

Finding shortest path is most common application of Breadth-first search d(s,u) = shortest path between s and any u  V d(s,u) =  if u is not reachable from s Weight of a path is the sum of the weights of its edges For an unweighted graph, weight of path = number of edges = length of path For weighted graph, least-weight is not necessarily shortest path

Theorem 22.5: On termination of BFS(G,s), all reachable vertices have been found (2) d[v] = d(s,v) for v  V (some may be infinite) (3) for any reachable v  s, a shortest path includes p[v] followed by edge (p[v],v).

Depth-first search: Explores all edges leaving a given vertex, v, before “backtracking” to explore edges leaving the vertex from which v was discovered Continues until all vertices reachable from a given source are discovered If the graph still contains undiscovered vertices, choose a new source p[v] = u if v was discovered in a search of the adjacency list of u As in BFS, vertices initialized to white, colored gray when discovered, colored back after their adjacency list has been examined d[v] is the timestamp field when v was discovered f[v] is the timestamp field when v was blackened range of timestamps is 1 to 2|V| d[v] < f[v]

Classification of edges: Tree edge connects vertex to its predecessor Back edge connects vertex to an ancestor in the same tree (also self loops in directed graph) Forward edge connects vertex to descendant in the same tree Cross edge: all others If in the same tree, then one vertex cannot be an ancestor of the other Edges (u,v) can be classified by the color v when it is first explored white v  tree edge gray v  back edge black v  forward or cross edge

DFS Pseudocodes: DFS(G) for each u  V (initialization) do color[u]  white, p[u]  NIL time  0 for each u  V (choose a new source) do if color[u] = white then DFS-Visit(u) DFG-Visit(u) (DFS from source u) color[u]  gray, time  time +1, d[u]  time (vertex u discovered) for each v  adj[u] do if color[v] = white then p[v]  u, DFG-Visit(v) color[u]  black, f[u]  time, time  time +1 (finished with vertex u)

DFS: Fig 22.4 p605

Fig 22.5 p607 Predecessor subgraph Gp = (V,Ep); Ep = {(p[v],v) ; v  V and p[v]  NIL} Gp contains all the vertices of G; hence, may be multiple trees Predecessor subgraph showing Non-tree edges B-edge: head () contains tail () F-edge: tail () contains head () C-edge: connects disjoint () Note C-edges inside trees. Predecessor subgraph showing “parenthesis” structure

Vertex v is a descendant of u in the depth-first forest of G Properties of DFS: Gp of DFS is a forest of trees, each of which reflects the pattern of recursive calls to DFS-Visit (slide 15) Pattern of discovery and finishing times has “parenthesis structure” (slide 18) For any 2 vertices u and v, exactly one of the following is true (1) intervals [d[u], f[u]] and [d[v], f[v]] are disjoint and neither u nor v is a descendent of the other (2) [d[u], f[u]] is contained entirely in [d[v], f[v]] and u is a descendent of v (3) [d[v], f[v]] is contained entirely in [d[u], f[u]] and v is a descendent of u. Corollary of the Parenthesis Theorem: Vertex v is a descendant of u in the depth-first forest of G if and only if d[u] < d[v] < f[v] < f[u]

Theorem 22.10: Only tree and back edges occur in a DFS of an undirected graph In DFS of undirected graph, edge type is determined by direction of search when edge is encountered. Consider edge(u,v) with v in u’s adjacency list. If edge(u,v) is traversed from u to v, then v is discovered on the traversal and edge(u,v) is a tree edge. The only remaining option for traversal of edge(u,v) is from v to u = p(v), which we call a “back” edge in an undirected graph. Predecessor graph is a single tree

CptS 450 Spring 2015 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 11: due 4/15/15 1. ex 22.2-2 p 601 Draw Gp Show all edges 2. ex 22.3-2 p 610 Draw Gp Show all edges

White-Path Theorem: Vertex v is a descendant of u in the depth-first forest of G (directed or undirected) if and only if at the time d[u] when u is discovered, v can be reached from u by a path consisting entirely of white vertices.

Lemma 22:11 A directed graph is acyclic if and only if a DFS yields no back edges Proof: If G has a back edge (u,v) then v is an ancestor of u and (u,v) completes a cycle in G. If G contains cycle c and v is the first vertex discovered in c, then a white path exist to vertex u the last vertex in c By white-path theorem, u is a decendent of v  (u,v) must be a back edge

DAG = directed acyclic graph Precedence among events: common use of DFS on DAGs Example: precedence of events getting dressed

topological sort: New graph topology: all edges point left-to-right linear ordering of vertices in a dag such that if G contains directed edge (u,v), then u appears before v in the ordering Pseudocode for topological sort: Topological-Sort(G) DFS(G) yields f[v] for all vertices At each f[v], insert the vertex into the front of a linked list return list New graph topology: all edges point left-to-right f(v) decreases left-to-right

Proof of the correctness of Topological-Sort(G) Theorem 22.12: Proof of the correctness of Topological-Sort(G) When edge (u,v) is explored in the DFS of a dag, v must be white or black because if v is gray, then (u,v) is a back edge (we would be exploring the adjacency list of v) and DFS of a dag cannot yield a back edge. If v is white, then v is a descendant of u and f[v] < f[u] (Corollary of the Parenthesis Theorem in slide #19) If v is black, then f[v] has already been assigned and, since we are still exploring the adj[u], f[v] < f[u]. Thus, for any edge(u,v) in the dag, f[v] < f[u] in topological sort, v will be on the right of u all edges point to the right

Strongly Connected Components (SCCs) Strongly connected components (SCCs) of directed graph G(V,E) are a set of vertices VSCC  V such that for every pair of vertices u and v, u ~> v and v ~> u (u reachable from v and v reachable from u) Component graph GSCC = (VSCC, ESCC) Let C1, C2, ..., Ck denote the SCCs of directed graph G VSCC = {v1, v2, ..., vk} contains a vertex from each SCC edge (vi,vj)  ESCC if G contains edge (x,y) for some x  Ci and y  Cj (i.e. edges the component graph connect the SCCs of G) Transpose of directed G(V,E) = GT(V,ET) where ET = {(u,v) : (v,u)  E} (i.e. GT has same vertices a G, but its edges are reversed)

SCC(G) Pseudocode: Call DFS(G) to get f[u] for all vertices Construct the transpose of G Call DFS(GT) with vertices in order of decreasing f[u] of DFS(G) The vertices of each tree of the DFS(GT) forest are a SCC of G Example: Fig 22.9 p616

SCC(G) Pseudocode: Call DFS(G) to get f[u] for all vertices Construct the transpose of G Call DFS(GT) with vertices in order of decreasing f[u] of DFS(G) The vertices of each tree of the DFS(GT) forest are a SCC of G

CptS 450 Spring 2015 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 12: due 4/22/15 1. ex 22.4-1 p 614 Topo sort on DAG of Fig 22.8 p615 2. ex 22.5-2 p 620 SCCs on graph in Fig 22.6 p611

Theorems behind the SCC pseudo-code Lemma 22.14 Let C and C’ be distinct SCCs of directed G(V,E). Let (u,v)  E be an edge with u in C and v in C’, then f(C) > f(C’) Case 1: d(C) < d(C’) use white path theorem Case 2: d(C) > d(C’) use parenthesis structure Corollary 22.15 Let C and C’ be distinct SCCs of directed G(V,E). Let (u,v)  ET be an edge with u in C and v in C’, then f(C) < f(C’)

How Strongly-Connected-Components(G) works: In the DFS of GT, start with a vertex in Cmax, the SCC that has the largest finish time. The search will visit all of the vertices of Cmax but will not find an edge that connects Cmax to any other SCC. If such an edge were found to C’, then by Corollary 22.15 of Lemma 22.14 f(Cmax) < f(C’), which violates the choice of Cmax as the SCC with the largest finish time As the source of the next DFS in GT, chose a vertex in C’, the SCC with f(C’) larger than all other finishing time except those in Cmax. The search will visit all the vertices of C’ and may find some edges that connect it to Cmax but not to any other SCC. Since the vertices of Cmax have already been discovered, the only new vertices visited from the second source are those in C’.