def f():
     snap = self.snap
     ret = []
     ComponentDist = snap.TIntPr64V()
     snap.GetWccSzCnt(self.graph, ComponentDist)
     for comp in ComponentDist:
         ret.append((comp.GetVal1(), comp.GetVal2()))
     return ret
def biggest_connected_component_on_the_network():
    WccV = snap.TIntPrV()
    snap.GetWccSzCnt(G, WccV)
    con_comp = {}
    print("Connected components info.\n")
    print("# of connected component", WccV.Len())
    for comp in WccV:
        con_comp[comp.GetVal1()] = comp.GetVal2()
    print("Biggest connected component has size of:", max(con_comp.values()))
    snap.PrintInfo(G, "tweet Information", "tweet_stats_extended.txt", False)
def component_distribution(g):
    print 'executing component distribution --- getting components'
    ComponentDist = snap.TIntPrV()
    snap.GetWccSzCnt(g, ComponentDist)
    f = open('component_distribution.txt', 'w')
    f.write("Size  - Number of Components:\n")
    for comp in ComponentDist:
        f.write("% d \t % d\n" % (comp.GetVal1(), comp.GetVal2()))
    f.close()
    print 'finshed componet distribution'
Exemple #4
0
def topWCC(G, outputFile, n=5):
    WccSzCnt = snap.TIntPrV()
    snap.GetWccSzCnt(G, WccSzCnt)
    Cnt = WccSzCnt.Len()
    L = []
    for i in range (Cnt):
        e = (WccSzCnt[i].Val1.Val, WccSzCnt[i].Val2.Val)
        L.append(e)

    # sort and get top 10    
    L = sorted(L, key=itemgetter(0), reverse=True)
    with open(outputFile, 'a') as fout:
        fout.write("\nWCC Info: \n")
        fout.write("\tWccSzCnt.Len(): " + str(Cnt) + "\n")
        m = min(n, Cnt)
        for i in range(m):
            e = L[i]
            fout.write("\tWccSzCnt[" + str(i) + "] = (" + str(e[0]) + "," + str(e[1]) + ")\n")
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")
Exemple #6
0
def computeNumberOfWeaklyConnectedComponentsPerSize(graph, outFile, fill):
    logger.info(
        "Computing Number of Weakly Connected Components Per Component Size")
    fw_cc = open(outFile, 'w')
    CntV = snap.TIntPrV()
    snap.GetWccSzCnt(graph, CntV)
    fw_cc.write('Size of WCC\tNumber of WCCs\n')
    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 Weakly Connected Components Per Component Size!")
    logger.info(
        "Number of Weakly Connected Components Per Component Size Exported to "
        + outFile)
Exemple #7
0
# get 3-core of G8
Core3 = snap.GetKCore(G8, 3)
print "Core3: Nodes %d, Edges %d" % (Core3.GetNodes(), Core3.GetEdges())

# delete nodes of out degree 3 and in degree 2
snap.DelDegKNodes(G8, 3, 2)

# create a directed random graph on 10k nodes and 1k edges
G9 = snap.GenRndGnm(snap.PNGraph, 10000, 1000)
print "G9: Nodes %d, Edges %d" % (G9.GetNodes(), G9.GetEdges())

# define a vector of pairs of integers (size, count) and
# get a distribution of connected components (component size, count)
CntV = snap.TIntPrV()
snap.GetWccSzCnt(G9, CntV)
for p in CntV:
    print "size %d: count %d" % (p.GetVal1(), p.GetVal2())

# get degree distribution pairs (out-degree, count):
snap.GetOutDegCnt(G9, CntV)
for p in CntV:
    print "degree %d: count %d" % (p.GetVal1(), p.GetVal2())

# generate a Preferential Attachment graph on 100 nodes and out-degree of 3
G10 = snap.GenPrefAttach(100, 3)
print "G10: Nodes %d, Edges %d" % (G10.GetNodes(), G10.GetEdges())

# define a vector of floats and get first eigenvector of graph adjacency matrix
EigV = snap.TFltV()
snap.GetEigVec(G10, EigV)
Exemple #8
0
def intro():

    # create a graph PNGraph
    G1 = snap.TNGraph.New()
    G1.AddNode(1)
    G1.AddNode(5)
    G1.AddNode(32)
    G1.AddEdge(1, 5)
    G1.AddEdge(5, 1)
    G1.AddEdge(5, 32)
    print("G1: Nodes %d, Edges %d" % (G1.GetNodes(), G1.GetEdges()))

    # create a directed random graph on 100 nodes and 1k edges
    G2 = snap.GenRndGnm(snap.PNGraph, 100, 1000)
    print("G2: Nodes %d, Edges %d" % (G2.GetNodes(), G2.GetEdges()))

    # traverse the nodes
    for NI in G2.Nodes():
        print("node id %d with out-degree %d and in-degree %d" %
              (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
    # traverse the edges
    for EI in G2.Edges():
        print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId()))

    # traverse the edges by nodes
    for NI in G2.Nodes():
        for Id in NI.GetOutEdges():
            print("edge (%d %d)" % (NI.GetId(), Id))

    # generate a network using Forest Fire model
    G3 = snap.GenForestFire(1000, 0.35, 0.35)
    print("G3: Nodes %d, Edges %d" % (G3.GetNodes(), G3.GetEdges()))

    # save and load binary
    FOut = snap.TFOut("test.graph")
    G3.Save(FOut)
    FOut.Flush()
    FIn = snap.TFIn("test.graph")
    G4 = snap.TNGraph.Load(FIn)
    print("G4: Nodes %d, Edges %d" % (G4.GetNodes(), G4.GetEdges()))

    # save and load from a text file
    snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges")
    G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1)
    print("G5: Nodes %d, Edges %d" % (G5.GetNodes(), G5.GetEdges()))

    # generate a network using Forest Fire model
    G6 = snap.GenForestFire(1000, 0.35, 0.35)
    print("G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges()))
    # convert to undirected graph
    G7 = snap.ConvertGraph(snap.PUNGraph, G6)
    print("G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges()))
    # get largest weakly connected component of G
    WccG = snap.GetMxWcc(G6)
    # get a subgraph induced on nodes {0,1,2,3,4,5}
    SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4))
    # get 3-core of G
    Core3 = snap.GetKCore(G6, 3)
    # delete nodes of out degree 10 and in degree 5
    snap.DelDegKNodes(G6, 10, 5)
    print("G6a: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges()))

    # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3
    G8 = snap.GenPrefAttach(1000, 3)
    print("G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges()))
    # vector of pairs of integers (size, count)
    CntV = snap.TIntPrV()
    # get distribution of connected components (component size, count)
    snap.GetWccSzCnt(G8, CntV)
    # get degree distribution pairs (degree, count)
    snap.GetOutDegCnt(G8, CntV)
    # vector of floats
    EigV = snap.TFltV()
    # get first eigenvector of graph adjacency matrix
    snap.GetEigVec(G8, EigV)
    # get diameter of G8
    snap.GetBfsFullDiam(G8, 100)
    # count the number of triads in G8, get the clustering coefficient of G8
    snap.GetTriads(G8)
    snap.GetClustCf(G8)
Exemple #9
0
G1 = snap.LoadEdgeListStr(snap.PUNGraph, "new.txt", 1, 0)
print "Number of Nodes: %d" % G1.GetNodes()

for NI in G1.Nodes():
    print "node id %d with degree %d" % (NI.GetId(), NI.GetDeg())
print "praveen"
for EI in G1.Edges():
    print "edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())

print "praveen"
total_no_of_communities = 0
community_list = {1: [], 2: [], 3: []}
SubG = snap.GetSubGraph(G1, snap.TIntV.GetV(0, 1, 2, 3, 4))
for EI in SubG.Nodes():
    print "Node - %d" % (EI.GetId())
'''
# generate a Preferential Attachment graph on 1000 nodes and node out degree of 3
G8 = snap.GenPrefAttach(1000, 3)
# vector of pairs of integers (size, count)
CntV = snap.TIntPrV()
# get distribution of connected components (component size, count)
snap.GetWccSzCnt(G8, CntV)
# get degree distribution pairs (degree, count)
snap.GetOutDegCnt(G8, CntV)
# vector of floats
EigV = snap.TFltV()
# get first eigenvector of graph adjacency matrix
snap.GetEigVec(G8, EigV)
# get diameter of G8
snap.GetBfsFullDiam(G8, 100)
# count the number of triads in G8, get the clustering coefficient of G8
Exemple #10
0
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")
Exemple #11
0
# convert to undirected graph
G7 = snap.ConvertGraph(snap.PUNGraph, G6)
WccG = snap.GetMxWcc(G6)
# get a subgraph induced on nodes {0,1,2,3,4,5}
SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4))
# get 3-core of G
Core3 = snap.GetKCore(G6, 3)
# delete nodes of out degree 10 and in degree 5
snap.DelDegKNodes(G6, 10, 5)

# %%
# stats
# generate a Preferential Attachment graph on 1000 nodes and node out degree of 3
G8 = snap.GenPrefAttach(1000, 3)
# vector of pairs of integers (size, count)
CntV = snap.TIntPrV()
# get distribution of connected components (component size, count)
snap.GetWccSzCnt(G8, CntV)
# get degree distribution pairs (degree, count)
snap.GetOutDegCnt(G8, CntV)
# vector of floats
EigV = snap.TFltV()
# get first eigenvector of graph adjacency matrix
snap.GetEigVec(G8, EigV)
# get diameter of G8
snap.GetBfsFullDiam(G8, 100)
# count the number of triads in G8, get the clustering coefficient of G8
snap.GetTriads(G8)
snap.GetClustCf(G8)
# %%
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()
Exemple #13
0
import numpy as np

G = snap.LoadEdgeList(snap.PNGraph, "test-graph.txt", 0, 1)

print "Num nodes: %d; Num Edges: %d" % (G.GetNodes(), G.GetEdges())
print "getting graph diamaters..."
# Get full diameter for directed and undirected
print "Approx. full diameter (directed): %d" % (snap.GetBfsFullDiam(
    G, 100, True))
print "Approx. full diameter (undirected): %d" % (snap.GetBfsFullDiam(
    G, 100, False))

print "getting WCC size distribution..."
# Get WCC size distribution
ComponentDist = snap.TIntPrV()
snap.GetWccSzCnt(G, ComponentDist)
size = []
counts = []
print "WCC counts"
for comp in ComponentDist:
    size.append(comp.GetVal1())
    counts.append(comp.GetVal2())
    print "Size: %d Count: %d" % (comp.GetVal1(), comp.GetVal2())

plt.clf()
plt.figure()
plt.plot(size, counts, '.')

ComponentDist2 = snap.TIntPrV()
snap.GetWccSzCnt(G, ComponentDist2)
print "SCC counts"
for i in range(len(Interests[0])):
    S.AddDat((10000 + i), Interests[0][i])
    GI.AddNode(10000 + i)
for i in range(len(followerFile)):
    GI.AddEdge(interestFile.iloc[i, 0], getval(S, interestFile.iloc[i, 1]))

snap.DrawGViz(G1, snap.gvlDot, "reco.png", "Network Diagram", True,
              snap.TIntStrH())

snap.PlotInDegDistr(G1, "Indeg", "Directed graph - in-degree")
snap.PlotOutDegDistr(G1, "Outdeg", "Directed graph - out-degree")

# vector of pairs of integers (size, count)
ComponentDist = snap.TIntPrV()
# get distribution of connected components (component size, count)
snap.GetWccSzCnt(G1, ComponentDist)
for comp in ComponentDist:
    print "Size: %d - Number of Components: %d" % (comp.GetVal1(),
                                                   comp.GetVal2())
Count = snap.CntUniqDirEdges(G1)
print "Directed Graph: Count of unique directed edges is %d" % Count

# get degree distribution pairs (degree, count)
snap.GetOutDegCnt(G1, ComponentDist)
print "Degree Distribution Pairs-"
xval = []
yval = []
for item in ComponentDist:
    print "%d nodes with out-degree %d" % (item.GetVal2(), item.GetVal1())
    xval.append(item.GetVal1())
    yval.append(item.GetVal2())
Exemple #15
0
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')
    plt.ylabel('Frequency')
Exemple #16
0
import snap

# create a random directed graph
G = snap.GenRndGnm(snap.PNGraph, 10000, 5000)

# test if the graph is connected or weakly connected
print("IsConnected(G) =", snap.IsConnected(G))
print("IsWeaklyConnected(G) =", snap.IsWeaklyConn(G))

# get the weakly connected component counts
WccSzCnt = snap.TIntPr64V()
snap.GetWccSzCnt(G, WccSzCnt)
#print (WccSzCnt[0],WccSzCnt[0].Val1,WccSzCnt[0].Val2)
for i in range(0, WccSzCnt.Len()):
    print("WccSzCnt[%d] = (%d, %d)" %
          (i, WccSzCnt[i].Val1.Val, WccSzCnt[i].Val2.Val))

# return nodes in the same weakly connected component as node 1
CnCom = snap.TInt64V()
snap.GetNodeWcc(G, 1, CnCom)
print("CnCom.Len() = %d" % (CnCom.Len()))

# get nodes in weakly connected components
WCnComV = snap.TCnComV()
snap.GetWccs(G, WCnComV)
for i in range(0, WCnComV.Len()):
    print("WCnComV[%d].Len() = %d" % (i, WCnComV[i].Len()))
    for j in range(0, WCnComV[i].Len()):
        print("WCnComV[%d][%d] = %d" % (i, j, WCnComV[i][j]))

# get the size of the maximum weakly connected component
    #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"
    print

    #properties of largest strongly connected component
    big_scc = snap.GetMxScc(repliesgraph)
    snap.PrintInfo(big_scc, "Largest strongly connected component")

    num_dir_edges = snap.CntUniqDirEdges(big_scc)
    print "{0:.2f}% of directed edges are reciprocal".format(
        snap.CntUniqBiDirEdges(big_scc) * 2 * 100 / num_dir_edges)
def get_connected_info(graph):
    cd = snap.TIntPrV()
    snap.GetWccSzCnt(graph, cd)
    for comp in cd:
        print "Size: %d - Number of Components: %d" % (comp.GetVal1(),
                                                       comp.GetVal2())