Reputation: 73
Currently trying to implement dijkstra's algorithm in C++ by the use of an adjacency list in a text file read into a map object. The map is initialized as:
map<int, vector<pair<int, int>>> = adjList;
Sample text file input:
1 2,1 8,2
2 1,1 3,1
3 2,1 4,1
4 3,1 5,1
5 4,1 6,1
6 5,1 7,1
7 6,1 8,1
8 7,1 1,2
Where the key is a vertex, and the x values of the pairs in the vector are connected to the key vertex. The y values are the path distances. I pass that map into my dijkstra function, where I initialize a vector for shortest distances and a vector for storing visited vertices. My loop is where things start going wrong, as I get outputs of zeroes and very large numbers. Here's my code:
//checks if vertex has been visited or not
bool booler(int vertex, vector<int> visited){
bool booly;
if(find(visited.begin(), visited.end(), vertex) != visited.end()){
booly = true;
}
else{
booly = false;
}
return booly;
}
//checks vector for the shortest distance vertex
int minDist(vector<int> distances, vector<int> visited){
int minDist = 1000000;
int index;
for(int v = 0; v < distances.size(); v++){
if(booler(v, visited) == false && distances[v] < minDist){
minDist = distances[v];
index = v;
}
}
return index;
}
void dijkstra(int source, map<int, vector<pair<int, int>>> adjList, int vSize){
vector<int> distances(vSize, 1000000);
vector<int> visited = {};
distances[source] = 0;
for(int c = 0; c < distances.size(); c++){
int u = minDist(distances, visited);
visited.push_back(u);
for(int v = 1; v < distances.size(); v++){
for(int s = 0; s < adjList[u].size(); s++){
//updates distances based on v connection to u
if(booler(v, visited) == false && distances[u] < 1000000 && adjList[u][s].second + distances[u] < distances[v]){
distances[v] = distances[u] + adjList[u][v].second;
}
}
}
}
//prints out shortest path
for(int x = 0; x < distances.size(); x++){
cout << distances[x] << " " << endl;
}
}
I haven't been able to fix this error, any help would be greatly appreciated!
Upvotes: 3
Views: 6055
Reputation: 1489
Here is an implement how to use dijkstra.
It is my solution for your problem:
#include "bits/stdc++.h"
using namespace std;
map<int, vector<pair<int, int> > > mp;
void addEdge(int u, int v, int dist) {
mp[u].push_back(make_pair(v, dist));
}
void startDijkstra(int u) {
vector<int> dist(1e2 + 1, 1e9);
set<pair<int, int> > st;
st.insert(make_pair(0, u));
dist[u] = 0;
while (!st.empty()) {
pair<int, int> now = *st.begin();
st.erase(st.begin());
int v = now.second;
int w = now.first;
const vector<pair<int, int> > &edges = mp[v];
for (const pair<int, int> &to : edges) {
if (w + to.second < dist[to.first]) {
st.erase(make_pair(dist[to.first], to.first));
dist[to.first] = w + to.second;
st.insert(make_pair(dist[to.first], to.first));
}
}
}
for (int i = 1; i <= 8; i++) {
cout << i << ' ' << dist[i] << endl;
}
}
int main() {
addEdge(1, 2, 1);
addEdge(1, 8, 2);
addEdge(2, 1, 1);
addEdge(2, 3, 1);
addEdge(3, 2, 1);
addEdge(3, 4, 1);
addEdge(4, 3, 1);
addEdge(4, 5, 1);
addEdge(5, 4, 1);
addEdge(5, 6, 1);
addEdge(6, 5, 1);
addEdge(6, 7, 1);
addEdge(7, 6, 1);
addEdge(7, 8, 1);
addEdge(8, 9, 1);
addEdge(8, 1, 2);
startDijkstra(1);
return 0;
}
Upvotes: 2