Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6101735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:32:01+00:00 2026-05-23T13:32:01+00:00

(I hope I used ‘inverting’ correctly) I have a collection of nodes (objects) and

  • 0

(I hope I used ‘inverting’ correctly)

I have a collection of nodes (objects) and edges (a list of other objects to which the node refers to). The whole graph is represented in a Dictionary<string, List<string>.

(Sidebar: The object in question is not string in reality. The actual type of the object is of no consequence)

Now, I need to invert the graph, so rather than having a list of objects and all the objects they refer to, I have a list of objects and all the objects that refer to them.

I can do this pretty easily with a loop, but I imagine there’s a better way using Linq. Is this the case, and if so, how do I do it?

Just to ensure that we’re clear, let’s 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" } }, //note that C and D refer to each other
};

I need to transform it into the moral equivalent of this:

var graph = new Dictionary<string, List<string>> {
    {"A", new string[] {  } },
    {"B", new string[] { "D" } },
    {"C", new string[] { "A" } },
    {"D", new string[] { "A", "C", "B" } },
};

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:32:02+00:00Added an answer on May 23, 2026 at 1:32 pm

    You can naively reverse by just saying “for every node, find all the vertices that have this node in its neighbors list” (the union is necessary in case there are nodes that can be reached, but don’t have any neighbors but is unnecessary if for such nodes you have an entry of the form v -> { } in your dictionary):

    var inverse = graph.Keys
                       .Union(
                           graph.Values
                                .SelectMany(v => v)
                                .Distinct()
                       )
                       .ToDictionary(
                           v => v,
                           v => graph.Keys.Where(key => graph[key].Contains(v))
                       );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope I'm asking this correctly: I have a project Projects.Client I have my class
Hope someone can help me. I have used the following tutorial as a guide
I actually have two simple questions, I hope. What is the TT used for
I have used Observer Pattern for my application. I have a subject which has
I am making one library which I hope so is going to be used
I hope there are someone who have used PDFCreator before. I am using PDFcreator
Hello just really a simple question (I hope). I have a border used by
Hope the AWK gurus can provide a solution to my problem . I have
Hope someone could help me with this: I would like to have a column
Hope someone will give me a hand with this problem I have. So here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.