Search
 
SCRIPT & CODE EXAMPLE
 

CPP

dfs in tree using adjacency list

/* 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 << '
';
 
    // 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: max of 3 numbers in c++ 
Cpp :: what type is this c++ 
Cpp :: sort using comparator anonymous function c++ 
Cpp :: assign array to array 
Cpp :: cocos2d c++ linux 
Cpp :: C++ (.NET CLI) 
Cpp :: http://nv-study.ru/http://nv-study.ru/http://nv-study.ru/ 
Cpp :: 2000pp pp play osu std 
Cpp :: MPI_PUT 
Cpp :: c++ constructor initializing list 
Cpp :: default argument c++ 
Cpp :: print an array c++ 
Cpp :: find number of 1s in a binary cv::mat image 
Cpp :: JAJA 
Cpp :: stp 
Cpp :: sort array using stl 
Cpp :: cpp reference array 
Cpp :: MPI_Sendrecv 
Cpp :: decising how many numbers after comma c++ 
Cpp :: Error: C++14 standard requested but CXX14 is not defined 
Cpp :: Int main ( ) { int i,n; cinn; i=n; while(i=1) { i=i+5; i=i-6; } } 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: log like printf c++ 
Cpp :: how to use comparitor in priority queu in c++ 
Cpp :: Consider a pair of integers, (a,b). The following operations can be performed on (a,b) in any order, zero or more times: - (a,b) - ( a+b, b ) - (a,b) - ( a, a+b ) 
Cpp :: Integer Literrals in C++ Programming 
Cpp :: char * in c++ 
Cpp :: c++ sudoku solver 
Cpp :: declare a variable in cpp 
Cpp :: passare un array a una funzione 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =