Beispiel #1
0
def analyzeMisc(FNGraph):
    # LCC, average distances, clustering
    t1 = time.time()

    print "Started calculating miscellaneous network statistics:"

    print '\tPercentage of nodes in LCC in Football network: %.3f' % (snap.GetMxWccSz(FNGraph) * 100.0)
    GraphClustCoeff = snap.GetClustCf (FNGraph, -1)
    print "\tClustering coefficient: %.3f" % GraphClustCoeff

    diam = snap.GetBfsFullDiam(FNGraph, 1432, False)
    print "\tNetwork diameter: %.3f\n" % diam

    print "\tCalculating average distance..."

    avgDist   = 0
    iter1     = 0
    allNodes1 = FNGraph.GetNodes()

    for NI in FNGraph.Nodes():
        if(iter1 % 100 == 0):
            print "\t\tCalculated for %d nodes" % iter1
        NIdToDistH = snap.TIntH()
        snap.GetShortPath(FNGraph, NI.GetId(), NIdToDistH)
        singleDistSum = 0

        for item in NIdToDistH:
            singleDistSum += NIdToDistH[item]

        avgDist += (1.0/allNodes1) * float(singleDistSum)/(allNodes1-1)
        iter1   += 1

    print "\tNetwork average distance: %.3f" % avgDist

    print "\nFinished calculating in %f seconds\n" % (time.time() - t1)
def benchmark_neanet(Graph):
    '''
    Perform benchmark tests for Directed Attribute Graphs
    '''

    results = {}
    results['num_nodes'] = Graph.GetNodes()
    results['num_edges'] = Graph.GetEdges()

    for degree in range(0, 11):
        num = snap.NodesGTEDegree(Graph, degree)
        percent_deg = float(num) / results['num_nodes']
        results['deg_gte_%d' % degree] = num
        results['deg_gte_%d_percent' % degree] = percent_deg

    # Check for over-weighted nodes
    results['max_degree'] = snap.MxDegree(Graph)

    num = snap.NodesGTEDegree(Graph, results['max_degree'])
    results['max_degree_num'] = num

    results['max_wcc_percent'] = snap.GetMxWccSz(Graph) \
      / results['num_nodes']
    results['max_scc_percent'] = snap.GetMxSccSz(Graph).GetNodes() \
      / results['num_nodes']

    return results
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')
def print_components(G):
    """
    Prints the fraction of nodes in the largest component of subgraph G
    Also prints the number of edge bridges and articulation points
    """

    print("Fraction of nodes in largest connected component:", round(snap.GetMxWccSz(G), 4))

    EdgeV = snap.TIntPrV()
    snap.GetEdgeBridges(G, EdgeV)
    print("Number of edge bridges:", EdgeV.Len())

    ArtNIdV = snap.TIntV()
    snap.GetArtPoints(G, ArtNIdV)
    print("Number of articulation points:", ArtNIdV.Len())
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")
Beispiel #6
0
def analyzeMisc(FNGraph):
    # LCC, average distances, clustering
    tStart = time.time()

    print "[Network Analyzr]  Started calculating miscellaneous network statistics..."

    LCCPercentage = snap.GetMxWccSz(FNGraph) * 100.0
    print '\t[Network Analyzr]  Percentage of nodes in LCC: %.3f' % LCCPercentage

    clusteringCoefficient = snap.GetClustCf (FNGraph, -1)
    print "\t[Network Analyzr]  Clustering coefficient: %.3f" % clusteringCoefficient

    diameter= snap.GetBfsFullDiam(FNGraph, 1432, False)
    print "\t[Network Analyzr]  Network diameter: %.3f\n" % diameter

    # Average distance
    print "\t[Network Analyzr]  Calculating average distance..."

    i       = 0
    avgDist = 0
    nodes   = FNGraph.GetNodes()

    for sourceNode in FNGraph.Nodes():
        if i % 100 == 0 and utils.mode == 'debug':
            print "\t\tCalculated for %d nodes" % i

        NIdToDistH = snap.TIntH()
        snap.GetShortPath(FNGraph, sourceNode.GetId(), NIdToDistH)
        distanceSum = 0

        for destinationNode in NIdToDistH:
            distanceSum += NIdToDistH[destinationNode]

        avgDist += (1.0 / nodes) * float(distanceSum) / (nodes - 1)

        i += 1

    print "\t[Network Analyzr]  Network average distance: %.3f" % avgDist

    timeSpent = time.time() - tStart

    print "\n[Network Analyzr]  Finished calculating in %f seconds\n" % timeSpent
Beispiel #7
0
def analyze(graph):

    n = graph.GetNodes()
    m = graph.GetEdges()

    maxSCCsize = snap.GetMxSccSz(graph)
    maxWCCsize = snap.GetMxWccSz(graph)
    avgDegree = (m * float(2)) / n

    # estimate power law exponent
    degs = []
    degCounts = []
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(graph, DegToCntV)
    for item in DegToCntV:
        degs.append(item.GetVal1())
        degCounts.append(item.GetVal2())
    xMin = min(degs) - 0.5
    m = graph.GetNodes()
    alphaMLLE = 1 + (m / (sum([np.log(i / xMin) * degCounts[degs.index(i)] for i in degs])))

    # erdos-renyi clustering coefficient
    graphER = snap.GenRndGnm(snap.PUNGraph, n, m)
    avgClustCoeffER = snap.GetClustCf(graphER, -1)

    # average shortest path
    graphWCC = snap.GetMxWcc(graph)
    avgClustCoeff = snap.GetClustCf(graphWCC, -1)
    numSamples = min(graphWCC.GetNodes(), 617) # all nodes or sample size
    Rnd = snap.TRnd(42)
    Rnd.Randomize()
    shortPathList = []
    for i in xrange(numSamples):
        s = graphWCC.GetRndNId(Rnd)
        NIdToDistH = snap.TIntH()
        snap.GetShortPath(graphWCC, s, NIdToDistH)
        for item in NIdToDistH:
            shortPathList.append(NIdToDistH[item])
    avgShortPath = np.mean(shortPathList)

    return avgClustCoeff, maxSCCsize, maxWCCsize, avgDegree, alphaMLLE, avgClustCoeffER, avgShortPath
Beispiel #8
0
# 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
MxWccSz = snap.GetMxWccSz(G)
print("MxWccSz = %.5f" % (MxWccSz))

# get the graph with the largest weakly connected component
GMx = snap.GetMxWcc(G)
print("GMx: GetNodes() = %d, GetEdges() = %d" %
      (GMx.GetNodes(), GMx.GetEdges()))

# get strongly connected components
SCnComV = snap.TCnComV()
snap.GetSccs(G, SCnComV)
for i in range(0, SCnComV.Len()):
    print("SCnComV[%d].Len() = %d" % (i, SCnComV[i].Len()))

# get the graph representing the largest bi-connected component
GMxBi = snap.GetMxBiCon(G)
Beispiel #9
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")