コード例 #1
0
 def f():
     snap = self.snap
     ret = []
     ComponentDist = snap.TIntPr64V()
     snap.GetSccSzCnt(self.graph, ComponentDist)
     for comp in ComponentDist:
         ret.append((comp.GetVal1(), comp.GetVal2()))
     return ret
コード例 #2
0
def _get_SCC(Graph, H, output_path):
    ComponentDist = snap.TIntPrV()
    snap.GetSccSzCnt(Graph, ComponentDist)
    dataset = list()
    for comp in ComponentDist:
        scc = dict()
        scc['size'] = comp.GetVal1()
        scc['freq'] = comp.GetVal2()
        dataset.append(scc)
    dataset = pd.DataFrame(dataset)
    dataset = dataset[['size', 'freq']]
    dataset.sort('size', ascending=0, inplace=True)
    dataset.to_csv(output_path, index=False, encoding='utf-8')
コード例 #3
0
def fraction_of_nodes_in_largest_connected_component(G, GName):

    ComponentDist = snap.TIntPrV()
    snap.GetSccSzCnt(G, ComponentDist)
    highest_nodes_in_connected_comp = 0

    for comp in ComponentDist:
        highest_nodes_in_connected_comp = max(comp.GetVal1(),
                                              highest_nodes_in_connected_comp)

    print "Fraction of nodes in largest connected component in {0}: {1}".format(
        GName[:-10],
        (float(str(highest_nodes_in_connected_comp)) / G.GetNodes()))
コード例 #4
0
def wikiVotingNetwork():

    Component = snap.TIntPrV()
    #Loding the graph
    Wiki = snap.LoadEdgeList(snap.PNGraph, "Wiki-Vote.txt", 0, 1)
    #Printing Number of Nodes in the Graph
    print "Number of Nodes: ", Wiki.GetNodes()
    #Printing Number of Edges in the Graph
    print "Number of Edges: ", Wiki.GetEdges()
    #Printing Number of Directed Edges in the Graph
    print "Number of Directed Edges: ", snap.CntUniqDirEdges(Wiki)
    #Printing Number of Un-Directed Edges in the Graph
    print "Number of Undirected Edges: ", snap.CntUniqUndirEdges(Wiki)
    #Printing Number of Directed Edges in the Graph
    print "Number of Self-Edges: ", snap.CntSelfEdges(Wiki)
    #Printing Number of Zero InDeg Nodes in the Graph
    print "Number of Zero InDeg Nodes: ", snap.CntInDegNodes(Wiki, 0)
    #Printing Number of Zero OutDeg Nodes in the Graph
    print "Number of Zero OutDeg Nodes: ", snap.CntOutDegNodes(Wiki, 0)
    #Printing Node ID with maximum degree in the Graph
    print "Node ID with maximum degree: ", snap.GetMxDegNId(Wiki)

    snap.GetSccSzCnt(Wiki, Component)

    for comp in Component:
        #printing number of strongly connected components with size
        print "Size: %d - Number of Strongly Connected Components: %d" % (
            comp.GetVal1(), comp.GetVal2())
    #printing size of largest connected components
    print "Size of largest connected component: ", snap.GetMxSccSz(Wiki)

    snap.GetWccSzCnt(Wiki, Component)

    for comp in Component:
        #printing number of weekly connected components with size
        print "Size: %d - Number of Weekly Connected Component Wikipedia: %d" % (
            comp.GetVal1(), comp.GetVal2())

    #printing size of weekly connected components
    print "Size of Weakly connected component: ", snap.GetMxWccSz(Wiki)
    #plotting out-degree distribution
    snap.PlotOutDegDistr(Wiki, "wiki-analysis",
                         "Directed graph - Out-Degree Distribution")
コード例 #5
0
def computeNumberOfStronglyConnectedComponentsPerSize(graph, outFile, fill):
    logger.info("Computing Strongly Connected Components Per Component Size")
    fw_cc = open(outFile, 'w')
    CntV = snap.TIntPrV()
    fw_cc.write('Size of SCC\tNumber of SCCs\n')
    snap.GetSccSzCnt(graph, CntV)
    last = 0
    array = []
    for p in CntV:
        if (fill & p.GetVal1() - last > 1):
            for i in range(last + 1, p.GetVal1()):
                fw_cc.write(str(i) + '\t' + str(0) + '\n')
        fw_cc.write(str(p.GetVal1()) + '\t' + str(p.GetVal2()) + '\n')
        array.append({"x": p.GetVal1(), "y": p.GetVal2()})
        last = p.GetVal1()

    with open(outFile + '.json', 'w') as file:
        json.dump(array, file)

    logger.info("Number of Strongly Connected Components Per Component Size!")
    logger.info(
        "Number of Strongly Connected Components Per Component Size Exported to "
        + outFile)
コード例 #6
0
import snap

G = snap.LoadEdgeList(snap.PNGraph, "Wiki-Vote.txt", 0, 1)
snap.PrintInfo(G, "votes Stats", "votes-info.txt", False)

# Node ID with maximum degree
NId1 = snap.GetMxDegNId(G)
print("Node ID with Maximum-Degree: %d" % NId1)

# Number of Strongly connected components
ComponentDist = snap.TIntPrV()
snap.GetSccSzCnt(G, ComponentDist)
for comp in ComponentDist:
    print("Size: %d - Number of Components: %d" %
          (comp.GetVal1(), comp.GetVal2()))

# Size of largest strongly connected component
print("Strongly Connected Component - Maximum size:", snap.GetMxSccSz(G))

# Number of Weakly Connected Components
CompDist = snap.TIntPrV()
snap.GetWccSzCnt(G, CompDist)
for comp in CompDist:
    print("Size: %d - Number of Components: %d" %
          (comp.GetVal1(), comp.GetVal2()))

# Size of largest weakly connected component
print("Weakly Connected Component - Maximum size:", snap.GetMxWccSz(G))

# Plot of Outdegree Distribution
snap.PlotOutDegDistr(G, "Wiki Votes", "Wiki-Votes Out Degree")
コード例 #7
0
# [4] Components of the network
SCC = snap.GetMxScc(G)
print("Fraction of nodes in largest connected component: {}".format(
    round(SCC.GetNodes() / G.GetNodes(), 4)))

Edge_Bridge = snap.TIntPrV()
snap.GetEdgeBridges(G, Edge_Bridge)
print("Number of edge bridges: {}".format(len(Edge_Bridge)))

ArticulationPoint = snap.TIntV()
snap.GetArtPoints(G, ArticulationPoint)
print("Number of articulation points: {}".format(len(ArticulationPoint)))

CComp = snap.TIntPrV()
snap.GetSccSzCnt(G, CComp)
connected_component = {}
for comp in CComp:
    connected_component[comp.GetVal1()] = comp.GetVal2()

# Plot Degree Distribution
plot_filename = 'connected comp_' + graph_filename[:-6] + '.png'
plot_filedir = os.path.join(plotpath, plot_filename)
plt.figure()
plt.scatter(list(connected_component.keys()),
            list(connected_component.values()),
            s=15)
plt.xlabel("Size of Connected Components")
plt.ylabel("Number of components")
plt.title("Connected Component Distribution ({})".format(graph_filename[:-6]))
plt.savefig(plot_filedir)
コード例 #8
0
    repliesgraph = snap.LoadEdgeList(snap.PNGraph, filename, 0, 1)
    snap.PrintInfo(repliesgraph, "Twitter replies network")
    print

    #reciprocity
    num_dir_edges = snap.CntUniqDirEdges(repliesgraph)
    print "{0:.2f}% of directed edges are reciprocal".format(
        snap.CntUniqBiDirEdges(repliesgraph) * 2 * 100 / num_dir_edges)

    #clustering coefficient
    print "The clustering coefficient is {0:.2f}%".format(
        snap.GetClustCf(repliesgraph) * 100)

    #strongly and weakly connected components
    CntV = snap.TIntPrV()
    snap.GetSccSzCnt(repliesgraph, CntV)
    num_cc = 0
    for p in CntV:
        print "{0} strongly connected component(s) of size {1}".format(
            p.GetVal2(), p.GetVal1())
        num_cc += p.GetVal2()
    print num_cc, "total strongly connected components"
    print

    snap.GetWccSzCnt(repliesgraph, CntV)
    num_cc = 0
    for p in CntV:
        print "{0} weakly connected component(s) of size {1}".format(
            p.GetVal2(), p.GetVal1())
        num_cc += p.GetVal2()
    print num_cc, "total weakly connected components"
コード例 #9
0
def GraphComponentDistributionDict(G):
    ''' return a dict of cardinality : number of strongly-connected components 
    with that size in the graph G.'''
    ComponentDist = snap.TIntPrV()
    snap.GetSccSzCnt(G, ComponentDist)
    return {comp.GetVal1(): comp.GetVal2() for comp in ComponentDist}
コード例 #10
0
def main():

    Component = snap.TIntPrV()
    #loading the real world graph
    realWorld = snap.LoadEdgeList(snap.PUNGraph, "CA-HepTh.txt", 0, 1)
    #deleting the self-edges from the graph
    snap.DelSelfEdges(realWorld)
    #calling the function
    wikiVotingNetwork()
    #Taking number of nodes in a graph from real world network
    n = realWorld.GetNodes()
    #Generating an Undirected Graph
    G = snap.TUNGraph.New()
    #Taking number of edges in a graph from user
    e = int(raw_input('Enter the number of Random Edges : '))

    p = float(
        raw_input('Enter the Probability of Edges between Nodes from 0-1  : '))
    #Generating Number of Nodes
    for i in range(n):
        #Adding Nodes into the graph
        G.AddNode(i)
    #calling the function
    erdosRenyi(G, p)
    #Printing the Clustering
    print 'Erdos Renyi Clustering Co-efficient: ', clustCoefficient(G)

    diam = snap.GetBfsFullDiam(G, 9877, False)
    #printing the diameter
    print 'Erdos Renyi Diameter: ', diam
    #plotting the graph
    snap.PlotOutDegDistr(G, "Erdos-Renyi",
                         "Un-Directed graph - Out-Degree Distribution")

    snap.GetSccSzCnt(G, Component)

    for comp in Component:
        #printing number of strongly connected components with size
        print "Size: %d - Number of Connected Component in Erdos-Renyi: %d" % (
            comp.GetVal1(), comp.GetVal2())
    #printing fraction of nodes and edges
    print "Fraction of Nodes and Edges in Erdos Renyi: ", snap.GetMxSccSz(G)
    #Drawing a Erdos Renyi Graph
    snap.DrawGViz(G, snap.gvlDot, "erdosRenyi1.png", "Erdos Renyi")
    #calling the function
    smallWorldRandomNetwork(G, e)
    #printing the clustering coefficient
    print 'Small World Random Network Clustering Co-efficient: ', clustCoefficient(
        G)

    diam = snap.GetBfsFullDiam(G, 9877, False)
    #printing the diameter
    print 'Small World Random Network Diameter: ', diam

    snap.GetSccSzCnt(G, Component)

    for comp in Component:

        #printing number of strongly connected components with size

        print "Size: %d - Number of Connected Component in Small World: %d" % (
            comp.GetVal1(), comp.GetVal2())
    #fraction of nodes and edges in small world
    print "Fraction of Nodes and Edges in Small World: ", snap.GetMxSccSz(G)
    #plotting the graph
    snap.PlotOutDegDistr(G, "Small-World",
                         "Un-Directed graph - Out-Degree Distribution")
    #drawinf the graph
    snap.DrawGViz(G, snap.gvlDot, "smallWorld1.png",
                  "Small World Random Network")
    #calculating the clustering co-efficient
    print 'Real World Random Network Clustering Co-efficient: ', clustCoefficient(
        realWorld)

    diam = snap.GetBfsFullDiam(G, 9877, False)

    print 'Real World Random Network Diameter: ', diam

    snap.GetSccSzCnt(realWorld, Component)

    for comp in Component:
        #printing number of strongly connected components with size

        print "Size: %d - Number of Weekly Connected Component in Real World: %d" % (
            comp.GetVal1(), comp.GetVal2())
    #printing fraction of nodes and edges
    print "Fraction of Nodes and Edges in Small World: ", snap.GetMxSccSz(
        realWorld)
    #plotting the real world network graph
    snap.PlotOutDegDistr(realWorld, "real-World",
                         "Un-Directed graph - Out-Degree Distribution")
    #Drawing Real WOrld Graph
    snap.DrawGViz(realWorld, snap.gvlDot, "realWorld.png",
                  "Real World Random Network")
コード例 #11
0
def get_graph_overview(G, Gd=None):
    '''
	G here is an undirected graph
	'''

    # degree distribution
    CntV = snap.TIntPrV()
    snap.GetOutDegCnt(G, CntV)
    deg_x, deg_y = [], []
    max_deg = 0
    for item in CntV:
        max_deg = max(max_deg, item.GetVal1())
        deg_x.append(item.GetVal1())
        deg_y.append(item.GetVal2())
        # print item.GetVal1(), item.GetVal2()
    print 'max_deg = ', max_deg
    deg_cnt = np.zeros(max_deg + 1)
    for item in CntV:
        deg_cnt[item.GetVal1()] = item.GetVal2()
    print deg_cnt
    # plt.loglog(deg_x, deg_y)
    # plt.xlabel('Degree of nodes')
    # plt.ylabel('Number of nodes')
    # plt.savefig('Giu_deg_dist.png')
    # plt.clf()

    # clustering coefficient distribution
    cf = snap.GetClustCf(G)
    print 'average cf =', cf
    NIdCCfH = snap.TIntFltH()
    snap.GetNodeClustCf(G, NIdCCfH)
    ccf_sum = np.zeros(max_deg + 1)
    for item in NIdCCfH:
        ccf_sum[G.GetNI(item).GetDeg()] += NIdCCfH[item]
        # print item, NIdCCfH[item]
    ccf_x, ccf_y = [], []
    for i in range(max_deg + 1):
        if deg_cnt[i] != 0:
            ccf_sum[i] /= deg_cnt[i]
            ccf_x.append(i)
            ccf_y.append(ccf_sum[i])
    print ccf_y
    # plt.loglog(ccf_x, ccf_y)
    # plt.xlabel('Degree of nodes')
    # plt.ylabel('Average clustering coefficient of nodes with the degree')
    # plt.savefig('Giu_ccf_dist.png')
    # plt.clf()
    # snap.PlotClustCf(G, 'investor_network', 'Distribution of clustering coefficients')

    # diameter and shortest path distribution
    diam = snap.GetBfsFullDiam(G, 100)
    print diam
    # snap.PlotShortPathDistr(G, 'investor_network', 'Distribution of shortest path length')
    # rewired_diams = []
    # for i in range(100):
    # 	print 'rewire: ', i
    # 	G_config = rewire_undirected_graph(G)
    # 	rewired_diams.append(snap.GetBfsFullDiam(G_config, 400))
    # print rewired_diams
    # print 'null model diam mean: ', np.mean(rewired_diams)
    # print 'null model diam std: ', np.std(rewired_diams)

    # wcc and scc size distribution
    WccSzCnt = snap.TIntPrV()
    snap.GetWccSzCnt(G, WccSzCnt)
    print 'Distribution of wcc:'
    for item in WccSzCnt:
        print item.GetVal1(), item.GetVal2()

    if Gd != None:
        print 'Distribution of scc:'
        ComponentDist = snap.TIntPrV()
        snap.GetSccSzCnt(Gd, ComponentDist)
        for item in ComponentDist:
            print item.GetVal1(), item.GetVal2()
コード例 #12
0
import snap
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":
    print("Loading graph...")
    FIn = snap.TFIn("results/snap-follow.graph")
    graph = snap.TNGraph.Load(FIn)

    print("Finding strongly connected components...")
    dist = snap.TIntPrV()
    snap.GetSccSzCnt(graph, dist)
    sccs = [(comp.GetVal1(), comp.GetVal2()) for comp in dist]

    print("Finding weakly connected components...")
    dist = snap.TIntPrV()
    snap.GetWccSzCnt(graph, dist)
    wccs = [(comp.GetVal1(), comp.GetVal2()) for comp in dist]

    print("Number of SCCs:", sum(scc[1] for scc in sccs))
    print("Number of WCCs:", sum(wcc[1] for wcc in wccs))

    plt.scatter([scc[0] for scc in sccs], [scc[1] for scc in sccs], s=4, label = 'WCC Distribution')
    plt.scatter([wcc[0] for wcc in wccs], [wcc[1] for wcc in wccs], s=4, label = 'SCC Distribution')

    plt.yscale('log')
    plt.xscale('log')

    plt.title('WCC / SCC Size Distribution')

    plt.xlabel('Component Size')
コード例 #13
0
    dic_path = "../datasets/chess/chess_ids.pkl"
    with open(dic_path, 'rb') as dic_id:
        mydict = pickle.load(dic_id)
        chess_graph = load.load_global("chess")

    graphs = [fencing_graph, tm_graph, tf_graph, chess_graph]
    names = ["Fencing Network", "Men's Tennis Network", "Women's Tennis Network", "Chess Network"]

    print 'names:', names
    print 'nodes:', [graph.GetNodes() for graph in graphs]
    print 'edges:', [graph.GetEdges() for graph in graphs]
    print 'mxscc:', [snap.GetMxScc(graph).GetNodes() for graph in graphs]
    print 'max degree:', [graph.GetNI(snap.GetMxDegNId(graph)).GetDeg() for graph in graphs]

    comp_dists = [snap.TIntPrV() for graph in graphs]
    scc_counts = [snap.GetSccSzCnt(graph, comp_dists[i]) for i, graph in enumerate(graphs)]
    print 'Strongly Connected Components'
    for i, comp_dist in enumerate(comp_dists):
        print names[i]
        for comp in comp_dist:
            print 'size:', comp.GetVal1(), 'count:', comp.GetVal2()

    p_ranks = [snap.TIntFltH() for graph in graphs]
    [snap.GetPageRank(graph, p_ranks[i]) for i, graph in enumerate(graphs)]
    pr_ys = [sorted([p_rank[x] for x in p_rank]) for p_rank in p_ranks]
    i_pr_ys = [[sum(pr_y[:i+1]) for i in range(len(pr_y))] for pr_y in pr_ys]
    pr_xs = [[float(i+1)/len(pr_y) for i in range(len(pr_y))] for pr_y in pr_ys]

    plt.figure()
    [plt.plot(pr_xs[i], i_pr_ys[i], '.', markersize=4) for i in range(len(graphs))]
    plt.title("Cumulative PageRank vs. Node Fraction")