i understand the map declaration but i dont understand the addEdge function.
adjList is a map .
then adjList[u] means what?and there is no pushback function for the map
Adjacency list representation of graph for generic data
adjList is not a map but an array of lists.
adjList[u] means the number of nodes connected with node u.
in class graph ,adjList is declared as
map<T,list> adjList;
and then in addEdge function ,it is trying to access adjList[u].push_back(v).
i understand that adjList[u] will access the list and push_back v ,but how it can since adjList[u] is not there yet and it is trying to do pushback.
Okay Right. It is a map. You are thinking right that adjList[u] accesses the list that corresponds to u. And in it we perform a push_back.
adjList[u] is empty initially. We push a new element in it. Why do you feel that it cannot be done?
okay.do correct me on my understanding.
map<string,list> adjList;
adjList[“a”].push_back(“b”);
the second statement will insert “b” as first element in the linkedlist ,whose key in the map is “a”.
such that if the key “a” is not present it will create the key and the associated value(linkedlist) and insert it into map.
if the key “a” is already present ,it will find the value that is linkedlist and do pushback on that linkedlist.
is that correct??
Yes, Right!
If the key is not present, it gets created.
if already present, b gets added in the last.
Do ask if you have any further doubt regarding this.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.