History of Graphs

ClassS04CS141 | recent changes | Preferences


Revision 2 . . May 4, 2004 7:54 pm by Neal
Revision 1 . . May 4, 2004 7:52 pm by Neal
  

Difference (from prior major revision) (no other diffs)

Changed: 4c4
[Koenigsberg bridge problem].
[Koenigsberg bridge problem].

Added: 26a27


Changed: 60,124c61,62

Depth-first search (DFS)




Depth-first search is a "backtracking" method of exploring a graph.
It searches from vertex to vertex, backtracking when it encounters
vertices that have already been discovered. Here is pseudo-code:


DFS(graph G, vertex v)
Array<int> visited(0);
DFS1(G, v, visited);
end

DFS1(graph G, vertex v, Array<int>& visited)
if (visited[v] == 1) return
visited[v] = 1
for each neighbor w of v do
DFS1(G, w, visited)
end


We demonstrated DFS on an example.
DFS on classifies the edge into two kinds:
* DFS tree edges -- these are the edges (v,w) such that DFS1(G, w, visited) is \
called directly from within DFS1(G, v, visited) when w is not yet visited.
* back edges -- these are the other edges.

The tree edges form a tree (essentially a recursion tree for the DFS).
For any back edge (u,v), either u is a descendant of v in the tree or
vice versa.

In a graph with N vertices and M edges, the running time for DFS
is O(N+M), because the work done for each vertex is proportional
to the degree of the vertex, and the sum of the vertex degrees
is 2M.

Breadth-first search




DFS is called "depth first" because it descends into the graph as quickly as possible.

Breadth-search first, in contrast, explores nodes by following a "wave front". It explores
in order of their distance from the start vertex.


BFS(graph G, vertex v)

FIFO_Queue Q;
Array<int> distance(-1);

distance[v] = 0;
insert vertex v into the queue Q.

while (Q is not empty)
take the next vertex W off the front of the queue Q
for each neighbor X of W do
if (distance[X] == -1) then
distance[X] = distance[W] + 1
add vertex X to the tail of the queue Q
end


We demonstrated BFS on an example,
and noted that it ran in time O(N+M)
on any graph with N vertices and M edges.

References



* Chapter 6 of GoodrichAndTomassia .

ClassS04CS141 | recent changes | Preferences
Search: