def get_graph_parameters(file_name):
    '''
    11 months
    4 parameters: density ,assortativity,clustering coefficient, , number of triads
    
    '''
    params = np.zeros([11, 4])
    with open(file_name) as fp:
        month_nets = json.load(fp)

    for ii in range(11):
        network = month_nets[ii]
        keys = network.keys()
        usr_dict = get_dict(network)

        G = nx.DiGraph()

        for i in range(len(usr_dict)):
            G.add_node(i)

        for i in range(len(network)):
            ls = network[keys[i]]
            for j in range(len(ls)):
                G.add_edge(usr_dict[keys[i]], usr_dict[ls[j]])

        e = G.number_of_edges()
        v = G.number_of_nodes()

        density = e / float((v * (v - 1)))
        assort = nx.algorithms.assortativity.degree_assortativity_coefficient(
            G)
        cc = nx.algorithms.average_clustering(G)

        Gb = snap.TNGraph.New()
        keys_b = usr_dict.keys()

        for i in range(len(usr_dict)):
            Gb.AddNode(i)

        for i in range(len(network)):
            ls = network[keys[i]]
            for j in range(len(ls)):
                Gb.AddEdge(usr_dict[keys[i]], usr_dict[ls[j]])

        triads = snap.GetTriads(Gb)

        params[ii, 0] = density
        params[ii, 1] = assort
        params[ii, 2] = cc
        params[ii, 3] = triads

    return params
    #%%

    params_r = {}

    for i in range(n):
        print(i)
        params_r[files[i]] = get_graph_parameters(dir_c + files[i])
Ejemplo n.º 2
0
def clustering_coefficient():
    # Count triads
    Triads = snap.GetTriads(G)
    print("# of triads", Triads)

    # Calculate clustering coefficient
    CC = snap.GetClustCf(G)
    print("clustering coefficient", CC)
Ejemplo n.º 3
0
def print_info(graph):
    for NI in graph.Nodes():
        print("node: %d, out-degree %d, in-degree %d" %
              (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
    print("Number of nodes: ", graph.GetNodes())
    print("Number of edges: ", graph.GetEdges())
    print("Maximum degree: ", graph.GetNI(snap.GetMxDegNId(graph)).GetDeg())
    print("Diameter (approximate): ", snap.GetBfsFullDiam(graph, 10))
    print("Triangles: ", snap.GetTriads(graph))
    print("Clustering coefficient: ", snap.GetClustCf(graph))
Ejemplo n.º 4
0
def getBasicProps(graph):
	# Assuming unweighted undirected graph
	return {
		'num_nodes': graph.GetNodes(),
		'num_edges': graph.GetEdges(),
		'avg_deg': 2. * graph.GetEdges() / graph.GetNodes(),
		'gamma': getGamma(graph),
		'deg_centr': getDegCentr(graph),
		'num_tris': snap.GetTriads(graph, -1),
		'global_cc': snap.GetClustCf(graph, -1),
	}
def processNetwork(Graph, id_to_groups):
    with open("../../data/fastinf_graph_noweights_features.txt", "w+") as f:
        f.write("RELATED GROUPS GRAPH:\n")
        f.write('Edges: %d\n' % Graph.GetEdges())
        f.write('Nodes: %d\n\n' % Graph.GetNodes())

        MxWcc = snap.GetMxWcc(Graph)
        f.write("MAX WCC:\n")
        f.write('Edges: %f ' % MxWcc.GetEdges())
        f.write('Nodes: %f \n' % MxWcc.GetNodes())
        f.write('Node List: ')
        for node in MxWcc.Nodes():
            f.write('%d, ' % node.GetId())
        f.write('\n')
        for node in MxWcc.Nodes():
            f.write('%s, ' % id_to_groups[node.GetId()])

        f.write("\n\nALL WCCs:")
        Components = snap.TCnComV()
        snap.GetWccs(Graph, Components)
        for i, CnCom in enumerate(Components):
            if CnCom.Len() < 10: continue
            f.write('\nWcc%d: ' % i)
            for nodeid in CnCom:
                f.write('%d, ' % nodeid)

        MxScc = snap.GetMxScc(Graph)
        f.write("\n\nMAX SCC:\n")
        f.write('Edges: %f ' % MxScc.GetEdges())
        f.write('Nodes: %f \n' % MxScc.GetNodes())
        f.write('Node List: ')
        for node in MxScc.Nodes():
            f.write('%d, ' % node.GetId())
        f.write('\n')
        for node in MxScc.Nodes():
            f.write('%s, ' % id_to_groups[node.GetId()])

        f.write("\n\nALL SCCs:")
        Components = snap.TCnComV()
        snap.GetSccs(Graph, Components)
        for i, CnCom in enumerate(Components):
            if CnCom.Len() < 10: continue
            f.write('\nScc%d: ' % i)
            for nodeid in CnCom:
                f.write('%d, ' % nodeid)

        f.write('\n\nCLUSTERING AND COMMUNITIES:\n')
        f.write('Clustering coefficient: %f\n' % snap.GetClustCf(Graph, -1))
        f.write('Num Triads: %d\n' % snap.GetTriads(Graph, -1))
        Nodes = snap.TIntV()
        for node in Graph.Nodes():
            Nodes.Add(node.GetId())
        f.write('Modularity: %f' % snap.GetModularity(Graph, Nodes))
Ejemplo n.º 6
0
def print_number_of_traids_participated_by_random_node(G, GName):

    TriadV = snap.TIntTrV()
    snap.GetTriads(G, TriadV)
    random_traid_node_index = snap.TInt.GetRnd(len(TriadV))
    counter = 0
    for triple in TriadV:
        if counter == random_traid_node_index:
            print "Number of traids of random node {0} participates in {1}: {2}".format(
                triple.Val1(), GName[:-10], triple.Val2())
            break
        counter = counter + 1

    number_of_edges_in_traids = snap.GetTriadEdges(G)
    print "Number of edges that participate in at least one triad in {0}: {1}".format(
        GName[:-10], number_of_edges_in_traids)
Ejemplo n.º 7
0
def print_connectivity_clustering(G):
    """
    Prints the average clustering coefficient, number of triads in subgraph G
    Also prints clustering coefficient and number of triads for random nodes
    Also prints the number of edges that participate in at least one triad
    """

    GraphClustCoeff = snap.GetClustCf(G)
    print("Average clustering coefficient:", round(GraphClustCoeff, 4))

    print("Number of triads:", snap.GetTriads(G))

    NId = G.GetRndNId()
    print(f'Clustering coefficient of random node {NId}:', round(snap.GetNodeClustCf(G, NId)))

    NId = G.GetRndNId()
    print(f'Number of triads random node {NId} participates:', snap.GetNodeTriads(G, NId))

    print('Number of edges that participate in at least one triad:', snap.GetTriadEdges(G))
Ejemplo n.º 8
0
def getUniqueTriads(G):
    '''
  :param - G: graph

  return type: Iterator, triplets identifying unique triads (only one set per triad)
  return: Return all triads in G.
  '''
    seenTriads = {}
    for node in G.Nodes():
        neighbors = [G.GetNI(node.GetNbrNId(i)) for i in xrange(node.GetDeg())]
        for neighbor in neighbors:
            for i in xrange(neighbor.GetDeg()):
                candidate = neighbor.GetNbrNId(i)
                if node.IsNbrNId(candidate):
                    triad = tuple(
                        sorted([node.GetId(),
                                neighbor.GetId(), candidate]))
                    if triad not in seenTriads:
                        seenTriads[triad] = True
                        yield triad
    assert len(seenTriads) == snap.GetTriads(G)
Ejemplo n.º 9
0
def computeTriadCounts(G, signs):
    """
  :param - G: graph
  :param - signs: Dictionary of signs (key = node pair (a,b), value = sign)

  return type: List, each position representing count of t0, t1, t2, and t3, respectively.
  return: Return the counts for t0, t1, t2, and t3 triad types. Count each triad
  only once and do not count self edges.
  """

    # each position represents count of t0, t1, t2, t3, respectively
    triad_count = [0, 0, 0, 0]

    ############################################################################
    mapping = {-1: 0, 1: 1}
    for triad in getUniqueTriads(G):
        index = sum(
            [mapping[signs[(triad[i], triad[i + 1])]] for i in range(-1, 2)])
        triad_count[index] += 1
    assert snap.GetTriads(G) == sum(triad_count)
    ############################################################################

    return triad_count
Ejemplo n.º 10
0
    def calculate(self):
        """
        Calculates the metrics on the network using both networkx and snap
        :return: self
        """
        self.calculate_basic()

        print "calculating clustering coefficient."
        self.clustering_coeff = snap.GetClustCf(self.g_snap, -1)

        print "calculating transitivity"
        self.transitivity = nx.transitivity(self.g_nx)

        print "calculating triads"
        self.num_triads = snap.GetTriads(self.g_snap, -1)

        print "calculating diameter"
        self.diameter = snap.GetBfsFullDiam(self.g_snap, 150, False)

        print "calculating spl"
        self.avg_spl = self.calculate_spl()

        return self
Ejemplo n.º 11
0
e = int(e * p)
SumDeg = 0
#problem1
#1
Graph = snap.GenRndGnm(snap.PUNGraph, n, e)

InDegV = snap.TIntPrV()
snap.GetNodeInDegV(Graph, InDegV)

for item in InDegV:
    SumDeg += item.Val2()
MeanDeg = SumDeg / n
a = pow(MeanDeg, 3) / 6

TriadV = snap.TIntTrV()
snap.GetTriads(Graph, TriadV, -1)
OpenTriads = 0
ClosedTriads = 0

for triple in TriadV:
    OpenTriads += triple.Val3()
    ClosedTriads += triple.Val2()

ClosedTriads = ClosedTriads / 3
TotalTriads = (3.0 * ClosedTriads) + OpenTriads
GlobalClcf = (3.0 * ClosedTriads) / TotalTriads

print 'number of nodes:', n
print 'number of edges', e
print 'number of open tri %d closed tri %d total tri %d' % (
    OpenTriads, ClosedTriads, TotalTriads)
Ejemplo n.º 12
0
    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)
nr = 0
for f in EigV:
    nr += 1
    print "%d: %.6f" % (nr, f)

# get an approximation of graph diameter
diam = snap.GetBfsFullDiam(G10, 10)
print "diam", diam

# count the number of triads:
triads = snap.GetTriads(G10)
print "triads", triads

# get the clustering coefficient
cf = snap.GetClustCf(G10)
print "cf", cf
Ejemplo n.º 13
0
from snap import *
import snap

import sys

# create a graph PUNGraph (undirected graph)
G1 = TUNGraph.New()

# read in vertices
numVertices = 0
textFile = open('vertices_' + str(sys.argv[1]) + '.txt')
lines = textFile.readlines()
for line in lines:
    stripped_line = line.rstrip('\n')
    G1.AddNode(int(stripped_line))
    numVertices = numVertices + 1

# read in edges
with open('edges_' + str(sys.argv[1]) + '.txt') as f:
    for line in f:
        int_list = [int(i) for i in line.split()]
        G1.AddEdge(int_list[0],int_list[1])

NumTriads = snap.GetTriads(G1, numVertices)

strValue = str.format("{0:.10f}", NumTriads)

f = open('triangles_' + str(sys.argv[1]) + '.txt', 'w')
f.write(str(strValue))

Ejemplo n.º 14
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)
Ejemplo n.º 15
0
InDegV = snap.TIntPrV()
snap.GetNodeInDegV(Graph, InDegV)

for item in InDegV:
    SumDeg += item.Val2()
MeanDeg = SumDeg / n
#Main Graph
print "------Original Graph-------"
#nodes
n = Graph.GetNodes()
print "Nodes:", n
#edges
print "Edges:", Graph.GetEdges()
#triads
NumTriads = snap.GetTriads(Graph, -1)
print "Number of triads: " + str(NumTriads)
#params to generate graph
print "Params to graph: Original Graph"
#avg path length
alist = list()
i = 0
for node in Graph.Nodes():
    avg = 0.0
    ndh = snap.TIntH()
    snap.GetShortPath(Graph, node.GetId(), ndh)
    for item in ndh:
        avg = avg + ndh[item]
    alist.append(avg / len(ndh))
print "Avg path length", float(sum(alist)) / len(alist)
#diameter
Ejemplo n.º 16
0
import snap
from IPython.display import Image

## Data Types

G = snap.LoadEdgeList(snap.PNGraph, "../../RepositoryData/data/cit-HepTh.txt")

## Get node degrees
CntV = snap.TIntPrV()
snap.GetOutDegCnt(G, CntV)
for p in CntV:
    print("degree %d: count %d" % (p.GetVal1(), p.GetVal2()))


print(snap.GetClustCf(G)) # clustering coefficient
print(snap.GetTriads(G))# diameter
print(snap.GetBfsFullDiam(G, 10))

## Betweenness centrality
Nodes = snap.TIntFltH()
Edges = snap.TIntPrFltH()
snap.GetBetweennessCentr(G, 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]))


Graph = snap.GenRndGnm(snap.PNGraph, 10, 20)
Nodes = snap.TIntFltH()
Edges = snap.TIntPrFltH()
Ejemplo n.º 17
0
plot_filename = 'connected comp_' + graph_filename[:-6] + '.png'
plot_filedir = os.path.join(plotpath, plot_filename)
plt.figure()
plt.scatter(list(connected_component.keys()),
            list(connected_component.values()),
            s=15)
plt.xlabel("Size of Connected Components")
plt.ylabel("Number of components")
plt.title("Connected Component Distribution ({})".format(graph_filename[:-6]))
plt.savefig(plot_filedir)

# [5] Connectivity and Clustering in the Network
cluster_coeff = snap.GetClustCf(G, -1)
print("Average clustering coefficient: {}".format(round(cluster_coeff, 4)))

num_triads = snap.GetTriads(G, -1)
print("Number of triads: {}".format(num_triads))

node_id = G.GetRndNId(Rnd)
node_cluster_coeff = snap.GetNodeClustCf(G, node_id)
print("Clustering coefficient of random node {}: {}".format(
    node_id, round(node_cluster_coeff, 4)))

node_id = G.GetRndNId(Rnd)
node_num_triads = snap.GetNodeTriads(G, node_id)
print("Number of triads random node {} participates: {}".format(
    node_id, node_num_triads))

triad_edge = snap.GetTriadEdges(G)
print("Number of edges that participate in at least one triad: {}".format(
    triad_edge))
Ejemplo n.º 18
0
for item in InDegV:
    SumDeg += item.Val2()
MeanDeg = SumDeg / n

BAGraph = snap.LoadEdgeList(snap.PUNGraph, 'imdb_actor_ba.elist.txt', 0, 1)
#BA Graph
print ""
print "------BA Graph-------"
#nodes
n = BAGraph.GetNodes()
print "Nodes:", n
#edges
print "Edges:", BAGraph.GetEdges()
#triads
NumTriads = snap.GetTriads(BAGraph, -1)
print "Number of triads: " + str(NumTriads)
#params to generate graph
print "Params to graph: Mean node degree k", MeanDeg
#avg path length
alist = list()
i = 0
for node in BAGraph.Nodes():
    avg = 0.0
    ndh = snap.TIntH()
    snap.GetShortPath(BAGraph, node.GetId(), ndh)
    for item in ndh:
        avg = avg + ndh[item]
    alist.append(avg / len(ndh))

print "Avg path length", float(sum(alist)) / len(alist)
Ejemplo n.º 19
0
print("Approximate effective diameter by sampling 1000 nodes: %.4f" %
      dEff_1000)
print("Approximate effective diameter (mean and variance): %.4f, %.4f" %
      (m_dEff, v_dEff))

print("Fraction of nodes in largest connected component: %.4f" %
      snap.GetMxSccSz(G))

EdgeV = snap.TIntPrV()
snap.GetEdgeBridges(G, EdgeV)
print("Number of edge bridges:", len(EdgeV))
ArtNIdV = snap.TIntV()
snap.GetArtPoints(G, ArtNIdV)
print("Number of articulation points:", len(ArtNIdV))
print("Average clustering coefficient: %.4f" % snap.GetClustCf(G, -1))
print("Number of triads:", snap.GetTriads(G, -1))
Ran_n = G.GetRndNId(Rnd)
print("Clustering coefficient of random node %d: %.4f" %
      (Ran_n, snap.GetNodeClustCf(G, Ran_n)))
Ran_n = G.GetRndNId(Rnd)
print("Number of triads random node %d participates: %d" %
      (Ran_n, snap.GetNodeTriads(G, Ran_n)))
print("Number of edges that participate in at least one triad:",
      snap.GetTriadEdges(G))

snap.PlotInDegDistr(G, "D_" + sys.argv[1], "Degree Distribution")
MoveFile(os.path.join(dirname, "inDeg.D_" + sys.argv[1] + ".png"),
         os.path.join(dirname, "plots", "deg_dist_" + sys.argv[1] + ".png"))

snap.PlotShortPathDistr(G, "S_" + sys.argv[1], "Shortest path Distribution")
MoveFile(
def triads(graph):
    """Get number of closed and open triads in graph"""
    closed, _, open = snap.GetTriadsAll(graph)
    return snap.GetTriads(graph), closed, open
Ejemplo n.º 21
0
import snap
import sys
import numpy as np
import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as plt

#loading steam-sweden dataset
Graph = snap.LoadEdgeList(snap.PUNGraph, "Steam-Sweden.txt", 0, 1)

#calculating number of triads with random sampling
NumTriads = snap.GetTriads(Graph, -1)
print "Number of triads: " + str(NumTriads)

#selecting  random node
rm_node = Graph.GetRndNId()

#random node clustering coefficient
rm_clus_coeff = snap.GetNodeClustCf(Graph, rm_node)
print "Clustering coefficient of random node ", rm_node, " in Steam-Sweden: ", rm_clus_coeff

#Number of triads a randomly selected node participates in
num_triads = snap.GetNodeTriads(Graph, rm_node)
print "Number of triads of node ", rm_node, " participates in ", num_triads, " triads"

#avg and global clustering coefficient
TriadV = snap.TIntTrV()
snap.GetTriads(Graph, TriadV, -1)
OpenTriads = 0
        round(snap.GetClustCf(cit_heph_subgraph, -1), 4))
if (sub_graph_name == "email-Enron-subgraph"):
    #Compute Average Clustering coeffiecient
    print "Average Clustering coeffiecient in email-Enron-subgraph :" + str(
        round(snap.GetClustCf(email_enron_subgraph, -1), 4))
if (sub_graph_name == "p2p-Gnutella04-subgraph"):
    #Compute Average Clustering coeffiecient
    print "Average Clustering coeffiecient in p2p-Gnutella04-subgraph :" + str(
        round(snap.GetClustCf(p2p_gnutella04_subgraph, -1), 4))

# Task 1.2.5.2

if (sub_graph_name == "soc-Epinions1-subgraph"):
    # Computing no of Triads
    print "Number of Triads in soc-Epinions1-subgraph :" + str(
        snap.GetTriads(soc_epinions1_subgraph, -1))
if (sub_graph_name == "cit-HepPh-subgraph"):
    # Computing no of Triads
    print "Number of Triads in cit-HepPh-subgraph :" + str(
        snap.GetTriads(cit_heph_subgraph, -1))
if (sub_graph_name == "email-Enron-subgraph"):
    # Computing no of Triads
    print "Number of Triads in email-Enron-subgraph :" + str(
        snap.GetTriads(email_enron_subgraph, -1))
if (sub_graph_name == "p2p-Gnutella04-subgraph"):
    # Computing no of Triads
    print "Number of Triads in p2p-Gnutella04-subgraph :" + str(
        snap.GetTriads(p2p_gnutella04_subgraph, -1))

# Task 1.2.5.3
    snap.PlotShortPathDistr(graph, "temp", "Undirected graph - shortest path")
    os.system("mv diam.temp.png plots/shortest_path_" + subgraph_name + ".png")
    os.system("rm diam.*")

    print("Fraction of nodes in largest connected component:",
          round(snap.GetMxSccSz(graph), 4))
    print("Number of edge bridges:", get_bridges(graph).Len())
    print("Number of articulation points:",
          get_articulation_points(graph).Len())

    snap.PlotSccDistr(graph, "temp", "Undirected graph - scc distribution")
    os.system("mv scc.temp.png plots/connected_comp_" + subgraph_name + ".png")
    os.system("rm scc.*")

    print("Average clustering coefficient:", round(snap.GetClustCf(graph), 4))
    print("Number of triads:", snap.GetTriads(graph))
    random_node = graph.GetRndNId()
    print("Clustering coefficient of random node", random_node, ":",
          round(get_each_nodes_ClusteringCofficient(graph)[random_node], 4))
    random_node = graph.GetRndNId()
    print("Number of triads random node", random_node, "participates:",
          snap.GetNodeTriads(graph, random_node))
    print("Number of edges that participate in at least one triad:",
          snap.GetTriadEdges(graph))

    snap.PlotClustCf(graph, "temp",
                     "Undirected graph - clustering coefficient")
    os.system("mv ccf.temp.png plots/clustering_coeff_" + subgraph_name +
              ".png")
    os.system("rm ccf.*")
Ejemplo n.º 24
0
ERGraph = snap.LoadEdgeList(snap.PUNGraph,'imdb_actor_er.elist.txt', 0, 1)


n = Graph.GetNodes()
p = float(Graph.GetEdges())/(n*(n-1)/2)
#ER Graph
print ""
print "------ER Graph-------"
#nodes
n = ERGraph.GetNodes()
print "Nodes:",n
#edges
print "Edges:",ERGraph.GetEdges()
#triads
NumTriads = snap.GetTriads(ERGraph,-1)  
print "Number of triads: "+str(NumTriads)
#params to generate graph
print "Params to graph: probability p = ",p
#avg path length
alist = list()
i=0
for node in ERGraph.Nodes():
	avg = 0.0
	ndh = snap.TIntH()
	snap.GetShortPath(ERGraph,node.GetId(),ndh)
	for item in ndh:
		avg = avg + ndh[item]
	alist.append(avg/len(ndh))

print "Avg path length",float(sum(alist))/len(alist)
edge_bridges = V_edges.Len()
print("Number of edge bridges: ", edge_bridges)

Art_points = snap.TIntV()
snap.GetArtPoints(Graph1, Art_points)
art = Art_points.Len()
print("Number of articulation points: ", art)

str2 = "connected_comp_" + file_name
snap.PlotSccDistr(Graph1, str2,
                  "Distribution of sizes of connected components")

#5.Connectivity and clustering in the network
avg_cc = snap.GetClustCf(Graph1, -1)
print("Average clustering coefficient: %0.4f" % avg_cc)
triads = snap.GetTriads(Graph1, -1)
print("Number of triads: ", triads)

random1 = Graph1.GetRndNId(Rnd)
node_cc = snap.GetNodeClustCf(Graph1, random1)
print("Clustering coefficient of random node %d: %0.4f" % (random1, node_cc))

random2 = Graph1.GetRndNId(Rnd)
node_triads = snap.GetNodeTriads(Graph1, random2)
print("Number of triads random node %d participates: %d" %
      (random2, node_triads))

triad_edges = snap.GetTriadEdges(Graph1, -1)
print("Number of edges that participate in at least one triad: ", triad_edges)

str3 = "clustering_coeff_" + file_name
Ejemplo n.º 26
0
                dataset['keyword']).get_feature_names()
            if re.search('[0-9].....', x) == None
        ]
    print 'Creating node and edge list'
    nx_input = output_network_inputs(id_dict, pack='snap')
    # pp.pprint(nx_input)
    # print_break('Network Graph: NetworkX')
    # G=nx.Graph()
    # G.add_nodes_from(nx_input['nodes'])
    # G.add_edges_from(nx_input['edges'])
    # measures = { 'centrality': nx.degree_centrality(G), 'clustering': nx.clustering(G), 'triads': nx.triangles(G) }
    # pp.pprint(measures)
    print_break('Network Graph: SNAP')
    t0 = time.time()
    G = snap.TUNGraph.New()
    print 'Adding Nodes'
    for i in tqdm(nx_input['nodes']):
        G.AddNode(i)
    print 'Adding Edges'
    for x in tqdm(nx_input['edges']):
        G.AddEdge(x[0], x[1])
    print 'Calculating measures'
    centrality = [snap.GetDegreeCentr(G, n.GetId()) for n in G.Nodes()]
    measures = {
        'centrality': np.mean(centrality),
        'clustering': snap.GetClustCf(G),
        'triads': snap.GetTriads(G)
    }
    pp.pprint(measures)
    print_break('SNAP Graph Measures Time elapsed: %s' % (time.time() - t0))
Ejemplo n.º 27
0
def main():

    parentDir = os.getcwd()
    os.chdir(parentDir + "/subgraphs")
    sub_graph = snap.LoadEdgeList(snap.PUNGraph, sys.argv[1], 0, 1)
    subGraphName = sys.argv[1].split(".")[0]
    os.chdir(parentDir)

    #### 1 ########
    node_count = 0
    for node in sub_graph.Nodes():
        node_count = node_count + 1

    printWithOutNewLine("Number of nodes:", node_count)
    printWithOutNewLine("Number of edges:", snap.CntUniqBiDirEdges(sub_graph))

    #### 2 ########
    printWithOutNewLine("Number of nodes with degree=7:",
                        snap.CntDegNodes(sub_graph, 7))

    rndMaxDegNId = snap.GetMxDegNId(sub_graph)
    nodeDegPairs = snap.TIntPrV()
    snap.GetNodeInDegV(sub_graph, nodeDegPairs)
    maxDegVal = 0

    for pair in nodeDegPairs:
        if (pair.GetVal1() == rndMaxDegNId):
            maxDegVal = pair.GetVal2()
            break

    maxDegNodes = []
    for pair in nodeDegPairs:
        if (pair.GetVal2() == maxDegVal):
            maxDegNodes.append(pair.GetVal1())

    print("Node id(s) with highest degree:", end=" ")
    print(*maxDegNodes, sep=',')

    #### 3 ########
    sampledFullDiam = []
    sampledFullDiam.append(snap.GetBfsFullDiam(sub_graph, 10, False))
    sampledFullDiam.append(snap.GetBfsFullDiam(sub_graph, 100, False))
    sampledFullDiam.append(snap.GetBfsFullDiam(sub_graph, 1000, False))

    sampledFullDiamStats = []
    sampledFullDiamStats.append(round(statistics.mean(sampledFullDiam), 4))
    sampledFullDiamStats.append(round(statistics.variance(sampledFullDiam), 4))

    printWithOutNewLine("Approximate full diameter by sampling 10 nodes:",
                        sampledFullDiam[0])
    printWithOutNewLine("Approximate full diameter by sampling 100 nodes:",
                        sampledFullDiam[1])
    printWithOutNewLine("Approximate full diameter by sampling 1000 nodes:",
                        sampledFullDiam[2])
    print("Approximate full diameter (mean and variance):", end=" ")
    print(*sampledFullDiamStats, sep=',')

    sampledEffDiam = []
    sampledEffDiam.append(round(snap.GetBfsEffDiam(sub_graph, 10, False), 4))
    sampledEffDiam.append(round(snap.GetBfsEffDiam(sub_graph, 100, False), 4))
    sampledEffDiam.append(round(snap.GetBfsEffDiam(sub_graph, 1000, False), 4))

    sampledEffDiamStats = []
    sampledEffDiamStats.append(round(statistics.mean(sampledEffDiam), 4))
    sampledEffDiamStats.append(round(statistics.variance(sampledEffDiam), 4))

    printWithOutNewLine("Approximate effective diameter by sampling 10 nodes:",
                        sampledEffDiam[0])
    printWithOutNewLine(
        "Approximate effective diameter by sampling 100 nodes:",
        sampledEffDiam[1])
    printWithOutNewLine(
        "Approximate effective diameter by sampling 1000 nodes:",
        sampledEffDiam[2])
    print("Approximate effective diameter (mean and variance):", end=" ")
    print(*sampledEffDiamStats, sep=',')

    #### 4 ########
    printWithOutNewLine("Fraction of nodes in largest connected component:",
                        round(snap.GetMxSccSz(sub_graph), 4))

    bridgeEdges = snap.TIntPrV()
    snap.GetEdgeBridges(sub_graph, bridgeEdges)
    printWithOutNewLine("Number of edge bridges:", len(bridgeEdges))

    articulationPoints = snap.TIntV()
    snap.GetArtPoints(sub_graph, articulationPoints)
    printWithOutNewLine("Number of articulation points:",
                        len(articulationPoints))

    #### 5 ########
    printWithOutNewLine("Average clustering coefficient:",
                        round(snap.GetClustCf(sub_graph, -1), 4))

    printWithOutNewLine("Number of triads:", snap.GetTriads(sub_graph, -1))

    randomNodeId = sub_graph.GetRndNId()
    nodeIdCcfMap = snap.TIntFltH()
    snap.GetNodeClustCf(sub_graph, nodeIdCcfMap)

    print("Clustering coefficient of random node", end=" ")
    print(randomNodeId, end=": ")
    print(round(nodeIdCcfMap[randomNodeId], 4))

    print("Number of triads random node", end=" ")
    print(randomNodeId, end=" participates: ")
    print(snap.GetNodeTriads(sub_graph, randomNodeId))

    printWithOutNewLine(
        "Number of edges that participate in at least one triad:",
        snap.GetTriadEdges(sub_graph, -1))

    #### plots ########
    if not os.path.isdir('plots'):
        os.makedirs('plots')

    os.chdir(parentDir + "/plots")
    plotsDir = os.getcwd()

    snap.PlotOutDegDistr(sub_graph, subGraphName,
                         subGraphName + " Subgraph Degree Distribution")
    snap.PlotShortPathDistr(
        sub_graph, subGraphName,
        subGraphName + " Subgraph Shortest Path Lengths Distribution")
    snap.PlotSccDistr(
        sub_graph, subGraphName,
        subGraphName + " Subgraph Connected Components Size Distribution")
    snap.PlotClustCf(
        sub_graph, subGraphName,
        subGraphName + " Subgraph Clustering Coefficient Distribution")

    files = os.listdir(plotsDir)

    for file in files:
        if not file.endswith(".png"):
            os.remove(os.path.join(plotsDir, file))

    plots = os.listdir(plotsDir)
    filePrefix = "filename"
    for file in plots:
        nameSplit = file.split(".")
        if (len(nameSplit) == 2):
            continue
        if (nameSplit[0] == "ccf"):
            filePrefix = "clustering_coeff_"
        elif (nameSplit[0] == "outDeg"):
            filePrefix = "deg_dist_"
        elif (nameSplit[0] == "diam"):
            filePrefix = "shortest_path_"
        elif (nameSplit[0] == "scc"):
            filePrefix = "connected_comp_"

        os.rename(file, filePrefix + nameSplit[1] + "." + nameSplit[2])

    os.chdir(parentDir)
Ejemplo n.º 28
0
import snap

G = snap.LoadEdgeList(snap.PUNGraph, './data/epinion_signed.txt', 0, 1)
print snap.GetTriads(G)
Ejemplo n.º 29
0
#b
EdgeBridgeV = snap.TIntPrV()
snap.GetEdgeBridges(fbsgel, EdgeBridgeV)
print("Number of edge bridges:", len(EdgeBridgeV))
#c
ArtNIdV = snap.TIntV()
snap.GetArtPoints(fbsgel, ArtNIdV)
print("Number of articulation points:", len(ArtNIdV))
#d Plot
snap.PlotSccDistr(fbsgel, "connected_comp_" + str(subgraph_name),
                  "connected_comp_" + str(subgraph_name))

#Q5
#a
print("Average clustering coefficient:", round(snap.GetClustCf(fbsgel, -1), 4))
#b
print("Number of triads:", snap.GetTriads(fbsgel, -1))
#c
RnId = fbsgel.GetRndNId(Rnd)
print("Clustering coefficient of random node " + str(RnId) + ":",
      round(snap.GetNodeClustCf(fbsgel, RnId), 4))
#d
print("Number of triads random node " + str(RnId) + " participates:",
      snap.GetNodeTriads(fbsgel, RnId))
#e
print("Number of edges that participate in at least one triad:",
      snap.GetTriadEdges(fbsgel, -1))
#f Plot
snap.PlotClustCf(fbsgel, "clustering_coeff_" + str(subgraph_name),
                 "clustering_coeff_" + str(subgraph_name))
Ejemplo n.º 30
0
InDegV = snap.TIntPrV()
snap.GetNodeInDegV(Graph, InDegV)

for item in InDegV:
    SumDeg += item.Val2()
MeanDeg = SumDeg / n
#WS Graph
print "------WS Graph-------"
#nodes
n = WSGraph.GetNodes()
print "Nodes:", n
#edges
print "Edges:", WSGraph.GetEdges()
#triads
NumTriads = snap.GetTriads(WSGraph, -1)
print "Number of triads: " + str(NumTriads)
#params to generate graph
print "Params to graph: Mean node degree k", MeanDeg
#avg path length
alist = list()
i = 0
for node in WSGraph.Nodes():
    avg = 0.0
    ndh = snap.TIntH()
    snap.GetShortPath(WSGraph, node.GetId(), ndh)
    for item in ndh:
        avg = avg + ndh[item]
    alist.append(avg / len(ndh))

print "Avg path length", float(sum(alist)) / len(alist)