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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:52:16+00:00 2026-06-16T03:52:16+00:00

I am trying to extract from a big graph the sub-graph of all connected

  • 0

I am trying to extract from a big graph the sub-graph of all connected nodes containing a specific node.

Is there a solution in the Networkx library?

[EDIT]
My graph is a DiGraph

[EDIT]
Rephrased simply:
I want the part of my graph that contain my specific node N_i and and all the nodes that are connected directly or indirectly (passing by other nodes) using any incoming or outcoming edges.
Example:

>>> g = nx.DiGraph()
>>> g.add_path(['A','B','C',])
>>> g.add_path(['X','Y','Z',])
>>> g.edges()
[('A', 'B'), ('B', 'C'), ('Y', 'Z'), ('X', 'Y')]

My desired result would be:

>>> g2 = getSubGraph(g, 'B')
>>> g2.nodes()
['A', 'B', 'C']
>>> g2.edges()
[('A', 'B'), ('B', 'C')]
  • 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-16T03:52:17+00:00Added an answer on June 16, 2026 at 3:52 am

    You can use shortest_path() to find all of the nodes reachable from a given node. In your case you need to first convert the graph to an undirected representation so both in- and out-edges are followed.

    In [1]: import networkx as nx
    
    In [2]: >>> g = nx.DiGraph()
    
    In [3]: >>> g.add_path(['A','B','C',])
    
    In [4]: >>> g.add_path(['X','Y','Z',])
    
    In [5]: u = g.to_undirected()
    
    In [6]: nodes = nx.shortest_path(u,'B').keys()
    
    In [7]: nodes
    Out[7]: ['A', 'C', 'B']
    
    In [8]: s = g.subgraph(nodes)
    
    In [9]: s.edges()
    Out[9]: [('A', 'B'), ('B', 'C')]
    

    Or in one line

    In [10]: s = g.subgraph(nx.shortest_path(g.to_undirected(),'B'))
    
    In [11]: s.edges()
    Out[11]: [('A', 'B'), ('B', 'C')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract values from AraryList but all i keep getting is
So Im trying to extract nodes from my database (by going through it recursively)
I am trying to extract links from specific web page like: http://www.directmirror.com/files/0GR7ZPCY but it
I am trying to extract data from this String: Hello there. Blah blahblah blah
I am trying to extract test from a huge file containing text in this
I am trying to extract values from an XML column. Unfortunately, whatever combination I
I'm trying to extract fields from a pipe-delimited file and provide them as arguments
I have been trying to extract data from a database and fill in a
I have a file which I'm trying to extract information from, the file has
I am new to Perl and I am trying to extract email from string

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.