3

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

MWPodgorni
  • 83
  • 2
  • 9
  • Normal method is to create a class NODE. Each node will have a property : List neighbors. I normally also have in the class : int name (which is the number of the node). The Graph is then : List graph. – jdweng Dec 23 '19 at 09:56
  • yeah, thats normal case, in this particular one, the "name", would be the string in first list, and then the matrix would contain the "neighbors". I just dont know how to code it – MWPodgorni Dec 23 '19 at 10:00
  • It is just adding new Nodes into the Graph. Do you know how to create new classes ? Do you know how to add new items into a list (including classes)? Node newNode = new Node(); graph.Add(newNode); – jdweng Dec 23 '19 at 10:06
  • Please see the edits – MWPodgorni Dec 23 '19 at 10:12
  • Using a List instead of a List is going to make the code more complicated because you are going to have to perform a lookup to move from Node to Node. Why are you using a two dimensional array? List> 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. – jdweng Dec 23 '19 at 10:20
  • I know, unfortunately this is the implementation I have to go with, I know that the graph will have its limitations but thats fine in this case. – MWPodgorni Dec 23 '19 at 10:23
  • It can't work. How do you get both the name and the neighbors into the Node? Do you want to have two lists? One for the name and one for the neighbors? That is totally against good programming practices. Go back a carefully read the teacher requirements. You are misinterpreting what the teacher wants. – jdweng Dec 23 '19 at 10:27

1 Answers1

0

Okay, so despite the negative comments I managed to solve it. I know that this approach doesn't make much sense but I haven't chosen it. If anyone interested, below both Add and AddConnection method, additionally with SaveMatrix method that writes it nicely in txt file.

   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);
    }
}
MWPodgorni
  • 83
  • 2
  • 9