Graph representation

Similar documents
Design and Analysis of Algorithms

Graph Representation

Chapter 22. Elementary Graph Algorithms

Elementary Graph Algorithms

Chapter 22 Elementary Graph Algorithms

Representations of Graphs

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Basic Graph Algorithms

Graph: representation and traversal

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

DFS & STRONGLY CONNECTED COMPONENTS

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Graph Search. Adnan Aziz

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Outlines: Graphs Part-2

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

COT 6405 Introduction to Theory of Algorithms

Elementary Graph Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Graph Algorithms. Definition

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Design and Analysis of Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Introduction to Algorithms. Lecture 11

CSI 604 Elementary Graph Algorithms

Unit 2: Algorithmic Graph Theory

Graph implementations :

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Announcements. HW3 is graded. Average is 81%

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Graph: representation and traversal

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Basic Graph Algorithms (CLRS B.4-B.5, )

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Data Structures and Algorithms. Werner Nutt

W4231: Analysis of Algorithms

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Data Structures and Algorithms. Chapter 7. Graphs

Strongly Connected Components

Review: Graph Theory and Representation

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Chapter 22: Elementary Graph Algorithms. Definitions:

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

22.3 Depth First Search

Figure 1: A directed graph.

csci 210: Data Structures Graph Traversals

22.1 Representations of graphs

Sample Solutions to Homework #4

Algorithm Design and Analysis

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Topic 12: Elementary Graph Algorithms

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

Homework Assignment #3 Graph

Announcements Problem Set 4 is out!

Graph Representations and Traversal

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Graph. Vertex. edge. Directed Graph. Undirected Graph

Minimum Spanning Trees Ch 23 Traversing graphs

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Algorithm Design and Analysis

csci 210: Data Structures Graph Traversals

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Algorithm Design and Analysis

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

CSC263 Week 8. Larry Zhang.

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

TIE Graph algorithms

Suggested Study Strategy

CS 310 Advanced Data Structures and Algorithms

Directed Graphs (II) Hwansoo Han

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Analysis of Algorithms Prof. Karen Daniels

Transcription:

Graph Algorithms 1

Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: 1. Adjacency lists. 2. Adjacency matrix. When expressing the running time of an algorithm, it s often in terms of both V and E. Example: O(V + E). 2

Adjacency lists Graph representation 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.) 3

Graph representation If edges have weights, can put the weights in the lists. Weight: w : E R We ll use weights later on for spanning trees and shortest paths. Space: (V + E). Time: to list all vertices adjacent to u: Θ(degree(u)). Time: to determine if (u, v) E: O(degree(u)). 4

Graph representation Adjacent matrix 5

Breadth-first search BFS is one of the simplest algorithms for searching a graph and the archetype for many important graph algorithms. Prim s minimum-spanning tree algorithm Shortest-paths algorithm. BFS expands the frontier between discovered and undiscovered vertices uniformly across the breath of the frontier. That is, the algorithm discovers all vertices at distance k from s before discovering any vertices at distance k + 1. 6

Breadth-first search Input: Graph G = (V, E), either directed or undirected, and source vertex s V. Output: d[v] = distance (smallest # of edges) from s to v, for all v V. π[v] = u such that (u, v) is last edge on path from s to v. u is v s predecessor. set of edges {(π[v], v) : v = s} forms a tree. 7

Breadth-first search BFS colors each vertex white, gray, or black. All vertices start out white and may later become gray and then black. A vertex is discovered the first time it is encountered during the search, at which time it becomes nonwhite. If (u, v) E and vertex u is black, then vertex v is either gray or black; that is, all vertices adjacent to black vertices have been discovered. Gray vertices may have some adjacent white vertices; they represent the frontier between discovered and undiscovered vertices. 8

Breadth-first search BFS(G, s) for each vertex u V[G] {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 9

Breadth-first search r s t u 0 v w x y Q s 0 10

Breadth-first search r s t u 1 0 1 v w x y Q w r 1 1 11

Breadth-first search r s t u 1 0 2 1 2 v w x y Q r t x 1 2 2 12

Breadth-first search r s t u 1 0 2 2 1 2 v w x y Q t x v 2 2 2 13

Breadth-first search r s t u 1 0 2 3 2 1 2 v w x y Q x v u 2 2 3 14

Breadth-first search r s t u 1 0 2 3 2 1 2 3 v w x y Q v u y 2 3 3 15

Breadth-first search r s t u 1 0 2 3 2 1 2 3 v w x y Q u y 3 3 16

Breadth-first search r s t u 1 0 2 3 2 1 2 3 v w x y Q y 3 17

Breadth-first search r s t u 1 0 2 3 2 1 2 3 v w x y Q 18

Breadth-first search 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. Each vertex gets a finite d value monotonically increasing over time. Time = O(V + E). O(V) because every vertex enqueued at most once (since only white vertices are enqueued). 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. 19

Depth-first search Input: G = (V, E), directed or undirected. Output: 2 timestamps on each vertex:. d[v] = discovery time. f [v] = finishing time These will be useful for other algorithms later on. Can also compute π[v]. Will methodically explore every edge.. Start over from different vertices as necessary. As soon as we discover a vertex, explore from it.. Unlike BFS, which puts a vertex on a queue so that we explore from it later. 20

Depth-first search 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. (start and finish time for each vertex). For all v, d[v] < f [v]. In other words, 1 d[v] < f [v] 2 V. 21

Depth-first search 22

Depth-first search Classification of edges 1. Tree edges are edges in the depth-first forest. Edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v). 2. Back edge are those edges (u, v) connecting a vertex u to an ancestor v in depth-first tree. Self-loops, which may occur in directed graphs, are considered to be back edges. 3. Forward edges are those edges (u, v), where v is a descendant of u, but not a tree edge. 4. Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees. Only tree edges form the edges in the depth-first trees, or depth-first forest. 23

Depth-first search 1/ u v w x y z 24

Depth-first search u v w 1/ 2/ x y z 25

Depth-first search u v w 1/ 2/ 3/ x y z 26

Depth-first search u v w 1/ 2/ 4/ 3/ x y z 27

Depth-first search u v w 1/ 2/ B 4/ 3/ x y z 28

Depth-first search u v w 1/ 2/ B 4/5 3/ x y z 29

Depth-first search u v w 1/ 2/ B 4/5 3/6 x y z 30

Depth-first search u v w 1/ 2/7 B 4/5 3/6 x y z 31

Depth-first search u v w 1/ 2/7 F B 4/5 3/6 x y z 32

Depth-first search u v w 1/8 2/7 F B 4/5 3/6 x y z 33

Depth-first search u 1/8 v 2/7 9/ w F B 4/5 3/6 x y z 34

Depth-first search u 1/8 v 2/7 9/ w F B C 4/5 3/6 x y z 35

Depth-first search u 1/8 v 2/7 9/ w F B C 4/5 3/6 10/ x y z 36

Depth-first search u 1/8 v 2/7 9/ w F B C B 4/5 3/6 10/ x y z 37

Depth-first search u 1/8 v 2/7 9/ w F B C B 4/5 3/6 10/11 x y z 38

Depth-first search u 1/8 v 2/7 w 9/12 F B C B 4/5 3/6 10/11 x y z 39

Depth-first search Time = O(V + E).. Similar to BFS analysis. 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. 40

y z s t 3/6 2/9 1/10 11/16 B F C B Results of DFS of a directed graph. 4/5 C 7/8 C 12/13 C 14/15 x w v u B s z F C C v t C B u y x C w Redrawn with all tree and forward edges going down within a depth-first tree and all back edges going up from a descendant to an ancestor. 41

x t z v u y w x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t) Intervals for the discovery time and finishing time of each vertex. If two intervals overlap, then one is nested within the other, and the vertex corresponding to the smaller interval is a descendant of the vertex corresponding 42 to the later.

Depth-first search Theorem (Parenthesis theorem) [Refer to the interval graph.] 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. So, d[u] < d[v] < f [u] < f [v] cannot happen. Like parentheses:. OK: ( ) [ ] ( [ ] ) [ ( ) ]. Not OK: ( [ ) ] [ ( ] ) 43

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 > a., i.e., not comparable between a and b. 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. 44

Topological sort 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.) TOPOLOGICAL-SORT(V, E) call DFS(V, E) to compute finishing times f [v] for all v V, output vertices in order of decreasing finish times Don t need to sort by finish times.. Can just output vertices as they re finished and understand that we want the reverse of this list.. Or put them onto the front of a linked list as they re finished. When done, the list contains vertices in topologically sorted order. Time: (V + E). 45

Topological sort Example: dag of dependencies for putting on goalie equipment: 46

Topological sort Order: 26 socks 24 shorts 23 hose 22 pants 21 skates 20 leg pads 14 t-shirt 13 chest pad 12 sweater 11 mask 6 batting glove 5 catch glove 4 blocker 47

Topological sort (Tree edge) If there is a cycle the following always happens: v discovered first and (u, v) exists. 48

Topological sort Correctness: Just need to show if (u, v) E (edge of a dag), then the result of topological sort gives f [v] < f [u]. When we explore (u, v) (within topological sort and within DFS(G)), u is gray. Now consider all the possible colors of v and show f [v] < f [u] for legal colors of dag. Is v gray, too? No, because then v would be ancestor of u. (u, v) is a back edge. contradiction of previous lemma (dag has no back edges). Is v white? Then becomes descendant of u. By parenthesis theorem, d[u] < d[v] < f [v] < f [u]. Is v black? Then v is already finished. Since we re exploring (u, v), we have not yet finished u. Therefore, f [v] < f [u]. 49

Topological sort Topological-sort(G) 1. Call DFS(G) to compute finishing time f[v] for each vertex v 2. As each vertex is finished, insert it onto the front of a linked list 3. Return the linked list of vertices 50

Strongly connected components A SCC is a cluster of vertices that are reachable from each other. Idea: If we reverse the direction of arrows and call DFS with vertices in order of decreasing f [u] each tree vertices corresponds to a SCC. 51

Strongly connected components * Computation for G T can be done in a manner similar to DFS. 52

Strongly connected components 53

Strongly connected components If it does then v, u, v, u are all in the same SCC. 54

Strongly connected components SCC(G) call DFS(G) to compute finishing times f [u] for all u compute G T call DFS(G T ), but in the main loop, consider vertices in order of decreasing f [u] (as computed in first DFS) output the vertices in each tree of the depth-first forest formed in second DFS as a separate SCC Time: Θ(V + E) 55

Strongly connected components T in GDS(G T ) 56