I have a task to create Graph
class in C#, that would have following properties:
private List<string> vertices;
private List<List<int>> adjacencyMatrix;
private int n;
n
is the number of nodes, the rest is self explanatory I believe.
The class should also have two methods that look like this:
Add(string vertex){}
AddConnection(string vertex1, string vertex2, int value){}
So far, I figured out the Add
method (assuming that 0 represents no connection between vertices):
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++;
}
But I still dont know how to add connection. Any help would be appreciated
> adjacencyMatrix; First a two dimensional array will not work for all graphs. If you did have a rectangular graph you would need List
>. An int would only contain the name of the node like a number 33. So you would have row 5, column 6 with name 33. You would not be able to have the neighbors.