C# 用C语言创建图形类#

C# 用C语言创建图形类#,c#,matrix,graph,vertices,C#,Matrix,Graph,Vertices,我的任务是在C#中创建Graph类,该类将具有以下属性: private List<string> vertices; private List<List<int>> adjacencyMatrix; private int n; 但我仍然不知道如何添加连接。 任何帮助都将不胜感激好吧,尽管有负面评论,我还是设法解决了。我知道这种方法没有多大意义,但我没有选择它。如果有人感兴趣,请在Add和AddConnection方法下面添加SaveMatrix方法,该方

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

private List<string> vertices;
private List<List<int>> adjacencyMatrix;
private int n;
但我仍然不知道如何添加连接。
任何帮助都将不胜感激

好吧,尽管有负面评论,我还是设法解决了。我知道这种方法没有多大意义,但我没有选择它。如果有人感兴趣,请在
Add
AddConnection
方法下面添加
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);
    }
}
public void添加(字符串顶点)
{
this.vertexs.Add(顶点);
列表温度=新列表();
foreach(此.adjacencyMatrix中的列表元素)
{
元素。添加(0);
}
foreach(顶点中的字符串元素)
{
临时添加(0);
}
此.adjacencyMatrix.Add(temp);
这个.n++;
}
public void AddConnection(字符串vertex1、字符串vertex2、int值)
{
if(this.vertexs.Contains(vertex1)和&this.vertexs.Contains(vertex2))
{
int index1=this.vertexs.IndexOf(vertex1);
int index2=this.vertexs.IndexOf(vertex2);
这个.adjacencyMatrix[index1][index2]=值;
这个.邻接矩阵[index2][index1]=值;
}
其他的
{
WriteLine(“图形不包含传递的顶点/顶点”);
}
}
公共void SaveMatrix(字符串文件名)
{
//用漂亮的格式保存矩阵
//如果顶点的名称小于16个字符,且边权重小于100(且为正值),则此选项有效
尝试
{
StreamWriter sw=新的StreamWriter(文件名);
sw.Write(“.PadRight(16));//用空格填充字符串,以精确获得指定的字符长度
foreach(顶点中的字符串s)sw.Write(s.PadRight(16));//用名称写入第一行
sw.WriteLine();
int计数器=0;
foreach(邻接矩阵中的列表列)//其他行
{
Write(顶点[counter].PadRight(16));//从顶点名称开始
计数器++;
foreach(第i列中的int i)
{
如果(i>=10)sw.Write(i+“”.PadRight(15));//两位数
如果(i>0)sw.Write(“0”+i+”.PadRight(15));//一个数字
else sw.Write(“--”+”.PadRight(15));//无连接
}
sw.WriteLine();
}
sw.Close();
}
捕获(IOE异常)
{
WriteLine(“错误-无法读取文件”);
控制台写入线(e.Message);
}
}

通常的方法是创建类节点。每个节点都有一个属性:列出邻居。我通常在类中还有:int name(节点的编号)。图是:列表图。是的,这是正常情况,在这个特殊的例子中,“名称”是第一个列表中的字符串,然后矩阵将包含“邻居”。我只是不知道如何编码它只是在图中添加新的节点。你知道如何创建新类吗?您知道如何将新项目添加到列表(包括类)中吗?Node newNode=新节点();添加(newNode);请参阅编辑使用列表而不是列表将使代码更加复杂,因为您必须执行查找才能从一个节点移动到另一个节点。为什么要使用二维数组?列表邻接矩阵;首先,二维数组不适用于所有图形。若你们有一个矩形图,你们需要一个列表。int只能包含节点的名称,如数字33。因此,第5行第6列的名称为33。你不可能有邻居。
   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);
    }
}