Given a graph represented by G(V, E) where V is the vertices and E represents the edges, we can do a Depth First Search Algorithm (DFS) on any node/vertex. The DFS will mark the current node visited and visit the node using using the (*visit) function (C++ function pointer), and recursively call itself with the connected edges.
void traverseDepthFirstSearch(int node, void(*visit)(int)) {
link t;
(*visit)(k);
visited[k] = 1; // mark the node as visited
for (t = adj[k]; t != NULL; t = t->next) {
if (!visited[t->v]) { // avoid cycle
traverseDepthFirstSearch(t->v, visit);
}
}
}
The algorithimic complexity is O(N) where N is the number of nodes connected to the given vertex. The space complexity is also O(N).
–EOF (The Ultimate Computing & Technology Blog) —
193 wordsLast Post: Beginner's Introduction to PHP Memcached
Next Post: The Minimum Absolute Difference Algorithm of an Array
