public bool IsAdjacentTo(Vertex v, Edge[] edges) { var connectingEdges = from e in edges where e.ConnectedTo(v) && e.ConnectedTo(this) orderby e.Cost select e; this.AdjacentEdge = connectingEdges.FirstOrDefault(); return this.AdjacentEdge != null; }
private static string GetPath(Vertex v) { string result = string.Empty; for (Vertex current = v; current != null; current = current.Parent) { if (current.Parent != null) { result = string.Format(" - {0}{1}", current.Name, result); } else { result = current.Name + result; } } return result; }
private static IEnumerable<Vertex> Dijkstra( Vertex[] nodes, Edge[] edges, Vertex source) { source.Cost = 0; var treeNodes = new List<Vertex>(); var fringe = new List<Vertex>() { source }; for (int i = 0; i < nodes.Length; i++) { var remainder = nodes.Where(n => !treeNodes.Contains(n)).ToList(); var u = fringe.OrderBy(n => n.Cost).First(); if (u != source) { WritePathTo(u); } treeNodes.Add(u); foreach (var r in remainder) { if (r.IsAdjacentTo(u, edges)) { int c = u.Cost + r.AdjacentEdge.Cost; if (c < r.Cost) { r.Cost = c; r.Parent = u; } if (!fringe.Contains(r)) { fringe.Add(r); } } } fringe.Remove(u); Console.Write("{0}|{1}|{2}", u, string.Join(", ", fringe), null); } return treeNodes; }
public Vertex OtherNode(Vertex that) { return Ends.Where(v => v != that).Single(); }
public bool ConnectedTo(Vertex v) { return Ends.Any(n => n == v); }
public Edge(int cost, Vertex v, Vertex u) { _Cost = cost; ends = new[] { v, u }; }
private static void Main() { Vertex[] nodes = new Vertex[] { new Vertex("a"), new Vertex("b"), new Vertex("c"), new Vertex("d"), new Vertex("e"), new Vertex("f"), new Vertex("g"), new Vertex("h"), new Vertex("i"), new Vertex("j"), new Vertex("k"), new Vertex("l"), }; Edge[] edges = new[] { new Edge(3, nodes[0], nodes[1]), new Edge(5, nodes[0], nodes[2]), new Edge(4, nodes[0], nodes[3]), new Edge(3, nodes[1], nodes[4]), new Edge(6, nodes[1], nodes[5]), new Edge(2, nodes[2], nodes[3]), new Edge(1, nodes[3], nodes[4]), new Edge(2, nodes[4], nodes[5]), new Edge(4, nodes[2], nodes[6]), new Edge(5, nodes[3], nodes[7]), new Edge(4, nodes[4], nodes[8]), new Edge(5, nodes[5], nodes[9]), new Edge(3, nodes[6], nodes[7]), new Edge(6, nodes[7], nodes[8]), new Edge(3, nodes[8], nodes[9]), new Edge(6, nodes[6], nodes[10]), new Edge(7, nodes[7], nodes[10]), new Edge(5, nodes[8], nodes[11]), new Edge(9, nodes[9], nodes[11]), new Edge(8, nodes[10], nodes[11]), }; Dijkstra(nodes, edges, nodes[0]); }
private static void WritePathTo(Vertex v) { Console.WriteLine( "to {0}: {1} of length {2}", v.Name, GetPath(v), v.Cost); }
/// <summary> /// Create the vertices (this is pretty long) /// </summary> public void InstantiateVertexes() { Vertex vA = new Vertex("A"); Vertex vB = new Vertex("B"); Vertex vC = new Vertex("C"); Vertex vD = new Vertex("D"); Vertex vE = new Vertex("E"); Vertex vF = new Vertex("F"); Vertex vG = new Vertex("G"); Vertex vH = new Vertex("H"); Vertex vI = new Vertex("I"); Vertex vJ = new Vertex("J"); Vertex vK = new Vertex("K"); Vertex vL = new Vertex("L"); Vertex vM = new Vertex("M"); Vertex vN = new Vertex("N"); Vertex vO = new Vertex("O"); Vertex vP = new Vertex("P"); Vertex vQ = new Vertex("Q"); Vertex vR = new Vertex("R"); Vertex vS = new Vertex("S"); Vertex vT = new Vertex("T"); Vertex vU = new Vertex("U"); Vertex vV = new Vertex("V"); Vertex vW = new Vertex("W"); Vertex vX = new Vertex("X"); Vertex vY = new Vertex("Y"); Vertex vZ = new Vertex("Z"); GraphList.Add(vA); GraphDictionary.Add("A", vA); GraphList.Add(vB); GraphDictionary.Add("B", vB); GraphList.Add(vC); GraphDictionary.Add("C", vC); GraphList.Add(vD); GraphDictionary.Add("D", vD); GraphList.Add(vE); GraphDictionary.Add("E", vE); GraphList.Add(vF); GraphDictionary.Add("F", vF); GraphList.Add(vG); GraphDictionary.Add("G", vG); GraphList.Add(vH); GraphDictionary.Add("H", vH); GraphList.Add(vI); GraphDictionary.Add("I", vI); GraphList.Add(vJ); GraphDictionary.Add("J", vJ); GraphList.Add(vK); GraphDictionary.Add("K", vK); GraphList.Add(vL); GraphDictionary.Add("L", vL); GraphList.Add(vM); GraphDictionary.Add("M", vM); GraphList.Add(vN); GraphDictionary.Add("N", vN); GraphList.Add(vO); GraphDictionary.Add("O", vO); GraphList.Add(vP); GraphDictionary.Add("P", vP); GraphList.Add(vQ); GraphDictionary.Add("Q", vQ); GraphList.Add(vR); GraphDictionary.Add("R", vR); GraphList.Add(vS); GraphDictionary.Add("S", vS); GraphList.Add(vT); GraphDictionary.Add("T", vT); GraphList.Add(vU); GraphDictionary.Add("U", vU); GraphList.Add(vV); GraphDictionary.Add("V", vV); GraphList.Add(vW); GraphDictionary.Add("W", vW); GraphList.Add(vX); GraphDictionary.Add("X", vX); GraphList.Add(vY); GraphDictionary.Add("Y", vY); GraphList.Add(vZ); GraphDictionary.Add("Z", vZ); }