Question

#include <iostream>
#include <fstream>
#include <functional>
#include <climits>
#include <vector>
#include <queue>
#include <list>


using namespace std;

struct Vertices {
    int vertex;
    int weight;
    Vertices(int v, int w) : vertex(v), weight(w) { };
    Vertices() { }
};

class CompareGreater {
    public:
        bool const operator()(Vertices &nodeX, Vertices &nodeY) {
            return (nodeX.weight > nodeY.weight) ;
        }
};

vector< list<Vertices> > adj;
vector<int> weights;
priority_queue<Vertices, vector<Vertices>, CompareGreater> Q;

int nrVertices, nrEdges;

void readData();
void Dijkstra(Vertices);
void writeData();

void writeData() {
    ifstream out;
    out.open("graph.txt");

    weights.resize(1);
    for (vector<int>::iterator it = weights.begin()+1; it != weights.end(); ++it) {
        cout << (*it) << " ";
    }

    out.close();
}

void readData() {
    ifstream myFile;
    myFile.open("graph.txt");

    int nodeX, nodeY, weight;

    myFile >> nrVertices >> nrEdges;

    adj.resize(nrVertices+1);
    weights.resize(1);

    for (int i = 1; i <= nrVertices; ++i) {
        weights.push_back(INT_MAX);
    }

    for (int i = 1; i <= nrEdges; ++i) {
        myFile >> nodeX >> nodeY >> weight;
        adj[nodeX].push_back(Vertices(nodeY, weight));
    }

    myFile.close();
}

void Dijkstra(Vertices startNode) {
    Vertices currVertex;

    weights[startNode.vertex] = 0;
    Q.push(startNode);

    while (!Q.empty()) {
        currVertex = Q.top();
        Q.pop();

        if (currVertex.weight <= weights[currVertex.vertex]) {
            for (list<Vertices>::iterator it = adj[currVertex.vertex].begin(); it != adj[currVertex.vertex].end(); ++it) {
                if (weights[it->vertex] > weights[currVertex.vertex] + it->weight) {
                    weights[it->vertex] = weights[currVertex.vertex] + it->weight;
                    Q.push(Vertices((it->vertex), weights[it->vertex]));
                }
            }
        }
    }
}

int main() {

    readData();
    Dijkstra(Vertices(1, 0));
    writeData();

    return 0;
}

So this is what I have so far in order to implement a Dijkstra algorithm with adjacency lists. However, my code will not print anything. Any help?

Graph.txt looks like this:

7
2
2  2
4  1
2
4  3
5  10
2
1  4
6  5
4
3  2
5  2
6  8
7  4
1
7  6
0
1
6  1

This means that there exists 7 vertices in order from vertex 1 to 7. Vertex 1 has 2 edges, one to vertex 2 with weight 2, the second to vertex 4 with weight 1. Vertex 2 has 2 edges, the first to vertex 4 with weight 3, the second to vertex 5 with weight 10. Vertex 3 has 2 edges, the first to vertex 1 with weight 4, the second to vertex 6 with weight 5. And so forth.

Was it helpful?

Solution

This code:

weights.resize(1);
for (vector<int>::iterator it = weights.begin()+1; it != weights.end(); ++it) {

will resize the weights vector to a length of 1 and then try to print out the second element of the vector. As there is no longer a second element, it won't print anything.

OTHER TIPS

#include <iostream>
#include <fstream>
#include <functional>
#include <climits>
#include <vector>
#include <queue>
#include <list>

using namespace std;

struct Vertices {
    int vertex;
    int weight;
    Vertices(int v, int w) : vertex(v), weight(w) { };
    Vertices() { }
};

class CompareGreater {
    public:
        bool const operator()(Vertices &nodeX, Vertices &nodeY) {
            return (nodeX.weight > nodeY.weight) ;
        }
};

vector< list<Vertices> > adj;
vector<int> weights;
priority_queue<Vertices, vector<Vertices>, CompareGreater> Q;

int nrVertices, nrEdges;

void readData();
void Dijkstra(Vertices);


void readData() {
    ifstream myFile;
    myFile.open("graph.txt");

    int nodeX, nodeY, weight;

    myFile >> nrVertices >> nrEdges;

    adj.resize(nrVertices+1);
    //weights.resize(1);

    for (int i = 1; i <= nrVertices; ++i) {
        weights.push_back(INT_MAX);
    }

    for (int i = 1; i <= nrEdges; ++i) {
        myFile >> nodeX >> nodeY >> weight;
        adj[nodeX].push_back(Vertices(nodeY, weight));
    }

    //weights.resize(1);
    for (vector<int>::iterator itr = weights.begin()+1; itr != weights.end(); ++itr) {
        cout << (*itr) << " "<<endl;
    }


    myFile.close();
}

void Dijkstra(Vertices startNode) {
    Vertices currVertex;

    weights[startNode.vertex] = 0;
    Q.push(startNode);

    while (!Q.empty()) {
        currVertex = Q.top();
        Q.pop();

        //cout<<"Removed "<<&currVertex<<"from heap"<<endl;

        if (currVertex.weight <= weights[currVertex.vertex]) {
            for (list<Vertices>::iterator it = adj[currVertex.vertex].begin(); it != adj[currVertex.vertex].end(); ++it) {
                if (weights[it->vertex] > weights[currVertex.vertex] + it->weight) {
                    weights[it->vertex] = weights[currVertex.vertex] + it->weight;
                    Q.push(Vertices((it->vertex), weights[it->vertex]));
                }
            }
        }
    }
}

int main() {

    readData();
    Dijkstra(Vertices(1, 0));

    return 0;
}

So this is my revised answer. But it just prints out:

2147483647 
2147483647 
2147483647 
2147483647 
2147483647 
2147483647 

When I need it to print out something like this:

V1: V2, 2; V4, 1
V2: v4, 3; V5, 10
V3: V1, 4; V6, 5
V4: V3, 2; V5, 2; V6, 8; V7, 4
V5: V7, 6
V6:
V7: V6, 1
Removed minimum 1 from heap
Print heap: V2, d=inf V4., d=inf v3, d= inf v7, d=inf v5......
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top