所有图中的2个节点之间的路径 [英] All the paths between 2 nodes in graph

查看:255
本文介绍了所有图中的2个节点之间的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做出一个不知情的搜索(广度优先搜索)程序,它有两个节点,并返回它们之间的所有路径。

I have to make an uninformed search (Breadth-first-Search) program which takes two nodes and return all the paths between them.

public void BFS(Nod start, Nod end) {
            Queue<Nod> queue = new Queue<Nod>();
            queue.Enqueue(start);
            while (queue.Count != 0)
            {
                Nod u = queue.Dequeue();
                if (u == end)  break;
                else
                {
                    u.data = "Visited";
                    foreach (Edge edge in u.getChildren())
                    {
                        if (edge.getEnd().data == "")
                        {
                            edge.getEnd().data = "Visited";
                            if (edge.getEnd() != end)
                            {
                                edge.getEnd().setParent(u);
                            }
                            else
                            {
                                edge.getEnd().setParent(u);
                                cost = 0; 
                                PrintPath(edge.getEnd(), true);
                                edge.getEnd().data = "";
                                //return;
                            }
                        }
                        queue.Enqueue(edge.getEnd());
                    }
                }
            }
        }

我的问题是,我只得到两个路径,而不是一切,我不知道在我的code编辑什么,让他们所有。我的问题的输入是基于在此显示:

My problem is that i only get two paths instead of all and i don't know what to edit in my code to get them all. The input of my problem is based on this map :

推荐答案

在BFS算法,你不能在你找到一个解决办法停下来。一种想法是设定数据空所有你去过除第一个城市,让功能运行长一点。我没有时间给你写一个片断,但如果欧不明白这一点,我将至少写一个伪code。如果你不明白我的想法发表评论你的问题,我会尽量解释好。

In the BFS algorithm you must not stop after you find a solution. One idea is to set data null for all the cities you visited except the first one and let the function run a little bit longer. I don't have time to write you a snippet but if ou don't get it i will write at least a pseudocode. If you didn't understood my idea post a comment with your question and i will try to explain better.

这篇关于所有图中的2个节点之间的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆