BFS并通过邻接矩阵找到最短路径

发布于 2025-02-01 18:16:36 字数 1313 浏览 3 评论 0原文

我正在尝试实现BFS并通过邻接矩阵找到最短的路径。但是BF几乎总是返回错误的结果。我在做什么错?

private static bool BFS(int[,] adj, int src, int dest, int v, int[] pred, int[] dist)
        {
            Queue<int> queue = new Queue<int>();
            bool[] visited = new bool[v];
            for (int i = 0; i < v; i++)
            {
                visited[i] = false;
                dist[i] = int.MaxValue;
                pred[i] = -1;
            }
            visited[src] = true;
            dist[src] = 0;
            queue.Enqueue(src);

            while (queue.Count != 0)
            {
             
               int u= queue.Dequeue();

                for (int i = 0; i < adj.GetLength(0); i++)
                {
                    if (visited[i] == false && adj[u,i]!=0)
                    {
                        visited[i] = true;
                        dist[i] = dist[u] + 1;
                        pred[i] = u;
                        queue.Enqueue(i);

                        // stopping condition (when we
                        // find our destination)
                        if (i == dest)
                            return true;
                    }
                    visited[u] = true;
                }
            }
            return false;
        }
    }

I'm trying to implement BFS and finding the shortest path through an adjacency matrix. But BFS almost always returns the result of false. What am I doing wrong?

private static bool BFS(int[,] adj, int src, int dest, int v, int[] pred, int[] dist)
        {
            Queue<int> queue = new Queue<int>();
            bool[] visited = new bool[v];
            for (int i = 0; i < v; i++)
            {
                visited[i] = false;
                dist[i] = int.MaxValue;
                pred[i] = -1;
            }
            visited[src] = true;
            dist[src] = 0;
            queue.Enqueue(src);

            while (queue.Count != 0)
            {
             
               int u= queue.Dequeue();

                for (int i = 0; i < adj.GetLength(0); i++)
                {
                    if (visited[i] == false && adj[u,i]!=0)
                    {
                        visited[i] = true;
                        dist[i] = dist[u] + 1;
                        pred[i] = u;
                        queue.Enqueue(i);

                        // stopping condition (when we
                        // find our destination)
                        if (i == dest)
                            return true;
                    }
                    visited[u] = true;
                }
            }
            return false;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2025-02-08 18:16:36
for (int i = 0; i < adj.GetLength(0); i++)

这对我来说很奇怪。应该不是:

for (int i = 0; i < adj.GetLength( u ); i++)
for (int i = 0; i < adj.GetLength(0); i++)

This looks wierd to me. Should it not be:

for (int i = 0; i < adj.GetLength( u ); i++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文