コード例 #1
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)
コード例 #2
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))
コード例 #3
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))
コード例 #4
0
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
snap.PlotClustCf(Graph1, str3, "The distribution of clustering coefficient")
コード例 #5
0
    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.*")
コード例 #6
0
def graphStructure(elistName, elistPath):
    """
        Calculate properties of the graph as given in the assignment

        Args:
        elistName (str) -> Input elist name
        elistPath (pathlib.Path) -> Input elist using which graph needs to be built

        Return:
        RESULTS (dict) -> Dictionary containing results for different subparts of the assignment
    """

    RESULTS = {}
    subGraph = snap.LoadEdgeList(snap.PUNGraph, elistPath, 0, 1)

    # Part 1 (Size of the network)
    RESULTS['nodeCount'] = subGraph.GetNodes()
    RESULTS['edgeCount'] = subGraph.GetEdges()

    # Part 2 (Degree of nodes in the network)
    maxDegree = 0
    maxDegreeNodes = []
    degree7Count = 0

    for node in subGraph.Nodes():
        if node.GetDeg() == 7:
            degree7Count += 1

        maxDegree = max(maxDegree, node.GetDeg())

    for node in subGraph.Nodes():
        if node.GetDeg() == maxDegree:
            maxDegreeNodes.append(node.GetId())

    plotFilename = f"deg_dist_{elistName}"
    # Since it is an undirected graph, in/out degree is unimportant
    snap.PlotOutDegDistr(subGraph, plotFilename)

    RESULTS['maxDegree'] = maxDegree
    RESULTS['maxDegreeNodes'] = ','.join(map(str, maxDegreeNodes))
    RESULTS['degree7Count'] = degree7Count

    # Part 3 (Paths in the network)
    # Full Diameter Calculation
    fullDiameters = {
        10: snap.GetBfsFullDiam(subGraph, 10, False),
        100: snap.GetBfsFullDiam(subGraph, 100, False),
        1000: snap.GetBfsFullDiam(subGraph, 1000, False)
    }
    fullMean, fullVariance = meanVariance(fullDiameters.values())
    fullDiameters['mean'] = fullMean
    fullDiameters['variance'] = fullVariance
    RESULTS['fullDiameters'] = fullDiameters

    # Effective Diameter Calculation
    effDiameters = {
        10: snap.GetBfsEffDiam(subGraph, 10, False),
        100: snap.GetBfsEffDiam(subGraph, 100, False),
        1000: snap.GetBfsEffDiam(subGraph, 1000, False),
    }
    effMean, effVariance = meanVariance(effDiameters.values())
    effDiameters['mean'] = effMean
    effDiameters['variance'] = effVariance
    RESULTS['effDiameters'] = effDiameters

    plotFilename = f"shortest_path_{elistName}"
    snap.PlotShortPathDistr(subGraph, plotFilename)

    # Part 4 (Components of the network)
    edgeBridges = snap.TIntPrV()
    articulationPoints = snap.TIntV()
    RESULTS['fractionLargestConnected'] = snap.GetMxSccSz(subGraph)
    snap.GetEdgeBridges(subGraph, edgeBridges)
    snap.GetArtPoints(subGraph, articulationPoints)
    RESULTS['edgeBridges'] = len(edgeBridges)
    RESULTS['articulationPoints'] = len(articulationPoints)

    plotFilename = f"connected_comp_{elistName}"
    snap.PlotSccDistr(subGraph, plotFilename)

    # Part 5 (Connectivity and clustering in the network)
    RESULTS['avgClusterCoefficient'] = snap.GetClustCf(subGraph, -1)
    RESULTS['triadCount'] = snap.GetTriadsAll(subGraph, -1)[0]

    nodeX = subGraph.GetRndNId(Rnd)
    nodeY = subGraph.GetRndNId(Rnd)
    RESULTS['randomClusterCoefficient'] = (nodeX,
                                           snap.GetNodeClustCf(
                                               subGraph, nodeX))
    RESULTS['randomNodeTriads'] = (nodeY, snap.GetNodeTriads(subGraph, nodeY))
    RESULTS['edgesTriads'] = snap.GetTriadEdges(subGraph)

    plotFilename = f"clustering_coeff_{elistName}"
    snap.PlotClustCf(subGraph, plotFilename)

    return RESULTS
コード例 #7
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)
コード例 #8
0
    # Number of node Triads of a random node
    print "Number of triads of random node  " + str(
        RandNode3) + "  participates in email-Enron-subgraph: " + str(
            snap.GetNodeTriads(email_enron_subgraph, RandNode3))
if (sub_graph_name == "p2p-Gnutella04-subgraph"):
    # Number of node Triads of a random node
    print "Number of triads of random node  " + str(
        RandNode4) + "  participates in p2p-Gnutella04-subgraph: " + str(
            snap.GetNodeTriads(p2p_gnutella04_subgraph, RandNode4))

# Task 1.2.5.5

if (sub_graph_name == "soc-Epinions1-subgraph"):
    # Calculating no  of edges in particular triads
    print "Number of edges that participate in at least one triad in soc-Epinions1-subgraph : " + str(
        snap.GetTriadEdges(soc_epinions1_subgraph))
if (sub_graph_name == "cit-HepPh-subgraph"):
    # Calculating no  of edges in particular triads
    print "Number of edges that participate in at least one triad in cit-HepPh-subgraph : " + str(
        snap.GetTriadEdges(cit_heph_subgraph))
if (sub_graph_name == "email-Enron-subgraph"):
    # Calculating no  of edges in particular triads
    print "Number of edges that participate in at least one triad in email-Enron-subgraph : " + str(
        snap.GetTriadEdges(email_enron_subgraph))
if (sub_graph_name == "p2p-Gnutella04-subgraph"):
    # Calculating no  of edges in particular triads
    print "Number of edges that participate in at least one triad in p2p-Gnutella04-subgraph : " + str(
        snap.GetTriadEdges(p2p_gnutella04_subgraph))

# Task 1.2.5.6
if (sub_graph_name == "soc-Epinions1-subgraph"):
コード例 #9
0
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))

cf_dist = snap.TFltPrV()
coeff = snap.GetClustCf(G, cf_dist, -1)
degree_coeff = {}
for pair in cf_dist:
    degree_coeff[pair.GetVal1()] = pair.GetVal2()

# Plot Degree Distribution
plot_filename = 'clustering_coeff_' + graph_filename[:-6] + '.png'
plot_filedir = os.path.join(plotpath, plot_filename)
plt.figure()
plt.scatter(list(degree_coeff.keys()), list(degree_coeff.values()), s=10)
plt.xlabel("Degree")
コード例 #10
0
        d[Community].append(NI)
        NodeIdList.Add(NI)

    SubGraph = snap.GetSubGraph(G1, NodeIdList)
    diam = snap.GetBfsFullDiam(SubGraph, 100, False)
    print "The diametre is %d" % diam
    DegToCCfV = snap.TFltPrV()
    DegList = list()
    result = snap.GetClustCfAll(SubGraph, DegToCCfV)
    for item in DegToCCfV:
        DegList.append(item.GetVal1())
        #print "degree: %d, clustering coefficient: %f" % (item.GetVal1(), item.GetVal2())
    print "average clustering coefficient", result[0]
    print "closed triads", result[1]
    print "open triads", result[2]
    NumTriadEdges = snap.GetTriadEdges(SubGraph)
    print "The number of TriadEdges is %d" % NumTriadEdges
    CountEdges = snap.CntUniqUndirEdges(SubGraph)
    print "Directed Graph: Count of undirected edges is %d" % CountEdges
    CountNodes = SubGraph.GetNodes()
    InternalDensity = CountEdges / (CountNodes * (CountNodes - 1) * 0.5)
    print "The Internal Density is %f" % InternalDensity
    AvgDeg = 2 * CountEdges / CountNodes
    print "The Average Degree is %f" % AvgDeg
    MedianDegree = np.median(DegList)
    print "The Median Degree is %d" % MedianDegree
    TPR = NumTriadEdges / CountEdges
    print "The Triangle Participation Rate is %d" % TPR
    NodeIdList.Clr()
print "The modularity of the network is %f" % modularity
コード例 #11
0
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(
    os.path.join(dirname, "diam.S_" + sys.argv[1] + ".png"),
    os.path.join(dirname, "plots", "shortest_path_" + sys.argv[1] + ".png"))

snap.PlotSccDistr(G, "C_" + sys.argv[1], "Component Size Distribution")
MoveFile(
    os.path.join(dirname, "scc.C_" + sys.argv[1] + ".png"),
    os.path.join(dirname, "plots", "connected_comp_" + sys.argv[1] + ".png"))
コード例 #12
0
    ## Connected Components Distribution
    sn.PlotSccDistr(graph, name, "Connected Component Distribution")
    plotRemove("scc", "connected_comp", name)

    #Question 5

    ## Clustering Coefficient
    print("Average clustering coefficient: {:0.4f}".format(
        sn.GetClustCf(graph)))

    ## Triads
    print("Number of triads: {}".format(sn.GetTriads(graph)))

    ## Random Clustering Coefficient
    rndNode = graph.GetRndNId()
    print("Clustering coefficient of random node {}: {:0.4f}".format(
        rndNode, sn.GetNodeClustCf(graph, rndNode)))

    ## Random node triads
    rndNode = graph.GetRndNId()
    print("Number of triads random node {} participates: {}".format(
        rndNode, sn.GetNodeTriads(graph, rndNode)))

    ## Edges in Triads
    print("Number of edges that participate in at least one triad: {}".format(
        sn.GetTriadEdges(graph)))

    ## Plot Clustering Coefficient
    sn.PlotClustCf(graph, name, "Clustering Coefficient Distribution")
    plotRemove("ccf", "clustering_coeff", name)
コード例 #13
0
def ratioTriadEdges(G):
    return snap.GetTriadEdges(G) / float(G.GetEdges())