public static long TotalCost <TData> (this DirectedGraph <TData> graph) { var rand = new Random(DateTime.UtcNow.Millisecond); var pivotVertex = graph.Vertices.ElementAt(rand.Next(0, graph.Vertices.Count - 1)); var totalCost = 0L; var frontier = new Frontier <TData>(graph); frontier.AddVertex(pivotVertex); while (frontier.SeenSoFar() < graph.Vertices.Count) { var minOutgoingEdge = frontier.MinEdge(); totalCost += minOutgoingEdge.Metric; var nextVertex = frontier.ExternalVertex(minOutgoingEdge); frontier.AddVertex(nextVertex); } return(totalCost); }
public static List <Vertex <TData, TMetric> > FindPath <TData, TMetric> (Graph <TData, TMetric> graph, Vertex <TData, TMetric> start, Vertex <TData, TMetric> finish) { var frontier = new Frontier <TData, TMetric>(); frontier.AddVertex(start, null); while (!frontier.Contains(finish)) { var shortestEdge = frontier.ShortestEdge(); frontier.AddVertex(shortestEdge.Ending, shortestEdge); if (!frontier.HasEdges) { break; } } //check whether finish node has been found if (!frontier.Contains(finish)) { return(null); } var path = new List <Vertex <TData, TMetric> >(); for (var current = finish; current != null;) { path.Add(current); var shortestEdge = frontier.EdgeForVertex(current); current = shortestEdge?.Beginning; } path.Reverse(); return(path); }