所有图中的2个节点之间的路径节点、图中、路径

2023-09-11 02:27:12 作者:余生愛浪

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

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.