algorithm - Complexity of non-recursiveDFS code -
i think complexity of code is: time : o (v) : v vertex space: o (v) : v vertex
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); (int v : graph.adj(vertex)) { if (!visited[v]) { visited[v] = true; stack.add(v); edgeto[v] = vertex; } } } }
please correct me if wrong
assuming graph.adj()
produce bounded number of vertices (maybe one), right.
however, if depends in way on total number of vertices present in system, not. if dependency linear, algorithm o(n^2).
generalizing, if f(n) average number of graph.adj()
per vertex, answer o(n*f(n)).
Comments
Post a Comment