Why addEdge(36, 36)

I am still not getting the purpose of adding this edge. We have already added the edges to 36 previously from 30, 31… 35. But if I don’t add it, I’m getting 0 as the answer!

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,
it might be confusing to understand what i am saying , so do clear any doubt you are having

Okay gotcha! Instead we could manually add edges from 30, 31,…35 to 36. Also where do the edges starting from 30, 31,…35 to 36 end?

from 30 you can go to 31,32,33,34,35,36 as dices can take value as 1 2 3 4 5 6, for 31 you can only go to 33 34 35 36 not 37 as there is no value 37 for this case and similarly for 33 34 35 you can we can go upto 36

I’m getting it. My point was we can run the outer for loop till 30 and manually add edges from 31 onwards. Anyways please answer my second question

yeah ! definitely you can, but it will be 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

This means we cannot back trace in a graph using this approach if the destination is the terminal node. We have to add another edge from and to itself.

yeah you have to add 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

Okay. Thanks a lot! :smiley: