用於遍歷圖中的節點,有些相似於樹的深度優先遍歷。這裏惟一的問題是,與樹不一樣,圖形可能包含循環,所以咱們可能會再次來到同一節點。java
借用一個鄰接表和布爾類型數組(判斷一個點是否查看過,用於避免重複到達同一個點,形成死循環等),先將全部點按必定次序存入鄰接表,再經過迭代器,對鄰接表的linklist和布爾數組作出操做,從而達到不重複遞歸遍歷的效果。node
(鄰接表是表示了圖中與每個頂點相鄰的邊集的集合,這裏的集合指的是無序集)算法
(以上圖爲例的代碼)數組
1 //深度優先搜索
2 import java.io.*; 3 import java.util.*; 4
5 //This class represents a directed graph using adjacency list 6 //representation
7 class Graph 8 { 9 private int V; // No. of vertices 10
11 // Array of lists for Adjacency List Representation
12 private LinkedList<Integer> adj[]; 13
14 // Constructor
15 Graph(int v) 16 { 17 V = v; 18 adj = new LinkedList[v]; 19 for (int i=0; i<v; ++i) 20 adj[i] = new LinkedList(); 21 } 22
23 //Function to add an edge into the graph
24 void addEdge(int v, int w) 25 { 26 adj[v].add(w); // Add w to v's list.
27 } 28
29 // A function used by DFS
30 void DFSUtil(int v,boolean visited[]) 31 { 32 // Mark the current node as visited and print it
33 visited[v] = true; 34 System.out.print(v+" "); 35
36 // Recur for all the vertices adjacent to this vertex
37 Iterator<Integer> i = adj[v].listIterator(); 38 while (i.hasNext()) 39 { 40 int n = i.next(); 41 if (!visited[n]) 42 DFSUtil(n,visited); 43 } 44 } 45
46 // The function to do DFS traversal. It uses recursive DFSUtil()
47 void DFS() 48 { 49 // Mark all the vertices as not visited(set as 50 // false by default in java)
51 boolean visited[] = new boolean[V]; 52
53 // Call the recursive helper function to print DFS traversal 54 // starting from all vertices one by one
55 for (int i=0; i<V; ++i) 56 if (visited[i] == false) 57 DFSUtil(i, visited); 58 } 59
60 public static void main(String args[]) 61 { 62 Graph g = new Graph(4); 63
64 g.addEdge(0, 1); 65 g.addEdge(0, 2); 66 g.addEdge(1, 2); 67 g.addEdge(2, 0); 68 g.addEdge(2, 3); 69 g.addEdge(3, 3); 70
71 System.out.println("Following is Depth First Traversal"); 72
73 g.DFS(); 74 } 75 }
DFS複雜度分析 DFS算法是一一個遞歸算法,須要藉助一個遞歸工做棧,故它的空問複雜度爲O(V)。 遍歷圖的過程實質上是對每一個頂點查找其鄰接點的過程,其耗費的時間取決於所採用結構。 鄰接表表示時,查找全部頂點的鄰接點所需時間爲O(E),訪問頂點的鄰接點所花時間爲O(V),此時,總的時間複雜度爲O(V+E)。this