[Koenigsberg bridge problem]. |
[Koenigsberg bridge problem]. |
|
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:
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 searchDFS 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.
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 . |
Search the web for the [Koenigsberg bridge problem].
In the figure below, it is possible to trace a path with a pencil so that the pencil never leaves the paper, and each line segment in the drawing is crossed exactly once:
Consider the following diagrams:
The figure above is a drawing of a graph.
The letters represent nodes (also called vertices) of the graph. The lines connecting pairs of letters represent edges.
The question about figure 1 is equivalent to the following question: