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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:01:17+00:00 2026-05-18T02:01:17+00:00

I have a sort of a one level tree structure as: Where p are

  • 0

I have a sort of a one level tree structure as:

alt text

Where p are parent nodes, c are child nodes and b are hypothetical branches.

I want to find all combinations of branches under the constraint that only one parent can branch to only one child node, and two branches can not share parent and/or child.

E.g. if combo is the set of combinations:

combo[0] = [b[0], b[3]]
combo[1] = [b[0], b[4]]
combo[2] = [b[1], b[4]]
combo[3] = [b[2], b[3]]

I think that’s all of them. =)

How can this be achived automaticly in Python for arbitrary trees of this structures i.e. the number of p:s, c:s and b:s are arbitrary.

EDIT:

It is not a tree but rather a bipartite directed acyclic graph

  • 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-18T02:01:18+00:00Added an answer on May 18, 2026 at 2:01 am

    Here’s one way to do it. There are lot’s of micro-optimizations that could be made but their efficacy would depend on the sizes involved.

    import collections as co
    import itertools as it
    
    def unique(list_):
        return len(set(list_)) == len(list_)
    
    def get_combos(branches):
        by_parent = co.defaultdict(list)
    
        for branch in branches:
            by_parent[branch.p].append(branch)
    
        combos = it.product(*by_parent.values())
    
        return it.ifilter(lambda x: unique([b.c for b in x]), combos)
    

    I’m pretty sure that this is at least hitting optimal complexity as I don’t see a way to avoid looking at every combination that is unique by parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 columns in a one-to-many relationship. I want to sort on the
I have a sort of tree structure that represent a hierarchy of layers in
I have a table with data representing a tree structure, with one column indicating
I have some sort of a jquery chat at one specific PHP page. Of
One of the huge benefits in languages that have some sort of reflection/introspecition is
i have one problem when i sort the NSMutablearray using date and time wise.
I have sort of a tricky problem I'm attempting to solve. First of all,
Let's say I have a person table, and I want it to classify all
I have a table with 3 columns and want the first one to be
I'm writing myself a forum, and I want to have one of those you

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.