Spring 2015 Lecture 10: Elementary 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, 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.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
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.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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?
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 –
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?
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.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
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.
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.
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.
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.
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.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
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.
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
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Basic Graph Algorithms
Elementary Graph Algorithms
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Spring 2015 Lecture 10: Elementary Graph Algorithms 2IL50 Data Structures Spring 2015 Lecture 10: Elementary Graph Algorithms

Networks and other graphs road network computer network execution order for processes process 1 process 2 process 3 process 5 process 4 process 6

Graphs: Basic definitions and terminology A graph G is a pair G = (V, E) V is the set of nodes or vertices of G E ⊂ VxV is the set of edges or arcs of G If (u, v) ∈ E then vertex v is adjacent to vertex u undirected graph (u, v) is an unordered pair ➨ (u, v) = (v, u) self-loops forbidden directed graph (u, v) is an ordered pair ➨ (u, v) ≠ (v, u) self-loops possible

Graphs: Basic definitions and terminology Path in a graph: sequence ‹v0, v1, …, vk› of vertices, such that (vi-1, vi) ∈ E for 1 ≤ i ≤ k Cycle: path with v0 = vk Length of a path: number of edges in the path Distance between vertex u and v: length of a shortest path between u and v (∞ if v is not reachable from u) path of length 4 cycle of length 3 not a path a path

Graphs: Basic definitions and terminology An undirected graph is connected if every pair of vertices is connected by a path. A directed graph is strongly connected if every two vertices are reachable from each other. (For every pair of vertices u, v we have a directed path from u to v and a directed path from v to u.) connected components strongly connected components

Check appendix B.4 for more basic definitions Some special graphs Tree connected, undirected, acyclic graph Every tree with n vertices has exactly n-1 edges DAG directed, acyclic graph Check appendix B.4 for more basic definitions

Graph representation Graph G = (V, E) Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E (works for both directed and undirected graphs) 5 4 3 2 1 3 1 4 5 2

Graph representation Graph G = (V, E) Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E (works for both directed and undirected graphs) 5 4 3 2 1 1 5 4 3 2

Graph representation Graph G = (V, E) Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E Adjacency matrix |V| x |V| matrix A = (aij) (also works for both directed and undirected graphs) 5 4 3 2 1 1 2 3 4 5 aij = if (i, j) ∈ E 0 otherwise

Graph representation Graph G = (V, E) Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E Adjacency matrix |V| x |V| matrix A = (aij) (also works for both directed and undirected graphs) 5 4 3 2 1 1 2 3 4 5 aij = if (i, j) ∈ E 0 otherwise

Adjacency lists vs. adjacency matrix 3 1 4 5 2 1 2 3 4 5 Adjacency lists Adjacency matrix Space Time to list all vertices adjacent to u Time to check if (u, v) ∈ E Θ(V + E) Θ(V2) Θ(degree(u)) Θ(V) better if the graph is sparse … use V for |V| and E for |E| Θ(degree(u)) Θ(1)

Searching a graph: BFS and DFS Basic principle: start at source s each vertex has a color: white = not yet visited (initial state) gray = visited, but not finished black = visited and finished

Searching a graph: BFS and DFS Basic principle: start at source s each vertex has a color: white = not yet visited (initial state) gray = visited, but not finished black = visited and finished color[s] = gray; S = {s} while S ≠ ∅ do remove a vertex u from S for each v ∈ Adj[u] do if color[v] == white then color[v] = gray; S = S υ {v} color[u] = black BFS and DFS choose u in different ways; BFS visits only the connected component that contains s. u s u u and so on …

BFS and DFS Breadth-first search BFS uses a queue ➨ it first visits all vertices at distance 1 from s, then all vertices at distance 2, … s

BFS and DFS Breadth-first search BFS uses a queue ➨ it first visits all vertices at distance 1 from s, then all vertices at distance 2, … Depth-first search DFS uses a stack s s

Breadth-first search (BFS) BFS(G, s) for each u ≠ s do color[u] = white; d[u] =∞; π[u] = NIL color[s] = gray; d[s] =0; π[s] = NIL Q ← ∅ Enqueue(Q, s) while Q ≠ ∅ do u = Dequeue(Q) for each v ∈ Adj[u] do if color[v] == white then color[v] = gray; d[v] = d[u]+1; π[v] = u; Enqueue(Q, v) color[u] = black d(u) becomes distance from s to u π[u] becomes predecessor of u

BFS on an undirected graph Adjacency lists 1: 3 2: 6, 7, 5 3: 1, 6, 4 4: 5, 3 5: 2, 4, 8 6: 3, 2 7: 2, 9 8: 9, 5 9: 7, 8 10: 11 11: 10 Source s = 6 2 ∞ 2 7 1 ∞ 6 3 ∞ 9 1 ∞ 2 1 ∞ ∞ 3 ∞ 3 8 2 ∞ 11 4 2 ∞ 5 ∞ 10 Queue Q: 6 3 2 1 4 7 5 9 8

BFS on an undirected graph Adjacency lists 1: 3 2: 6, 7, 5 3: 1, 6, 4 4: 5, 3 5: 2, 4, 8 6: 3, 2 7: 2, 9 8: 9, 5 9: 7, 8 10: 11 11: 10 Source s = 6 2 2 7 ∞ 1 6 3 ∞ 9 1 2 1 ∞ 3 3 8 2 11 4 2 5 ∞ 10 Queue Q: 6 3 2 1 4 7 5 9 8 Note: BFS only visits the nodes that are reachable from s

0 or more nodes with d[∙] = d[u]+1 BFS: Properties Invariants Q contains only gray vertices gray and black vertices never become white again the queue has the following form: the d[∙] fields of all gray vertices are correct for all white vertices we have: (distance to s) > d[u] d[∙] = d[u] 0 or more nodes with d[∙] = d[u]+1 Enqueue Dequeue u

BFS: Analysis Invariants Q contains only gray vertices gray and black vertices never become white again This implies every vertex is enqueued at most once ➨ every vertex is dequeued at most once processing a vertex u takes Θ(1+|Adj[u]|) time ➨ running time at most ∑u Θ(1+|Adj[u]|) = O(V + E)

BFS: Properties After BFS has been run from a source s on a graph G we have each vertex u that is reachable from s has been visited for each vertex u we have d[u] = distance to s if d[u] < ∞, then there is a shortest path from s to u that is a shortest path from s to π[u] followed by the edge (π[u], u) Proof: follows from the invariants (details see book)

Depth-first search (DFS) DFS(G) for each u ∈ V do color[u] = white; π[u] = NIL time = 0 do if color[u] == white then DFS-Visit(u) DFS-Visit(u) color[u] = gray; d[u] =time; time = time + 1 for each v ∈ Adj[u] do if color[v] == white then π[v] = u; DFS-Visit(v) color[u] = black; f[u] =time; time = time + 1 time = global timestamp for discovering and finishing vertices d[u] = discovery time f[u] = finishing time

DFS on a directed graph Adjacency lists 1: 2, 4 2: 5 3: 5, 6 4: 2 5: 4 1: 2, 4 2: 5 3: 5, 6 4: 2 5: 4 6: -- f=7 f=6 d=0 d=1 f=11 f=10 2 d=8 d=9 1 3 6 4 5 d=2 d=3 f=5 f=4

DFS on a directed graph Adjacency lists 1: 2, 4 2: 5 3: 5, 6 4: 2 5: 4 1: 2, 4 2: 5 3: 5, 6 4: 2 5: 4 6: -- f=7 f=6 f=11 f=10 d=0 d=1 2 d=8 d=9 1 3 6 4 5 d=2 d=3 f=5 f=4 Note: DFS always visits all vertices

DFS: Properties DFS visits all vertices and edges of G Running time: 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. Θ(V + E)

DFS: Edge classification Tree edges edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v); the tree edges form a forest, the DF-forest Back edges edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree Forward edges non-tree edges (u, v) connecting a vertex u to a descendant v Cross edges all other edges f=7 f=6 d=0 d=1 f=11 f=10 d=8 2 d=9 1 3 6 4 5 d=2 d=3 f=5 f=4

DFS: Edge classification Tree edges edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v); the tree edges form a forest, the DF-forest Back edges edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree Forward edges non-tree edges (u, v) connecting a vertex u to a descendant v Cross edges all other edges Undirected graph ➨ (u, v) and (v, u) are the same edge; classify by first type that matches.

DFS: Properties DFS visits all vertices and edges of G Running time: 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. DFS of an undirected graph yields only tree and back edges. No forward or cross edges. Discovery and finishing times have parenthesis structure. Θ(V + E) [ ] { } [ { } ] { [ ] } { [ } ] [ { ] }

DFS: Discovery and finishing times Theorem In any depth-first search of a (directed or undirected) graph G = (V, E), for any two vertices u and v, exactly one of the following three conditions holds: the intervals [d[u], f[u]] and [d[v], f[v]] are entirely disjoint ➨ neither of u or v is a descendant of the other the interval [d[u], f[u]] entirely contains the interval [d[v], f[v]] ➨ v is a descendant of u the interval [d[v], f[v]] entirely contains the interval [d[u], f[u]] ➨ u is a descendant of v d[u] d[v] f[v] f[u] time not possible d[v] d[u] f[u] f[v] time d[u] d[v] f[v] f[u] time d[v] d[u] f[u] f[v] time

DFS: Discovery and finishing times Proof (sketch): assume d[u] < d[v] case 1: v is discovered in a recursive call from u ➨ v becomes a descendant of u recursive calls are finished before u itself is finished ➨ f[v] < f[u] case 2: v is not discovered in a recursive call from u ➨ v is not reachable from u and not one of u’s descendants ➨ v is discovered only after u is finished ➨ f[u] < d[v] ➨ u cannot become a descendant of v since it is already discovered ■

DFS: Discovery and finishing times Corollary v is a proper descendant of u if and only if d[u] < d[v] < f[v] < f[u]. Theorem (White-path theorem) 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. (Except for u which was just colored gray.) (See the book for details and proof.)

Using depth-first search … Topological sort Using depth-first search …

execution order for processes Topological sort Input: directed, acyclic graph (DAG) G = (V, E) Output: a linear ordering of v1 ,v2 ,…, vn of the vertices such that if (vi ,vj ) ∈ E then i < j DAGs are useful for modeling processes and structures that have a partial order Partial order a > b and b > c ➨ a > c but may have a and b such that neither a > b nor b > a execution order for processes process 1 process 2 process 3 process 5 process 4 process 6

Topological sort Input: directed, acyclic graph (DAG) G = (V, E) Output: a linear ordering of v1 ,v2 ,…, vn of the vertices such that if (vi ,vj ) ∈ E then i < j DAGs are useful for modeling processes and structures that have a partial order Partial order a > b and b > c ➨ a > c but may have a and b such that neither a > b nor b > a a partial order can always be turned into a total order (either a > b or b > a for all a ≠ b) (that’s what a topological sort does …)

Topological sort Input: directed, acyclic graph (DAG) G = (V, E) Output: a linear ordering of v1 ,v2 ,…, vn of the vertices such that if (vi ,vj ) ∈ E then i < j Every directed, acyclic graph has a topological order Lemma A directed graph G is acyclic if and only if DFS of G yields no back edges. v6 v5 v2 v3 v4 v1

Topological sort TopologicalSort(V, E) call DFS(V, E) to compute finishing time f[v] for all v ∈ E output vertices in order of decreasing finishing time 11 17 underwear socks 16 18 9 watch 10 12 13 pants shoes 15 14 1 shirt 6 belt 8 7 2 tie 5 3 jacket 4

Topological sort TopologicalSort(V, E) call DFS(V, E) to compute finishing time f[v] for all v ∈ E output vertices in order of decreasing finishing time 17 18 socks 16 11 underwear 12 15 pants 13 14 shoes 9 10 watch 4 3 jacket 2 5 tie 7 6 belt 1 8 shirt

Topological sort TopologicalSort(V, E) call DFS(V, E) to compute finishing time f[v] for all v ∈ E output vertices in order of decreasing finishing time Lemma TopologicalSort(V, E) produces a topological sort of a directed acyclic graph G = (V, E).

Topological sort Lemma TopologicalSort(V, E) produces a topological sort of a directed acyclic graph G = (V, E). Proof: To show: f[u] > f[v] Consider the intervals [d[∙], f[∙]] and assume f[u] < f[v] case 1: case 2: When DFS-Visit(u) is called, v has not been discovered yet. DFS-Visit(u) examines all outgoing edges from u, also (u, v). ➨ v is discovered before u is finished. Let (u, v) ∈ E be an arbitrary edge. d[v] d[u] f[u] f[v] ➨ u is a descendant of v ➨ G has a cycle d[u] d[v] f[v] f[u]

Topological sort Lemma TopologicalSort(V, E) produces a topological sort of a directed acyclic graph G = (V, E). Running time? we do not need to sort by finishing times just output vertices as they are finished ➨ Θ(V + E) for DFS and Θ(V) for output ➨ Θ(V + E)