mercredi 27 février 2019

Adjacency list representation of a weighted graph

So guys, recently i have been practicing a lot with data structures, graphs and etc. And i encountered a problem with a given code. I am implementing a graph, that is represented with an adjacency list.The problem i stumble across is when i try to implement the solution, using user input. I have tried to do it in many ways but i still stumble across some problems in the program. Because i am using a vector that takes a list of integers, whenever i try to fill the vector with a while loop(for instance) i don't know why, but i can't quite fill it up correctly. And since my mistake starts even from there i don't know how to proceed with the program. If you guys can give me some sort of hint on how i can implement my code to work with user input, or even give me a similar code that i work with i would be really grateful !

This is my code:

const int N = 4;

//purpose of the class is to tell what the weight of the given edge is
class Edge{
   private:
       double weight;
       int vertex_id;

   public:
    //constructor that initializes the weight and vertex id

    Edge(double w, int id)
    {
        weight = w;
       vertex_id = id;
    }

    double getWeight() const
        {
            return weight;
        }

    int getId() const
    {
        return vertex_id;
    }
};

int main()
{
    std::vector<std::list<Edge>> adjList(N); //creating our vector that will store a list of integers

    adjList[0].push_back(Edge(4,1)); //pushing back the first neighbours of our Zero list
    adjList[0].push_back(Edge(2,2)); //pushing back the second neighbours of our Zero list and so on...

    adjList[1].push_back(Edge(4,0));
    adjList[1].push_back(Edge(5,2));

    adjList[2].push_back(Edge(2,0));
    adjList[2].push_back(Edge(5,1));
    adjList[2].push_back(Edge(1,3));

    adjList[3].push_back(Edge(1,2));

    std::vector<std::list<Edge>>::iterator i; //declaring our vector iterator

    int c = 0; //we create a counter(and ofcourse assign zero to it)


    //create the for loop, where the iterator starts at the begining of the vector
    //and ends when the vector (adjList) ends


    //*KEEP IN MIND THAT THE FIRST FOR LOOP ONLY ITERATES THROUGH THE NODES OF THE VECTOR
    for (std::vector<std::list<Edge>>::iterator i = adjList.begin(); i != adjList.end(); i++)
    {

        cout << "Vertices connected to our node: " << c << endl;
        std::list<Edge> li = *i; //this pointer serves the purpose to get the list for each different node

        //NOW THE SECOND FOR LOOP ITERATES THROUGH THE LISTS, THAT EACH NODE CONTAINS
        for (std::list<Edge>::iterator iter = li.begin(); iter != li.end(); iter++)
        {
            cout << " (V = " << (*iter).getId() << " weight= " << (*iter).getWeight() <<")";
        }

        cout << endl; // we end the line between the different nodes of the vector

        c++; //increment our counter
    }

Aucun commentaire:

Enregistrer un commentaire