cse101_10_24_11

# cse101_10_24_11 - Look at path from u to w. U -> z...

This preview shows pages 1–2. Sign up to view the full content.

Depth-first search 1. Exploring an undirected graph a. Procedure explore(G,4) Input graph G = (V,E); node u in V Output: visited[w] is set to true for all nodes w reachable from u Visited[u] = true For each(u,w) in E: if not visited[w]: explore(G,w) b. Example a->g->f b->c->g c->d->b->i d->c->i e->j->k do basic DFS algorithm, visit first node, put in queue the rest of the nodes, then go through the queue, add their neighbors to the queue and keep going. Tree edges are edges in E that are traversed during the search Back edges are not traversed they lead back to nodes already visited c. Does the algorithm ever halt? Explore (G,w) is called at most once for each node w, hereafter, visited[w] = true. d. Does it find all nodes reachable from u? Claim: for all k>=0, explore will find every node within distance k of u Proof: induction on k. Base case k = 0. There is one node at distance 0. From u: u itself. Explore starts there. General case: suppose explore visits all nodes at distance <= k from u. let w be any node at distance k + 1.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Look at path from u to w. U -> z ->w Z is at distance k. therefore z is visited, and during explore(G,Z) w will be noticed 2. Depth-first search a. Def: u, w are connected if there is a path from u to w. A,F are connected Def: A connected component is the set of all nodes reachable from a particular starting point. Ex. The example graph has three connected components b. Explore(G,u) identifies the connected component containing u. To see more o the graph, restart explore at a node that hasn’t been visited. Procedure dfs(G) For all u in V: Visited[u] = false; For all u in V: If not visited[u]: Explore (G,u); c. Ex: run DFS in reverse alphabetical order of nodes Explore(I): I->N Explore(h): h->g->c Explore(f): f->e->a->b d. Time analysis of DFS d.i. Explore(G,u) is called exactly once for each node u. d.ii. The time it takes ignoring recursion is O(degree(u)) + O(1) Total time: sigma from u to V, degree(u) + abs(v) d.iii....
View Full Document

## This note was uploaded on 01/09/2012 for the course CSE 101 taught by Professor Staff during the Spring '08 term at UCSD.

### Page1 / 2

cse101_10_24_11 - Look at path from u to w. U -> z...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online