using System; using System.Collections.Generic; using System.Linq; using Interfaces; namespace Graph { public class Vertex : IVertex, INode, IComparable> where T : IEdge, new() where U : IComparable { public int Id { get; set; } public U Key { get; set; } public int Parent { get; set; } = -1; public List Edges { get; set; } = new List(); // indexer for accessing edge with v of index public T this[int index] { get => Edges.Find(edge => edge.V == index); } public Vertex() { } public Vertex(int id) => Id = id; public Vertex(int id, List edges) : this(id) => Edges = edges; public Vertex(IVertex vertex) { Id = vertex.Id; Parent = vertex.Parent; Key = vertex.Key; foreach (T edge in vertex.Edges) { T newEdge = new T(); newEdge.U = edge.U; newEdge.V = edge.V; newEdge.Weight = edge.Weight; Edges.Add(newEdge); } } public int CompareTo(INode vertex) => Key.CompareTo(vertex.Key); public override string ToString() => $"{Id} {String.Join(" ", Edges.Select(e => (e.V, e.Weight)))}"; } }