DFS for a n-ary tree (acyclic graph) represented as adjacency list

A tree consisting of n nodes is given, we need to print its DFS.

Examples :

Input : Edges of graph
        1 2
        1 3
        2 4
        3 5
Output : 1 2 4 3 5



A simple solution is to do implement standard DFS.

We can modify our approach to avoid extra space for visited nodes. Instead of using the visited array, we can keep track of parent. We traverse all adjacent nodes but the parent.

Below is the implementation :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* CPP code to perform DFS of given tree : */
#include <bits/stdc++.h>
using namespace std;
  
// DFS on tree
void dfs(vector<int> list[], int node, int arrival)
{
    // Printing traversed node
    cout << node << '\n';
  
    // Traversing adjacent edges
    for (int i = 0; i < list[node].size(); i++) {
  
        // Not traversing the parent node
        if (list[node][i] != arrival)
            dfs(list, list[node][i], node);
    }
}
  
int main()
{
    // Number of nodes
    int nodes = 5;
  
    // Adjacency list
    vector<int> list[10000];
  
    // Designing the tree
    list[1].push_back(2);
    list[2].push_back(1);
  
    list[1].push_back(3);
    list[3].push_back(1);
  
    list[2].push_back(4);
    list[4].push_back(2);
  
    list[3].push_back(5);
    list[5].push_back(3);
  
    // Function call
    dfs(list, 1, 0);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//JAVA Code For DFS for a n-ary tree (acyclic graph)
// represented as adjacency list
import java.util.*;
  
class GFG {
      
    // DFS on tree
    public static void dfs(LinkedList<Integer> list[],
                             int node, int arrival)
    {
        // Printing traversed node
        System.out.println(node);
       
        // Traversing adjacent edges
        for (int i = 0; i < list[node].size(); i++) {
       
            // Not traversing the parent node
            if (list[node].get(i) != arrival)
                dfs(list, list[node].get(i), node);
        }
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
  
        // Number of nodes
        int nodes = 5;
       
        // Adjacency list
        LinkedList<Integer> list[] = new LinkedList[nodes+1];     
          
        for (int i = 0; i < list.length; i ++){
            list[i] = new LinkedList<Integer>();
        }
          
        // Designing the tree
        list[1].add(2);
        list[2].add(1);
       
        list[1].add(3);
        list[3].add(1);
       
        list[2].add(4);
        list[4].add(2);
       
        list[3].add(5);
        list[5].add(3);
       
        // Function call
        dfs(list, 1, 0);
          
          
    }
}
// This code is contributed by Arnav Kr. Mandal.  

chevron_right



Output:

1
2
4
3
5

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.