dfs of graph

i am new to c++ , and wanna learn more about struct , i was learning about DFS, i come through a article in GFG, but it used class in that , can someone provide me a dfs solution where struct is implemented.

1 Like

why do you need a struct for dfs? I think there are many easy implementations than using class or struct.

1 Like

As @sudip_95 said, you don’t necessarily need to use a class or structure to do a DFS on a graph. You can simply do it this way.

bool visited[V];
vector < int > G[V];    // adjacency list of V vertices

void dfs(int u)
{
    visited[u] = 1;

    for(int v: G[u])
        if(!visited[v])
            dfs(v); 
}

However, if you really want to use structures, the code in the geeksforgeeks page can be easily changed to use a structure.

See the following link for the code.

https://ideone.com/dxjWye

2 Likes
//