Graph traversals Breadth first search Depth first search
Graph traversals ¬Breadth first search ¬Depth first search 3/11/2021 Graph traversals / cutler 1
Some applications ¬Is G a tree? ¬Is G bipartite? ¬Is G connected? ¬Does G contain a cycle? ¬Find connected components? ¬Topological sorting ¬Does G contain an articulation vertex? 3/11/2021 Graph traversals / cutler 2
Breadth first search ¬Given a graph G=(V, E) and a source vertex s, BFS explores the edges of G to “discover” (visit) each node of G reachable from s. ¬Idea - expand a frontier one step at a time. ¬Frontier is a FIFO queue (O(1) time to update) 3/11/2021 Graph traversals / cutler 3
Breadth first search ¬ Computes the shortest distance (dist) from s to any reachable node. ¬ Computes a breadth first tree (of parents) with root s that contains all the reachable vertices from s. ¬ To get O(|V|+|E|) we use an adjacency list representation. If we used an adjacency matrix it would be O(|V|2) 3/11/2021 Graph traversals / cutler 4
Coloring the nodes ¬We use colors (white, gray and black) to denote the state of the node during the search. ¬A node is white if it has not been reached (discovered). ¬Discovered nodes are gray or black. Gray nodes are at the frontier of the search. Black nodes are fully explored nodes. 3/11/2021 Graph traversals / cutler 5
BFS - initialize procedure BFS(G: graph; s: node; var color: carray; dist: iarray; parent: parray); for each vertex u do color[u]: =white; dist[u]: =¥; (V) parent[u]: =nil; end for color[s]: =gray; dist[s]: =0; init(Q); enqueue(Q, s); 3/11/2021 Graph traversals / cutler 6
BFS - main while not (empty(Q)) do u: =head(Q); for each v in adj[u] do if color[v]=white then color[v]: =gray; dist[v]: =dist[u]+1; parent[v]: =u; enqueue(Q, v); dequeue(Q); color[u]: =black; end BFS 3/11/2021 Graph traversals / cutler O(E) 7
BFS example r ¥ s 0 t u r t 1 s 0 ¥ ¥ 1 y v w s 0 ¥ x ¥ y ¥ ¥ s u wr ¥ ¥ v w s 0 r 1 ¥ x t 2 u ¥ rtx r 1 t 2 u ¥ 2 x ¥ y txv ¥ v 1 w 3/11/2021 2 x ¥ y 2 v 1 w Graph traversals / cutler 8
BFS example r 1 s 0. t u 2 r 3 xvu 1 s 0 t u 3 2 vuy 2 v r 1 uy 1 w s 0 t 2 2 ¥ y 2 x v u 3 r 1 1 2 3 w x y s t u 0 2 3 y 2 2 1 2 3 1 2 v w x y v w x now y is removed from the Q and colored black 3/11/2021 Graph traversals / cutler 3 y 9
Analysis of BFS ¬ Initialization is Q(|V|). ¬ Each node can be added to the queue at most once (it needs to be white), and its adjacency list is searched only once. At most all adjacency lists are searched. ¬ If graph is undirected each edge is reached twice, so loop repeated at most 2|E| times. ¬ If graph is directed each edge is reached exactly once. So the loop repeated at most |E| times. ¬ Worst case time O(|V|+|E|) 3/11/2021 Graph traversals / cutler 10
Depth First Search ¬Goal - explore every vertex and edge of G ¬We go “deeper” whenever possible. ¬Directed or undirected graph G = (V, E). ¬To get worst case time Q(|V|+|E|) we use an adjacency list representation. If we used an adjacency matrix it would be Q(|V|2) 3/11/2021 Graph traversals / cutler 11
Depth First Search ¬ Until there are no more undiscovered nodes. – Picks an undiscovered node and starts a depth first search from it. – The search proceeds from the most recently discovered node to discover new nodes. – When the last discovered node v is fully explored, backtracks to the node used to discover v. Eventually, the start node is fully explored. 3/11/2021 Graph traversals / cutler 12
Depth First Search ¬ In this version all nodes are discovered even if the graph is directed, or undirected and not connected ¬The algorithm saves: – A depth first forest of the edges used to discover new nodes. – Timestamps for the first time a node u is discovered d[u] and the time when the node is fully explored f[u] 3/11/2021 Graph traversals / cutler 13
DFS procedure DFS(G: graph; var color: carray; d, f: iarray; parent: parray); for each vertex u do color[u]: =white; parent[u]: =nil; (V) end for time: =0; for each vertex u do if color[u]=white then DFS-Visit(u); end if; end for end DFS 3/11/2021 Graph traversals / cutler 14
DFS-Visit(u) color[u]=gray; time: =time+1; d[u]: =time for each v in adj[u] do if color[v]=white then parent[v]: =u; DFS-Visit(v); end if; end for; color[u]: =black; time: =time+1; f[u]: =time; end DFS-Visit 3/11/2021 Graph traversals / cutler 15
DFS example (1) u v w 1/ x u y v 1/ 2/ z w u v 1/ 2/ x y u v 1/ 2/ w z w B 3/ x 3/11/2021 y z 4/ 3/ x y Graph traversals / cutler z 16
DFS example (2) u v 1/ u w 2/ 1/ B 4/5 3/ x y w 2/ B 4/5 3/6 z u 1/ v x w y z 2/7 B 4/5 3/6 x y 3/11/2021 v z Graph traversals / cutler 17
DFS example (3) u v 1/8 2/7 w 2/7 9 F B C 4/5 3/6 10 x y z 3/11/2021 v 1/8 F B 4/5 3/6 x y z u v w 1/8 u w 9 2/7 F B C 4/5 3/6 x y z u v w 1/8 2/7 9 F B C 4/5 3/6 10/11 x y z Graph traversals / cutler 18
DFS example (4) u 1/8 v w 2/7 9/12 F B C 4/5 3/6 10/11 x y z 3/11/2021 Graph traversals / cutler 19
Analysis ¬DFS is Q(|V|) (excluding the time taken by the DFS-Visits). ¬DFS-Visit is called once for each node v. Its for loop is executed |adj(v)| times. The DFS-Visit calls for all the nodes take Q(|E|). ¬Worst case time Q(|V|+|E|) 3/11/2021 Graph traversals / cutler 20
Some applications ¬Is undirected G connected? Do dfs. Visit(v). If all vertices are reached return yes, otherwise no. O(V + E) ¬Find connected components. Do DFS. Assign the nodes in a single component a unique component number. Theta(V+E) 3/11/2021 Graph traversals / cutler 21
Labeling the edges (digraph) ¬ Tree edges - those belonging to the forest ¬ Back edges - edges from a node to an ancestor in the tree. ¬ Forward edges - a non tree edge from a node to a descendant in the tree. ¬ Cross edges - the rest of the edges, between trees and subtrees ¬ When a graph is undirected its edges are tree or back edges for DFS, tree or cross for BFS 3/11/2021 Graph traversals / cutler 22
Classifying edges of a digraph ¬ (u, v) is: – Tree edge – if v is white – Back edge – if v is gray – Forward or cross - if v is black ¬ (u, v) is: – Forward edge – if v is black and d[u] < d[v] (v was discovered after u) – Cross edge – if v is black and d[u] > d[v] (u discovered after v) 3/11/2021 Graph traversals / cutler 23
More applications ¬ Does directed G contain a directed cycle? Do DFS if back edges yes. Time O(V+E). ¬ Does undirected G contain a cycle? Same as directed but be careful not to consider (u, v) and (v, u) a cycle. Time O(V) since encounter at most |V| edges (if (u, v) and (v, u) are counted as one edge), before cycle is found. ¬ Is undirected G a tree? Do dfs. Visit(v). If all vertices are reached and no back edges G is a tree. O(V) 3/11/2021 Graph traversals / cutler 24
Some applications ¬ Is G bipartite? ¬ Shortest distance from s to all the nodes in an acyclic graph – Do topological sort. Then, for every node u in the ordering (starting at s) use each edge (u, v) to compute the shortest distance from s to v, dist[v]. (dist[v] = min(dist[v], dist[u] + w(u, v)) Does G contain an articulation vertex? 3/11/2021 Graph traversals / cutler 25
Topological sort ¬ Given a DAG G ¬ Topological sort is a linear ordering of all the vertices of G such that if G contains the edge (u, v) u appears before v in the ordering TOPOLOGICAL-SORT(G) 1. Apply DFS(G) to compute f(v) for each vertex v 2. As each vertex is finished insert it at the front of a list 3. return the list 3/11/2021 Graph traversals / cutler 26
- Slides: 26