Revision bcfab97a0adb8e201cad960b05c188b56a5516d6 authored by Fabian Brandt on 23 February 2021, 16:43:03 UTC, committed by GitHub on 23 February 2021, 16:43:03 UTC
Bump version 8.1
2 parent s 48162bf + 217e895
Raw File
sampling.py
""" Sampling from graphs """

__author__ = "Elisabetta Bergamini"

def bfsSample(G, source=None, k = 50):
    """ Start a BFS from source node, return node-induced subgraph of the first k nodes discovered"""
    if not source:
        source = nk.graphtools.randomNode(G)
    n = G.numberOfNodes()
    visited = [False]*n
    Q = [source]
    closest = set([source])
    global found
    found = 0
    while len(Q) > 0 and found < k:
        u = Q.pop(0)
        def enqueue(u,v,weight, eid):
            global found
            if not visited[v] and found < k:
                found += 1
                visited[v] = True
                Q.append(v)
                closest.add(v)
        G.forEdgesOf(u, enqueue)
    print("found {0} nodes".format(len(closest)))
    G1 = G.subgraphFromNodes(closest)
    return G1
back to top