def number_of_articulation_points(G, GName): ArtNIdV = snap.TIntV() snap.GetArtPoints(G, ArtNIdV) articulation_points_in_graph = len(ArtNIdV) print "Number of articulation points in {0}: {1}".format( GName[:-10], articulation_points_in_graph)
def getArticulationPoints(Graph, node_to_g): ''' A vertex in an undirected connected graph is an articulation point (or cut vertex) iff removing it (and edges through it) disconnects the graph. Articulation points represent vulnerabilities in a connected network – single points whose failure would split the network into 2 or more disconnected components. They are useful for designing reliable networks. ''' prot_to_articulation_points = {} ArtNIdV = snap.TIntV() snap.GetArtPoints(Graph, ArtNIdV) for node in ArtNIdV: my_prot = node_to_g[node] prot_to_articulation_points[my_prot] = True return prot_to_articulation_points
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())
(average, variance)) #c Plot snap.PlotShortPathDistr(fbsgel, "shortest_path_" + str(subgraph_name), "shortest_path_" + str(subgraph_name)) #Q4 #a print("Fraction of nodes in largest connected component:", round(snap.GetMxSccSz(fbsgel), 4)) #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
(effmean, effvar)) str1 = 'shortest_path_' + file_name snap.PlotShortPathDistr(Graph1, str1, "Distribution of shortest path lengths") #4.Components of the network fraction = snap.GetMxSccSz(Graph1) print("Fraction of nodes in largest connected component: %0.4f" % fraction) V_edges = snap.TIntPrV() snap.GetEdgeBridges(Graph1, V_edges) 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)
def get_articulation_points(graph): articulation_points = snap.TIntV() snap.GetArtPoints(graph, articulation_points) return articulation_points
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
def is_articulation_point(G, n): ArtNIdV = snap.TIntV() snap.GetArtPoints(G, ArtNIdV) return n in ArtNIdV
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)
EdgeV = snap.TIntPrV() snap.GetEdgeBridges(p2p_gnutella04_subgraph, EdgeV) edge_bridge = 0 for i in EdgeV: edge_bridge = edge_bridge + 1 print "Number of edge bridges in p2p-Gnutella04-subgraph :" + str( edge_bridge) # Task 1.2.4.3 if (sub_graph_name == "soc-Epinions1-subgraph"): # Calculating no of Articulation points ArtNIdV = snap.TIntV() snap.GetArtPoints(soc_epinions1_subgraph, ArtNIdV) art_point = 0 for NI in ArtNIdV: art_point = art_point + 1 print "Number of articulation points in soc-Epinions1-subgraph :" + str( art_point) if (sub_graph_name == "cit-HepPh-subgraph"): # Calculating no of Articulation points ArtNIdV = snap.TIntV() snap.GetArtPoints(cit_heph_subgraph, ArtNIdV) art_point = 0 for NI in ArtNIdV: art_point = art_point + 1
""" FOR FASTER COMPUTATION, UNCOMMENT THE FOLLOWING LINE AND COMMENT OUT LINE 107-125 """ # snap.PlotShortPathDistr(G, "shortest_path_{}".format(graph_filename[:-6]), "Shortest Path Distribution ({})".format(graph_filename[:-6])) # [4] Components of the network SCC = snap.GetMxScc(G) print("Fraction of nodes in largest connected component: {}".format( round(SCC.GetNodes() / G.GetNodes(), 4))) Edge_Bridge = snap.TIntPrV() snap.GetEdgeBridges(G, Edge_Bridge) print("Number of edge bridges: {}".format(len(Edge_Bridge))) ArticulationPoint = snap.TIntV() snap.GetArtPoints(G, ArticulationPoint) print("Number of articulation points: {}".format(len(ArticulationPoint))) CComp = snap.TIntPrV() snap.GetSccSzCnt(G, CComp) connected_component = {} for comp in CComp: connected_component[comp.GetVal1()] = comp.GetVal2() # Plot Degree Distribution 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)
NIdHubH = snap.TIntFltH() NIdAuthH = snap.TIntFltH() snap.GetHits(G, NIdHubH, NIdAuthH) write(NIdHubH, "hub.txt") write(NIdAuthH, "auth.txt") Nodes = snap.TIntFltH() Edges = snap.TIntPrFltH() snap.GetBetweennessCentr(G, Nodes, Edges, 1.0) write(Nodes, "between.txt") rows = [] for i, node in enumerate(G.Nodes()): if i % 10000 == 0: print "on iteration {}".format(i) nid = node.GetId() ecc = snap.GetNodeEcc(G, nid) clust = snap.GetNodeClustCf(G, nid) rows.append([nid, ecc, clust]) with open(base + "ecc_clust.txt", 'w') as f: for row in rows: f.write(",".join(map(str, row)) + "\n") ArtNIdV = snap.TIntV() snap.GetArtPoints(G, ArtNIdV) with open(base + "art.txt", "w") as f: for NI in ArtNIdV: f.write("{}\n".format(NI))
#Question 4 ## Max Comp Fraction MxConCompSize = sn.GetMxScc(graph).GetNodes() print("Fraction of nodes in largest connected component: {:0.4f}".format( MxConCompSize / graph.GetNodes())) ## Edge Bridges edgeBridge = sn.TIntPrV() sn.GetEdgeBridges(graph, edgeBridge) print("Number of edge bridges: {}".format(len(edgeBridge))) ## Articulation Points artPoints = sn.TIntV() sn.GetArtPoints(graph, artPoints) print("Number of articulation points: {}".format(len(artPoints))) ## 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)))