diff options
Diffstat (limited to 'Graph.cs')
-rw-r--r-- | Graph.cs | 8 |
1 files changed, 6 insertions, 2 deletions
@@ -14,19 +14,23 @@ namespace Graph Vertices = vertices; } - // Indexer for accessing vertex with id of index + // indexer for accessing vertex with id of index public Vertex<T> this[int index] { get => Vertices.Find(vertex => vertex.Id == index); } + // create virtex if needed, then add the edge to it's edges list public void AddEdge(Edge<T> edge) { + // look for existing vertex, and use it if found Vertex<T> vertex = Vertices.Find(u => u.Id == edge.U); if (vertex == null) { + // if not found, create a new vertex Vertices.Add(vertex = new Vertex<T>(edge.U)); vertex = Vertices[Vertices.Count - 1]; } - + + // add the edge to the new/found vertex vertex.Edges.Add(edge); } |