def f(): snap = self.snap ret = [] EdgeV = snap.TIntPr64V() snap.GetEdgeBridges(self.graph, EdgeV) for edge in EdgeV: ret.append((edge.GetVal1(), edge.GetVal2())) return ret
def number_of_bridges(G, GName): EdgeV = snap.TIntPrV() snap.GetEdgeBridges(G, EdgeV) bridges_in_graph = len(EdgeV) print "Number of edge bridges in {0}: {1}".format(GName[:-10], bridges_in_graph)
def getEdgeBridges(network): UGraph = snap.ConvertGraph(snap.PUNGraph, network) EdgeV = snap.TIntPrV() snap.GetEdgeBridges(UGraph, EdgeV) for edge in EdgeV: print("edge: (%d, %d)" % (edge.GetVal1(), edge.GetVal2())) print(len(EdgeV)) return EdgeV
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())
variance += (diam * diam) average /= 3 variance = (variance / 3) - average * average print("Approximate effective diameter(mean and variance): %0.4f,%0.4f" % (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
" nodes: %0.4f" % eff3) effmean = (eff1 + eff2 + eff3) / 3.0 effvar = (((eff1 * eff1) + (eff2 * eff2) + (eff3 * eff3)) / 3.0) - (effmean * effmean) print("Approximate effective diameter (mean and variance): %0.4f,%0.4f" % (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)
def get_bridges(graph): bridges = snap.TIntPrV() snap.GetEdgeBridges(graph, bridges) return bridges
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_edge_a_bridge(G, e): EdgeV = snap.TIntPrV() snap.GetEdgeBridges(G, EdgeV) return (e.GetVal1(), e.GetVal2()) in set(EdgeV)
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)
largest_connected = snap.GetMxScc(p2p_gnutella04_subgraph) node = 0 for i in largest_connected.Nodes(): node = node + 1 print "Fraction of nodes in largest connected component in p2p-Gnutella04-subgraph :" + str( round(node * 1.0 / len(v4), 3)) # Task 1.2.4.2 if (sub_graph_name == "soc-Epinions1-subgraph"): # Getting no of edge bridges in th network EdgeV = snap.TIntPrV() snap.GetEdgeBridges(soc_epinions1_subgraph, EdgeV) edge_bridge = 0 for i in EdgeV: edge_bridge = edge_bridge + 1 print "Number of edge bridges in soc-Epinions1-subgraph :" + str( edge_bridge) if (sub_graph_name == "cit-HepPh-subgraph"): # Getting no of edge bridges in th network EdgeV = snap.TIntPrV() snap.GetEdgeBridges(cit_heph_subgraph, EdgeV) edge_bridge = 0 for i in EdgeV: edge_bridge = edge_bridge + 1 print "Number of edge bridges in cit-HepPh-subgraph :" + str(edge_bridge)
plt.xlabel("Shortest Path Length") plt.ylabel("Frequency") plt.title("Shortest Path Distribution ({})".format(graph_filename[:-6])) plt.savefig(plot_filedir) """ 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)
dEff_10 = snap.GetBfsEffDiam(G, 10, False) dEff_100 = snap.GetBfsEffDiam(G, 100, False) dEff_1000 = snap.GetBfsEffDiam(G, 1000, False) m_dEff, v_dEff = map(float, MeanAndVariance(dEff_10, dEff_100, dEff_1000)) print("Approximate effective diameter by sampling 10 nodes: %.4f" % dEff_10) print("Approximate effective diameter by sampling 100 nodes: %.4f" % dEff_100) 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))
format(np.mean(effDia), np.var(effDia))) ## Plot Shortest Path Distr sn.PlotShortPathDistr(graph, name, "Shortest Path Distribution") plotRemove("diam", "shortest_path", name) #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(
for line in iter(fin): ugraph.AddNode(int(line)) numnod += 1 fin.close() fin = open("An_Edges.txt", "rb") for line in iter(fin): ugraph.AddEdge(int(line.split(",", 1)[0]), int(line.split(",", 1)[1])) numedg += 1 fin.close() triads = snap.GetTriads(ugraph) print "triads : ", triads EdgeV = snap.TIntPrV() snap.GetEdgeBridges(ugraph, EdgeV) print "bridges : ", len(EdgeV) diameter = snap.GetBfsFullDiam(ugraph, 50) print "diameter : ", diameter snap.PlotInDegDistr(ugraph, "degDist", "degDist") u_edges = dict() fin = open("An_Edges.txt", "rb") cnt = 1 for n in iter(fin): u_edges[cnt] = str(n) cnt += 1 fin.close()
import snap flnme = "dataset/Slashdot0902.txt" graph = snap.LoadEdgeList(snap.PNGraph, flnme, 0, 1, '\t') snap.PrintInfo(graph) '''bridge = snap.TIntPrV() snap.GetEdgeBridges(graph, bridge) print("Number of bridges: {}".format(len(bridge)))'''