Slammer 0 Newbie Poster

Hi there.

I need to write a recursive DFS function for graph which is implemented as follows:

struct nodes_arr
{
    unsigned int position[50000];
    unsigned int amount[50000];
    node *next[50000];
} nodes_table[] = {0};
struct node
{
    unsigned int position;
    unsigned int amount;
    node *next;
};

as a result i have a graph which for example looks like:

1:100->5:50->12:50
2:200->7:110->6:70->11:20
3:300->8:165->7:110
5:50->11:20->10:30
6:70
7:110->9:100
8:165
9:100
10:30
11:20
12:50
15:0->29:50
29:50

where first column is nodes_table->position (vertice) : nodes_table->amount (pointless in my problem here), and then pointers to structures (nodes_table->next) of nodes, if vertice links to another vertice.

Everything was fine for me till now when i need to write recursive DFS for this graph.

Given is total graph size. And count which in this example will be 3. Count stands for main (parent) vertices to whose are added child vertices from top. (in example - for vertices 1, 2, 3 all other vertices may be only as children)

Okey, returning to my problem:

1. I created array filled with 0's visited[50000] = {0} (50000 - because vertices position number is available from 1 to 50000) which we will mark with 1 if vertice is reachable and visited from first count vertices (in example from 1 to 3)
2. then tried to write recursive DFS

int visited[50000] = {0};

void error2_check(int count, int size)
{
    for(int i=0; i<size+1; i++)
        dfs(i, nodes_table->position[i]);
}

void dfs(int i, int pos)
{
    visited[pos] = 1;
    if(nodes_table->next[i] != NULL)
    {
        for(node *p = nodes_table->next[i]; p != NULL; p=p->next)
        {
            if(visited[p->position] == 0)
                dfs(i, p->position);
            if(p->next == NULL) i++;
        }
    }
    else {
        if(i<size)
        {
            i++;
            dfs(i, nodes_table->position[i]);
        }
    }
}

but it works wrong - it reaches unreachable vertices 15 and 29 :-(
here is visited array output: 0 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 (0)
Maybe You could help me out to find mistake in function.
Appreciate your help and sorry for my writing mistakes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.