1
#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";
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
afasgag
  • 11
  • 2
  • 1
    You pass `adjacencyList` *by value*. Which means that the `addNewEdge` function gets its own copy. No modifications to this copy will be propagated to the original vector. Same with `isVisited`. – Some programmer dude Jun 22 '21 at 06:17
  • 2
    By the way, please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jun 22 '21 at 06:19
  • Changes to parameters of `Explore `are purely local because they are copied values (passed by value). it's a 1001th dupe of roughly same question.. where ever a fallback question of this though – Swift - Friday Pie Jun 22 '21 at 06:26
  • O yes! that worked.(passing by reference). Thank you sire! I'm such a noob:/ – afasgag Jun 22 '21 at 06:32

1 Answers1

1

If you want to edit the actual value of a vector passed to a function you must not do it in this way. What you're doing is copying it and adding edges to the copy which is destructed as soon as the function goes out of scope.
You can either pass it by reference in this way or by pointer. Same with visited list.

way you can pass by reference.

void addNewEdge(vector<vector <int>> &adjacencyList,int a,int b, vector<bool> &isVisited)

way you can pass by pointer.

void addNewEdge(vector<vector <int>> * adjacencyList,int a,int b, vector<bool> * isVisited)

This will also dramatically reduce the time complexity as the vector wont have to be copied each time you call the function.

JRazek
  • 191
  • 2
  • 11