user2938332
user2938332

Reputation: 143

Subgraph function from networknx python

I would like to ask if there is someone who could help me understand subgraph function from this python module. E.g on some simple graph. Subgraph function is between lines 1400 and 1458.

https://github.com/networkx/networkx/blob/e73face328281b9afe7eb848887f17d97be1709d/networkx/classes/graph.py#L1400-L1458

Upvotes: 0

Views: 450

Answers (1)

BenjaminGolder
BenjaminGolder

Reputation: 1592

Here is some pseudocode for that function.

def get_subgraph(existing_graph, node_ids):
    get the nodes that correspond to the node_ids
    create a new graph, make sure it is the same type
    for each node in the list of chosen nodes:
        copy the node to the new graph, retaining its associated data
    get a lookup table of edges for the nodes in the new graph
    for each node in the new graph:
        get its neighbors from the old graph
        for each of the neighbors:
            if the neighbor is in the new graph:
                add the edge to the new graph (from both directions if needed)

Upvotes: 1

Related Questions