public void AddVertex(int d) { Graphnode gn = new Graphnode(d); Vertices.Add(gn); V++; }
public void AddVertex(Graphnode gn) { if (Vertices.Contains(gn)) { throw new InvalidOperationException("This vertex already exists in the graph!"); } else { Vertices.Add(gn); V++; } }
public void AddEdge(Graphnode src, Graphnode dst, int flag) { if (flag > 1) { return; //we increment the flag so that after the recursion for the first time, it stops the loop } if (src == dst) { flag++; //if we are adding an edge from a node to intself, we just need to add it once. So we increment the flag so that it stops after one loop. } if (!Vertices.Contains(src) || !Vertices.Contains(dst)) { throw new InvalidOperationException("At least one of the Graphnodes (vertives) do not exist in this graph"); } else { if (!Edges.ContainsKey(src)) { Edges.Add(src, new List <Graphnode>()); } Edges[src].Add(dst); } AddEdge(dst, src, ++flag); //recursively add the other direction of the edge since it's an undirected graph so both vertices should see eachother. }
///////////////////////////////////////↓ADD EDGE BETWEEN TWO VERTICES↓////////////////////////////////////////// public void AddEdge(Graphnode src, Graphnode dst) //wrapper method for the below AddEdge method. { AddEdge(src, dst, 0); }