using System; using System.Collections.Generic; namespace Graph { public class Graph where T : IComparable { public List> Vertices { get; set; } = new List>(); public Graph() { } public Graph(List> vertices) { Vertices = vertices; } // indexer for accessing vertex with id of index public Vertex 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 edge) { // look for existing vertex, and use it if found Vertex vertex = Vertices.Find(u => u.Id == edge.U); if (vertex == null) { // if not found, create a new vertex Vertices.Add(vertex = new Vertex(edge.U)); vertex = Vertices[Vertices.Count - 1]; } // add the edge to the new/found vertex vertex.Edges.Add(edge); } public override string ToString() => $"{String.Join("\n", Vertices)}"; } }