Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.

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.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
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.
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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
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:
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
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.
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.
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.
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.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
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
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
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.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
COSC 3101A - Design and Analysis of Algorithms 9 Knapsack Problem Huffman Codes Introduction to Graphs Many of these slides are taken from Monica Nicolescu,
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
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
Graphs Breadth First Search & Depth First Search
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
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20

Graph Representation Adjacency list representation of G = (V, E) –An array of  V  lists, one for each vertex in V –Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u] contains the vertices adjacent to u (in arbitrary order) –Can be used for both directed and undirected graphs / / / Undirected graph CS 477/677 - Lecture 202

Graph Representation Adjacency matrix representation of G = (V, E) –Assume vertices are numbered 1, 2, …  V  –The representation consists of a matrix A  V  x  V  : –a ij = 1 if (i, j)  E 0 otherwise Undirected graph For undirected graphs matrix A is symmetric: a ij = a ji A = A T CS 477/677 - Lecture 203

Properties of Adjacency Matrix Representation Memory required –  (|V| 2 ), independent on the number of edges in G Preferred when –The graph is dense:  E  is close to  V  2 –We need to quickly determine if there is an edge between two vertices Time to list all vertices adjacent to u: –  (|V|) Time to determine if (u, v)  E: –  (1) CS 477/677 - Lecture 204

Weighted Graphs Weighted graphs = graphs for which each edge has an associated weight w(u, v) w: E  R, weight function Storing the weights of a graph –Adjacency list: Store w(u,v) along with vertex v in u ’s adjacency list –Adjacency matrix: Store w(u, v) at location (u, v) in the matrix CS 477/677 - Lecture 205

Searching in a Graph Graph searching = systematically follow the edges of the graph so as to visit the vertices of the graph Two basic graph searching algorithms: –Breadth-first search –Depth-first search The difference between them is in the order in which they explore the unvisited edges of the graph Graph algorithms are typically elaborations of the basic graph-searching algorithms CS 477/677 - Lecture 206

Breadth-First Search (BFS) Input: –A graph G = (V, E) (directed or undirected) –A source vertex s  V Goal: –Explore the edges of G to “discover” every vertex reachable from s, taking the ones closest to s first Output: –d[v] = distance (smallest # of edges) from s to v, for all v  V –A “breadth-first tree” rooted at s that contains all reachable vertices CS 477/677 - Lecture 207

Breadth-First Search (cont.) Keeping track of progress: –Color each vertex in either white, gray or black –Initially, all vertices are white –When being discovered a vertex becomes gray –After discovering all its adjacent vertices the node becomes black –Use FIFO queue Q to maintain the set of gray vertices source CS 477/677 - Lecture 208

Breadth-First Tree BFS constructs a breadth-first tree –Initially contains the root (source vertex s ) –When vertex v is discovered while scanning the adjacency list of a vertex u  vertex v and edge (u, v) are added to the tree –u is the predecessor (parent) of v in the breadth-first tree –A vertex is discovered only once  it has only one parent source CS 477/677 - Lecture 209

BFS Additional Data Structures G = (V, E) represented using adjacency lists color[u] – the color of the vertex for all u  V  [u] – predecessor of u –If u = s (root) or node u has not yet been discovered   [u] = NIL d[u] – the distance from the source s to vertex u Use a FIFO queue Q to maintain the set of gray vertices d=1  =1 d=1  =1 d=2  =5 d=2  =2 source CS 477/677 - Lecture 2010

BFS( V, E, s ) 1.for each u  V - {s} 2. do color[u]  WHITE 3. d[u] ←  4.  [u] = NIL 5.color[s]  GRAY 6.d[s] ← 0 7.  [s] = NIL 8.Q   9.Q ← ENQUEUE( Q, s ) Q: s  0   rstu vwxy   rstu vwxy rstu vwxy CS 477/677 - Lecture 2011

BFS( V, E, s ) 10. while Q   11. do u ← DEQUEUE( Q ) 12. for each v  Adj[u] 13. do if color[v] = WHITE 14. then color[v] = GRAY 15. d[v] ← d[u]  [v] = u 17. ENQUEUE( Q, v ) 18. color[u]  BLACK  0   1  rstu vwxy Q: w Q: s  0   rstu vwxy 10   1  rstu vwxy Q: w, r CS 477/677 - Lecture 2012

Example 10   1  rstu vwxy Q: s  0   rstu vwxy Q: w, r vwxy 102   12  rstu Q: r, t, x 102  212  rstu vwxy Q: t, x, v  rstu vwxy Q: x, v, u rstu vwxy Q: v, u, y rstu vwxy Q: u, y rstu vwxy Q: y rstu vwxy Q:  CS 477/677 - Lecture 2013

Analysis of BFS 1.for each u  V - {s} 2. do color[u]  WHITE 3. d[u] ←  4.  [u] = NIL 5.color[s]  GRAY 6.d[s] ← 0 7.  [s] = NIL 8.Q   9.Q ← ENQUEUE(Q, s) O(|V|)  (1) CS 477/677 - Lecture 2014

Analysis of BFS 10. while Q   11. do u ← DEQUEUE( Q ) 12. for each v  Adj[u] 13. do if color[v] = WHITE 14. then color[v] = GRAY 15. d[v] ← d[u]  [v] = u 17. ENQUEUE( Q, v ) 18. color[u]  BLACK  (1) Scan Adj[u] for all vertices u in the graph Each vertex u is processed only once, when the vertex is dequeued Sum of lengths of all adjacency lists =  (|E|) Scanning operations: O(|E|) Total running time for BFS = O(|V| + |E|) CS 477/677 - Lecture 2015

Shortest Paths Property BFS finds the shortest-path distance from the source vertex s  V to each node in the graph Shortest-path distance =  (s, u) –Minimum number of edges in any path from s to u r s t u vwx y source CS 477/677 - Lecture 2016

Depth-First Search Input: –G = (V, E) (No source vertex given!) Goal: –Explore the edges of G to “discover” every vertex in V starting at the most current visited node –Search may be repeated from multiple sources Output: –2 timestamps on each vertex: d[v] = discovery time f[v] = finishing time (done with examining v ’s adjacency list) –Depth-first forest CS 477/677 - Lecture 2017

Depth-First Search Search “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges After all edges of v have been explored, the search “backtracks” from the parent of v The process continues until all vertices reachable from the original source have been discovered If undiscovered vertices remain, choose one of them as a new source and repeat the search from that vertex DFS creates a “depth-first forest” CS 477/677 - Lecture 2018

DFS Additional Data Structures Global variable: time-step –Incremented when nodes are discovered/finished color[u] – similar to BFS –White before discovery, gray while processing and black when finished processing  [u] – predecessor of u d[u], f[u] – discovery and finish times GRAY WHITE BLACK 02|V|d[u]f[u] 1 ≤ d[u] < f [u] ≤ 2 |V| CS 477/677 - Lecture 2019

DFS( V, E ) 1.for each u  V 2. do color[u] ← WHITE 3.  [u] ← NIL 4.time ← 0 5.for each u  V 6. do if color[u] = WHITE 7. then DFS-VISIT(u) Every time DFS-VISIT(u) is called, u becomes the root of a new tree in the depth-first forest u vw xyz CS 477/677 - Lecture 2020

DFS-VISIT( u ) 1.color[u] ← GRAY 2.time ← time+1 3.d[u] ← time 4.for each v  Adj[u] 5. do if color[v] = WHITE 6. then  [v] ← u 7. DFS-VISIT( v ) 8.color[u] ← BLACK 9.time ← time f[u] ← time 1/ u vw xyz u vw xyz time = 1 1/2/ u vw xyz CS 477/677 - Lecture 2021

Example 1/2/ u vw xyz 1/ u vw xyz 2/ 3/ u vw xyz 1/2/ 4/3/ u vw xyz 1/2/ 4/3/ u vw xyz B 1/2/ 4/53/ u vw xyz B 1/2/ 4/53/6 u vw xyz B 1/2/7 4/53/6 u vw xyz B 1/2/7 4/53/6 u vw xyz B F CS 477/677 - Lecture 2022

Example (cont.) 1/82/7 4/53/6 u vw xyz B F 1/82/79/ 4/53/6 u vw xyz B F 1/82/79/ 4/53/6 u vw xyz B F C 1/82/79/ 4/53/610/ u vw xyz B F C 1/82/79/ 4/53/610/ u vw xyz B F C B 1/82/79/ 4/53/610/11 u vw xyz B F C B 1/82/79/12 4/53/610/11 u vw xyz B F C B The results of DFS may depend on: The order in which nodes are explored in procedure DFS The order in which the neighbors of a vertex are visited in DFS-VISIT CS 477/677 - Lecture 2023

Edge Classification Tree edge (reaches a WHITE vertex): –(u, v) is a tree edge if v was first discovered by exploring edge (u, v) Back edge (reaches a GRAY vertex): –(u, v), connecting a vertex u to an ancestor v in a depth first tree –Self loops (in directed graphs) are also back edges 1/ u vw xyz 2/ 4/3/ u vw xyz B CS 477/677 - Lecture 2024

Edge Classification Forward edge (reaches a BLACK vertex & d[u] < d[v] ): –Non-tree edge (u, v) that connects a vertex u to a descendant v in a depth first tree Cross edge (reaches a BLACK vertex & d[u] > d[v] ): –Can go between vertices in same depth-first tree (as long as there is no ancestor / descendant relation) or between different depth-first trees 1/2/7 4/53/6 u vw xyz B F 1/82/79/ 4/53/6 u vw xyz B F C CS 477/677 - Lecture 2025

Analysis of DFS( V, E ) 1.for each u  V 2. do color[u] ← WHITE 3.  [u] ← NIL 4.time ← 0 5.for each u  V 6. do if color[u] = WHITE 7. then DFS-VISIT(u)  (|V|)  (|V|) – without counting the time for DFS-VISIT CS 477/677 - Lecture 2026

Analysis of DFS-VISIT( u ) 1.color[u] ← GRAY 2.time ← time+1 3.d[u] ← time 4.for each v  Adj[u] 5. do if color[v] = WHITE 6. then  [v] ← u 7. DFS-VISIT( v ) 8.color[u] ← BLACK 9.time ← time f[u] ← time Each loop takes |Adj[u]| DFS-VISIT is called exactly once for each vertex Total: Σ u  V |Adj[u]| +  (|V|) =  (|E|) =  (|V| + |E|) CS 477/677 - Lecture 2027

CS 477/677 - Lecture 20 Properties of DFS u =  [v]  DFS-VISIT( v ) was called during a search of u ’s adjacency list Vertex v is a descendant of vertex u in the depth first forest  v is discovered during the time in which u is gray 1/2/ 3/ u vw xyz 28

CS 477/677 - Lecture 20 Parenthesis Theorem In any DFS of a graph G, for all u, v, exactly one of the following holds: 1.[d[u], f[u]] and [ d[v], f[v]] are disjoint, and neither of u and v is a descendant of the other 2.[ d[v], f[v]] is entirely within [d[u], f[u]] and v is a descendant of u 3.[ d[u], f[u]] is entirely within [d[v], f[v]] and u is a descendant of v 3/62/91/10 4/57/812/13 uvw x yzs 11/16 14/15 t s z t v u y w x (s(z(y(xx)y)(ww)z)s)v)(t(v(uu)t) Well-formed expression: parenthesis are properly nested 29

CS 477/677 - Lecture 20 Other Properties of DFS Corollary Vertex v is a proper descendant of u  d[u] < d[v] < f[v] < f[u] Theorem (White-path Theorem) In a depth-first forest of a graph G, vertex v is a descendant of u if and only if at time d[u], there is a path u  v consisting of only white vertices. 1/2/ u v 1/82/79/12 4/53/610/11 u v B F C B 30

CS 477/677 - Lecture 20 Topological Sort Topological sort of a directed acyclic graph G = (V, E): a linear order of vertices such that if there exists an edge (u, v), then u appears before v in the ordering. Directed acyclic graphs (DAGs) –Used to represent precedence of events or processes that have a partial order a before b b before c b before c a before c a before c What about a and b? Topological sort helps us establish a total order 31

Readings Chapter 22 CS 477/677 - Lecture 2032