w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
create an adjacency list type structure from a list of pairs

You can do this with LINQ's GroupBy method, like this:

var adj = sList
    .GroupBy(p => p.val1)
    .ToDictionary(g => g.Key, g => g.Select(p =>
p.val2).ToList());

Note that this would not compute a transitive closure of your graph, i.e. only the direct links will be present.

In .NET 4 and up you can also use Tuple<int,int> instead of making your own Pair class.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.