You are on page 1of 6

Graphs - BFS and DFS

Overview Simple Algorithms: Breadth-first and Depth-first Search

Common Graph Algorithms Two common graph algorithms: Breadth-first Search (BFS) Depth-first Search (DFS)

Search: find a node with a given characteristic Example: search a call graph to find a call to a particular procedure

Both do more than searching

Breadth First Search Algorithm Common graph algoriths uses a breadth-first approach Example Problem: Search all nodes for a node containing a given value Example Problem: Find length of shortest path from node s to all other nodes Example Problem: Find shortest path from node s to all other nodes

Breadth-first Algorithm: Depth-first: visit all neighbors before visiting neighbors of your neighbors

Start from node s. Visit all neighbors of node s. Then visit all of their neighbors, if not already visited Continue until all nodes visited

Intuition of Implementation Keep a queue of nodes to visit When visiting node u, put neighbors of u on the queue, if neighbor not yet visited Queue contains all nodes that have been seen, but not yet visited

d represent length of shortest path from nodes to node u Remember: length is number of edges from s to u Code: BFS(V.Code Problem: find length of shortest path from s to each node Let u.Breadth First Search . s) -.Initialize all nodes as unvisited for each node u loop u.d = -1 then -.d := 0 enqueue(Q. s) -.How many times per node? enqueue(Q.Put first node on queue of nodes to visit s.d := u. v) end if end loop .d + 1 -.What does the value 0 represent? -. as needed if v.Process edge (u. as needed for each vertex v adjacent to u loop -. v).While there are more nodes to visit while not ismpty(Q) loop u := dequeue(Q) -.Not yet visited v.dequeues and returns front -. E.Process vertex u.Mark first node as seen -.d := -1 end loop -.

Example Example . A): Description: [BFS Example] Breadth First Search .BFS(V.end loop Breadth First Search . E.Discussion Time: O(V + E) What happens if not all nodes are connected? Can also calculate path from s to each node How? Depth First Search Algorithm Another common type of graph algorithm is a depth-first algorithm Depth-first: visit all neighbors of a neighbor before visiting your other neighbors First visit all nodes reachable from node s (ie visit neighbors of s and their neighbors) Then visit all (unvisited) nodes that are neighbors of s Repeat until all nodes are visited Algorithm: .

color := white end loop for each vertex u in G loop if u. -.f finish time stamp = time when last visited (ie all nodes reachable from u have been visited Code: time := 0. u) end if end loop end DFS DFS-Visit(G.Don't use a queue Instead.d and u. the nodes also have start and finish stamps These stamps give the time (ie step in the algorithm) when each node is first seen and when it is finished Depth First Algorithm: Code This algorithm performs a depth first traversal of G also calculate u.Global! DFS(G) for each vertex u in G loop u.d discovery time stamp = time when first visited u. but not finished Node u is finished if we have visited all nodes reachable from u Black: finished For some algs. mark nodes as to their status White: not yet seen (initial color) Gray: seen.f .color = white then DFS-Visit(G. u) .start and finish times It also calculates: u.

E C: E D: C. F E: A F: C G: D.color := black u. as needed for each vertex v adjacent to u loop -. C. v). F. v) end if end loop u.process vertex u.color = white DFS-Visit(G.color := gray -.u. as needed if v.f := ++time end DFS-Visit Example DFS Example (From CLRS IM?): Description: [DFS Example] Adjacency list for G: A: B. H H: C .d := ++time u. D B: C.process edge (u.

for example Times and colors help visualize the algorithm's operation and help with other algs (which we don't study) Do we need the 3 colors? Useful for other algorithms The edges whose end colors are (gray.Let's trace DFS from node A. u <v Useful for ordering events. white) form a tree . v) ∈ G.Discussion Performance: DFS-Visit called once for each Edge: Θ(| E|) Initialization is Θ(| V|) Total: Θ(| V| + |E|) Uses: Topological sort: sort vertices so that for all edges (u. and add colors and times DFS .