/// <summary> /// Returns the shortest paths /// </summary> /// <param name="u">generic string</param> /// <param name="v">generic string</param> /// <param name="map">DirectedGraph map</param> /// <param name="paths">Dictionary paths</param> /// <returns></returns> private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths) { paths = new Dictionary <string, string>(); MinPriorityQueue <decimal, Edge <string, decimal> > tempQueue = new MinPriorityQueue <decimal, Edge <string, decimal> >(); paths.Add(u, u); if (u.Equals(v)) { return(0); } foreach (Edge <string, decimal> outGoEdge in map.OutgoingEdges(u)) { tempQueue.Add(outGoEdge.Data, outGoEdge); } while (tempQueue.Count > 0) //possibly wrong { decimal p = tempQueue.MinimumPriority; Edge <string, decimal> edgeRemoved = tempQueue.RemoveMinimumPriority(); if (!paths.ContainsKey(edgeRemoved.Destination)) { paths.Add(edgeRemoved.Destination, edgeRemoved.Source); if (edgeRemoved.Destination.Equals(v)) { return(p); } foreach (Edge <string, decimal> outGoEdge in map.OutgoingEdges(edgeRemoved.Destination)) { tempQueue.Add(p + outGoEdge.Data, outGoEdge); } } } return(-1); }
/// <summary> /// Finds a shortest path from the given start node to the given end node in the given graph. /// </summary> /// <param name="u">The start node.</param> /// <param name="v">The end node.</param> /// <param name="map">The graph.</param> /// <param name="paths">The resulting shortest paths.</param> /// <returns>The length of the shortest path.</returns> private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths) { paths = new Dictionary <string, string>(); MinPriorityQueue <decimal, Edge <string, decimal> > pq = new MinPriorityQueue <decimal, Edge <string, decimal> >(); paths.Add(u, u); if (u == v) { return(0); } foreach (Edge <string, decimal> e in map.OutgoingEdges(u)) { pq.Add(e, e.Data); } while (pq.Count > 0) { decimal p = pq.MininumPriority; Edge <string, decimal> e = pq.RemoveMinimumPriority(); if (!paths.ContainsKey(e.Destination)) { paths.Add(e.Destination, e.Source); if (e.Destination == v) { return(p); } foreach (Edge <string, decimal> outgoing in map.OutgoingEdges(e.Destination)) { pq.Add(outgoing, p + outgoing.Data); } } } return(-1); }
/// <summary> /// Computes the shortest path from u to v in the map. /// </summary> /// <param name="u"></param> /// <param name="v"></param> /// <param name="map"></param> /// <param name="paths"></param> /// <returns></returns> private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths) { paths = new Dictionary <string, string>(); MinPriorityQueue <decimal, Edge <string, decimal> > queue = new MinPriorityQueue <decimal, Edge <string, decimal> >(); paths.Add(u, u); if (u.Equals(v)) { return(0); } foreach (Edge <string, decimal> i in map.OutgoingEdges(u)) { queue.Add(i.Data, i); } while (queue.Count > 0) { decimal priority = queue.MinimumPriority; Edge <string, decimal> temp = queue.RemoveMinimumPriority(); if (!paths.ContainsKey(temp.Destination)) { paths.Add(temp.Destination, temp.Source); if (temp.Destination == v) { return(priority); } foreach (Edge <string, decimal> i in map.OutgoingEdges(temp.Destination)) { queue.Add(priority + i.Data, i); } } } return(-1); }
/// <summary> /// Finds a shortest path from the given start node to the given end node in the given graph. /// </summary> /// <param name="u">The start node.</param> /// <param name="v">The end node.</param> /// <param name="map">The graph.</param> /// <param name="paths">The resulting shortest paths.</param> /// <returns>The length of the shortest path.</returns> private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths) { //throw new NotImplementedException(); paths = new Dictionary <string, string>(); paths.Add(u, u); if (u == v) { return(0); } MinPriorityQueue <decimal, Tuple <string, string> > q = new MinPriorityQueue <decimal, Tuple <string, string> >(); //add all outgoing edges from u foreach (Tuple <string, decimal> edge in map.OutgoingEdges(u)) { //add this edge to priority queue q.Add(new Tuple <string, string>(u, edge.Item1), edge.Item2); } while (q.Count != 0) { decimal priority = q.MininumPriority; Tuple <string, string> edge = q.RemoveMinimumPriority(); string x = edge.Item1; string w = edge.Item2; if (!paths.ContainsKey(w)) { paths.Add(w, x); if (w == v) { return(priority); } else { foreach (Tuple <string, decimal> edge2 in map.OutgoingEdges(w)) { q.Add(new Tuple <string, string>(w, edge2.Item1), edge2.Item2 + priority); } } } } return(-1); }
/// <summary> /// Finds a shortest path from the given start node to the given end node in the given graph. /// </summary> /// <param name="u">The start node.</param> /// <param name="v">The end node.</param> /// <param name="map">The graph.</param> /// <param name="paths">The resulting shortest paths.</param> /// <returns>The length of the shortest path.</returns> private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths) { paths = new Dictionary <string, string>(); MinPriorityQueue <decimal, Tuple <string, string> > q = new MinPriorityQueue <decimal, Tuple <string, string> >(); paths.Add(u, u); if (u == v) { return(0); } foreach (Tuple <string, decimal> t in map.OutgoingEdges(u)) { string w = t.Item1; decimal len = t.Item2; q.Add(new Tuple <string, string>(u, w), len); } while (q.Count != 0) { decimal p = q.MininumPriority; Tuple <string, string> edge = q.RemoveMinimumPriority(); string w = edge.Item1; string x = edge.Item2; if (!paths.ContainsKey(x)) { paths.Add(x, w); if (x == v) { return(p); } foreach (Tuple <string, decimal> t in map.OutgoingEdges(x)) { string y = t.Item1; decimal len = t.Item2; q.Add(new Tuple <string, string>(x, y), p + len); } } } return(-1); }
/// <summary> /// Uses Dijkstra's Algorithm to find the shortest distance in a graph between point u and v! /// </summary> /// <param name="u"> Name of the node we are starting at </param> /// <param name="v">Name of the node that serves as our destination </param> /// <param name="map"> Map that we are sifting through to find the shortest path </param> /// <param name="paths"> Dictionary whose keys are a node, and the value is it's previous node </param> /// <returns> The shortest path in decimal form! </returns> private static decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths) { paths = new Dictionary <string, string>(); // String value in Edge is a node that has already been visited! // Priority is the shortest path up until the source node (string value in Edge) MinPriorityQueue <decimal, Edge <string, decimal> > queue = new MinPriorityQueue <decimal, Edge <string, decimal> >(); paths.Add(u, u); if (u == v) { return(0); } foreach (Edge <string, decimal> edge in map.OutgoingEdges(u)) { queue.Add(edge.Data, edge); } while (queue.Count != 0) { decimal minPriority = queue.MinimumPriority; Edge <string, decimal> nextEdge = queue.RemoveMinimumPriority(); if (paths.ContainsKey(nextEdge.Destination) == false) { paths.Add(nextEdge.Destination, nextEdge.Source); if (nextEdge.Destination == v) { return(minPriority); } foreach (Edge <string, decimal> edge in map.OutgoingEdges(nextEdge.Destination)) { //queue.Add(edge.Data, edge); queue.Add(minPriority + edge.Data, edge); } } } return(-1); }