summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/City.cs
blob: eaf4ef3750d423fe96443fd2de726a98188901ac (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
using System;
using System.Collections.Generic;
using System.Linq;
using Graph;
using Interfaces;

namespace TSP
{
    using INode = INode<double>;
    using IVertex = IVertex<Edge<double>, double>;
    using Road = Edge<double>;

    public class City : IVertex, IComparable<INode>
    {
        public int Id { get; set; }
        public double Key { get => this[Parent].Weight; set => Key = value; }

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

        // indexer for accessing edge with v of index 
        public Road this[int index] { get => Edges.Find(edge => edge.V == index); }

        public City() { }

        public City(int id) =>
            Id = id;

        public City(int id, List<Road> edges) : this(id) =>
            Edges = edges;

        public City(IVertex city)
        {
            Id = city.Id;
            Parent = city.Parent;
            foreach (Road edge in city.Edges)
            {
                Road newEdge = new Road(edge.U, edge.V, edge.Weight);
                Edges.Add(newEdge);
            }
        }

        public int CompareTo(INode node) =>
            Key.CompareTo(node.Key);

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