public void AddLink(Node toNode, int weight) { Links.Add( new Link(toNode, weight) ); }
private Node GetOrCreate(string id) { Node node; Nodes.TryGetValue(id, out node); if (node == null) { node = new Node(id); Nodes.Add(id, node); } return node; }
public Link(Node neighbour, int weight) { this.Neighbour = neighbour; this.Weight = weight; }
public Graph(string description) { var sentences = description.Trim().Split(new string[] { "\r\n", "\n", "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); var start = sentences.First().Trim(); var finish = sentences.Last().Trim(); sentences.RemoveAt(0); sentences.RemoveAt(sentences.Count - 1); this.Start = GetOrCreate(start); this.Finish = GetOrCreate(finish); foreach (var edge in sentences) { var triple = edge.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var from = triple[0]; var weight = int.Parse( triple[1].Substring(1) ); var to = triple[2]; var fromN = GetOrCreate(from); var toN = GetOrCreate(to); fromN.AddLink(toN, weight); toN.AddLink(fromN, weight); } }