summaryrefslogtreecommitdiffstats
path: root/Vertex.cs
blob: 36a6dd654847efe334785aed82d53ac7dad9c920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using Interfaces;

namespace Graph
{
    public class Vertex<T, U> : IVertex<T, U>, INode<U>, IComparable<INode<U>>
        where T : IEdge<U>, new()
        where U : IComparable<U>
    {
        public int Id { get; set; }
        public U Key { get; set; }

        public int Parent { get; set; } = -1;
        public List<T> Edges { get; set; } = new List<T>();

        // 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<T> edges) : this(id) =>
            Edges = edges;

        public Vertex(IVertex<T, U> 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<U> vertex) =>
            Key.CompareTo(vertex.Key);

        public override string ToString() => $"{Id} {String.Join(" ", Edges.Select(e => (e.V, e.Weight)))}";
    }
}