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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:58:53+00:00 2026-05-26T10:58:53+00:00

In networkX, I have a tree as DiGraph(). #!/usr/bin/python # -*- coding: utf-8 -*-

  • 0

In networkX, I have a tree as DiGraph().

#!/usr/bin/python
# -*- coding: utf-8 -*-
import networkx as nx
t = nx.DiGraph()
t.add_edge(1,'r')
t.add_edge(2,'r')
t.add_edge(3,'r')
t.add_edge(4,2)
t.add_edge(5,2)
t.add_edge(6,5)
print t.edges() 

If a take the node 2 of tree.
how I can get the subtree of 2 ?

Edit

I expected this subtree

[(4,2),(5,2),(6,5)]
  • 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-26T10:58:54+00:00Added an answer on May 26, 2026 at 10:58 am

    If you mean the subtree rooted at node 2, that’s

    from networkx.algorithms.traversal.depth_first_search import dfs_tree
    
    subtree_at_2 = dfs_tree(t, 2)
    

    Edit: it seems you’ve reversed the order of nodes in your edges. In a directed tree, all paths proceed from the root to a leaf, not the other way around. dfs_tree(t.reverse(), 2) gives you the tree that you want, but do change your code.

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

Sidebar

Related Questions

I'm writing a Java Tree in which tree nodes could have children that take
I have a program written in python using the module networkx to create a
Let me explain the tree structure: I have a network directory where several times
Using Python's Networkx library, I created an undirected graph to represent a relationship network
I'm working on a graphical model project with python using NetworkX . NetworkX provides
I have defined a pyparsing rule to parse this text into a syntax-tree... TEXT
I'm working on some code for a directed graph in NetworkX, and have hit
My friend does not have Python or the libraries needed installed on his machine
I was wondering: With a tree, the root can have multiple children and no
I need the edge end points from a graph. I have installed networkx. I

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.