CS302 --- Graph Searching
Graph Searching
I. Graph Searching Terminology
A. Visited Vertices: Vertices that have already been visited
B. Fringe Vertices: Vertices adjacent to visited vertices but not yet visited
C. Unseen Verticies: Vertices that haven't been encountered at all yet
II. Depth-first search: A search of a graph in which fringe vertices are
visited in LIFO order (last-in, first-out):
A. Depth-first search requires O(V + E) time if implemented with
adjacency lists
B. Pseudo-Code
void dfs( Vertex v ) {
v.visited = true;
for each w adjacent to v
if ( !w.visited )
dfs( w );
}
III. Breadth-first search: A search of a graph in which fringe vertices are
visited in FIFO order (first-in, first-out):
A. Strategy: Remove vertices from the front of a queue and add their
adjacent vertices to the back of the queue. This strategy ensures
that while level l vertices are being processed, level l+1 vertices
are being added to the back of the queue. The level l+1 vertices
will not be visited until all level l vertices have been exhausted.
B. Pseudo-Code
bfs(vertex v) {
VtxQueue.queue(v);
while (!VtxQueue.empty()) do
current_vtx = VtxQueue.dequeue()
for each w adjacent to v
if ( !w.visited )
w.visited = true
VtxQueue.queue(w)
}
C. Time Complexity
1. Breadth-first search requires O(V + E) time if implemented with
adjacency lists
2. Breadth-first search requires O(V2) time if implemented
with an adjacency matrix