def hubs_and_authorities_score(self, MaxIter=20):
        '''
        Computes the Hubs and Authorities score of every node in Graph

        return tuple of hubs score and authorrities score
        :param MaxIter: Maximum number of iterations.
        
        '''
        snap = self.snap

        ret1 = []
        ret2 = []
        NIdHubH = snap.TIntFlt64H()
        NIdAuthH = snap.TIntFlt64H()
        snap.GetHits(self.graph, NIdHubH, NIdAuthH, MaxIter)
        for item in NIdHubH:
            ret1.append((item, NIdHubH[item]))
        for item in NIdAuthH:
            ret2.append((item, NIdAuthH[item]))

        return ret1, ret2
    def page_rank_score(self, C=0.85, Eps=1e-4, MaxIter=100):
        '''
        Computes the PageRank score of every node in Graph

        :param C: Damping factor.
        :param Eps: Convergence difference.
        :param MaxIter: Maximum number of iterations.
        
        '''
        snap = self.snap

        ret = []
        PRankH = snap.TIntFlt64H()
        snap.GetPageRank(self.graph, PRankH, C, Eps, MaxIter)
        for item in PRankH:
            ret.append((item, PRankH[item]))

        return ret
    def eigenvector_centrality(self, Eps=1e-4, MaxIter=100):
        '''
        Computes eigenvector centrality of all nodes in Graph. 
        Eigenvector Centrality of a node N is defined recursively as the average of centrality values of N’s neighbors in the network.
        
        :param Eps: Convergence difference.
        :param MaxIter: Maximum number of iterations.
        
        '''
        snap = self.snap

        ret = []

        NIdEigenH = snap.TIntFlt64H()
        snap.GetEigenVectorCentr(self.graph, NIdEigenH, Eps, MaxIter)
        for item in NIdEigenH:
            ret.append((item, NIdEigenH[item]))

        return ret
    def sample_betweenness_centrality(self, quality=1, isDir=False):
        '''
         Computes (approximate) Node and Edge Betweenness Centrality based on a sample
        
        :param quality: Quality of the approximation. 1.0 gives exact betweenness values.

        :param isDir: consider direct or not
        
        '''
        snap = self.snap
        Nodes = snap.TIntFlt64H()
        Edges = snap.TIntPrFlt64H()
        snap.GetBetweennessCentr(self.graph, Nodes, Edges, float(quality),
                                 isDir)
        node_btwn = []
        edge_btwn = []
        for node in Nodes:
            node_btwn.append(Nodes[node])
        for edge in Edges:
            edge_btwn.append(Edges[edge])
        return (node_btwn, edge_btwn)
Пример #5
0
import snap

data = snap.LoadEdgeList(snap.PNGraph, "stackoverflow-Java.txt", 0, 1, '\t')

# The number of weakly connected components in the network.
Components = snap.TCnComV()
snap.GetWccs(data, Components)
print("Number of Weakly Connected Components:", Components.Len())

# The number of edges and the number of nodes in the largest weakly connected component
MxWcc = snap.GetMxWcc(data)
print("Number of MxWcc Edges:", MxWcc.GetEdges())
print("Number of MxWcc Nodes:", MxWcc.GetNodes())

# IDs of the top 3 most central nodes in the network by PagePank scores
PRankH = snap.TIntFlt64H()
snap.GetPageRank(data, PRankH)
PRankH.SortByDat(False)

i = 0
itr = PRankH.BegI()
print("The top 3 most central nodes in the network by PagePank scores:")
while i < 3:
    print("Node:", itr.GetKey())
    itr.Next()
    i += 1
print("")

# IDs of the top 3 hubs and top 3 authorities in the network by HITS scores.
NIdHubH = snap.TIntFlt64H()
NIdAuthH = snap.TIntFlt64H()
Пример #6
0
def all_node_centrality(g, centralityFunc):
    centralities = snap.TIntFlt64H()
    for node in g.Nodes():
        centralities[node.GetId()] = centralityFunc(g, node.GetId())
    return centralities
Пример #7
0
#g_outdeg is a vector of pairs of floats. Each pair is addressed like (Val1,Val2)
outdeg_gt_10 = list(filter(lambda x: x.GetVal2() > 10, g_outdeg))
indeg_gt_10 = list(filter(lambda x: x.GetVal2() > 10, g_indeg))
print(f'Nodes with outdegree > 10: {len(outdeg_gt_10)}')
print(f'Nodes with indegree > 10: {len(indeg_gt_10)}')
#Problem 2
so = snap.LoadEdgeList(snap.PNGraph, "stackoverflow-Java.txt")
#2.1
so_wcc = snap.TCnComV()
snap.GetWccs(so, so_wcc)
print(f'# of connected components: {len(so_wcc)}')
#2.2
so_mx_wcc = snap.GetMxWcc(so)
snap.PrintInfo(so_mx_wcc, "Largest connected component of StackOverflow-Java")
#2.3
so_pr = snap.TIntFlt64H()
snap.GetPageRank(so, so_pr)
so_pr.SortByDat(False)  #Ascending=False
#The code below might be a naive way to do it
#Mb try GetKeyV() for hashtable types then just grab top 3 elements
so_pr_ordered = []
so_pr_iter = so_pr.BegI()
while not so_pr_iter.IsEnd():
    so_pr_ordered.append((
        so_pr_iter.GetKey(),
        so_pr_iter.GetDat(),
    ))
    so_pr_iter.Next()
print("Top 3 nodes by PageRank (nodeId,PageRank):")
for kv_pair in so_pr_ordered[0:3]:
    print(kv_pair)
Пример #8
0
import snap

Graph = snap.GenRndGnm(snap.PNGraph, 100, 1000)
PRankH = snap.TIntFlt64H()
snap.GetPageRank(Graph, PRankH)
for item in PRankH:
    print item, PRankH[item]

Graph = snap.GenRndGnm(snap.PUNGraph, 5000000, 50000000)
PRankH = snap.TIntFlt64H()
snap.GetPageRank(Graph, PRankH)
for item in PRankH:
    print item, PRankH[item]

Graph = snap.GenRndGnm(snap.PNEANet, 100, 1000)
PRankH = snap.TIntFlt64H()
snap.GetPageRank(Graph, PRankH)
for item in PRankH:
    print item, PRankH[item]

Пример #9
0
import snap

Graph = snap.GenRndGnm(snap.PNGraph, 100, 1000)
Nodes = snap.TIntFlt64H()
Edges = snap.TIntPrFlt64H()
snap.GetBetweennessCentr(Graph, Nodes, Edges, 1.0)
for node in Nodes:
    print("node: %d centrality: %f" % (node, Nodes[node]))
for edge in Edges:
    print("edge: (%d, %d) centrality: %f" %
          (edge.GetVal1(), edge.GetVal2(), Edges[edge]))

UGraph = snap.GenRndGnm(snap.PUNGraph, 100, 1000)
Nodes = snap.TIntFlt64H()
Edges = snap.TIntPrFlt64H()
snap.GetBetweennessCentr(UGraph, Nodes, Edges, 1.0)
for node in Nodes:
    print("node: %d centrality: %f" % (node, Nodes[node]))
for edge in Edges:
    print("edge: (%d, %d) centrality: %f" %
          (edge.GetVal1(), edge.GetVal2(), Edges[edge]))

Network = snap.GenRndGnm(snap.PNEANet, 100, 1000)
Nodes = snap.TIntFlt64H()
Edges = snap.TIntPrFlt64H()
snap.GetBetweennessCentr(Network, Nodes, Edges, 1.0)
for node in Nodes:
    print("node: %d centrality: %f" % (node, Nodes[node]))
for edge in Edges:
    print("edge: (%d, %d) centrality: %f" %
          (edge.GetVal1(), edge.GetVal2(), Edges[edge]))