在C#中创建图形类

问题描述 投票:2回答:1

我有一个任务用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++;
}

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

c# matrix graph vertices
1个回答
0
投票

好,尽管有负面评论,我还是设法解决了。我知道这种方法没有多大意义,但我没有选择它。如果有兴趣,可以在AddAddConnection方法下面,还可以使用SaveMatrix方法将其很好地写到txt文件中。

© www.soinside.com 2019 - 2024. All rights reserved.