def bowtie_components(graph, name):
    """Give sizes of DISCONNECTED, IN, OUT, SCC"""
    results = {}

    N = graph.GetNodes()

    SCC = snap.GetMxScc(graph)
    n = SCC.GetRndNId()

    disc = N - snap.GetMxWcc(graph).GetNodes()
    scc = SCC.GetNodes()
    SCC_in = snap.GetBfsTree(graph, n, False, True)
    SCC_out = snap.GetBfsTree(graph, n, True, False)
    in1 = SCC_in.GetNodes() - scc
    out = SCC_out.GetNodes() - scc
    tt = N - disc - scc - in1 - out

    results["a. SCC"] = scc
    results["b. IN"] = in1
    results["c. OUT"] = out
    results["d. TENDRILS + TUBES"] = tt
    results["e. DISCONNECTED"] = disc

    print 'Total nodes in {} network: {}'.format(name, N)
    print 'DISCONNECTED: {}'.format(disc)
    print 'SCC: {}'.format(scc)
    print 'IN: {}'.format(in1)
    print 'OUT: {}'.format(out)
    print 'TENDRILS + TUBES: {}'.format(tt)

    return results
Exemplo n.º 2
0
    def reachable_analysis(graph, name):
        reached_out_cum = []
        reached_in_cum = []
        for i in range(100):
            node = graph.GetRndNId()
            bfs_out = snap.GetBfsTree(graph, node, True, False)
            bfs_in = snap.GetBfsTree(graph, node, False, True)
            reached_out_cum.append(bfs_out.GetNodes())
            reached_in_cum.append(bfs_in.GetNodes())

        reached_out_cum = sorted(reached_out_cum)
        reached_in_cum = sorted(reached_in_cum)

        y = reached_out_cum
        if True:#name == 'epinions':
            plt.plot(y, label='Reachability using outlinks for %s' % name)
            plt.yscale('log')
        #else:
        #    plt.plot(y, label='Reachability using outlinks for %s' % name)
        plt.savefig('outlinks_%s.png' % name)
        plt.close()

        y = reached_in_cum
        if True:#name == 'email':
            plt.plot(y, label='Reachability using inlinks for %s' % name)
            plt.yscale('log')
        #else:
        #    plt.plot(y, label='Reachability using inlinks for %s' % name)
        plt.savefig('inlinks_%s.png' % name)
        plt.close()
Exemplo n.º 3
0
def q2_1_aux(name,id):
    G = load_graph(name)

    # Your code here:

    OutTreeEp = snap.GetBfsTree(G, id, True, False)
    InTreeEp = snap.GetBfsTree(G, id, False, True)
    sccOneRandNodeId = snap.GetMxScc(G).GetRndNId()

    sccInOutTree = OutTreeEp.IsNode(sccOneRandNodeId)
    sccInInTree = InTreeEp.IsNode(sccOneRandNodeId)
    print "graph:",name
    print "nodeId",id

    OutTree = snap.GetBfsTree(G, id, True, False)
    InTree = snap.GetBfsTree(G, id, False, True)
    sizeOutTree = OutTree.GetNodes()
    sizeInTree = InTree.GetNodes()
    print "sizegraph", G.GetNodes()
    print "sizeOutTree", sizeOutTree
    print "sizeInTree", sizeInTree

    if (sccInOutTree):
        if (sccInInTree):
            print "node in SCC"
        else:
            print "node in IN"
    else:
        print "node in OUT"
Exemplo n.º 4
0
def q2_2_util(dataset_name, outward):
    G = load_graph(dataset_name)
    Rnd = snap.TRnd(42)
    Rnd.Randomize()
    rand_ids = set()
    while(len(rand_ids)<100):
        NId = G.GetRndNId(Rnd)
        rand_ids.add(NId)
    
    cumulative_list = []
    for rand_nid in rand_ids:
        if outward:
            BfsTree = snap.GetBfsTree(G, rand_nid, True, False)
        else:
            BfsTree = snap.GetBfsTree(G, rand_nid, False, True)
        outward_set = set()
        for EI in BfsTree.Edges():
            outward_set.add(EI.GetDstNId())
        cumulative_list.append(len(outward_set))
        # print(len(cumulative_list))
    x = np.linspace(0, 1, 100)
    # print(x)
    cumulative_list.sort()
    cumulative_list = np.array(cumulative_list)
    # print(cumulative_list)
    plt.plot(x, cumulative_list, lw=2)
    plt.legend()
    plt.xlabel('Frac of rand ids')
    plt.ylabel('%s Reached notes (%s)' % (dataset_name, 'outward' if outward else 'inward'))
    plt.yscale('log')
    plt.show()
Exemplo n.º 5
0
def determineLocation(Graph, nodeID):
    '''
  Given a nodeID, determines if it belongs to the SCC, IN, or OUT
  components of the Graph using heuristics and assuming the Graph
  has a BowTie structure.

  Assumptions based on Experiments:
      IN nodes have < 1e-4% backwards trees, > 15% forward.
      OUT nodes have < 1e-4% forward trees, > 15% forward.
      SCC nodes have > 15% in both directions.
      All else is considered "AMBIGUOUS"

  returns: Location
  '''
    forwardTree = snap.GetBfsTree(Graph, nodeID, True, False)
    backwardTree = snap.GetBfsTree(Graph, nodeID, False, True)
    N = float(Graph.GetNodes())
    forward_p = forwardTree.GetNodes() / N
    backward_p = backwardTree.GetNodes() / N
    if forward_p > 0.15 and backward_p < 1e-4:
        return Location.IN
    if forward_p < 1e-4 and backward_p > 0.15:
        return Location.OUT
    if forward_p > 0.15 and backward_p > 0.15:
        return Location.SCC
    return Location.AMBIGUOUS
Exemplo n.º 6
0
def q1_3_grpah(Graph):
    n_nodes = Graph.GetNodes()
    MxWcc = snap.GetMxWcc(Graph)
    MxScc = snap.GetMxScc(Graph)
    n_MxWcc = MxWcc.GetNodes()
    n_MxScc = MxScc.GetNodes()
    print(" TOTAL          : ", n_nodes)
    print(" DISCONNECTED   : ", n_nodes - n_MxWcc)
    print(" SCC            : ", n_MxScc)

    SCC_nodes = []
    for NI in MxScc.Nodes():
        SCC_nodes.append(NI.GetId())

    num_test = 100
    random_sampled_scc = random.sample(SCC_nodes, num_test)

    num_out = []
    num_in = []
    for i in range(0, num_test):
        NodeId = random_sampled_scc[i]
        BfsTreeOut = snap.GetBfsTree(Graph, NodeId, True, False)
        BfsTreeIn = snap.GetBfsTree(Graph, NodeId, False, True)
        num_out.append(BfsTreeOut.GetNodes())  # roughly SCC + OUT
        num_in.append(BfsTreeIn.GetNodes())  # roughly SCC + IN
    num_out.sort()
    num_in.sort()

    print(" OUT            : ", num_out[-1] - n_MxScc)
    print(" IN             : ", num_in[-1] - n_MxScc)

    num_tendrils = n_MxWcc - n_MxScc - (num_out[-1] - n_MxScc) - (num_in[-1] -
                                                                  n_MxScc)
    print(" TENDRILS+TUBES : ", num_tendrils)
Exemplo n.º 7
0
def SizeOfBowtieRegions(Graph, sccNodeID):
    '''
    Given a Graph with a BowTie structure as described in
    http://snap.stanford.edu/class/cs224w-readings/broder00bowtie.pdf
    and an sccNodeID of a node known to belong to the central SCC,
    determines the size of each component.
    
    returns: tuple of sizes (SCC, IN, OUT, TENDRILS, DISCONNECTED)
    '''
    totalNodes = Graph.GetNodes()
    wcc = snap.GetMxWcc(Graph)
    assert wcc.IsNode(sccNodeID)
    wccNodes = wcc.GetNodes()
    disconnectedNodes = totalNodes - wccNodes

    scc = snap.GetMxScc(Graph)
    # Sanity check the input.
    assert scc.IsNode(sccNodeID)
    sccNodes = scc.GetNodes()

    sccAndOutNodes = snap.GetBfsTree(Graph, sccNodeID, True, False).GetNodes()
    sccAndInNodes = snap.GetBfsTree(Graph, sccNodeID, False, True).GetNodes()

    inNodes = sccAndInNodes - sccNodes
    outNodes = sccAndOutNodes - sccNodes
    tendrilNodes = wccNodes - (inNodes + outNodes + sccNodes)

    nodes = (sccNodes, inNodes, outNodes, tendrilNodes, disconnectedNodes)
    assert sum(nodes) == Graph.GetNodes()
    return nodes
Exemplo n.º 8
0
def q1_1():
    '''
    You will have to run the inward and outward BFS trees for the 
    respective nodes and reason about whether they are in SCC, IN or OUT.
    You may find the SNAP function GetBfsTree() to be useful here.
    '''

    ##########################################################################
    #TODO: Run outward and inward BFS trees from node 2018, compare sizes
    #and comment on where node 2018 lies.
    G = load_graph("email")
    #Your code here:
    email_in = snap.GetBfsTree(G, 2018, False, True)
    email_out = snap.GetBfsTree(G, 2018, True, False)
    print("Total nodes of email: ", G.GetNodes())
    print("IN of 2018 in email: ", email_in.GetNodes())
    print("OUT of 2018 in email: ", email_out.GetNodes())
    ##########################################################################

    ##########################################################################
    #TODO: Run outward and inward BFS trees from node 224, compare sizes
    #and comment on where node 224 lies.
    G = load_graph("epinions")
    #Your code here:
    ep_in = snap.GetBfsTree(G, 224, False, True)
    ep_out = snap.GetBfsTree(G, 224, True, False)
    print("Total nodes of epinions: ", G.GetNodes())
    print("IN of 224 in epinions: ", ep_in.GetNodes())
    print("OUT of 224 in epinions: ", ep_out.GetNodes())
    ##########################################################################

    print('1.1: Done!\n')
Exemplo n.º 9
0
def plot_reachablity(g, name=""):
    tot_nodes = g.GetNodes()
    sampled_nodes = np.random.randint(tot_nodes, size=100)
    nodes_in_cnt = []
    nodes_out_cnt = []

    for ni in sampled_nodes:
        bfs_in = snap.GetBfsTree(g, int(ni), False, True)
        nodes_in_cnt.append(bfs_in.GetNodes())
        bfs_out = snap.GetBfsTree(g, int(ni), True, False)
        nodes_out_cnt.append(bfs_out.GetNodes())

    plt.title("Reachability Using Links " + name)
    plt.xlabel("Frac. of Starting Nodes")
    plt.ylabel("Number of Nodes Reached")

    X = np.array(range(1, 101)) / 100
    Y = np.array(sorted(nodes_in_cnt))
    plt.plot(X, Y, label="in link")
    for (i, (x, y)) in enumerate(zip(X, Y)):
        if i + 1 < 100 and Y[i + 1] - y > 1000:
            plt.text(x + 0.03, y + 10, x)
    Y = np.array(sorted(nodes_out_cnt))
    plt.plot(X, Y, label="out link")
    for (i, (x, y)) in enumerate(zip(X, Y)):
        if i + 1 < 100 and Y[i + 1] - y > 1000:
            plt.text(x + 0.03, y + 10, x)
    plt.legend()
    plt.show()
def cumul_BFS(graph, name, N=1000):
    """Get n random nodes and find the number of nodes in their inward and outward BFS trees.
    Plot the cumulative number of nodes reached in the BFS runs"""

    X_in, X_out = [], []
    for i in range(N):
        n = graph.GetRndNId()

        g_out = snap.GetBfsTree(graph, n, True, False)
        g_in = snap.GetBfsTree(graph, n, False, True)

        X_in.append(g_in.GetNodes())
        X_out.append(g_out.GetNodes())

    X_in, X_out = sorted(X_in), sorted(X_out)

    x = np.linspace(0.0, 1.0, N)
    plt.subplot(1, 2, 1)
    plt.plot(x, X_in)
    plt.title('{}, using inlinks'.format(name))

    plt.subplot(1, 2, 2)
    plt.plot(x, X_out)
    plt.title('{}, using outlinks'.format(name))

    plt.savefig("../results/reachability_bfs/reachability_{}".format(name),
                bbox_inches="tight")
Exemplo n.º 11
0
def getbfs(G1,nodekeys,search=0,startnode=0,FollowOut=True, FollowIn=False):
    bfstrees=[]
    if search == 0:
        bfstrees.append(snap.GetBfsTree(G1,startnode,FollowOut,FollowIn))
    if search == 1:
        for key in nodekeys:
            bfstrees.append(snap.GetBfsTree(G1,key,FollowOut,FollowIn))
    return bfstrees
Exemplo n.º 12
0
def BfsDfsTest(NNodes, NEdges):
  Graph = snap.GenRndGnm(NNodes, NEdges, 1)
  
  snap.GetBfsTree(Graph, False, False, 1)
  
  G2 = snap.GetBfsTree(Graph, False, False, 1)
  
  return G2
Exemplo n.º 13
0
def Judge(Graph, num):
    G1 = snap.GetBfsTree(Graph, num, True, False)
    G2 = snap.GetBfsTree(Graph, num, False, True)
    forward = G1.GetNodes()
    backward = G2.GetNodes()
    if (forward < backward
            and forward * 10 > backward) or (forward > backward
                                             and forward < backward * 10):
        return True
Exemplo n.º 14
0
def GetForwardBackwardProp(Graph, nodeID):
    '''
    Returns the proportion of nodes visited during a forward/backward
    bfs starting at nodeID
    '''

    forwardTree = snap.GetBfsTree(Graph, nodeID, True, False)
    backwardTree = snap.GetBfsTree(Graph, nodeID, False, True)
    return (float(forwardTree.GetNodes()) / Graph.GetNodes(),
            float(backwardTree.GetNodes()) / Graph.GetNodes())
def findComponent(G, NId):
    BfsTreeOut = snap.GetBfsTree(G, NId, True, False)
    BfsTreeIn = snap.GetBfsTree(G, NId, False, True)
    #print (BfsTreeOut.GetNodes(), BfsTreeIn.GetNodes())

    if ((BfsTreeOut.GetNodes() < BfsTreeIn.GetNodes()) & (BfsTreeOut.GetNodes() == 1)):
        print (NId, " : OUT")
    elif ((BfsTreeOut.GetNodes() > BfsTreeIn.GetNodes()) & (BfsTreeIn.GetNodes() == 1)):
        print (NId, " : IN")
    else:
        print (NId, " : SCC")
Exemplo n.º 16
0
def randomStartTraversalDistribution(Graph):
    '''
    returns: X, Y where X[i], Y[i] are the number of reached nodes during
    the forward and backward passes respectively for the i-th randomly
    chosen node.
    '''
    X, Y = [], []
    for i in xrange(NUM_NODES):
        srcID = Graph.GetRndNId(Rnd)
        X.append(snap.GetBfsTree(Graph, srcID, True, False).GetNodes())
        Y.append(snap.GetBfsTree(Graph, srcID, False, True).GetNodes())
    return X, Y
Exemplo n.º 17
0
def Judge(Graph, num):
    G1 = snap.GetBfsTree(Graph, num, True, False)
    G2 = snap.GetBfsTree(Graph, num, False, True)
    forward = G1.GetNodes()
    backward = G2.GetNodes()
    if (forward < backward
            and forward * 5 > backward) or (forward > backward
                                            and forward < backward * 5):
        print "Node %d belongs to SCC " % (num)
    elif forward > backward:
        print "Node %d belongs to IN " % (num)
    elif forward < backward:
        print "Node %d belongs to OUT " % (num)
    def getOverlapSet(self, nodeDegH):
        nodeIdV = snap.TIntV()
        dnodeIdV = snap.TIntV()
        nodeDegH.GetKeyV(nodeIdV)
        randNodeIdV=snap.TIntV()
        # random choice
        if self.overlap_choice == 1:
            randNodeIdV=np.random.choice(nodeIdV, self.overlap_size, replace=False)
        # random choice over the higher degree nodes
        elif self.overlap_choice == 2:
            nodeDegH_walker = nodeDegH.BegI()
            counter=0
            while not nodeDegH_walker.IsEnd():
                if (counter>self.overlap_size*2):
                    break;
                dnodeIdV.Add(nodeDegH_walker.GetKey())
                nodeDegH_walker.Next()
            randNodeIdV = np.random.choice(dnodeIdV, self.overlap_size, replace=False)
        # bsf tree nodes from the highest degree node
        elif self.overlap_choice == 3:
            startNodeId=nodeDegH.BegI().GetKey()
            BfsTree = snap.GetBfsTree(self.G, startNodeId, True, False)
            for EI in BfsTree.Edges():
                sourceNodeId=EI.GetSrcNId()
                if(sourceNodeId not in randNodeIdV):
                    randNodeIdV.Add(sourceNodeId)
                destNodeId = EI.GetDstNId()
                if (destNodeId not in randNodeIdV):
                    randNodeIdV.Add(destNodeId)
                if(randNodeIdV.Len()==self.overlap_size):
                    break;
        # bsf tree nodes from random node
        elif self.overlap_choice == 4:
            startNodeId = np.random.choice(nodeIdV, 1, replace=False)
            #print startNodeId
            BfsTree = snap.GetBfsTree(self.G, startNodeId[0], True, False)
            for EI in BfsTree.Edges():
                sourceNodeId = EI.GetSrcNId()
                #print sourceNodeId
                if (sourceNodeId not in randNodeIdV):
                    randNodeIdV.Add(sourceNodeId)
                destNodeId = EI.GetDstNId()
                #print destNodeId
                if (destNodeId not in randNodeIdV):
                    randNodeIdV.Add(destNodeId)
                if (randNodeIdV.Len() == self.overlap_size):
                    break;



        return randNodeIdV
Exemplo n.º 19
0
def PlotRandomBFS(Graph, num_test=100):
    Rnd = snap.TRnd(42)
    Rnd.Randomize()
    num_out = []
    num_in = []
    for i in range(0, num_test):
        NodeId = Graph.GetRndNId(Rnd)
        BfsTreeOut = snap.GetBfsTree(Graph, NodeId, True, False)
        BfsTreeIn = snap.GetBfsTree(Graph, NodeId, False, True)
        num_out.append(np.log(BfsTreeOut.GetNodes()))
        num_in.append(np.log(BfsTreeIn.GetNodes()))
    num_out.sort()
    num_in.sort()
    return num_out, num_in
Exemplo n.º 20
0
def cal_con_prob(g, scc_set, In, Out):
    sample_points = []
    probs = []
    samples = np.random.randint(g.GetNodes(), size=g.GetNodes())
    connected_cnt = 0
    for i in range(1, int(g.GetNodes() * 0.03)):
        sample_points.append(i / g.GetNodes())
        probs.append(connected_cnt / i)

        # if len(probs) > 100 and abs(probs[-1] - probs[i - 100]) < 1e-6:
        #     break

        u = samples[i << 1]
        v = samples[i << 1 | 1]
        if (u in scc_set or u in In) and (v in scc_set or v in Out):
            connected_cnt += 1
            continue
        if (u in Out and (v in In or v in scc_set)) or (v in In and (u in Out or u in scc_set)):
            continue
        try:
            g_out = snap.GetBfsTree(g, int(u), True, False)
        except Exception as e:
            print(e)
            i -= 1
            continue
        for nii in g_out.Nodes():
            if nii.GetId() == v:
                connected_cnt += 1
                break

    return sample_points, probs
Exemplo n.º 21
0
def q2_2_aux(name):
    G = load_graph(name)

    vectOut = []
    vectIn = []
    for i in range(100):
        randInt = G.GetRndNId()

        OutTreeNodesN = snap.GetBfsTree(G, randInt, True, False).GetNodes()
        InTreeEpNodesN = snap.GetBfsTree(G, randInt, False, True).GetNodes()

        vectIn.append(InTreeEpNodesN)
        vectOut.append(OutTreeNodesN)
    vectOut.sort()
    vectIn.sort()
    return vectIn,vectOut
Exemplo n.º 22
0
def getInletRadiusPlot(G, name, filename):
    """
    Run an outward BFS from each inlet and plot the size of the outset vs node "Radius"
    """

    inlets = getInletIds(G)
    BFS_out, x_cords = [], []
    BFS_mean_rad = {}
    if name == 'Healthy':
        radii = util_data.loadNodeAttr(filename, small=True)
    else:
        radii = util_data.loadNodeAttr(filename)

    for inlet in inlets:
        BFS_out.append((snap.GetBfsTree(G, inlet, True,
                                        False).GetNodes(), radii[inlet]))

    for elem in BFS_out:
        if elem[0] in BFS_mean_rad:
            BFS_mean_rad[elem[0]].append(elem[1])
        else:
            BFS_mean_rad[elem[0]] = [elem[1]]

    for elem in BFS_mean_rad:
        BFS_mean_rad[elem] = sum(BFS_mean_rad[elem]) / len(BFS_mean_rad[elem])

    # y_cords, x_cords = zip(*BFS_out)
    print "Pearson Coeff for {}: {}".format(
        name, pearsonr(BFS_mean_rad.values(), BFS_mean_rad.keys()))
    plt.plot(BFS_mean_rad.values(), BFS_mean_rad.keys(), 'o')
    plt.xlabel('Mean Radius $(\mu m)$', usetex=True)
    plt.ylabel('Size of Outset')
    plt.savefig("Inlet_BFS_Radius_" + name + ".pdf")
    plt.clf()
Exemplo n.º 23
0
def Cal(Graph, num):
    Scc = 0
    G1 = snap.GetBfsTree(Graph, num, True, False)
    G2 = snap.GetBfsTree(Graph, num, False, True)
    G3 = snap.GetBfsTree(Graph, num, True, True)
    IN = G2.GetNodes()
    OUT = G1.GetNodes()
    print "Nodes DISCONNECTED: %d" % (Graph.GetNodes() - G3.GetNodes())
    for N1 in Graph.Nodes():
        if G1.IsNode(N1.GetId()) == True and G2.IsNode(N1.GetId()) == True:
            Scc += 1
    IN = IN - Scc
    OUT = OUT - Scc
    print "Nodes SCC: %d" % (Scc)
    print "Nodes IN: %d" % (IN)
    print "Nodes OUT: %d" % (OUT)
    print "Nodes TENDRILS: %d" % (G3.GetNodes() - IN - OUT - Scc)
Exemplo n.º 24
0
def q2_3_util(dataset_name):
        # G = load_graph("email")
    G = load_graph(dataset_name)
    MxWcc = snap.GetMxWcc(G)
    total_size = G.GetNodes()
    wcc_size = MxWcc.GetNodes()
    disconnected_size = total_size - wcc_size
    print 'Total size: ', total_size
    print 'WCC size: ', wcc_size
    print 'DISCONNECTED: ', disconnected_size
    Rnd = snap.TRnd(42)
    Rnd.Randomize()
    MxScc = snap.GetMxScc(G)
    scc_size = MxScc.GetNodes()
    number_of_trials = 1
    scc_plus_out = 0
    scc_plus_in = 0
    out_size = 0
    in_size = 0
    tendrils_plus_tubes = 0
    for i in xrange(number_of_trials):
        NId = MxScc.GetRndNId(Rnd)
        # print 'Random node id', NId
        outward_set = set()
        BfsTree = snap.GetBfsTree(G, NId, True, False)
        for EI in BfsTree.Edges():
            outward_set.add(EI.GetDstNId())
        scc_plus_out = max(scc_plus_out, len(outward_set))
        out_size = max( out_size, scc_plus_out - scc_size)
        #
        inward_set = set()
        BfsTree = snap.GetBfsTree(G, NId, False, True)
        for EI in BfsTree.Edges():
            inward_set.add(EI.GetDstNId())
        scc_plus_in = max(scc_plus_in, len(inward_set))
        in_size = max(in_size, scc_plus_in - scc_size)
        tendrils_plus_tubes = max(tendrils_plus_tubes, wcc_size - in_size - out_size)

    print 'IN: ', in_size
    print 'scc_size', scc_size
    print 'scc + out: ', scc_plus_out
    print 'OUT: ', out_size
    print 'scc + in: ', scc_plus_in
    print 'TENDRILS + TUBES', tendrils_plus_tubes
    print '------------------'
Exemplo n.º 25
0
def analyze_graph(G):
    WCC = snap.GetMxWcc(G)
    SCC = snap.GetMxScc(G)

    id = SCC.GetRndNId()
    out_tree = snap.GetBfsTree(G, id, True, False)
    in_tree = snap.GetBfsTree(G, id, False, True)

    G_size = G.GetNodes()
    SCC_size = SCC.GetNodes()
    WCC_size = WCC.GetNodes()
    DISCONNECTED_size = G_size - WCC_size
    in_size = in_tree.GetNodes() - SCC_size
    out_size = out_tree.GetNodes() - SCC_size
    Tendril_size = G_size - SCC_size - DISCONNECTED_size - in_size - out_size

    print 'Total Graph Size: %d' % G_size
    print 'SCC Size: %d' % SCC_size
    print 'WCC Size: %d' % WCC_size
    print 'IN Size: %d' % in_size
    print 'OUT Size: %d' % out_size
    print 'DISCONNECTED Size: %d' % DISCONNECTED_size
    print 'Tendril tube size (remaining): %d' % Tendril_size
    print()
Exemplo n.º 26
0
    def per_graph(graph, name):
        mxWcc = snap.GetMxWcc(graph)
        mxScc = snap.GetMxScc(graph)
        print ''
        print 'Size analysis on {}'.format(name)
        print 'Disconnected size = {}'.format(graph.GetNodes() - mxWcc.GetNodes())
        print 'SCC size = {}'.format(mxScc.GetNodes())
        
        trials = 200
        avg_reached_out = 0
        avg_reached_in = 0
        for _ in range(trials):
            nodeId = mxScc.GetRndNId()
            avg_reached_out += snap.GetBfsTree(graph, nodeId, True, False).GetNodes()
            avg_reached_in += snap.GetBfsTree(graph, nodeId, False, True).GetNodes()

        scc_out = float(avg_reached_out) / trials
        scc_in = float(avg_reached_in) / trials

        out_sz = scc_out - mxScc.GetNodes()
        in_sz = scc_in - mxScc.GetNodes()
        print 'OUT size = {}'.format(out_sz)
        print 'IN size = {}'.format(in_sz)
        print 'Tendrils/Tubes size = {}'.format(mxWcc.GetNodes() - mxScc.GetNodes() - out_sz - in_sz)
Exemplo n.º 27
0
def get_random_subgraph_connected(G, banned_ids, subgraph_size = 300):
  root = random.choice(banned_ids)
  # do bfs from root, which is a banned subreddit
  bfs_G = snap.GetBfsTree(G, root, True, False)
  Vec_of_bfs_G_nodes = snap.TIntV()
  level = [root]
  # iteratively levels of BFS tree
  while (len(level) > 0):
    curr = level.pop(0)
    if (curr not in Vec_of_bfs_G_nodes):
      Vec_of_bfs_G_nodes.Add(curr)
    if (Vec_of_bfs_G_nodes.Len() == subgraph_size):
      break
    for neigh in G.GetNI(curr).GetOutEdges():
      level.append(neigh)
  return snap.GetSubGraph(G, Vec_of_bfs_G_nodes)
Exemplo n.º 28
0
def getInletHist(G, name):
    """
    Run an outward BFS from each inlet and plot the size of the outset
    """

    inlets = getInletIds(G)
    BFS_out = []
    for inlet in inlets:
        BFS_out.append(snap.GetBfsTree(G, inlet, True, False).GetNodes())
    # print len(BFS_out)
    # print sorted(BFS_out, key=lambda args: args[1])
    plt.hist(BFS_out)
    plt.xlabel('Size of Outset')
    plt.ylabel('Number of Nodes')
    plt.savefig("Inlet_BFS_" + name + "_Hist.pdf")
    plt.clf()
Exemplo n.º 29
0
def getInletScatter(G, name):
    """
    Run an outward BFS from each inlet and plot the size of the outset
    """

    inlets = getInletIds(G)
    BFS_out = []
    for inlet in inlets:
        BFS_out.append(snap.GetBfsTree(G, inlet, True, False).GetNodes())
    # print len(BFS_out)
    # print sorted(BFS_out, key=lambda args: args[1])
    plt.scatter(*zip(*collections.Counter(BFS_out).items()))
    plt.xscale('log')
    plt.yscale('log')
    plt.xlabel('Size of Outset')
    plt.ylabel('Number of Nodes')
    plt.savefig("Inlet_BFS_" + name + "_Scatter.pdf")
    plt.clf()
Exemplo n.º 30
0
def Plot(Graph, bool1, bool2):
    nodes = set()
    listy = []
    listx = []
    while len(nodes) < 100:
        length = len(nodes)
        node = Graph.GetRndNId()
        nodes.add(node)
        if len(nodes) - length == 0:
            continue
        Traverse = snap.GetBfsTree(Graph, node, bool1, bool2)
        listy.append(Traverse.GetNodes())
        listx.append(len(nodes) / 100)
    listy.sort()
    plt.figure()
    plt.plot(listx, listy, marker='o', markersize=3, color="red")
    plt.ylim((0.1, Graph.GetNodes()))
    plt.xlim((0, 1.1))
    plt.yscale('log')
    plt.show()