1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Finding two 2-hop neighborhood graph?

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I have nodes of interest and was able to extract a subgraph (code below) but I believe the NetworkX subgraph() or induced_subgraph() only extracts 1-hop neighborhood of the nodes.

    ## Extract subgraph using nodes of interest (list)
    G_S = induced_subgraph(G, S) # S is nodes of interest (list)


    I looked at Ego Graph but it only extracts graphs on a single node. How can I extract a 2 or 3-hop (or even beyond) subgraph? If NetworkX can't be used does graph-tool have a function where I can extract a k-hop subgraph?

    Attempt on 2-hop subgraph:

    def extend_subgraph(G, S):
    # Start with the original set of nodes of interest
    extended_nodes = set(S)

    # Loop over each node in the current subgraph
    for node in S:
    # Get 1-hop neighbors of this node
    one_hop = set(nx.neighbors(G, node))

    # For each 1-hop neighbor, get its 1-hop neighbors
    for neighbor in one_hop:
    if neighbor not in extended_nodes:
    extended_nodes.add(neighbor)
    neighbors_of_neighbor = set(nx.neighbors(G, neighbor))

    # Add the 1-hop neighbors of the neighbor if they haven't been seen
    extended_nodes.update(neighbors_of_neighbor - extended_nodes)

    # Create a new subgraph with the extended set of nodes
    G_extended_subgraph = G.subgraph(extended_nodes)

    return G_extended_subgraph

    G2 = extend_subgraph(G, S)

    Continue reading...

Compartilhe esta Página