Andew Tayloe
Andew Tayloe

Reputation: 25

Problem when implementing DFS for a graph

I implemented the structure of the graph in the programming language c++, which contains the number of nodes, a array of strings in which the information contents are placed, as well as the adjacency matrix.

typedef struct Graph
{
   int n;
   string nodes[MAX];
   double ms[MAX][MAX]; 
}GRAPH;

I want to make an algorithm for DFS graph traversal, I made it but there is an error.

void dfs(GRAPH* g)
{
    int visit[MAX] = {};
    void dfs_visit(int u) //Here is error: Expected ;
    {
        int v;
        cout << g->nodes[u];
        visit[u] = 1;
        for (v = 0; v < g->n; v++)
        {
            if (g->ms[u][v] && !visit[v])
            dfs_visit(v);
        }
        dfs_visit(0);
     }
 }

In the programming language c++ gives me an error that I need ; function code dfs_visit? Can someone help me how to solve this?

Thanks in advance!

Upvotes: 0

Views: 83

Answers (1)

ravenspoint
ravenspoint

Reputation: 20575

You need to decide which node you want to start searching from.

Here is the general plan for a recursive graph searching code

main function

  • Construct the graph
  • Select a starting node
  • Call the recursive function, passing in the starting node

recursive function

  • mark node passed in as visited
  • loop over adjacent nodes
    • if node not visited
      • call recursive function, passing in node

Upvotes: 0

Related Questions