(Hope I used invert correctly)
I have a set of nodes (objects) and edges (a list of other objects that node belongs to). The entire graph is presented in Dictionary<string, List<string>.
(Sidebar: The object in question is not true string. The actual type of the object does not matter)
Now I need to invert the graph, so instead of having a list of objects and all the objects to which they refer, I have a list of objects and all objects that belong to them.
I can do this quite easily with a loop, but I think it's better to use Linq. This is so, and if so, how to do it?
Just to make sure we are clear, let it pretend that my dataset looks like this:
var graph = new Dictionary<string, List<string>> {
{"A", new string[] { "C", "D" } },
{"B", new string[] { "D" } },
{"C", new string[] { "D" } },
{"D", new string[] { "B" } },
};
:
var graph = new Dictionary<string, List<string>> {
{"A", new string[] { } },
{"B", new string[] { "D" } },
{"C", new string[] { "A" } },
{"D", new string[] { "A", "C", "B" } },
};
!