// // Follows the RELAX specifications dictated in CLRS: Page 649. // w(u, v) : weight from vertex u to vertex v // Reduces the shortest path of a node (if necessary) and updates the value of the node in the heap (DecreaseKey) // //void Relax(int u, int v, int w, FibonacciHeap<int> heap) void Relax(int u, int v, int w, MinHeap heap) //void Relax(int u, int v, int w, NaiveMinContainer<int> heap) { // // If we find a shorter path, update the new shortest path for a node AND the predecessor // if (shortPathEst[v].d > shortPathEst[u].d + w) { shortPathEst[v].d = shortPathEst[u].d + w; shortPathEst[v].pi = u; // // Relaxing may result in a node in the heap having a shorter path to that node; // so decrease key to ensure the heap represents the new state // heap.DecreaseKey(shortPathEst[v].heapNode, shortPathEst[v].d); } }
// // Performs Dijkstra's shortest path algorithm on the the constructed PathGraph object // // @param path -- Returns the actual shortest path in the List parameter // @return the length of the shortest path via the return value // public int Dijkstra(List <int> path) { // Initialize all shortest paths to a node to be large and the shortest path to a start node to be 0 InitializeSingleSource(int.MaxValue - 100000); // -100000 prevents issues with arithmetic overflow // // Create the min-Fibonacci Heap by pushing all vertices onto the Heap // int numV = graph.NumVertices(); //FibonacciHeap<int> vertices = new FibonacciHeap<int>(); MinHeap vertices = new MinHeap(numV); for (int i = 0; i < numV; i++) // VertexValues node : shortPathEst) { // Insert the weight, d, which is the short path to node i HeapNode <int> node = new HeapNode <int>(i); vertices.Insert(node, shortPathEst[i].d); shortPathEst[i].heapNode = node; // maintain a pointer to the object for DecreaseKey } // // Visit all vertices in the PathGraph (always visiting the shortest distance from the start node first) // Do so by using a Fibonacci Heap // while (!vertices.IsEmpty()) { // // Acquire the minimum: min will contain a pair <shortest distance to node n, node n> // HeapNode <int> min = vertices.ExtractMin(); // Now, extract the node // // For each adjacent vertex, relax // int nodeIndex = min.data; List <int> adjNodes = graph.AdjacentNodes(nodeIndex); foreach (int neighbor in adjNodes) { Relax(nodeIndex, neighbor, graph.GetWeight(nodeIndex, neighbor), vertices); } } // // Look at predecessors to acquire the actual shortest path // path.Add(goalNode); // Add the last node // Predecessor node int predNode = shortPathEst[goalNode].pi; // Loop while we have not yet reached the start node // If we hit a NIL, a path does not exist while (predNode != startNode && predNode != NIL) { path.Insert(0, predNode); // Insert at the beginning of the List; index 0 predNode = shortPathEst[predNode].pi; } // Check if the goal node was even reachable if (predNode == NIL) { path.Clear(); return(NIL); } // Insert the first node at the beginning path.Insert(0, predNode); return(shortPathEst[goalNode].d); // recall .d contains the shortest path length to the goalNode }
// // Performs Dijkstra's shortest path algorithm on the the constructed PathGraph object // // @param path -- Returns the actual shortest path in the List parameter // @return the length of the shortest path via the return value // public int Dijkstra(List<int> path) { // Initialize all shortest paths to a node to be large and the shortest path to a start node to be 0 InitializeSingleSource(int.MaxValue - 100000); // -100000 prevents issues with arithmetic overflow // // Create the min-Fibonacci Heap by pushing all vertices onto the Heap // int numV = graph.NumVertices(); //FibonacciHeap<int> vertices = new FibonacciHeap<int>(); MinHeap vertices = new MinHeap(numV); for (int i = 0; i < numV; i++) // VertexValues node : shortPathEst) { // Insert the weight, d, which is the short path to node i HeapNode<int> node = new HeapNode<int>(i); vertices.Insert(node, shortPathEst[i].d); shortPathEst[i].heapNode = node; // maintain a pointer to the object for DecreaseKey } // // Visit all vertices in the PathGraph (always visiting the shortest distance from the start node first) // Do so by using a Fibonacci Heap // while (!vertices.IsEmpty()) { // // Acquire the minimum: min will contain a pair <shortest distance to node n, node n> // HeapNode<int> min = vertices.ExtractMin(); // Now, extract the node // // For each adjacent vertex, relax // int nodeIndex = min.data; List<int> adjNodes = graph.AdjacentNodes(nodeIndex); foreach (int neighbor in adjNodes) { Relax(nodeIndex, neighbor, graph.GetWeight(nodeIndex, neighbor), vertices); } } // // Look at predecessors to acquire the actual shortest path // path.Add(goalNode); // Add the last node // Predecessor node int predNode = shortPathEst[goalNode].pi; // Loop while we have not yet reached the start node // If we hit a NIL, a path does not exist while (predNode != startNode && predNode != NIL) { path.Insert(0, predNode); // Insert at the beginning of the List; index 0 predNode = shortPathEst[predNode].pi; } // Check if the goal node was even reachable if (predNode == NIL) { path.Clear(); return NIL; } // Insert the first node at the beginning path.Insert(0, predNode); return shortPathEst[goalNode].d; // recall .d contains the shortest path length to the goalNode }
//void Relax(int u, int v, int w, NaiveMinContainer<int> heap) // // Follows the RELAX specifications dictated in CLRS: Page 649. // w(u, v) : weight from vertex u to vertex v // Reduces the shortest path of a node (if necessary) and updates the value of the node in the heap (DecreaseKey) // //void Relax(int u, int v, int w, FibonacciHeap<int> heap) void Relax(int u, int v, int w, MinHeap heap) { // // If we find a shorter path, update the new shortest path for a node AND the predecessor // if (shortPathEst[v].d > shortPathEst[u].d + w) { shortPathEst[v].d = shortPathEst[u].d + w; shortPathEst[v].pi = u; // // Relaxing may result in a node in the heap having a shorter path to that node; // so decrease key to ensure the heap represents the new state // heap.DecreaseKey(shortPathEst[v].heapNode, shortPathEst[v].d); } }