I was trying to implement graphs(to check if a path..) using vectors in cpp. Why does not the "addNewEdge" function add values to the adjacency list?

  adjacency-list, c++, graph
#include <bits/stdc++.h>
using namespace std;

//This function does not work as i intended it to. It should add the node a to the list of b and vice versa(an undirected graph.

void addNewEdge(vector<vector <int>> adjacencyList,int a,int b, vector<bool> isVisited){
    adjacencyList[a].push_back(b);
    adjacencyList[b].push_back(a);
    isVisited[a] = false;
    isVisited[b] = false;
}

//the following function is meant to mark a node as visited and all the nodes present in the list(i.e. those connected by edges) as  visited.


void Explore(vector<vector <int>> adjacencyList,int a,vector<bool> isVisited){
    isVisited[a] = true;
    auto itr = adjacencyList[a].begin();
    while (itr!=adjacencyList[a].end()){
        if (!isVisited[*itr])
        Explore(adjacencyList,*itr,isVisited);
        ++itr;
    }

}


int main(){
    //m is the no of vertices and n the no. of edges
    int m,n,i,a,b,node_1,node_2;
    cin >> m >> n;
    //cout << m << n;
    vector<vector <int>> adjacencyList(m+1);
    vector<bool> isVisited(m+1);

// it is where the magic was supposed to happen. Sadly the integer b does not push_back to the vector adjacencyList[a]

    for (i=0;i<n;++i){
        cin >> a >> b ;
        //cout << a << b << i;
        addNewEdge(adjacencyList,a,b,isVisited);
    }
    cin >> node_1 >> node_2;
    Explore(adjacencyList,node_1,isVisited);
    if(isVisited[node_2]) cout << "A path exists.";
    else cout << "No path exists";
}

Source: Windows Questions C++

LEAVE A COMMENT