【问题标题】:Finding path between nodes using BFS in C language在 C 语言中使用 BFS 查找节点之间的路径
【发布时间】:2017-09-22 20:09:16
【问题描述】:

我是 C 语言的新手,在使用 Java 之后使用指针变得更加困难???? 我试图编写一个使用广度优先搜索在图中的两个节点之间查找路径(不是必需的最小值)的代码。 这是我的代码:

#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();
    }
}

我知道它很大,但请介意一次。我得到的问题是在我输入所有值之后和 dfs() 函数调用之前的分段错误!请帮忙。

为了理解:我使用了列表数组。每个数组索引表示一个节点,列表表示它连接到的所有边。例如:如果我的图表有 1->2、1->3、2-3 条边; graph[1] 将有一个列表 2->3->NULL。并且 graph[2] 将有 3->NULL。

谢谢。

编辑 正如 Aditi 所指出的,错误出现在 nextnode 函数运行 while 循环的行中。修改代码后为

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

程序运行完美。 谢谢!

【问题讨论】:

  • 首先,为什么for 循环的索引在for(i=1; i&lt;=n; i++) 行中从1 开始并以n 结束
  • 因为当用户输入节点数为 5 时,我假设创建的图的节点编号从 1 到 5,因此分配图 [1]、图 [2].. .graph[5] 最初为 NULL,(因为尚未输入边)我在 for 循环中使用 i=1 到 N。

标签: c segmentation-fault graph-theory breadth-first-search path-finding


【解决方案1】:

我认为您要做的不是 graph[i] = NULL 而是 graph[i]->next = NULL

【讨论】:

  • 不,最初 graph[i] 为空。一旦输入来自 I 的新节点,我将首先检查它是否为 null,如果是则 graph[i] 用一个节点初始化,否则我将遍历到 graph[i] 的末尾并插入一个新的节点在那里。
  • 在函数 nextnode() 中,当 c->next 指向 NULL 但根据您的代码 c 不指向 NULL 时,您递增 c。现在 c 指向 NULL。在下一次迭代中,您尝试访问 c->data 但 c 现在指向 NULL。我认为这是您的错误的根源。
  • 非常感谢!代码运行成功然后✌
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多