0

Can someone please explain to me this adjacency list ?

Hi, can someone please explain to me this adjacency list ? I understand the vectors and stuff but cant seem to comprehend adj[s].push_back(d) what does this mean ? Say s is 0 and d is 1 , will it create a new position and store the value 1 after 2 ? Or it will do this at the end, here is the code: #include <iostream> #include <vector> using namespace std; // Add edge void addEdge(vector<int> adj[], int s, int d) { adj[s].push_back(d); adj[d].push_back(s); } int main() { int V = 5; // Create a graph vector<int> adj[V]; // Add edges addEdge(adj, 0, 1); addEdge(adj, 0, 2); addEdge(adj, 0, 3); addEdge(adj, 1, 2); }

30th Jun 2020, 12:32 PM
Ovidiu Călin
Ovidiu Călin - avatar
2 Antworten
+ 2
adj[s].push_back(d) means: create a directed edge from node s to d (s->d) in that code, the edge is added both ways so the function is meant to create edges for undirected graph.
30th Jun 2020, 12:55 PM
Bobby Fischer
Bobby Fischer - avatar
0
Thank you a lot !!
30th Jun 2020, 12:58 PM
Ovidiu Călin
Ovidiu Călin - avatar