0

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

BenjaminGolder
  • 1,573
  • 5
  • 19
  • 37
user2938332
  • 133
  • 1
  • 1
  • 9
  • Can you ask some questions? It's unclear what you do or do not understand about it. What are you hoping to do with it? Have you tried it? Have you already made a `networkx.Graph` object that you are working with? – BenjaminGolder Oct 31 '15 at 22:19
  • I want to understand algorithm of that function and how to get subgraph of certain vertices from original graph. I want to code it in different language and but do not have sufficient knowledge of python. – user2938332 Oct 31 '15 at 22:24
  • Perhaps you should copy the code into a [gist](https://gist.github.com/) or paste it in your question, and then ask about specific lines in the code. Which lines are confusing to you? – BenjaminGolder Oct 31 '15 at 22:26
  • What language are you familiar with? Which portions do you not understand how to implement in your chosen language? – BenjaminGolder Oct 31 '15 at 22:27
  • https://gist.github.com/anonymous/11d26c0c6db2f798ae8e I would like to know how this concrete code works and steps which are processed when subgraph is computed. I added example of graph at the end. – user2938332 Oct 31 '15 at 22:34
  • I am familiar with language called Julia and i want to implement this function in it. – user2938332 Oct 31 '15 at 22:35
  • you should edit the gist and rename it with `.py` extension in place of `.txt`. You should also explain which lines you don't understand (you can add comments to the code using the `#` symbol at the beginning of a line), and explain what language you are trying to convert this to. – BenjaminGolder Oct 31 '15 at 22:36
  • Try doing your best to write in Julia (you can just add a second file to that gist and denote that it is in the Julia language) even if you can only translate a few lines. Then explain which parts you don't understand or any questions you have. – BenjaminGolder Oct 31 '15 at 22:39
  • I do not understand the whole function i posted in gist file. I am looking for someone who will explain it step by step on some simple example. This function returns a subgraph from graph for certain vertices that user selected. – user2938332 Oct 31 '15 at 22:44
  • I would say that i need something like pseudocode of that function. – user2938332 Oct 31 '15 at 22:45

1 Answers1

1

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)
BenjaminGolder
  • 1,573
  • 5
  • 19
  • 37