C#中的Dijkstra算法出错



我是C#编程的新手,我试图在C#中实现Dijkstra算法,以获得两个节点之间的最短距离,但我遇到了以下错误-

错误CS1525:意外的符号void', expecting class"、delegate', enum"、interface', partial"或"struct"

prog.cs(54,16):错误CS1525:意外的符号int', expecting 类"、delegate', 枚举"、interface', 部分"或"结构"

prog.cs(54,21):错误CS514:意外符号[', expecting 。'或"{"

prog.cs(54,21):警告CS0658:","是无效的属性目标。此属性部分中的所有属性都将被忽略

这是我的代码:

using System;
using System.Collections.Generic;
namespace Dijkstras
{
    class Graph
    {
        int V = 9;
        int minDistance(int[] dist, bool[] sptSet)
        {
 
            int min = 100; int min_index=0;
            for (int v = 0; v < V; v++)
                if (sptSet[v] == false && dist[v] <= min)
                   { min = dist[v]; min_index = v;}
            return min_index;
        }
        int printSolution(int[] dist, int n)
         {
            Console.WriteLine("Vertex   Distance from Source");
            for (int i = 0; i < V; i++)
            {
                Console.Write(i); Console.Write("      ");
                Console.WriteLine(dist[i]);
            }return 0;
         }
        void dijkstra(int [,] graph , int src)
          {    //graph=new int[V,V];
                int [] dist=new int[V];    
                bool [] sptSet=new bool[V];
                for (int i = 0; i < V; i++)
                 {  dist[i] = 100;
                    sptSet[i] = false;
                 }
                dist[src] = 0;
                for (int count = 0; count < V-1; count++)
                     {
                        int u = minDistance(dist, sptSet);
                        sptSet[u] = true;
                        for (int v = 0; v < V; v++)
                         {  if (sptSet[v]==false && dist[u] != 100 && dist[u]+graph[u,v] < dist[v])
                                dist[v] = dist[u] + graph[u,v];
                         }
                     }
                printSolution(dist, V);
          }
    }
       
     public static void Main()
      {
                int [,] graph =new int[,] {{0, 4, 0, 0, 0, 0, 0, 8, 0},
                      {4, 0, 8, 0, 0, 0, 0, 11, 0},
                      {0, 8, 0, 7, 0, 4, 0, 0, 2},
                      {0, 0, 7, 0, 9, 14, 0, 0, 0},
                      {0, 0, 0, 9, 0, 10, 0, 0, 0},
                      {0, 0, 4, 0, 10, 0, 2, 0, 0},
                      {0, 0, 0, 14, 0, 2, 0, 1, 6},
                      {8, 11, 0, 0, 0, 0, 1, 0, 7},
                      {0, 0, 2, 0, 0, 0, 6, 7, 0}
                     };
                dijkstra(graph, 0);
     }
        
 }

有人能告诉我我的代码出了什么问题吗?

这里有几个问题。

  1. Main函数不在类内。在C#中,所有方法都必须在类内。在Graph类中移动Main
  2. 函数需要是静态的,这样才能从Main方法中引用它们
  3. 您的变量需要是静态的,这样才能从现在的静态函数中引用它们

作为2&3,您可以使用面向对象编程,并从Graph类中生成一个对象,而不是执行所有静态函数。这可以通过将主功能的最后一行更改为类似的内容来完成

Graph graphObject = new Graph();
graphObject.dijkstra(graph, 0);

然后你可以让一切都不静止。

完成所有这三个步骤,它就会编译。不过我没有检查正确性。

这里发现的第一个问题:

public static void Main()

它是在任何类之外声明的方法,只在命名空间中声明——这在C#中是做不到的。把它包装成一些类。

您已经将主方法放在了图类之外。在图类中移动主方法,并像这样调用dijkstra:

new Graph().dijkstra(graph, 0);

最新更新