why we have add edge to 36 , look when we came across 35 , ith has edge 36 , so its distance should be increased why we we need to add edge to 36
Unable to understand
is it a reason because while printing distance , we does not get 36 as it is not any node , its just neighrbood
Hey @Shubham800 we did this because there was no edge which originate from 36, and since we are using map for our adjacency list, a index is created in map when we assign some value to it , and since there is no edge originating from 36, due to which map does’nt have 36 as index i.e l[36] does’nt exists , so by doing addedge(36,36) we are ensuring that there is an index 36 in l ,so if we traverse map we get 36 as key ,
you won’t have to do this if you are using adjacency matrix or vector for adjacency list.
but when we dist[36]=dist[node]+1; , in dist map it would create a 36 key , would n’t it ???
and for ajacency list 36 would be in list of any other no. , so we would traverse it as any no. neighrbour don’t we ???
It won’t
you have to add edge manually as if i is 36 then i+dice value is always greater than 36 so no entry of 36 will be present in the list
It’s just easier if you add a if statement in the for loop like
if(i+diceValue <=36){
G.addEdge(i,i+diceValue);
}
where diceValue can take 1 2 3 4 5 6 values
To get a detailed idea i would suggest to first code and then print all the key values and linked list in it. You will get a clear idea of what’s happening.
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.
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.