2
\$\begingroup\$

Is there anything I could do better in this Dijkstra's Implementation. This is my implementation of Dijkstra after understanding the concept. I used hash table wherever possible to have faster access

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
string lowestnode(unordered_map<string,int> &costs, vector<string> &processed);
int main(){
 //Finding Shortest Path from 'S' to 'F'
 unordered_map<string,unordered_map<string, int>> graph;
 graph["S"] = {make_pair("A", 6), make_pair("B", 2)};
 graph["A"] = {make_pair("F", 1)};
 graph["B"] = {make_pair("A", 3), make_pair("F", 5)};
 graph["F"] = {};
 unordered_map<string, int> costs;
 costs["A"] = 6;
 costs["B"] = 2;
 costs["F"] = INT_MAX;
 unordered_map<string, string> parents;
 parents["A"] = "S";
 parents["B"] = "S";
 parents["F"] = "None";
 vector<string> processed;
 processed.reserve(4);
 string node = lowestnode(costs, processed);
 while(node != ""){
 int cost = costs[node];
 unordered_map<string, int> neighbours;
 neighbours = graph[node];
 for (auto neighbour : neighbours){
 int newcost = cost + neighbour.second;
 if(costs[neighbour.first] > newcost){
 costs[neighbour.first] = newcost;
 parents[neighbour.first] = node;
 }
 }
 processed.push_back(node);
 node = lowestnode(costs, processed);
 }
 cout<<"Costs : ";
 for (auto cost : costs){
 cout<<"[ "<<cost.first<<":"<<cost.second<<" ] ";
 }
 
 //'F' is the final Destination
 string answer = "F";
 string child = "F";
 while(parents[child] != ""){
 answer += parents[child];
 child = parents[child];
 }
 cout<<"\nPath : ";
 for (int i=answer.size()-1; i>=0; i--){
 cout<<answer[i];
 if(i!= 0) cout<<" -> ";
 }
}
string lowestnode(unordered_map<string,int> &costs, vector<string> &processed){
 string minnode = "";
 int minvalue = INT_MAX;
 for (auto node : costs){
 int flag = 0;
 for (auto visited : processed){
 if(node.first == visited){
 flag = 1;
 break;
 }
 }
 if(flag) continue;
 if(minvalue > node.second){
 minvalue = node.second;
 minnode = node.first;
 }
 }
 return minnode;
}
Joop Eggen
4,54615 silver badges19 bronze badges
asked Feb 12, 2022 at 16:58
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Avoid using namespace std; \$\endgroup\$ Commented Feb 12, 2022 at 18:33
  • 1
    \$\begingroup\$ Dykstra's is normally implemented with a priority queue, and even though the c++ std::priority_queue does not allow element modification, it is easy to "keep pushing" and discard stale entries on pop. You should research this approach as it is quite canonical I think. \$\endgroup\$ Commented Feb 16, 2022 at 22:10

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.