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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
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.
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.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
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.
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 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
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?
CSCE 411H Design and Analysis of Algorithms Set 2: Brute Force & Exhaustive Search Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 2 1.
Tirgul 8 Graph algorithms: Strongly connected components.
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.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
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.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
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.
Shortest Path Algorithms
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.
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.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
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.
CSCE 411 Design and Analysis of Algorithms Set 2: Brute Force & Exhaustive Search Dr. J. Michael Moore Fall 2014 CSCE 411, Fall 2014: Set 2 1 Based primarily.
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 Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Graph Algorithms.
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.
Properties and Applications of Depth-First Search Trees and Forests
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.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
 2004 SDU 1 Lecture5-Strongly Connected Components.
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
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
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
Applications of DFS Topological sort (for directed acyclic graph)
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.
Presentation transcript:

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

2 Directed Graphs Let V be a finite set and E a binary relation on V, that is, E  VxV. Then the pair G=(V,E) is called a directed graph. The elements in V are called vertices. The elements in E are called edges. Self-loops are allowed, i.e., E may contain (v,v).

3 Undirected Graphs Let V be a finite set and E a subset of { e | e  V, |e|=2 }. Then the pair G=(V,E) is called an undirected graph. The elements in V are called vertices. The elements in E are called edges, e={u,v}. Self-loops are not allowed, e  {u,u}={u}.

4 Adjacency By abuse of notation, we will write (u,v) for an edge {u,v} in an undirected graph. If (u,v) in E, then we say that the vertex v is adjacent to the vertex u. For undirected graphs, adjacency is a symmetric relation.

5 Graph Representations Adjacency lists Adjacency matrix

6 Adjacency List Representation a cd b e bca b c d e ade + Space-efficient: just O(|V|) space for sparse graphs - Testing adjacency is O(|V|) in the worst case

7 Adjacency Matrix Representation a cd b e a b c d e a b c d e Can check adjacency in constant time - Needs  (|V| 2 ) space

8 Graph Traversals Ways to traverse or search a graph such that every node is visited exactly once

9 Breadth-First Search

10 Breadth First Search (BFS) Input: A graph G = (V,E) and source node s in V for each node v do mark v as unvisited od mark s as visited enq(Q,s) // first-in first-out queue Q while Q is not empty do u := deq(Q) for each unvisited neighbor v of u do mark v as visited; enq(Q,v); od

11 BFS Example Visit the nodes in the order: s a, d b, c Workout the evolution of the state of queue. a c d b s

12 BFS Tree We can make a spanning tree rooted at s by remembering the "parent" of each node

13 Breadth First Search #2 Input: G = (V,E) and source s in V for each node v do mark v as unvisited parent[v] := nil mark s as visited parent[s] := s enq(Q,s) // FIFO queue Q

14 Breadth First Search #2 while Q is not empty do u := deq(Q) for each unvisited neighbor v of u do mark v as visited parent[v] := u enq(Q,v)

15 BFS Tree Example a c d b s

16 BFS Trees BFS tree is not necessarily unique for a given graph Depends on the order in which neighboring nodes are processed

17 BFS Numbering During the breadth-first search, assign an integer to each node Indicate the distance of each node from the source s

18 Breadth First Search #3 Input: G = (V,E) and source s in V for each node v do mark v as unvisited parent[v] := nil d[v] := infinity mark s as visited parent[s] := s d[s] := 0 enq(Q,s) // FIFO queue Q

19 Breadth First Search #3 while Q is not empty do u := deq(Q) for each unvisited neighbor v of u do mark v as visited parent[v] := u d[v] := d[u] + 1 enq(Q,v)

20 BFS Numbering Example a c d b s d = 0 d = 1 d = 2

21 Shortest Path Tree Theorem: BFS algorithm visits all and only nodes reachable from s sets d[v] equal to the shortest path distance from s to v, for all nodes v, and sets parent variables to form a shortest path tree

22 Proof Ideas Use induction on distance from s to show that the d-values are set properly. Basis: distance 0. d[s] is set to 0. Induction: Assume true for all nodes at distance x-1 and show for every node v at distance x. Since v is at distance x, it has at least one neighbor at distance x-1. Let u be the first of these neighbors that is enqueued.

23 Proof Ideas u c d vs dist=x-1 dist=x dist=x-1 dist=x+1 Key property of shortest path distances: If v has distance x, it must have a neighbor with distance x-1, no neighbor has distance less than x-1, and no neighbor has distance more than x+1

24 Proof Ideas Fact: When u is dequeued, v is still unvisited. because of how queue operates and since d never underestimates the distance By induction, d[u] = x-1. When v is enqueued, d[v] is set to d[u] + 1= x

25 BFS Running Time Initialization of each node takes O(V) time Every node is enqueued once and dequeued once, taking O(V) time When a node is dequeued, all its neighbors are checked to see if they are unvisited, taking time proportional to number of neighbors of the node, and summing to O(E) over all iterations Total time is O(V+E)

26 Depth-First Search

27 Depth-First Search Input: G = (V,E) for each node u do mark u as unvisited od; for each unvisited node u do recursiveDFS(u); od; recursiveDFS(u): mark u as visited; for each unvisited neighbor v of u do recursiveDFS(v) od

28 DFS Example Example taken from

29 Example taken from

30 Disconnected Graphs What if the graph is disconnected or is directed? We call DFS on several nodes to visit all nodes purpose of second for-loop in non-recursive wrapper a cb d e

31 DFS Forest By keeping track of parents, we want to construct a forest resulting from the DFS traversal.

32 Depth-First Search #2 Input: G = (V,E) for each node u do mark u as unvisited parent[u] := nil for each unvisited node u do parent[u] := u // a root call recursive DFS(u) recursiveDFS(u): mark u as visited for each unvisited neighbor v of u do parent[v] := u call recursiveDFS(v)

33 Further Properties of DFS Let us keep track of some interesting information for each node. We will timestamp the steps and record the discovery time, when the recursive call starts finish time, when its recursive call ends

34 Depth-First Search #3 Input: G = (V,E) for each node u do mark u as unvisited parent[u] := nil time := 0 for each unvisited node u do parent[u] := u // a root call recursive DFS(u) recursiveDFS(u): mark u as visited time++ disc[u] := time for each unvisited neighbor v of u do parent[v] := u call recursiveDFS(v) time++ fin[u] := time

35 Running Time of DFS initialization takes O(V) time second for loop in non-recursive wrapper considers each node, so O(V) iterations one recursive call is made for each node in recursive call for node u, all its neighbors are checked; total time in all recursive calls is O(E) Total time is O(V+E)

36 Nested Intervals Let interval for node v be [disc[v],fin[v]]. Fact: For any two nodes, either one interval precedes the other or one is enclosed in the other [Reason: recursive calls are nested.] Corollary: v is a descendant of u in the DFS forest iff the interval of v is inside the interval of u.

37 Classifying Edges Consider edge (u,v) in directed graph G = (V,E) w.r.t. DFS forest tree edge: v is a child of u back edge: v is an ancestor of u forward edge: v is a descendant of u but not a child cross edge: none of the above

38 Example of Classifying Edges a c b efd in DFS forest not in DFS forest tree forward back cross 1/8 4/5 2/7 3/610/11 9/12 a/b disc./finish. time tree edge: v child of u back edge: v ancestor of u forward edge: v descendant of u, but not child cross edge: none of the above

39 DFS Application: Topological Sort Given a directed acyclic graph (DAG), find a linear ordering of the nodes such that if (u,v) is an edge, then u precedes v. DAG indicates precedence among events: events are graph nodes, edge from u to v means event u has precedence over event v Partial order because not all events have to be done in a certain order

40 Precedence Example Tasks that have to be done to eat breakfast: get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat. Certain events must happen in a certain order (ex: get bowl before pouring milk) For other events, it doesn't matter (ex: get bowl and get spoon)

41 Precedence Example get glass pour juice get bowl pour cereal pour milk get spoon eat breakfast Order: glass, juice, bowl, cereal, milk, spoon, eat.

42 Why Acyclic? Why must a directed graph be acyclic for the topological sort problem? Otherwise, no way to order events linearly without violating a precedence constraint.

43 Idea for Topological Sort Alg. What does DFS do on a DAG? eat juice glass milk cereal bowl spoon consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat

44 Topological Sort Algorithm input: DAG G = (V,E) 1. call DFS on G to compute finish[v] for all nodes v 2. when each node's recursive call finishes, insert it on the front of a linked list 3. return the linked list Running Time: O(V+E)

45 Correctness of T.S. Algorithm Show that if (u,v) is an edge, then v finishes before u. Case 1: v is finished when u is discovered. Then v finishes before u finishes. Case 2: v is not yet discovered when u is discovered. Claim: v will become a descendant of u and thus v will finish before u finishes. Case 3: v is discovered but not yet finished when u is discovered. Show not possible…

46 Correctness of T.S. Algorithm v is discovered but not yet finished when u is discovered. Then u is a descendant of v. But that would make (u,v) a back edge and a DAG cannot have a back edge (the back edge would form a cycle). Thus Case 3 is not possible.

47 DFS Application: Strongly Connected Components Consider a directed graph. A strongly connected component (SCC) of the graph is a maximal set of nodes with a (directed) path between every pair of nodes Problem: Find all the SCCs of the graph.

48 What Are SCCs Good For? packaging software modules construct directed graph of which modules call which other modules A SCC is a set of mutually interacting modules pack together those in the same SCC from

49 SCC Example h fae g cbd four SCCs

50 How Can DFS Help? Suppose we run DFS on the directed graph. All nodes in the same SCC are in the same DFS tree. But there might be several different SCCs in the same DFS tree. Example: start DFS from node h in previous graph

51 Main Idea of SCC Algorithm DFS tells us which nodes are reachable from the roots of the individual trees Also need information in the "other direction": is the root reachable from its descendants? Run DFS again on the "transpose" graph (reverse the directions of the edges)

52 SCC Algorithm input: directed graph G = (V,E) 1.call DFS(G) to compute finishing times 2.compute G T // transpose graph 3.call DFS(G T ), considering nodes in decreasing order of finishing times 4.each tree from Step 3 is a separate SCC of G

53 SCC Algorithm Example h fae g cbd input graph - run DFS

54 After Step 1 c bg a f d e h fin(c) fin(d)fin(b)fin(e)fin(a)fin(h)fin(g) fin(f) Order of nodes for Step 3: f, g, h, a, e, b, d, c

55 After Step 2 h fae g cbd transposed input graph - run DFS with specified order of nodes

56 After Step 3 g he f a d SCCs are {f,h,g} and {a,e} and {b,c} and {d}. b c

57 Running Time of SCC Algorithm Step 1: O(V+E) to run DFS Step 2: O(V+E) to construct transpose graph, assuming adjacency list rep. Step 3: O(V+E) to run DFS again Step 4: O(V) to output result Total: O(V+E)

58 Correctness of SCC Algorithm Proof uses concept of component graph, G SCC, of G. Nodes are the SCCs of G; call them C 1, C 2, …, C k Put an edge from C i to C j iff G has an edge from a node in C i to a node in C j

59 Example of Component Graph {a,e}{f,h,g} {d} {b,c} based on example graph from before

60 Facts About Component Graph Claim: G SCC is a directed acyclic graph. Why? Suppose there is a cycle in G SCC such that component C i is reachable from component C j and vice versa. Then C i and C j would not be separate SCCs.

61 Facts About Component Graph Consider any component C during Step 1 (running DFS on G) Let d(C) be earliest discovery time of any node in C Let f(C) be latest finishing time of any node in C Lemma: If there is an edge in G SCC from component C' to component C, then f(C') > f(C).

62 Proof of Lemma Case 1: d(C') < d(C). Suppose x is first node discovered in C'. By the way DFS works, all nodes in C' and C become descendants of x. Then x is last node in C' to finish and finishes after all nodes in C. Thus f(C') > f(C). C'C

63 Proof of Lemma Case 2: d(C') > d(C). Suppose y is first node discovered in C. By the way DFS works, all nodes in C become descendants of y. Then y is last node in C to finish. Since C'  C, no node in C' is reachable from y, so y finishes before any node in C' is discovered. Thus f(C') > f(C). C'C

64 SCC Algorithm is Correct Prove this theorem by induction on number of trees found in Step 3 (calling DFS on G T ). Hypothesis is that the first k trees found constitute k SCCs of G. Basis: k = 0. No work to do !

65 SCC Algorithm is Correct Induction: Assume the first k trees constructed in Step 3 correspond to k SCCs, and consider the (k+1)st tree. Let u be the root of the (k+1)st tree. u is part of some SCC, call it C. By the inductive hypothesis, C is not one of the k SCCs already found and all nodes in C are unvisited when u is discovered. By the way DFS works, all nodes in C become part of u's tree

66 SCC Algorithm is Correct Show only nodes in C become part of u's tree. Consider an outgoing edge from C. w z u C' C GT:GT: w z u C G:

67 SCC Algorithm is Correct By lemma, in Step 1 the last node in C' finishes after the last node in C finishes. Thus in Step 3, some node in C' is discovered before any node in C is discovered. Thus all of C', including w, is already visited before u's DFS tree starts w z u C' C G: