简体   繁体   中英

Finding path between nodes using BFS in C language

I'm new to C language and it's been harder for me to work with pointers after working in Java😥 I was trying to write a code of finding a path (not necessary minimum) between two nodes in a graph using breadth-first-search. Here is my code :

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 200

void push(int a);
int  pop(void);
void bfs(int a,int b,int len);
int nextnode(int a);

typedef struct node{
    int data;
    struct node* next;
}node;

int res[MAXSIZE];
int visited[MAXSIZE];
int rear,front;
node* graph[MAXSIZE];
int len;


int path[MAXSIZE];


int nextnode(int a)
{

    if(graph[a]==NULL)
        return -1;
    else
    {

        struct node* c=graph[a];
        while(visited[c->data]!=1 && c!=NULL)
        {
            c=c->next;
        }
        if(c==NULL)
            return -1;
        else
            return c->data;

    }
}

void push(int a)
{
    path[rear]=a;
    rear++;
}
int pop()
{
    if(front==rear)
        return -1;
    int num=path[front];
    front++;
    return num;
}

int main()
{
    rear=0;
    len=0;
    front=0;
    int n,e;
    int i,a,b;
    printf("%s\n%s", "Inputting Graph... ","Enter number of nodes and edges: ");
    scanf("%d %d",&n,&e);
    printf("%s %d %s\n", "Graph Created with",n,"nodes without any edge.");
    printf("%s\n","Enter the edges in 1 2 format if an edge exist from Node 1 to Node 2" );
    for(i=1;i<=n;i++)
    {
        graph[i]=NULL;
        visited[i]=0;
    }
    struct node* new = (struct node*)malloc(sizeof(struct node));
    for(i=0;i<e;i++)
    {
        scanf("%d %d",&a,&b);
        new->data=b;
        new->next=NULL;
        struct node* curr=graph[a];
        if(curr==NULL)
        {
            graph[a]=new;
        }
        else
        {
            while(curr->next!=NULL)
            {
                curr=curr->next;
            }
            curr->next=new;
        }
    }

    printf("%s\n", "Graph Created Successfully.");
    printf("%s", "Enter the node numbers between which the path is to be found between:  ");
    scanf("%d %d",&a,&b);
    bfs(a,b,0);
    printf("Length is %d\n",len);
    for(i=1;i<=len;i++)
    {
        printf("%d\n",res[len]);
    }

}

void bfs(int a,int b,int len)
{
    int c;
    visited[a]=1;
    int flag=0;

    while(a!=-1)
    {
        c=nextnode(a);
        while(c!=-1)
        {
            c=nextnode(a);
            if(c==b)
            {
                flag=1;
                break;
            }
            push(c);
            visited[c]=1;
        }
        len++;
        res[len]=a;
        if(flag==1)
        {
            res[len]=b;
            break;
        }
        a=pop();
    }
}

I know it's huge, but please mind going through it once. The problem I'm getting is Segmentation Fault after I input all the values, and before dfs() function call! Please Help.

For understanding: I have used array of Lists. Each array index denotes a node and the list denotes all the edges it is connected to. eg: if my Graph has 1->2, 1->3, 2-3 edges; graph[1] will have a list 2->3->NULL. And graph[2] will have 3->NULL.

Thank you.

EDIT As pointed out by Aditi, the error was in the line where nextnode function ran the while loop. After changing the code to

        while(c != NULL && visited[c->data] == 1 )

the program ran flawlessly. Thanks!

我认为您要执行的操作不是graph [i] = NULL,而是graph [i]-> next = NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM