【问题标题】:Creating Graph class in C#在 C# 中创建图形类
【发布时间】:2019-12-23 09:52:42
【问题描述】:

我的任务是在 C# 中创建 Graph 类,该类具有以下属性:

private List<string> vertices;
private List<List<int>> adjacencyMatrix;
private int n;

n 是节点数,我相信其余的都是不言自明的。 该类还应该有两个如下所示的方法:

Add(string vertex){}

AddConnection(string vertex1, string vertex2, int value){}

到目前为止,我想出了Add的方法(假设0代表顶点之间没有连接):

public void Add(string vertex)
{
     this.vertices.Add(vertex);
    List<int> temp = new List<int>();
    foreach(List<int> element in this.adjacencyMatrix)
    {
        element.Add(0);
    }
    foreach(string element in vertices)
    {
        temp.Add(0);
    }
    this.adjacencyMatrix.Add(temp);

    this.n++;
}

但我仍然不知道如何添加连接。 任何帮助将不胜感激

【问题讨论】:

  • 正常的方法是创建一个类NODE。每个节点都有一个属性:List 邻居。我通常在类中也有:int name(这是节点的编号)。然后图表是: List graph.
  • 是的,这是正常情况,在这个特殊的情况下,“名称”将是第一个列表中的字符串,然后矩阵将包含“邻居”。我只是不知道如何编码
  • 只是将新节点添加到图表中。你知道如何创建新课程吗?你知道如何将新项目添加到列表中(包括类)吗?节点新节点 = 新节点(); graph.Add(newNode);
  • 请查看编辑
  • 使用 List 而不是 List 会使代码更加复杂,因为您必须执行查找才能从一个节点移动到另一个节点。为什么要使用二维数组? List> adjacencyMatrix;首先,二维数组不适用于所有图形。如果您确实有一个矩形图,则需要 List>。 int 将只包含节点的名称,如数字 33。因此,您将拥有名称为 33 的第 5 行第 6 列。您将无法拥有邻居。

标签: c# matrix graph vertices


【解决方案1】:

好的,尽管有负面的 cmets,我还是设法解决了它。我知道这种方法没有多大意义,但我没有选择它。如果有人有兴趣,请在AddAddConnection 方法下方,另外还有SaveMatrix 方法,可以很好地将其写入txt 文件。

   public void Add(string vertex)
{
    this.vertices.Add(vertex);
    List<int> temp = new List<int>();

    foreach(List<int> element in this.adjacencyMatrix)
    {
        element.Add(0);
    }
    foreach(string element in vertices)
    {
        temp.Add(0);
    }
    this.adjacencyMatrix.Add(temp);

    this.n++;
}

  public void AddConnection(string vertex1, string vertex2, int value)
{
   if(this.vertices.Contains(vertex1) && this.vertices.Contains(vertex2))
    {
       int index1= this.vertices.IndexOf(vertex1);
        int index2 = this.vertices.IndexOf(vertex2);

        this.adjacencyMatrix[index1][index2] = value;
        this.adjacencyMatrix[index2][index1] = value;
    }
    else
    {
        Console.WriteLine("Graph doesn't contain passed vertex/vertices.");
    }
}

public void SaveMatrix(string filename)
{
    // save matrix with nice formatting
    // works if the names of the vertices are shorter than 16 chars and if edge weights are shorter than 100 (and positive)
    try
    {
        StreamWriter sw = new StreamWriter(filename);
        sw.Write("".PadRight(16)); // pad a string with white spaces to get exactly the specified length in chars
        foreach (string s in vertices) sw.Write(s.PadRight(16)); // write first row with names
        sw.WriteLine();
        int counter = 0;
        foreach (List<int> column in adjacencyMatrix) // other rows
        {
            sw.Write(vertices[counter].PadRight(16)); // start with vertex name
            counter++;
            foreach (int i in column)
            {
                if (i >= 10) sw.Write(i + "".PadRight(15)); // two digits
                else if (i > 0) sw.Write("0" + i + "".PadRight(15)); // one digit
                else sw.Write("--" + "".PadRight(15)); // no connection
            }
            sw.WriteLine();
        }
        sw.Close();
    }
    catch (IOException e)
    {
        Console.WriteLine("Error - the file could not be read");
        Console.WriteLine(e.Message);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2017-01-08
    • 2019-06-07
    相关资源
    最近更新 更多