public List <GraphVertex> GetPath(Graph graph, GraphVertex start, GraphVertex end) { if (graph == null || start == null || end == null || !graph.AllVertices.ContainsKey(start.Id) || !graph.AllVertices.ContainsKey(end.Id)) { // error condition pertaining to the input arguments throw new ArgumentException(); } MinHeapMap <GraphVertex> priorityQueue = new MinHeapMap <GraphVertex>(graph.AllVertices.Count); Dictionary <GraphVertex, GraphVertex> backtrack = new Dictionary <GraphVertex, GraphVertex>(); Dictionary <GraphVertex, int> finalDistance = new Dictionary <GraphVertex, int>(); // start node will have a distance of 0+manhattandistance start.CurrentDistance = ManhattanDistance(start, end); priorityQueue.Insert(start); backtrack[start] = null; while (priorityQueue.Count > 0) { GraphVertex vertex = priorityQueue.ExtractMin(); finalDistance[vertex] = vertex.CurrentDistance; if (vertex == end) { // return the path from start to end return(BackTractToGetThePath(backtrack, start, end)); } foreach (KeyValuePair <GraphVertex, int> edge in vertex.NeighbourEdges) { GraphVertex neighbour = edge.Key; int edgeWeight = edge.Value; if (!finalDistance.ContainsKey(neighbour)) { // this node's minimum distance has not yet been calculated if (neighbour.CurrentDistance > vertex.CurrentDistance + edgeWeight) { // A better distance has been found for neighbour neighbour.CurrentDistance = vertex.CurrentDistance + edgeWeight; neighbour.DistanceWithHeuristic = vertex.CurrentDistance + edgeWeight + ManhattanDistance(vertex, neighbour); priorityQueue.AddOrChangePriority(neighbour); backtrack[neighbour] = vertex; } } } } // we do not have a path from source to destination return(null); }
List <Edge> PrimsMST(Graph g) { Dictionary <int, Edge> map = new Dictionary <int, Edge>(); MinHeapMap <Vertex> queue = new MinHeapMap <Vertex>(); List <Edge> mst = new List <Edge>(); List <Vertex> allVertex = new List <Vertex>(g.allVertex.Values); Vertex start = allVertex[0]; foreach (Vertex v in allVertex) { queue.Enqueue(v, int.MaxValue); } queue.UpdatePriority(start, 0); HashSet <Vertex> visited = new HashSet <Vertex>(); visited.Add(start); while (queue.Count() > 0) { Vertex cur = queue.Dequeue(); visited.Add(cur); if (map.ContainsKey(cur.id)) { mst.Add(map[cur.id]); } foreach (Edge e in cur.adjEdge) { Vertex adj = g.GetAdjVertexForEdge(cur, e); if (!visited.Contains(adj) && queue.GetPriority(adj) > e.weight) { queue.UpdatePriority(adj, e.weight); if (!map.ContainsKey(adj.id)) { map.Add(adj.id, e); } else { map[adj.id] = e; } } } } return(mst); }
public List <int> GetShortestPath(List <GraphVertex> allVertices, GraphVertex source, GraphVertex destination) { List <GraphVertex> shortestPath = new List <GraphVertex>(); DistanceHeapMap = new MinHeapMap <GraphVertexWithDistance>(allVertices.Count); ParentBacktrack = new Dictionary <int, int>(); FinalDistance = new Dictionary <int, int>(); foreach (GraphVertex gv in allVertices) { if (gv == source) { DistanceHeapMap.Insert(new GraphVertexWithDistance(gv, 0)); } else { DistanceHeapMap.Insert(new GraphVertexWithDistance(gv)); } } while (DistanceHeapMap.Count != 0) { GraphVertexWithDistance currentVertex = DistanceHeapMap.ExtractMin(); FinalDistance[currentVertex.Id] = currentVertex.Distance; if (currentVertex.Id == destination.Id) { // we have reached the destination return(BacktrackToGetPath(currentVertex.Id, 0)); } foreach (KeyValuePair <GraphVertex, int> edge in currentVertex.NeighbouringEdgesWithWeight) { if (DistanceHeapMap.AllEntities.ContainsKey(edge.Key.Id)) { GraphVertexWithDistance endVertex = DistanceHeapMap.AllEntities[edge.Key.Id]; if (endVertex.Distance > currentVertex.Distance + edge.Value) { GraphVertexWithDistance gvClone = endVertex.ShallowClone(); gvClone.Distance = currentVertex.Distance + edge.Value; ParentBacktrack[endVertex.Id] = currentVertex.Id; DistanceHeapMap.ChangePriority(endVertex, gvClone); } } } } // we do not have a path from source to destination return(null); }
public static void Main5(string[] args) { MinHeapMap <string> pq = new MinHeapMap <string>(); pq.Enqueue("Bob", 5); pq.Enqueue("Rob", 10); pq.Enqueue("Mike", 6); pq.Enqueue("Tina", 3); pq.Enqueue("Andy", 2); pq.Enqueue("Judy", 9); pq.UpdatePriority("Tina", 10); pq.UpdatePriority("Rob", 5); int ct = 6; while (ct > 0) { Console.WriteLine(pq.Dequeue()); ct--; } Console.WriteLine(); }