Beispiel #1
0
def plotDegDistr(G):
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    numNodes = G.GetNodes()
    print numNodes
    deg = []
    nodes = []
    tups = []
    for item in DegToCntV:
        if item.GetVal1() == 0:
            numNodes -= item.GetVal2()
            continue
        deg.append(item.GetVal1())
        nodes.append(item.GetVal2())  #float(item.GetVal2()) / float(numNodes))
        tups.append((item.GetVal1(), float(item.GetVal2())))

    fig = plt.figure()
    ax = plt.gca()
    G1Dots, = ax.plot(deg, [float(x) / float(numNodes) for x in nodes],
                      'o',
                      c='blue',
                      alpha=0.75,
                      markeredgecolor='none')
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.set_ylabel('Proportion of Users')
    ax.set_xlabel('Number of Friends')
    plt.show()
Beispiel #2
0
def get_dists(G):
    deg_counts = []
    degs = []
    deg_vect = snap.TIntPrV()
    snap.GetDegCnt(G, deg_vect)
    for item in deg_vect:
        deg = item.GetVal1()
        cnt = item.GetVal2()
        deg_counts.append(cnt)
        degs.append(deg)

    out_deg = []
    out_counts = []
    cur_deg = min(degs)
    for deg, cnt in zip(degs, deg_counts):
        # while cur_deg < deg:
        #     out_deg.append(cur_deg)
        #     out_counts.append(0)
        #     cur_deg += 1
        out_deg.append(deg)
        out_counts.append(cnt)
        cur_deg += 1

    deg_counts = np.asarray(out_counts)
    degs = np.asarray(out_deg)
    pdf = deg_counts.astype(float) / sum(deg_counts)
    cdf = np.cumsum(pdf)
    cdf = np.insert(cdf, 0, 0)
    ccdf = 1 - cdf
    return deg_counts, degs, cdf, ccdf, pdf
Beispiel #3
0
def saveDegreeDistribution(graph, filename):
    degToCntV = snap.TIntPrV()
    snap.GetDegCnt(graph, degToCntV)
    with open(filename, 'w') as f:
        for item in degToCntV:
            dist = float(item.GetVal2()) / float(graph.GetNodes())
            f.write('%d\t%f\n' % (item.GetVal1(), dist))
Beispiel #4
0
def getGraphInfo(G):
    
    Degree = []
    DegreeCount = []
    CluDegree = []
    Clu = []

    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    DegreeCountMax = 0
    for item in DegToCntV:
        tempy = item.GetVal2()
        if tempy > DegreeCountMax:
            DegreeCountMax = tempy
        Degree.append(item.GetVal1())
        DegreeCount.append(tempy)


    CfVec = snap.TFltPrV()
    Cf = snap.GetClustCf(G, CfVec, -1)
    for pair in CfVec:
        CluDegree.append(pair.GetVal1())
        Clu.append(pair.GetVal2())

    return DegreeCountMax,Degree,DegreeCount,CluDegree,Clu
Beispiel #5
0
 def degreeDistribution(self):
     result = snap.TFltPrV()
     resultMap = collections.defaultdict(lambda:0)
     snap.GetDegCnt(self.rawGraph, result)
     for x in result:
         resultMap[int(x.GetVal1())] = int(x.GetVal2())
     return resultMap
Beispiel #6
0
def analyzeDegrees(FNGraph):
    t1 = time.time()

    print "Started analysing network degrees: \n"

    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(FNGraph, DegToCntV)

    avgDeg = 0
    xVec   = list()
    yVec   = list()

    for item in DegToCntV:
        avgDeg += int(item.GetVal2()) * int(item.GetVal1())
        xVec.append(item.GetVal1())
        yVec.append(item.GetVal2())

    avgDeg = avgDeg/FNGraph.GetNodes()

    print "\tNetwork average degree %d" % avgDeg

    # plot degree distribution
    plt.figure(0)
    plt.plot(xVec, yVec, 'b-')
    plt.title("Degree distribution for Football network \n Average degree: %d" % avgDeg)
    plt.ylabel("Number of nodes")
    plt.xlabel("Degrees")
    plt.savefig('DegreeDistribution.png')

    print "\nFinished calculating in %.3f seconds\n" % (time.time()-t1)
Beispiel #7
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 f():
     snap = self.snap
     DegToCntV = snap.TFltPr64V()
     snap.GetDegCnt(self.graph, DegToCntV)
     ret = []
     for item in DegToCntV:
         ret.append((item.GetVal1(), item.GetVal2()))
     return ret
def degree_distribution():
    # Get node with max degree
    NId = snap.GetMxDegNId(G)
    print("max degree node", NId)

    # Get degree distribution
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    for item in DegToCntV:
        print("%d nodes with degree %d" % (item.GetVal2(), item.GetVal1()))
Beispiel #10
0
def CalculatePowerLawDistribution(graph, user):
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(graph, DegToCntV)
    x = []
    y = []
    for item in DegToCntV:
        x.append(item.GetVal2())
        y.append(item.GetVal1())
        # print "%d nodes with degree %d" % (item.GetVal2(), item.GetVal1())
    plt.plot(x, y, 'ro')
    plt.show()
Beispiel #11
0
def getDegreeToCount(G):
    """ get (degree, count-of-node, fraction-of-node) sequences
    """
    N = G.GetNodes()
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    degreeV = [ e.GetVal1() for e in DegToCntV ]
    countV = [ e.GetVal2() for e in DegToCntV ]
    normCountV = [ k / float(N) for k in countV ]
    D = {'degreeV': degreeV, 'countV': countV, 'normCountV': normCountV}
    return D
Beispiel #12
0
def graphInfo(net):
	print '|V| = {}'.format(net.GetNodes())
	print '|E| = {}'.format(net.GetEdges())

	DegToCntV = snap.TIntPrV()
	snap.GetDegCnt(net, DegToCntV)
	overallDeg = 0
	for item in DegToCntV:
		overallDeg = overallDeg + item.GetVal2()*item.GetVal1()

	print 'Average degree: {:.1f}'.format(float(overallDeg)/net.GetNodes())
	print 'Graph connected: {}'.format(snap.IsConnected(net))
Beispiel #13
0
def getDataPointsToPlot(Graph, degType):
    """
    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    DegToCntV = snap.TIntPrV()

    if degType == "In":
        snap.GetInDegCnt(Graph, DegToCntV)
    elif degType == "Out":
        snap.GetOutDegCnt(Graph, DegToCntV)
    elif degType == "Total":
        snap.GetDegCnt(Graph, DegToCntV)
    else:
        raise ValueError("Invalid degree type: please use 'In', 'Out' or 'Total'.")

    NumNodes = Graph.GetNodes()
    DegToFrqV = { item.GetVal1() : float(item.GetVal2())/NumNodes for item in DegToCntV }
    DegToFrqV = sorted(DegToFrqV.items())
    X, Y = zip(*DegToFrqV)
    ############################################################################
    return X, Y

    def plot_graph(name):
        G = load_graph(name)
        print "{} graph nodes: {}".format(name, G.GetNodes()) 
        print "{} graph edges: {}".format(name, G.GetEdges()) 

        x_in, y_in = getDataPointsToPlot(G, 'In')
        plt.loglog(x_in, y_in, marker=',', color = 'y', label = 'In Degree')

        x_out, yout = getDataPointsToPlot(G, 'Out')
        plt.loglog(x_out, y_out, marker=',', color = 'r', label = 'Out Degree')

        x_total, y_total = getDataPointsToPlot(G, 'Total')
        plt.loglog(x_total, y_total, marker=',', color = 'b', label = 'Total Degree')  #linestyle = 'dotted'

        plt.xlabel('Node Degree (log)')
        plt.ylabel('Proportion of Nodes with a Given Degree (log)')
        plt.title('Degree Distribution of In, Out, and Total degree for {} network'.format(name))
        plt.legend()
        plt.show()


    if __name__ == "__main__":
        # Plot distribution graphs for RT, MT, RE, Social networks
        plot_graph("retweet")
        plot_graph("mention")
        plot_graph("reply")
        plot_graph("social")
Beispiel #14
0
def getGamma(graph):
	kmin = float('inf')
	kmax = -float('inf')

	out_arr = snap.TIntPrV()
	snap.GetDegCnt(graph, out_arr)
	for item in out_arr:
		deg = item.GetVal1()
		num = item.GetVal2()

		kmin = max(1, min(kmin, deg))
		kmax = max(kmax, deg)

	return 1. + np.log(graph.GetNodes()) / (np.log(kmax) - np.log(kmin))
def getDataPointsToPlot(Graph):

    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)

    totalNodes = Graph.GetNodes()

    X, Y = [], []

    for output in DegToCntV:
        X.append(output.GetVal1())
        Y.append(output.GetVal2() / float(totalNodes))

    return X, Y
Beispiel #16
0
 def degDist(self):
     # 度分布 
     DegToCntV = snap.TIntPrV()
     snap.GetDegCnt(self.SnapGraph, DegToCntV)
     degree = []
     counts = []
     for item in DegToCntV:
         counts.append(item.GetVal2())
         degree.append(item.GetVal1())
     plt.title('degree ditribution')
     plt.xlabel('degree')
     plt.ylabel('frequency')
     plt.plot(degree[0:50], counts[0:50])
     plt.show()
Beispiel #17
0
def getNodeDegForPlot(Graph):
    """
    :param - Graph: snap.PUNGraph object representing an undirected graph

    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    # TODO: Your code here!
    N = float(Graph.GetNodes())
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)
    X, Y = zip(*[(item.GetVal1(), item.GetVal2()/N) for item in DegToCntV])
    ############################################################################
    return X, Y
Beispiel #18
0
def node_deg_count_vec(graph):
    """return a vector of dimension MaxDeg for undirected graph
    Args:
        graph: 

    Return: np array [count of nodes with deg = i] 
    """
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(graph, DegToCntV)
    counts = [(item.GetVal1(), item.GetVal2())
              for item in DegToCntV]  # [(deg, count)
    max_deg = max(x[0] for x in counts)
    ans = np.zeros(max_deg)
    for d, c in counts:
        ans[d - 1] = c
    return ans
Beispiel #19
0
def getDataPointsToPlot(Graph):
  """
  :param - Graph: snap.PUNGraph object representing an undirected graph

  return values:
  X: list of degrees
  Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
  """
  degreeDistribution = snap.TIntPrV()
  snap.GetDegCnt(Graph, degreeDistribution)
  N = float(Graph.GetNodes())
  X, Y = [], []
  for item in degreeDistribution:
    X.append(item.GetVal1())
    Y.append(float(item.GetVal2()) / N)
  return X, Y
def getDataPointsToPlot(Graph):
    """
    :param - Graph: snap.PUNGraph object representing an undirected graph

    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    X, Y = [], []
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)
    for p in DegToCntV:
        X.append(p.GetVal1())
        Y.append(p.GetVal2())
    ############################################################################
    return X, Y
Beispiel #21
0
def calcExpectedDegree(Graph):
    """
    :param Graph - snap.PUNGraph object representing an undirected graph

    return type: float
    return: expected degree of Graph
    """
    ############################################################################
    # TODO: Your code here!
    ed = 0.0
    degreeDistribution = snap.TIntPrV()
    snap.GetDegCnt(Graph, degreeDistribution)
    N = float(Graph.GetNodes())
    for item in degreeDistribution:
        ed += float(item.GetVal1()) * float(item.GetVal2()) / N
    ############################################################################
    return ed
Beispiel #22
0
def getDataPointsToPlot(Graph):
    """
    :param - Graph: snap.PUNGraph object representing an undirected graph

    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)
    N = Graph.GetNodes()

    X, Y = [item.GetVal1() for item in DegToCntV
            ], [np.true_divide(item.GetVal2(), N) for item in DegToCntV]

    ############################################################################
    return X, Y
Beispiel #23
0
def degreeDistribution(G):
    print "Nodes number: ", G.GetNodes()  # 37444
    print "Edges number: ", G.GetEdges()  # 561119
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    X = []
    Y = []
    for item in DegToCntV:
        X.append(item.GetVal1())
        Y.append(item.GetVal2())
    plt.loglog(X,
               Y,
               linestyle='dotted',
               color='b',
               label='Collaboration Network')
    plt.ylabel('Number of Users')
    plt.xlabel('Number of Neighbors (degree)')
    plt.show()
Beispiel #24
0
    def form_degree_distribution(self, normalized=False):
        """
        sets self._degree_distribution as a dict of degrees to count of nodes with that degree.
        :return: the histogram dict described above.
        """
        data = snap.TIntPrV()
        snap.GetDegCnt(self._graph, data)

        histogram = {}
        for item in data:
            histogram[item.GetVal1()] = item.GetVal2()

        if normalized:
            total = sum(histogram.values())
            for deg in histogram:
                histogram[deg] = 1.0 * shistogram[deg] / total

        self._degree_distribution = histogram
Beispiel #25
0
def getDataPointsToPlot(Graph):
    """
    :param - Graph: snap.PUNGraph object representing an undirected graph

    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    # TODO: Your code here!
    X, Y = [], []
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)
    for item in DegToCntV:
        X += [item.GetVal1()]
        Y += [item.GetVal2()]
    ############################################################################
    return X, Y
Beispiel #26
0
def getDataPointsToPlot(Graph):
    """
    :param - Graph: snap.PUNGraph object representing an undirected graph

    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    # TODO: Your code here!
    X, Y = [], []
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)

    for item in DegToCntV:
        X.append(item.GetVal1()) #get degree
        Y.append(item.GetVal2()/float(Graph.GetNodes())) #get proportion of nodes with certain degree
    ############################################################################
    return X, Y
Beispiel #27
0
def getDataPointsToPlot(Graph):
    """
    :param - Graph: snap.PUNGraph object representing an undirected graph

    return values:
    X: list of degrees
    Y: list of frequencies: Y[i] = fraction of nodes with degree X[i]
    """
    ############################################################################
    # TODO: Your code here!
    N = Graph.GetNodes()
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(Graph, DegToCntV)
    X, Y = [], []
    for item in DegToCntV:
        #print "%d nodes with degree %d" % (item.GetVal2(), item.GetVal1())
        X.append(item.GetVal1())
        Y.append(item.GetVal2() * 1.0 / N)
    ############################################################################
    return X, Y
Beispiel #28
0
def plotDegDistr(G):
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(G, DegToCntV)
    numNodes = G.GetNodes()
    print numNodes
    deg = []
    nodes = []
    tups = []
    for item in DegToCntV:
        if item.GetVal1() == 0:
            numNodes -= item.GetVal2()
            continue
        deg.append(item.GetVal1())
        nodes.append(item.GetVal2())  #float(item.GetVal2()) / float(numNodes))
        tups.append((item.GetVal1(), float(item.GetVal2())))
    pdf = []
    for tup in tups:
        pdf.append((tup[0], tup[1] / float(numNodes)))
    pdf.sort(key=lambda x: x[0])
    print pdf[:10]

    alpha = mleA(deg, nodes)
    estPDF = getPDF(pdf[1][0], pdf[-1][0], alpha)

    fig = plt.figure()
    ax = plt.gca()
    G1Dots, = ax.plot(deg, [float(x) / float(numNodes) for x in nodes],
                      'o',
                      c='blue',
                      alpha=0.75,
                      markeredgecolor='none')
    PDFDots, = ax.plot(range(pdf[1][0] - 1, pdf[-1][0]),
                       estPDF,
                       c='green',
                       alpha=0.75,
                       markeredgecolor='none')
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.set_ylabel('Proportion of Users')
    ax.set_xlabel('Number of Friends')
    plt.show()
Beispiel #29
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 #30
0
def userDeg(userGraph):
    # Degree distribution of userGraph
    userNum = userGraph.GetNodes()
    degrees = list()
    counts = list()
    DegToCntV = snap.TIntPrV()
    snap.GetDegCnt(userGraph, DegToCntV)
    for item in DegToCntV:
        degrees.append(item.GetVal1())
        counts.append(item.GetVal2())
    # Normalize the counts
    counts = [value / (1.0 * userNum) for value in counts]

    plt.plot(degrees, counts, color="#fa8072")
    plt.xscale('log')
    plt.yscale('log')
    plt.title('Degree Distribution for User-User Network')
    plt.xlabel('degree')
    plt.ylabel('frequency')
    plt.savefig('degree-user-user.pdf')
    plt.close()