我有一个任务用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++;
}
但是我仍然不知道如何添加连接。任何帮助,将不胜感激
好,尽管有负面评论,我还是设法解决了。我知道这种方法没有多大意义,但我没有选择它。如果有兴趣,可以在Add
和AddConnection
方法下面,还可以使用SaveMatrix
方法将其很好地写到txt文件中。