我覺得這段代碼的複雜性: 時間:O(V):v是頂點 空間:O(V):v是頂點複雜
public void dfs() {
Stack<Integer> stack = new Stack<Integer>();
stack.add(source);
while (!stack.empty()) {
int vertex = stack.pop();
System.out.println(" print v: " + vertex);
for (int v : graph.adj(vertex)) {
if (!visited[v]) {
visited[v] = true;
stack.add(v);
edgeTo[v] = vertex;
}
}
}
}
請糾正我,如果我錯了
[Wikipedia](http://en.wikipedia.org/wiki/Depth-first_search)回答它,不是嗎? – Dukeling