Depth-first search:
The strategy followed by depth-first search is, as its name implies, to search “deeper” in the graph whenever possible. Depth-first search explores edges out of the most recently discovered vertex that still has unexplored edges leaving it. Once all of ’s edges have been explored, the search “backtracks” to explore edges leaving the vertex from which was discovered. This process continues until we have discovered all the vertices that are reachable from the original source vertex. If any undiscovered vertices remain, then depth-first search selects one of them as a new source, and it repeats the search from that source. The algorithm repeats this entire process until it has discovered every vertex.
As in breadth-first search, whenever depth-first search discovers a vertex during a scan of the adjacency list of an already discovered vertex u, it records this event by setting ’s predecessor attribute : to u. Unlike breadth-first search, whose predecessor subgraph forms a tree, the predecessor subgraph produced by a depth-first search may be composed of several trees, because the search may repeat from multiple sources. Therefore, we define the predecessor subgraph of a depth-first search slightly differently from that of a breadth-first search:
The predecessor subgraph of a depth-first search forms a depth-first forest comprising several depth-first trees. As in breadth-first search, depth-first search colors vertices during the search to indicate their state. Each vertex is initially white, is grayed when it is discovered in the search, and is blackened when it is finished, when its adjacency list has been examined completely. This technique guarantees that each vertex ends up in exactly one depth-first tree, so that these trees are disjoint.
Besides creating a depth-first forest, depth-first search also timestamps each vertex. Each vertex v has two timestamps: the first timestamp v.d records when is first discovered (and grayed), and the second timestamp v.f records when the search finishes examining ’s adjacency list (and blackens v). These timestamps provide important information about the structure of the graph and are generally helpful in reasoning about the behavior of depth-first search.
The procedure DFS below records when it discovers vertex u in the attribute u.d and when it finishes vertex u in the attribute u.f . These timestamps are integers between 1 and 2 |V|, since there is one discovery event and one finishing event for each of the |V| vertices. For every vertex u. The following pseudocode is the basic depth-first-search algorithm. The input graph G may be undirected or directed. The variable time is a global variable that we use for timestamping.
Below figure illustrates the progress of DFS
The running time of DFS is θ(V + E).
Properties of depth-first search:
Depth-first search yields valuable information about the structure of a graph. Perhaps the most basic property of depth-first search is that the predecessor sub-graph G does indeed form a forest of trees, since the structure of the depth-first trees exactly mirrors the structure of recursive calls of DFS-VISIT. That is, u=v.π if and only ifDFS-VISIT(G,v) was called during a search of u’s adjacency list. Additionally, vertex v is a descendant of vertex u in the depth-first forest if and only if v is discovered during the time in which u is gray.
Another important property of depth-first search is that discovery and finishing times have parenthesis structure. If we represent the discovery of vertex u with a left parenthesis “(u” and represent its finishing by a right parenthesis “u)”, then the history of discoveries and finishings makes a well-formed expression in the sense that the parentheses are properly nested. Below figures are examples:
Java Implementation:
底下是針對上面介紹的 BFS 使用 Java 進行 algorithm 的實作, 首先需要準備一些會使用到的類別:
- enum EColor: 標示 Vertex 的顏色
- class Vertex: 代表 Graph 中的 Vertex, 從屬性
adjacent_list 可以知道該點的鄰近點.
- class GAlg: 在這個類別我們定義一些常用的 Graph algorithms, 這邊會用到的是 DFS
透過方法
GenDFSData() 與 GenDFSData2() 我們產生書上範例的 Graphs, 並呼叫方法 DFS() 進行 Deep-first search, 執行結果如下:
底下是針對上面介紹的 BFS 使用 Java 進行 algorithm 的實作, 首先需要準備一些會使用到的類別:
- enum EColor: 標示 Vertex 的顏色
- package intralg.ch22;
- public enum EColor {
- White("White"),Gray("Gray"),Black("Black");
- private String msg;
- private EColor(String msg){this.msg = msg;}
- @Override
- public String toString(){return msg;}
- }
- package intralg.ch22;
- import java.util.LinkedList;
- public class Vertex
{ - public Vertex(E data){this.data = data;adjacent_list=new LinkedList
>();} - public E data; // data
- public int d=Integer.MAX_VALUE; // distance or timestamp in DFS
- public int f=Integer.MAX_VALUE; // timestamp in DFS
- public EColor color=EColor.White; // White/Gray/Black
- public Vertex
pi; // parent, predecessor - public List
> adjacent_list; // adjacency list - public void addNeighborVtx(Vertex
...vs) - {
- for(Vertex
v:vs) adjacent_list.add(v); - }
- @Override
- public String toString()
- {
- //return String.format("{data:%s,distance:%d,color:%s,pi:%s}", data, d, color, pi!=null?pi.data:"NIL");
- return String.format("{data:%s,d:%d,f:%d,color:%s,pi:%s}", data, d, f, color, pi!=null?pi.data:"NIL");
- }
- }
- package intralg.ch22;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- public class GAlg {
- @SuppressWarnings("unchecked")
- public static List
> GenDFSData2() - {
- List
> vertex_list = new ArrayList >(); - Vertex
s = new Vertex ( 's'); vertex_list.add(s); - Vertex
t = new Vertex ( 't'); vertex_list.add(t); - Vertex
u = new Vertex ( 'u'); vertex_list.add(u); - Vertex
v = new Vertex ( 'v'); vertex_list.add(v); - Vertex
w = new Vertex ( 'w'); vertex_list.add(w); - Vertex
x = new Vertex ( 'x'); vertex_list.add(x); - Vertex
y = new Vertex ( 'y'); vertex_list.add(y); - Vertex
z = new Vertex ( 'z'); vertex_list.add(z); - y.addNeighborVtx(x);
- z.addNeighborVtx(y,w);
- s.addNeighborVtx(z,w);
- t.addNeighborVtx(v,u);
- x.addNeighborVtx(z);
- w.addNeighborVtx(x);
- v.addNeighborVtx(s,w);
- u.addNeighborVtx(v,t);
- return vertex_list;
- }
- @SuppressWarnings("unchecked")
- public static List
> GenDFSData() - {
- List
> vertex_list = new ArrayList >(); - Vertex
u = new Vertex ( 'u'); vertex_list.add(u); - Vertex
v = new Vertex ( 'v'); vertex_list.add(v); - Vertex
w = new Vertex ( 'w'); vertex_list.add(w); - Vertex
x = new Vertex ( 'x'); vertex_list.add(x); - Vertex
y = new Vertex ( 'y'); vertex_list.add(y); - Vertex
z = new Vertex ( 'z'); vertex_list.add(z); - u.addNeighborVtx(v,x);
- v.addNeighborVtx(y);
- w.addNeighborVtx(y,z);
- x.addNeighborVtx(v);
- y.addNeighborVtx(x);
- z.addNeighborVtx(z);
- return vertex_list;
- }
- public static int TIME = 0;
- public static void _DFS_VISIT(Vertex
u) - {
- TIME += 1;
- u.d = TIME;
- u.color = EColor.Gray;
- System.out.printf("\t\tDiscover %s...\n", u);
- for(Vertex
v:u.adjacent_list) - {
- if(v.color.equals(EColor.White))
- {
- _DFS_VISIT(v);
- v.pi = u;
- }
- }
- u.color = EColor.Black;
- TIME += 1;
- u.f = TIME;
- System.out.printf("\t[Info] Vertex=%s is visited!\n", u);
- }
- public static void DFS(List
> graph) - {
- // Initialize
- TIME = 0;
- for(Vertex
v:graph) - {
- v.pi = null;
- v.d = v.f = 0;
- v.color = EColor.White;
- }
- for(Vertex
v:graph) - {
- if(v.color.equals(EColor.White)) _DFS_VISIT(v);
- }
- /*for(Vertex
v:graph) - {
- if(v.color.equals(EColor.White)) System.out.printf("\t[Info] Vertex=%s is not visited!\n", v);
- }*/
- }
- public static void main(String args[])
- {
- List
> graph = GenDFSData2(); - DFS(graph);
- }
- }
沒有留言:
張貼留言