Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
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.
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.
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.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
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 – Depth First Search ORD DFW SFO LAX
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 Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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]
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
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.
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.
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 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
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.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
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 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.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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.
 2004 SDU 1 Lecture5-Strongly Connected Components.
November 19, Algorithms and Data Structures Lecture XI 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
Graphs – Breadth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
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
Advanced Algorithms Analysis and Design
Basic Graph Algorithms
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea

Elementary Graph Algorithms

2014/11Algorithm AnalysisL11.3 Graph Representation Given graph G = (V, E). May be either directed or undirected When expressing the running time of an algorithm, it is often in terms of both |V| and |E|. Two common ways to represent for algorithms: 1.Adjacency lists 2.Adjacency matrix

2014/11Algorithm AnalysisL11.4 Graph Representation by Adjacency Lists Array Adj of |V| lists, one per vertex Vertex u’s list has all vertices v such that (u, v) ∈ E. (Works for both directed and undirected graphs.) Example: For an undirected graph:

2014/11Algorithm AnalysisL11.5 Space: (V + E). Time: to list all vertices adjacent to u: Θ(degree(u)). Time: to determine if (u, v) ∈ E: O(degree(u)). Graph Representation by Adjacency Lists (2)

2014/11Algorithm AnalysisL11.6 Graph Representation by Adjacency Lists (3) Example for directed graph:

2014/11Algorithm AnalysisL11.7 Graph Representation by Adjacency Matrix |V| × |V| matrix A = (a ij ), such that

2014/11Algorithm AnalysisL11.8 Graph Representation by Adjacency Matrix Space: Θ(V 2 ) Time: to list all vertices adjacent to u: Θ(V) Time: to determine if (u, v) ∈ E: Θ(1) (Can store weights instead of bits for weighted graph.)

Breadth-First Search

2014/11Algorithm AnalysisL11.10 Breadth-First Search (BFS) Input: Graph G = (V, E), either directed or undirected, and source vertex s ∈ V. Output: 1.d[v] = distance (smallest # of edges) from s to v, for all v ∈ V 2.π[v] = u such that (u, v) is last edge on shortest path from s to v. –u is v’s predecessor. –set of edges {(π[v], v) : v ≠ s} forms a tree

2014/11Algorithm AnalysisL11.11 BFS - Idea Send a wave out from s. –First hits all vertices 1 edge from s. –From there, hits all vertices 2 edges from s. –Etc. Use FIFO queue Q to maintain wavefront. v ∈ Q if and only if wave has hit v but has not come out of v yet.

2014/11Algorithm AnalysisL11.12 ∈ BFS - Pseudocode ∈

2014/11Algorithm AnalysisL11.13 BFS - Example Graph and its distances computed by BFS

2014/11Algorithm AnalysisL11.14 BFS - Vertex Coloring As BFS progresses, every vertex has a color: –WHITE = undiscovered –GRAY = discovered, in the Queue or under inspection, but not finished –BLACK = finished, not in the Queue

2014/11Algorithm AnalysisL11.15 BFS - Properties Can show that queue Q consists of vertices with d values i, i, i,... i, i + 1, i + 1,... i + 1 –Only 1 or 2 values. –If 2, differ by 1 and all smallest are first. Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. BFS may not reach all vertices !

2014/11Algorithm AnalysisL11.16 BFS - Complexity Time = O(V + E). –O(V) because every vertex enqueued at most once. –O(E) because every vertex dequeued at most once and we examine (u, v) only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.

Depth-First Search

2014/11Algorithm AnalysisL11.18 Depth-First Search Input: G = (V, E), directed or undirected. No source vertex given! Output: 2 timestamps on each vertex: –d[v] = discovery time –f [v] = finishing time Will be useful for other algorithms later on. Properties: –Will methodically explore every edge –Start over from different vertices as necessary –As soon as we discover a vertex, explore from it

2014/11Algorithm AnalysisL11.19 Depth-First Search (cont.) As DFS progresses, every vertex has a color: –WHITE = undiscovered –GRAY = discovered, but not finished (not done exploring from it) –BLACK = finished (have found everything reachable from it) Discovery and finish times: –Unique integers from 1 to 2|V| –For all v, d[v] < f [v] –In other words, 1 ≤ d[v] < f [v] ≤ 2|V|

2014/11Algorithm AnalysisL11.20 Depth-First Search – Pseudocode

2014/11Algorithm AnalysisL11.21 Depth-First Search Example

2014/11Algorithm AnalysisL11.22 DFS - Complexity Time = Θ(V + E). Similar to BFS analysis. Θ, not just O, since guaranteed to examine every vertex and edge. DFS forms a depth-first forest comprised of > 1 depth-first trees. Each tree is made of edges (u, v) such that u is gray and v is white when (u, v) is explored.

2014/11Algorithm AnalysisL11.23 Parenthesis theorem Theorem: For all u, v, exactly one of the following holds: 1.d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and neither of u and v is a descendant of the other. 2.d[u] < d[v] < f [v] < f [u] and v is a descendant of u. 3.d[v] < d[u] < f [u] < f [v] and u is a descendant of v. Proof: in Textbook Like parentheses: ( ) [ ] ( [ ] ) [ ( ) ] So d[u] < d[v] < f [u] < f [v] cannot happen. Corollary: v is a proper descendant of u if and only if d[u] < d[v] < f [v] < f [u].

2014/11Algorithm AnalysisL11.24 White-Path Theorem Theorem: In a depth-first forest of a graph G=(V,E), vertex v is a descendant of u if and only if at time d[u], there is a path from u to v consisting of only white vertices. Proof: in Textbook

2014/11Algorithm AnalysisL11.25 Classification of Edges Tree edge: in the depth-first forest. Found by exploring (u, v). Back edge: (u, v), where u is a descendant of v. Forward edge: (u, v), where v is a descendant of u, but not a tree edge. Cross edge: any other edge. Can go between vertices in same depth-first tree or in different depth- first trees. Theorem: In DFS of an undirected graph, we get only tree and back edges. No forward or cross edges.

2014/11Algorithm AnalysisL11.26 Classification of Edges (cont.) DFS algorithm can be modified to classify edges as it encounters them. When an edge (u,v) is reached it can be classified according to the color of v: –WHITE: tree edge –GRAY: back edge –BLACK: forward or cross edge

Topological Sorting

2014/11Algorithm AnalysisL11.28 Topological Sort Directed acyclic graph (dag) –A directed graph with no cycles. Good for modeling processes and structures that have a partial order: a > b and b > c ⇒ a > c. But may have a and b such that neither a > b nor b > c. Can always make a total order (either a > b or b > a for all a ≠ b) from a partial order. In fact, that’s what a topological sort will do.

2014/11Algorithm AnalysisL11.29 Topological Sort (cont.) Topological sort of a dag: a linear ordering of vertices such that if (u, v) ∈ E, then u appears somewhere before v. (Not like sorting numbers.) Pseudocode: TOPOLOGICAL-SORT(V, E) 1. call DFS(V, E) to compute finishing times f [v] for all v ∈ V 2. output vertices in order of decreasing finish times Don’t need to sort by finish times. –Can just output vertices as they are finished and understand that we want the reverse of this list.

2014/11Algorithm AnalysisL11.30 Topological Sort - Example

2014/11Algorithm AnalysisL11.31 Deciding whether some Graph is acyclic Lemma: A directed graph G is acyclic if and only if a DFS of G yields no back edges. Proof ⇒ : Show that back edge ⇒ cycle. Suppose there is a back edge (u, v). Then v is ancestor of u in depth-first forest. Therefore, there is a path from v to u, this implies the existence of some cycle.

2014/11Algorithm AnalysisL11.32 DAG Lemma (cont.) ⇐ : Show that cycle ⇒ back edge. Suppose G contains cycle c. Let v be the first vertex discovered in c, and let (u, v) be the preceding edge in c. At time d[v], vertices of c form a white path from v to u (since v is the first vertex discovered in c). By white-path theorem, u is descendant of v in depth-first forest. Therefore, (u, v) is a back edge.

Strongly Connected Components

2014/11Algorithm AnalysisL11.34 Strongly Connected Components Given directed graph G = (V, E). A strongly connected component (SCC) of G is a maximal set of vertices C ⊆ V such that for all u, v ∈ C, there is a path form u to v and a path form v to u. Example:

2014/11Algorithm AnalysisL11.35 Transposed Form of Directed Graph Algorithm uses G T = transpose of G. G T = (V, E T ), E T = {(u, v) : (v, u) ∈ E}. –G T is G with all edges reversed. Can create G T in Θ(V + E) time if using adjacency lists. Observation: G and G T have the same SCC’s. (u and v are reachable from each other in G if and only if reachable from each other in G T.)

2014/11Algorithm AnalysisL11.36 Component Graph G SCC = (V SCC, E SCC ). –V SCC has one vertex for each SCC in G. –E SCC has an edge if there is an edge between the corresponding SCC’s in G. SCC for example (2 slides before):

2014/11Algorithm AnalysisL11.37 G SCC is a dag Lemma: G SCC is a dag. More formally, let C and C' be distinct SCC’s in G, let u, v ∈ C, u’, v’ ∈ C’, and suppose there is a path from u to u’ in G. Then there cannot also be a path from v’ to v in G. Proof: (informal) Draw a diagram. If you insert a path form v’ to v, you will see a circle covering u, u’, v and v’.

2014/11Algorithm AnalysisL11.38 Constructing G SCC using DFS SCC(G) 1.call DFS(G) to compute finishing times f [u] for all u 2.compute G T 3.call DFS(G T ), but in the main loop, consider vertices in order of decreasing f [u] (as computed in first DFS) 4.output the vertices in each tree of the depth- first forest formed in second DFS as a separate SCC

2014/11Algorithm AnalysisL11.39 Constructing G SCC for Example 1.Do DFS on G 2.Create G T 3.DFS on G T (roots blackened)

2014/11Algorithm AnalysisL11.40 Correctness of SCC Construction Idea: By considering vertices in second DFS in decreasing order of finishing times from first DFS, we are visiting vertices of the component graph in topological sort order. To prove that it works, first deal with 2 notational issues: –If we will be discussing d[u] and f [u]. These always refer to first DFS. Extend notation for d and f to sets of vertices U ⊆ V: –d(U) = min u ∈ U {d[u]} (earliest discovery time) –f (U) = max u ∈ U {f [u]} (latest finishing time)

2014/11Algorithm AnalysisL11.41 Important Observation Lemma: Let C and C’ be distinct SCC’s in G = (V, E). Suppose there is an edge (u, v) ∈ E such that u ∈ C and v ∈ C’. Then f(C) > f(C’)

2014/11Algorithm AnalysisL Corollary Corollary Let C and C’ be distinct SCC’s in G = (V, E). Suppose there is an edge (u, v) ∈ E T, where u ∈ C and v ∈ C’. Then f(C) f(C’). Corollary Let C and C’ be distinct SCC’s in G = (V, E), and suppose that f(C) > f(C’). Then there cannot be an edge from C to C’ in G T.

2014/11Algorithm AnalysisL11.43 Correctness of SCC Construction (cont.) When we do the second DFS, on G T, we start with SCC C such that f (C) is maximal. The second DFS starts from some x ∈ C, and it visits all vertices in C. Corollary says that since f(C) > f(C’) for all other C’ ≠ C, there are no edges from C to any other C’ in G T. Therefore, DFS will visit only vertices in C. Which means that the depth-first tree rooted at x contains exactly the vertices of C.