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 148273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:55:45+00:00 2026-05-11T08:55:45+00:00

I’m looking for an algorithm to invert (reverse? turn inside-out?) a DAG: A* #

  • 0

I’m looking for an algorithm to ‘invert’ (reverse? turn inside-out?) a DAG:

       A*      # I can't ascii-art the arrows, so just       / \      # pretend the slashes are all pointing      B   C     # 'down' (south-east or south-west)     /   / \    # e.g.     G   E   D   # A -> (B -> G, C -> (E -> F, D -> F))         \ /          F 

The representation I’m using is immutable truly a DAG (there are no ‘parent’ pointers). I’d like to traverse the graph in some fashion while building a ‘mirror image’ graph with equivalent nodes, but with the direction of relations between nodes inverted.

         F*         / \    G*  E   D   # F -> (E -> C -> A, D -> C -> A), G -> B -> A     \   \ /    #       B   C     # Again, arrows point 'down'       \ /      #         A       #  

So the input is a set of ‘roots’ (here, {A}). The output should be a set of ‘roots’ in the result graph: {G, F}. (By root I mean a node with no incoming references. A leaf is a node with no outgoing references.)

The roots of the input become the leaves of the output and visa versa. The transformation should be an inverse of itself.

(For the curious, I’d like to add a feature to a library I’m using to represent XML for structural querying by which I can map each node in the first tree to its ‘mirror image’ in the second tree (and back again) to provide more navigational flexibility for my query rules.)

  • 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. 2026-05-11T08:55:46+00:00Added an answer on May 11, 2026 at 8:55 am

    Traverse the graph building a set of reversed edges and a list of leaf nodes.

    Perform a topological sort of the reversed edges using the leaf (which are now root) nodes to start with.

    Construct the reversed graph based on the reversed edges starting from the end of the sorted list. As the nodes are constructed in reverse topological order, you are guaranteed to have constructed the children of a given node before constructing the node, so creating an immutable representation is possible.

    This is either O(N) if you use structures for your intermediate representation which track all links in both directions associated with a node, or O(NlnN) if you use sorting to find all the links of a node. For small graphs, or languages which don’t suffer from stack overflows, you can just construct the graph lazily rather than explicitly performing the topological sort. So it depends a little what you’re implementing it all in how different this would be.

    A -> (B -> G, C -> (E -> F, D -> F))  original roots: [ A ] original links: [ AB, BG, AC, CE, EF, CD, DF ]  reversed links: [ BA, GB, CA, EC, FE, DC, FD ] reversed roots: [ G, F ] reversed links: [ BA, CA, DC, EC, FE, FD, GB ] (in order of source) topologically sorted: [ G, B, F, E, D, C, A ] construction order : A, C->A, D->C, E->C, F->(D,E), B->A, G->B 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 388k
  • Answers 388k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I see the issue now with the program below (stdout.cpp… May 15, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer 1) how do I get the .format on there? api.connect… May 15, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer You should find my answer to this question helpful. It… May 15, 2026 at 12:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.