简体   繁体   English

使用bfs的图中节点之间的距离

[英]Distance between nodes in a graph using bfs

I've written the following code: 我编写了以下代码:

void bfs(graph *g, int start)
{
    int i;
    int visited[MAXVERTS], next;
    for (i = 0; i < g -> nodes; i++)
        visited[i] = 0;
    visited[start] = 1;
    printf("%d", start);
    queuePtr q;
    q = QueueCreate();
    QueueEnter(q,start);
    while(!QueueIsEmpty(q))
    {
        next=QueueDelete(q);
        node *p=g->adjList[next];
        while(p)
        {  
            if(!visited[p->index])  
                visited[p->index] = 1;  
            QueueEnter(q,p->index);  
        }  
        p=p->link;  
    }  
}  

What do I need to add to make it calculate the distance between two nodes in a graph? 我需要添加什么以使其计算图形中两个节点之间的距离? I've been trying and I can't get it to work. 我一直在尝试,但无法正常工作。

Well, I implemented that same code some time ago, but can't find it. 好吧,我前段时间实现了相同的代码,但找不到它。 Amyway, the Dijkstra's algorithm is what you need. Amyway,您需要Dijkstra的算法。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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