Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.

Similar presentations


Presentation on theme: "Graph traversals / cutler1 Graph traversals Breadth first search Depth first search."— Presentation transcript:

1 Graph traversals / cutler1 Graph traversals Breadth first search Depth first search

2 Graph traversals / cutler2 Some applications Is G a tree Is G connected? Does G contain a cycle? Find connected components? Topological sorting

3 Graph traversals / cutler3 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)

4 Graph traversals / cutler4 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  (|V| 2 )

5 Graph traversals / cutler5 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.

6 Graph traversals / cutler6 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);

7 Graph traversals / cutler7 BFS - main while not (empty(Q)) do u:=head(Q); for each v in adj[u] do if color[v]=white then O(E) color[v]:=gray; dist[v]:=dist[u]+1; parent[v]:=u; enqueue(Q, v); dequeue(Q); color[u]:=black; end BFS

8 Graph traversals / cutler8 BFS example 0 1 10 1 r s t u r s t u v w x y v w x y r s t u r s t u v w x y       0 sw r 10   1  12 2   r t xt x v 2 2 2  

9 Graph traversals / cutler9 BFS example 2 1 102 1 2 1 2 12 1 2 023 12 r s t u r s t u v w x y v w x y r s t u r s t u v w x y      0.0. x v uv u y 0      u yy  now y is removed from the Q and colored black

10 Graph traversals / cutler10 Analysis of BFS Initialization is  (|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|)

11 Graph traversals / cutler11 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  (|V|+|E|) we use an adjacency list representation. If we used an adjacency matrix it would be  (|V| 2 )

12 Graph traversals / cutler12 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.

13 Graph traversals / cutler13 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]

14 Graph traversals / cutler14 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

15 Graph traversals / cutler15 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

16 Graph traversals / cutler16 DFS example (1) x y z u v w 1/ u v w 1/ x y z 2/ u v w 1/ x y z 2/ 3/ u v w 1/ x y z 2/ 3/4/ B

17 Graph traversals / cutler17 DFS example (2) u v w x y z u v w x y z 4/5 1/ 2/ 3/ B u v w 4/5 3/6 1/2/ B 4/5 3/6 1/2/7 B

18 Graph traversals / cutler18 DFS example (3) u v w x y z u v w x y z u v w x y z u v w F 4/5 3/6 1/82/7 B F 4/5 9 3/6 1/82/7 B F 4/5 9 3/6 10 1/82/7 B F 4/5 9 3/6 10/11 1/82/7 B C C C

19 Graph traversals / cutler19 DFS example (4) x y z F 4/5 9/12 3/6 10/11 1/82/7 B C u v w

20 Graph traversals / cutler20 Analysis DFS is  (|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  (|E|). Worst case time  (|V|+|E|)

21 Graph traversals / cutler21 Some applications Is undirected G connected? Do dfsVisit(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)

22 Graph traversals / cutler22 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

23 Graph traversals / cutler23 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)

24 Graph traversals / cutler24 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 dfsVisit(v). If all vertices are reached and no back edges G is a tree. O(V)

25 Graph traversals / cutler25 Some applications 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))

26 Graph traversals / cutler26 Topological sort Given a DAG G Topological sort is a linear ordering of all the vertices of directed graph 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


Download ppt "Graph traversals / cutler1 Graph traversals Breadth first search Depth first search."

Similar presentations


Ads by Google