Beispiel #1
0
def get_properties_with_sanppy(extension, input_folder):
    id_map = {}
    next_id = 0
    graph = snap.PNGraph.New()
    files_read = 0
    for file in os.listdir(input_folder):
        if not extension or file.endswith(extension):
            files_read += 1
            with open(os.path.join(input_folder, file)) as file:
                for line in file.readlines():
                    edge = line.rstrip().split()
                    n1 = id_map.get(edge[0], next_id)
                    if n1 == next_id:
                        id_map[edge[0]] = n1
                    next_id += 1
                    n2 = id_map.get(edge[1], next_id)
                    if n2 == next_id:
                        id_map[edge[1]] = n2
                    next_id += 1
                    if not graph.IsNode(n1):
                        graph.AddNode(n1)
                    if not graph.IsNode(n2):
                        graph.AddNode(n2)
                    graph.AddEdge(n1, n2)
    ef_diam_l, ef_diam_h, diam, sp = snap.GetBfsEffDiamAll(graph, 10, True)
    properties = [
        snap.CntNonZNodes(graph),
        snap.CntUniqDirEdges(graph),
        snap.IsConnected(graph),
        snap.CntNonZNodes(graph) / snap.CntUniqDirEdges(graph),
        snap.GetClustCf(graph), sp, diam,
        snap.CntUniqDirEdges(graph) /
        (snap.CntNonZNodes(graph) * snap.CntNonZNodes(graph))
    ]
    return dict(zip(get_property_names(), properties))
def loadGraphPrintStats(inputFile, graphType):
    outGraph = snap.LoadEdgeList(snap.PUNGraph, inputFile, 0, 1)
    nodeCount = snap.CntNonZNodes(outGraph)
    edgeCount = snap.CntUniqUndirEdges(outGraph)
    avgDeg = float(edgeCount) / nodeCount
    print "Average Degree for %s graph is %f" % (graphType, avgDeg)
    return outGraph
def degree_distribution_graphs():
    InDegV = snap.TIntPrV()
    snap.GetNodeInDegV(G, InDegV)
    a = np.arange(1, snap.CntNonZNodes(G) - snap.CntInDegNodes(G, 0) + 2)
    i = 0
    for item in InDegV:
        if item.GetVal2() > 0:
            i = i + 1
            a[i] = item.GetVal2()

    bars, bins = np.histogram(a, bins=np.arange(1, max(a)))
    plt.hist(bars, bins)
    plt.grid()
    plt.show()

    plt.loglog(bins[0:-1], bars)
    plt.ylabel('# users per degree')
    plt.xlabel('in-degree')
    plt.grid()
    plt.show()

    plt.loglog(bins[0:-1], sum(bars) - np.cumsum(bars))
    plt.ylabel('# users with degree larger or equal than x')
    plt.xlabel('in-degree')
    plt.grid()
    plt.show()
Beispiel #4
0
def getBasicInfo(strPath, net):

    G = snap.LoadEdgeList(snap.PUNGraph,strPath,0,1)
    GraphInfo = {}
    GraphInfo['nodes'] = G.GetNodes()
    GraphInfo['edges'] = G.GetEdges()
    GraphInfo['zeroDegNodes'] = snap.CntDegNodes(G, 0)
    GraphInfo['zeroInDegNodes'] = snap.CntInDegNodes(G, 0)
    GraphInfo['zeroOutDegNodes'] = snap.CntOutDegNodes(G, 0)
    GraphInfo['nonZeroIn-OutDegNodes'] = snap.CntNonZNodes(G)
    GraphInfo['uniqueDirectedEdges'] = snap.CntUniqDirEdges(G)
    GraphInfo['uniqueUndirectedEdges'] = snap.CntUniqUndirEdges(G)
    GraphInfo['selfEdges'] = snap.CntSelfEdges(G)
    GraphInfo['biDirEdges'] = snap.CntUniqBiDirEdges(G)

    NTestNodes = 10
    IsDir = False
    GraphInfo['approxFullDiameter'] = snap.GetBfsEffDiam(G, NTestNodes, IsDir)
    GraphInfo['90effectiveDiameter'] = snap.GetAnfEffDiam(G)

    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    sumofNode = G.GetNodes()
    L = [item.GetVal1()*item.GetVal2() for item in DegToCntV]
    GraphInfo['averageDegree'] = float(sum(L))/(sumofNode)

    (DegreeCountMax ,Degree, DegreeCount, CluDegree, Clu) = getGraphInfo(G)
    # creatNet(G,net)

    return GraphInfo,DegreeCountMax , Degree, DegreeCount, CluDegree, Clu
def get_robustness(file_path, LSCC_output_path, LWCC_output_path):
    frac_list = [
        0.0001, 0.001, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9
    ]
    Graph, H = load_graph(file_path)
    InDegV = snap.TIntPrV()
    snap.GetNodeInDegV(Graph, InDegV)
    OutDegV = snap.TIntPrV()
    snap.GetNodeOutDegV(Graph, OutDegV)
    degree = dict()
    for item in InDegV:
        ID = item.GetVal1()
        InDeg = item.GetVal2()
        degree[ID] = InDeg
    for item in OutDegV:
        ID = item.GetVal1()
        OutDeg = item.GetVal2()
        degree[ID] += OutDeg
    sorted_degree = sorted(degree.items(), key=itemgetter(1), reverse=True)
    tot = len(sorted_degree)
    pos = [int(tot * frac) for frac in frac_list]
    print pos
    cur = 0
    LSCC_robust = list()
    LWCC_robust = list()
    for i in range(tot):
        Graph.DelNode(sorted_degree[i][0])
        if i == pos[cur] - 1:
            LSCC_frac = snap.GetMxSccSz(Graph)
            LWCC_frac = snap.GetMxWccSz(Graph)
            singleton_frac = 1.0 - 1.0 * snap.CntNonZNodes(
                Graph) / Graph.GetNodes()
            LSCC_robust.append({
                'removed': frac_list[cur],
                'singleton': singleton_frac,
                'middle': 1.0 - singleton_frac - LSCC_frac,
                'LSCC': LSCC_frac
            })
            LWCC_robust.append({
                'removed': frac_list[cur],
                'singleton': singleton_frac,
                'middle': 1.0 - singleton_frac - LWCC_frac,
                'LWCC': LWCC_frac
            })
            cur += 1
        if cur >= len(pos):
            break
    LSCC_robust = pd.DataFrame(LSCC_robust)
    LSCC_robust = LSCC_robust[['removed', 'singleton', 'middle', 'LSCC']]
    LSCC_robust.to_csv(LSCC_output_path, index=False, encoding='utf-8')
    LWCC_robust = pd.DataFrame(LWCC_robust)
    LWCC_robust = LWCC_robust[['removed', 'singleton', 'middle', 'LWCC']]
    LWCC_robust.to_csv(LWCC_output_path, index=False, encoding='utf-8')
Beispiel #6
0
def analyze_with_sanppy(extension, input_path):
    id_map = {}
    next_id = 0
    graph = snap.PNGraph.New()
    files_read = 0
    if os.path.isdir(input_path):
        for file in os.listdir(input_path):
            if not extension or file.endswith(extension):
                files_read += 1
                next_id = load_graph_from_file(os.path.join(input_path, file), graph, id_map, next_id)
    else:
        load_graph_from_file(input_path, graph, id_map, next_id)
    print("Nodes: {}".format(snap.CntNonZNodes(graph)))
    print("Edges: {}".format(snap.CntUniqDirEdges(graph)))
    print("Connected: {}".format(snap.IsConnected(graph)))
    print("Average degree: {}".format(snap.CntNonZNodes(graph) / snap.CntUniqDirEdges(graph)))
    print("Average clustering coefficient: {}".format(snap.GetClustCf(graph)))
    ef_diam_l, ef_diam_h, diam, sp = snap.GetBfsEffDiamAll(graph, 10, True)
    print("Average shortest-path: {}".format(sp))
    print("Diameter: {}".format(diam))
    print("Density: {}".format(snap.CntUniqDirEdges(graph)/(snap.CntNonZNodes(graph)*snap.CntNonZNodes(graph))))
def power_law_fit():
    InDegV = snap.TIntPrV()
    snap.GetNodeInDegV(G, InDegV)
    a = np.arange(1, snap.CntNonZNodes(G) - snap.CntInDegNodes(G, 0) + 2)
    fit = pl.Fit(a)
    pl.plot_pdf(a, color='r')
    fig2 = fit.plot_pdf(color='b', linewidth=2)
    # power-law exponent
    print("Power Law Data\n")
    print("Power Law Exponential:", fit.alpha)
    print("Min value for X:", fit.xmin)
    print("Kolmogorov-Smirnov test:", fit.D)
    # comparison of data and Pl-fits of pdf (blue) and ccdf (red)"
    figCCDF = fit.plot_pdf(color='b', linewidth=2)
    fit.power_law.plot_pdf(color='b', linestyle='--', ax=figCCDF)
    fit.plot_ccdf(color='r', linewidth=2, ax=figCCDF)
    fit.power_law.plot_ccdf(color='r', linestyle='--', ax=figCCDF)
    ####
    figCCDF.set_ylabel(u"p(X),  p(X≥x)")
    figCCDF.set_xlabel(r"in-degree")
Beispiel #8
0
import snap

graphfilename = "C:\Python27\HW1\wiki-vote.txt"
schema = snap.Schema()
context = snap.TTableContext()
schema.Add(snap.TStrTAttrPr("srcID", snap.atStr))
schema.Add(snap.TStrTAttrPr("dstID", snap.atStr))
sample_table = snap.TTable.LoadSS(schema, graphfilename, context, "\t",
                                  snap.TBool(False))

# graph will be an object of type snap.PNGraph
graph = snap.ToGraph(snap.PNGraph, sample_table, "srcID", "dstID",
                     snap.aaFirst)
#no of nodes
Count = snap.CntNonZNodes(graph)
print "Count of nodes with degree greater than 0 is %d" % Count
#no of edges
Count = snap.CntOutDegNodes(graph, 0)
print "Count of nodes with out-degree 0 is %d" % Count
#no of nodes with zero in-degree
Count = snap.CntInDegNodes(graph, 0)
print "Count of nodes with in-degree 0 is %d" % Count
#no of directed edges
Count = snap.CntUniqDirEdges(graph)
print "Count of directed edges is %d" % Count
#no of undirected edges
Count = snap.CntUniqUndirEdges(graph)
print "Count of undirected edges is %d" % Count
#no of self edges
Count = snap.CntSelfEdges(graph)
print "Count of self edges is %d" % Count
Beispiel #9
0
		graph.AddEdge(3,4)
		graph.AddEdge(4,5)
		graph.AddEdge(3,5)
		graph.AddEdge(13,5)
		
		print(RankDegreeGraphDSC(graph))
		'''
        print "Graph User: %d" % users[i]
        print "Graph noder: %d" % countAllNode(graph)

        for itemRate in samplingRates:
            print itemRate
            samplingRate = itemRate
            subgraph = []
            seed = []
            x = samplingRate * snap.CntNonZNodes(
                graph) / 10  #UKURAN SUBGRAPH OUTPUT

            #------------------ RANGKING GRAPH BERDASARKAN DEGREE SECARA DESCENDING-----------
            sortedGraphDictionary = RankDegreeGraphDSC(graph)

            #------------------ INISIALISASI SUBGRAPH DG MEMILIH m NODE TERATAS -----------
            subgraph = Select_m_VertexAsSeed(graph, subgraph,
                                             sortedGraphDictionary, m)
            seed = list(subgraph)

            Si = m
            while (Si < x):
                newseed = []
                for itemSeed in seed:
                    #Rank adjacent vertices of si based on degree values
                    sortedAdjacentNodeDictionary = RankDegreeAdjacentNodeDSC(
Beispiel #10
0
    print "Please enter only one file"
    sys.exit()

if input_file == "random5000by6.txt":
    #create random graph
    Graph = snap.GenRndGnm(snap.PUNGraph, 5000, 5000 * (5000 - 1) / 2)
    V = snap.TIntV()
    for i in range(5000):
        if (i % 6 != 0):
            V.Add(i)
    snap.DelNodes(Graph, V)
    snap.SaveEdgeList(Graph, 'random5000by6.txt')
else:
    Graph = snap.LoadEdgeList(snap.PUNGraph, input_file, 0, 1)

nodes = snap.CntNonZNodes(Graph)
nodes0 = snap.CntDegNodes(Graph, 0)
final_nodes = nodes + nodes0
edges = snap.CntUniqUndirEdges(Graph)
nodes7 = snap.CntDegNodes(Graph, 7)
DegToCntV = snap.TIntPrV()
snap.GetDegCnt(Graph, DegToCntV)
vector_length = len(DegToCntV)
maxdegreencount = DegToCntV[vector_length - 1].GetVal2()

#for item in DegToCntV:
# print "%d nodes with degree %d" % (item.GetVal2(), item.GetVal1())
print ""
print "Number of nodes in " + input_file + ": ", final_nodes
print "Number of edges in " + input_file + ": ", edges
print "Number of nodes with degree=7 in " + input_file + ": ", nodes7
import sys
import snap
# FIn = snap.TFIn(sys.argv[1])
#!/usr/bin/env python
# coding: utf-8

FIn = snap.TFIn("facebook.elist")
Fb_graph = snap.TNGraph.Load(FIn)

# # 1 and 2 c(partial)

no_of_nodes = snap.CntNonZNodes(Fb_graph)
print("Number of Nodes: " + str(no_of_nodes))
no_of_edges = snap.CntUniqUndirEdges(Fb_graph)
print("Number of Edges: " + str(no_of_edges))
max_degree = -1
degdis_y = []
degdis_x = []
for i in range(1, no_of_nodes - 1):
    degree = snap.CntDegNodes(Fb_graph, i)
    max_degree = max(degree, max_degree)
    degdis_x.append(i)
    degdis_y.append(degree)

# # 2 b

print("Max degree:" + str(max_degree))
print("Node id(s) with highest degree:", end=" ")
for NI in Fb_graph.Nodes():
    if (NI.GetInDeg() == max_degree):
        print(NI.GetId(), end=",")
Beispiel #12
0
#####################################################################
###EX4
import snap
import matplotlib.pyplot as plt

gh = snap.LoadEdgeList(snap.PUNGraph, "facebook/0.edges", 0, 1)
DegToCntV1 = snap.TIntPrV()
snap.GetDegCnt(gh, DegToCntV1)
print("Number of nodes::" + str(snap.CntNonZNodes(gh)))
print("Number of edges::" + str(snap.CntUniqUndirEdges(gh)))
facebook_hist={}
for item in DegToCntV1:
    facebook_hist[item.GetVal2()] = item.GetVal1()
plt.bar(facebook_hist.keys(), facebook_hist.values(), color='g')
fig_size = [14, 7]
plt.rcParams["figure.figsize"] = fig_size
plt.show()

gh2 = snap.LoadEdgeList(snap.PUNGraph, "facebook/348.edges", 0, 1)
DegToCntV2 = snap.TIntPrV()
snap.GetDegCnt(gh2, DegToCntV2)
print("Number of nodes::" + str(snap.CntNonZNodes(gh2)))
print("Number of edges::" + str(snap.CntUniqUndirEdges(gh2)))
f2_hist={}
for item in DegToCntV2:
    f2_hist[item.GetVal2()] = item.GetVal1()
    
plt.bar(map(str, f2_hist.keys()), f2_hist.values(), color='y')
plt.xticks(rotation=90)
plt.show()
Beispiel #13
0
for n in iter(fin):
    u_edges[cnt] = str(n)
    cnt += 1
fin.close()

u_scc = dict()
for p in range(100, 0, -1):
    u_removal_graph = snap.TUNGraph.New()
    fin = open("An_Nodes.txt", "rb")
    for n in iter(fin):
        u_removal_graph.AddNode(int(n))
    fin.close()

    if p == 100:
        csize = snap.GetMxWcc(u_removal_graph)
        lcsize = snap.CntNonZNodes(csize)
    else:
        toAdd = ((100 - p) * (cnt - 1)) / 100
        random_edges = random.sample(xrange(1, cnt - 1), toAdd)
        for edge_num in random_edges:
            ln = u_edges.get(edge_num)
            u_removal_graph.AddEdge(int(ln.split(",", 1)[0]),
                                    int(ln.split(",", 1)[1]))
        csize = snap.GetMxWcc(u_removal_graph)
        lcsize = snap.CntNonZNodes(csize)
    u_scc[p] = lcsize

print "\n"
for k1, v1 in u_scc.iteritems():
    print k1, v1