Algorithm Implementation Flashcards

1
Q

Depth First Search

A

// A function used by DFS
void DFSUtil(int v, boolean visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v + “ “);

    // Recur for all the vertices adjacent to this
    // vertex
    Iterator<Integer> i = adj[v].listIterator();
    while (i.hasNext()) {
        int n = i.next();
        if (!visited[n])
            DFSUtil(n, visited);
    }
}
 
// The function to do DFS traversal.
// It uses recursive DFSUtil()
void DFS(int v)
{
    // Mark all the vertices as
    // not visited(set as
    // false by default in java)
    boolean visited[] = new boolean[V];
 
    // Call the recursive helper
    // function to print DFS
    // traversal
    DFSUtil(v, visited);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly