given a jagged array:
int[][] edges = new int[][]
{
new int[] {0,1},
new int[] {0,2},
new int[] {0,3},
new int[] {1,4},
};
is there a more elegant way to do this:
var adjlist = new List<List<int>>();
for(int i=0; i<n; i++)
{
adjlist.Add(new List<int>());
}
foreach(var arr in edges)
{
int src = arr[0];
int dst = arr[1];
adjlist[src].Add(dst);
adjlist[dst].Add(src);
}
something that improves on the time complexity for this would be ideal.
Thank you.
new List<>(), list indexing and adding items to a list are constant time operations).