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

  • Home
  • SEARCH
  • 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 9039695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:47:36+00:00 2026-06-16T09:47:36+00:00

I have a Python code which finds the transitive closure. Example: Input: {(‘A’,’B’),(‘B’,’C’),(‘C’,’D’),(‘E’,’F’)} Output:

  • 0

I have a Python code which finds the transitive closure.

Example:

Input: {(‘A’,’B’),(‘B’,’C’),(‘C’,’D’),(‘E’,’F’)}

Output: {(‘B’, ‘C’), (‘A’, ‘D’), (‘A’, ‘B’), (‘C’, ‘D’), (‘B’, ‘D’), (‘E’, ‘F’), (‘A’, ‘C’)}

The code works perfect, but what I’m looking for is to have the output as a set of subgraphs. I’m a beginner in Python, and I’m not sure how to do that.

According to the given input, here is the output that I’m looking for, which has two elements in the set each represents a subgraph from the transitive closure output: {(A, B, C, D), (E, F)}

Here is the code:

from collections import defaultdict

def transitive_closure(elements):
    edges = defaultdict(set)
    # map from first element of input tuples to "reachable" second elements
    for x, y in elements: edges[x].add(y)

    for _ in range(len(elements) - 1):
        edges = defaultdict(set, (
                                  (k, v.union(*(edges[i] for i in v)))
                                  for (k, v) in edges.items()
                                  ))

    return set((k, i) for (k, v) in edges.items() for i in v)

result = set(transitive_closure([('A','B'),('B','C'),('C','D'),('E','F')]))
print result
  • 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-06-16T09:47:38+00:00Added an answer on June 16, 2026 at 9:47 am

    Problem solved using networkx in Python. networkx provides a functionality to find all sub-graphs of a given one.

    All I needed is to have the output of the transitive_closure() method, translate it to a graph for networkx and then having the new created graph as an input to the connected_component_subgraphs() method provided by networkx.

    H=nx.connected_component_subgraphs(G)
    

    H is a set contains all sub-graphs needed.

    The main drawback is processing time, but that’s the best I could find.

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

Sidebar

Related Questions

I have the following Python code, and it works perfect with the directed graph.
I have created some python code which creates an object in a loop, and
I currently have a working python code which generates a 2D animation of a
I am writing a code in Python 2.7 in which I have defined a
I have some python code that I wrote to convert a python list into
I have some python code using shutil.copyfile: import os import shutil src='C:\Documents and Settings\user\Desktop\FilesPy'
I have some python code that currently performs expensive computation by performing the computation
I have a C program with embedded python code. I have compiled python 2.7.2
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have been writing Python code for only a couple of weeks, so I'm

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.