Adjacency list implementation

we defined list of arrays for graph implementation.
list *l;
l=new list v[];

then for printing the no.
we write
for (i=0; i<v; i++) // v is the no. of vertices or the no. of arrays which can be stored
{
cout<<i<<"->";
for (int vertex: l[i]) // MY DOUBT IS THIS STEP
cout<<l[i];
}
what is the significance of the step i mentioned as my doubt ?
also i dont know how for loop can be used by this?
also if we could do this with normal for loop?
thankyou

Hey Satyam, this is simple as a matrix (i.e 2D array) is stored in memory as the below picture shows:
47%20PM

So, here i is iterating over all the rows and
vertex is iterating over all the columns of ith row.

For loop is used because an array which represents columns is stored corresponding to each row.
And yes you can do it using the normal for loop also. And the one you have mentioned is also normal for loop, its just the another way of writing for loop using iterator.

If you are still unable to understand how 2D arrays works, i would recommend you to watch the online course videos of 2D arrays section.

thankyou
i got it
:smile:

yeahh :slight_smile: