Вы находитесь на странице: 1из 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

Breadth First Search - Code Problem: find length of shortest path from s to each node Let u.d represent length of shortest path from nodes to node u Remember: length is number of edges from s to u

Code: BFS(V, E, s) -- Initialize all nodes as unvisited for each node u loop u.d := -1 end loop

-- Mark first node as seen -- What does the value 0 represent? -- Put first node on queue of nodes to visit s.d := 0 enqueue(Q, s)

-- While there are more nodes to visit while not ismpty(Q) loop u := dequeue(Q) -- dequeues and returns front

-- Process vertex u, as needed for each vertex v adjacent to u loop -- Process edge (u, v), as needed if v.d = -1 then -- Not yet visited

v.d := u.d + 1 -- How many times per node? enqueue(Q, v) end if end loop

end loop

Breadth First Search - Example Example - BFS(V, E, A): Description: [BFS Example]

Breadth First Search - 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:

Don't use a queue Instead, mark nodes as to their status White: not yet seen (initial color) Gray: seen, but not finished Node u is finished if we have visited all nodes reachable from u Black: finished

For some algs, 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.d and u.f - start and finish times

It also calculates: u.d discovery time stamp = time when first visited u.f finish time stamp = time when last visited (ie all nodes reachable from u have been visited

Code: time := 0; -- Global! DFS(G) for each vertex u in G loop u.color := white end loop

for each vertex u in G loop if u.color = white then DFS-Visit(G, u) end if end loop end DFS

DFS-Visit(G, u)

u.d := ++time u.color := gray -- process vertex u, as needed for each vertex v adjacent to u loop -- process edge (u, v), as needed if v.color = white DFS-Visit(G, v) end if end loop u.color := black u.f := ++time end DFS-Visit

Example DFS Example (From CLRS IM?):

Description: [DFS Example]

Adjacency list for G: A: B, C, D B: C, E C: E D: C, F E: A F: C G: D, F, H H: C

Let's trace DFS from node A, and add colors and times

DFS - 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, v) G, u <v Useful for ordering events, 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, white) form a tree

Вам также может понравиться