def benchmark_neanet(Graph): ''' Perform benchmark tests for Directed Attribute Graphs ''' results = {} results['num_nodes'] = Graph.GetNodes() results['num_edges'] = Graph.GetEdges() for degree in range(0, 11): num = snap.NodesGTEDegree(Graph, degree) percent_deg = float(num) / results['num_nodes'] results['deg_gte_%d' % degree] = num results['deg_gte_%d_percent' % degree] = percent_deg # Check for over-weighted nodes results['max_degree'] = snap.MxDegree(Graph) num = snap.NodesGTEDegree(Graph, results['max_degree']) results['max_degree_num'] = num results['max_wcc_percent'] = snap.GetMxWccSz(Graph) \ / results['num_nodes'] results['max_scc_percent'] = snap.GetMxSccSz(Graph).GetNodes() \ / results['num_nodes'] return results
def runRobustnessTestMax(graph, rounds=30): graph = cloneGraph(graph) result = [] originalNodesCnt = graph.GetNodes() for i in range(rounds): fractionRemoved = 1.0 - float( graph.GetNodes()) / float(originalNodesCnt) result.append((fractionRemoved, snap.GetMxSccSz(graph))) for n in range(originalNodesCnt / rounds): graph.DelNode(snap.GetMxDegNId(graph)) return result
def get_robustness(file_path, LSCC_output_path, LWCC_output_path): frac_list = [ 0.0001, 0.001, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 ] Graph, H = load_graph(file_path) InDegV = snap.TIntPrV() snap.GetNodeInDegV(Graph, InDegV) OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(Graph, OutDegV) degree = dict() for item in InDegV: ID = item.GetVal1() InDeg = item.GetVal2() degree[ID] = InDeg for item in OutDegV: ID = item.GetVal1() OutDeg = item.GetVal2() degree[ID] += OutDeg sorted_degree = sorted(degree.items(), key=itemgetter(1), reverse=True) tot = len(sorted_degree) pos = [int(tot * frac) for frac in frac_list] print pos cur = 0 LSCC_robust = list() LWCC_robust = list() for i in range(tot): Graph.DelNode(sorted_degree[i][0]) if i == pos[cur] - 1: LSCC_frac = snap.GetMxSccSz(Graph) LWCC_frac = snap.GetMxWccSz(Graph) singleton_frac = 1.0 - 1.0 * snap.CntNonZNodes( Graph) / Graph.GetNodes() LSCC_robust.append({ 'removed': frac_list[cur], 'singleton': singleton_frac, 'middle': 1.0 - singleton_frac - LSCC_frac, 'LSCC': LSCC_frac }) LWCC_robust.append({ 'removed': frac_list[cur], 'singleton': singleton_frac, 'middle': 1.0 - singleton_frac - LWCC_frac, 'LWCC': LWCC_frac }) cur += 1 if cur >= len(pos): break LSCC_robust = pd.DataFrame(LSCC_robust) LSCC_robust = LSCC_robust[['removed', 'singleton', 'middle', 'LSCC']] LSCC_robust.to_csv(LSCC_output_path, index=False, encoding='utf-8') LWCC_robust = pd.DataFrame(LWCC_robust) LWCC_robust = LWCC_robust[['removed', 'singleton', 'middle', 'LWCC']] LWCC_robust.to_csv(LWCC_output_path, index=False, encoding='utf-8')
def runRobustnessTestRandomly(graph, rounds=30): graph = cloneGraph(graph) result = [] rnd = snap.TRnd() rnd.Randomize() originalEdgesCnt = graph.GetEdges() for i in range(rounds): fractionRemoved = 1.0 - float( graph.GetEdges()) / float(originalEdgesCnt) result.append((fractionRemoved, snap.GetMxSccSz(graph))) for n in range(originalEdgesCnt / rounds): graph.DelEdge(graph.GetRndEId(rnd)) return result
def genGraphInfo(self): graphName = self.graphName # get the number of nodes and edges in the graph print "Number of nodes in %s: %d" % (graphName, self.G.GetNodes()) print "Number of edges in %s: %d" % (graphName, self.G.GetEdges()) # get the node id(s) with highest degree nodeIdMaxDegree = snap.GetMxOutDegNId(self.G) maxDegree = -1 for node in self.G.Nodes(): if (node.GetId() == nodeIdMaxDegree): maxDegree = node.GetOutDeg() break nodeIdsMaxDegreeT = "" for node in self.G.Nodes(): if (maxDegree == node.GetOutDeg()): nodeIdsMaxDegreeT += str(node.GetId()) + "," print "Node id(s) with highest degree in %s: %s" % (graphName, nodeIdsMaxDegreeT) # plot degree distribution snap.PlotOutDegDistr(self.G, graphName, "Degree Distribution") degreeFileName = "outDeg." + graphName + ".png" print "Degree distribution of %s is in: %s" % (graphName, degreeFileName) # plot shortest path distribution snap.PlotShortPathDistr(self.G, graphName, "Shortest Path Distribution") shortestPathFileName = "diam." + graphName + ".png" print "Shortest path distribution of %s is in: %s" % ( graphName, shortestPathFileName) # get the fraction of nodes in largest cc print "Fraction of nodes in largest connected component in %s: %f" % ( graphName, snap.GetMxSccSz(self.G)) # plot the component size distribution snap.PlotSccDistr(self.G, graphName, "Component size distribution") sccFileName = "scc." + graphName + ".png" print "Component size distribution of %s is in: %s" % (graphName, sccFileName)
def wikiVotingNetwork(): Component = snap.TIntPrV() #Loding the graph Wiki = snap.LoadEdgeList(snap.PNGraph, "Wiki-Vote.txt", 0, 1) #Printing Number of Nodes in the Graph print "Number of Nodes: ", Wiki.GetNodes() #Printing Number of Edges in the Graph print "Number of Edges: ", Wiki.GetEdges() #Printing Number of Directed Edges in the Graph print "Number of Directed Edges: ", snap.CntUniqDirEdges(Wiki) #Printing Number of Un-Directed Edges in the Graph print "Number of Undirected Edges: ", snap.CntUniqUndirEdges(Wiki) #Printing Number of Directed Edges in the Graph print "Number of Self-Edges: ", snap.CntSelfEdges(Wiki) #Printing Number of Zero InDeg Nodes in the Graph print "Number of Zero InDeg Nodes: ", snap.CntInDegNodes(Wiki, 0) #Printing Number of Zero OutDeg Nodes in the Graph print "Number of Zero OutDeg Nodes: ", snap.CntOutDegNodes(Wiki, 0) #Printing Node ID with maximum degree in the Graph print "Node ID with maximum degree: ", snap.GetMxDegNId(Wiki) snap.GetSccSzCnt(Wiki, Component) for comp in Component: #printing number of strongly connected components with size print "Size: %d - Number of Strongly Connected Components: %d" % ( comp.GetVal1(), comp.GetVal2()) #printing size of largest connected components print "Size of largest connected component: ", snap.GetMxSccSz(Wiki) snap.GetWccSzCnt(Wiki, Component) for comp in Component: #printing number of weekly connected components with size print "Size: %d - Number of Weekly Connected Component Wikipedia: %d" % ( comp.GetVal1(), comp.GetVal2()) #printing size of weekly connected components print "Size of Weakly connected component: ", snap.GetMxWccSz(Wiki) #plotting out-degree distribution snap.PlotOutDegDistr(Wiki, "wiki-analysis", "Directed graph - Out-Degree Distribution")
def analyze(graph): n = graph.GetNodes() m = graph.GetEdges() maxSCCsize = snap.GetMxSccSz(graph) maxWCCsize = snap.GetMxWccSz(graph) avgDegree = (m * float(2)) / n # estimate power law exponent degs = [] degCounts = [] DegToCntV = snap.TIntPrV() snap.GetDegCnt(graph, DegToCntV) for item in DegToCntV: degs.append(item.GetVal1()) degCounts.append(item.GetVal2()) xMin = min(degs) - 0.5 m = graph.GetNodes() alphaMLLE = 1 + (m / (sum([np.log(i / xMin) * degCounts[degs.index(i)] for i in degs]))) # erdos-renyi clustering coefficient graphER = snap.GenRndGnm(snap.PUNGraph, n, m) avgClustCoeffER = snap.GetClustCf(graphER, -1) # average shortest path graphWCC = snap.GetMxWcc(graph) avgClustCoeff = snap.GetClustCf(graphWCC, -1) numSamples = min(graphWCC.GetNodes(), 617) # all nodes or sample size Rnd = snap.TRnd(42) Rnd.Randomize() shortPathList = [] for i in xrange(numSamples): s = graphWCC.GetRndNId(Rnd) NIdToDistH = snap.TIntH() snap.GetShortPath(graphWCC, s, NIdToDistH) for item in NIdToDistH: shortPathList.append(NIdToDistH[item]) avgShortPath = np.mean(shortPathList) return avgClustCoeff, maxSCCsize, maxWCCsize, avgDegree, alphaMLLE, avgClustCoeffER, avgShortPath
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)
def q2_1(): ''' You will have to run the inward and outward BFS trees for the respective nodes and reason about whether they are in SCC, IN or OUT. You may find the SNAP function GetBfsTree() to be useful here. ''' ########################################################################## #TODO: Run outward and inward BFS trees from node 2018, compare sizes #and comment on where node 2018 lies. G = load_graph("email") #Your code here: outward_set = set() BfsTree = snap.GetBfsTree(G, 2018, True, False) for EI in BfsTree.Edges(): outward_set.add(EI.GetDstNId()) # print "Edge from %d to %d in generated tree." % (EI.GetSrcNId(), EI.GetDstNId()) inward_set = set() BfsTree = snap.GetBfsTree(G, 2018, False, True) for EI in BfsTree.Edges(): inward_set.add(EI.GetDstNId()) # print "Edge from %d to %d in generated tree." % (EI.GetSrcNId(), EI.GetDstNId()) print('inward_set', len(inward_set)) print('outward_set', len(outward_set)) print('G size', G.GetEdges()) MxScc = snap.GetMxScc(G) mxSccSize = MxScc.GetNodes() print 'SCC size:', mxSccSize print 'Relative size of SCC in Directed Graph:', snap.GetMxSccSz(G) ########################################################################## ########################################################################## #TODO: Run outward and inward BFS trees from node 224, compare sizes #and comment on where node 224 lies. G = load_graph("epinions") #Your code here: #Your code here: outward_set = set() BfsTree = snap.GetBfsTree(G, 224, True, False) for EI in BfsTree.Edges(): outward_set.add(EI.GetDstNId()) # print "Edge from %d to %d in generated tree." % (EI.GetSrcNId(), EI.GetDstNId()) inward_set = set() BfsTree = snap.GetBfsTree(G, 224, False, True) for EI in BfsTree.Edges(): inward_set.add(EI.GetDstNId()) # print "Edge from %d to %d in generated tree." % (EI.GetSrcNId(), EI.GetDstNId()) print('inward_set', len(inward_set)) print('outward_set', len(outward_set)) print('G size', G.GetEdges()) print 'Relative size of SCC in Directed Graph:', snap.GetMxSccSz(G) ########################################################################## print '2.1: Done!\n'
G = snap.LoadEdgeList(snap.PNGraph, "Wiki-Vote.txt", 0, 1) snap.PrintInfo(G, "votes Stats", "votes-info.txt", False) # Node ID with maximum degree NId1 = snap.GetMxDegNId(G) print("Node ID with Maximum-Degree: %d" % NId1) # Number of Strongly connected components ComponentDist = snap.TIntPrV() snap.GetSccSzCnt(G, ComponentDist) for comp in ComponentDist: print("Size: %d - Number of Components: %d" % (comp.GetVal1(), comp.GetVal2())) # Size of largest strongly connected component print("Strongly Connected Component - Maximum size:", snap.GetMxSccSz(G)) # Number of Weakly Connected Components CompDist = snap.TIntPrV() snap.GetWccSzCnt(G, CompDist) for comp in CompDist: print("Size: %d - Number of Components: %d" % (comp.GetVal1(), comp.GetVal2())) # Size of largest weakly connected component print("Weakly Connected Component - Maximum size:", snap.GetMxWccSz(G)) # Plot of Outdegree Distribution snap.PlotOutDegDistr(G, "Wiki Votes", "Wiki-Votes Out Degree")
plt.ylabel("Cumulative PageRank") #get node in-degree distribution plt.figure() for graph in graphs: y = sorted([float(node.GetInDeg())/node.GetDeg() for node in graph.Nodes()]) #y = [float(val-min(y))/(max(y)-min(y)) for val in y] x = [float(i+1)/graph.GetNodes() for i in range(len(y))] plt.plot(x,y, '.', markersize=4) plt.title("Node Distribution by Win Percentage") plt.legend(names) plt.xlabel("Node Fraction") plt.ylabel("Win Percentage") Rnd = snap.TRnd(42) Rnd.Randomize() min_edges_per_node = min([float(graph.GetEdges())/graph.GetNodes() for graph in graphs]) for graph in graphs: while float(graph.GetEdges())/graph.GetNodes() > min_edges_per_node: graph.DelEdge(graph.GetRndEId(Rnd)) print 'names:', names print 'nodes:', [graph.GetNodes() for graph in graphs] print 'edges:', [graph.GetEdges() for graph in graphs] print 'mxscc:', [snap.GetMxSccSz(graph) for graph in graphs] print 'max degree:', [graph.GetNI(snap.GetMxDegNId(graph)).GetDeg() for graph in graphs] plt.show()
" nodes: %0.4f" % eff1) print("Approximate effective diameter by sampling ", 100, " nodes: %0.4f" % eff2) print("Approximate effective diameter by sampling ", 1000, " 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")
print("Approximate full diameter (mean and variance): %.4f, %.4f" % (m_dFull, v_dFull)) 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)))
em = mean(effData) ev = variance(effData) print "Approx. effective diameter in %s with sampling 10 nodes: %d" % ( file, effDiam10) print "Approx. effective diameter in %s with sampling 100 nodes: %d" % ( file, effDiam100) print "Approx. effective diameter in %s with sampling 1000 nodes: %d" % ( file, effDiam1000) print "Approx. effective diameter in %s (mean and variance): %d, %d\n" % ( file, em, ev) # c) Plot of the distribution of the shortest path plotFN1 = file + ".diam.short-path-plot.png" snap.PlotShortPathDistr(UGraph, plotFN1, "Undirected graph - Shortest path for file " + file) print "\nShortest path distribution of %s is in: %s\n" % (file, plotFN1) # 4) Components of the network: print "Components of the network:\n" # a) Fraction of nodes in the largest connected component nodeFrac = snap.GetMxSccSz(UGraph) print "Fraction of nodes in largest connected component in '%s': %d\n" % ( file, nodeFrac) # b) Plot of the distribution of sizes of connected components. plotFN2 = file + ".scc.connected-components-plot.png" snap.PlotSccDistr(UGraph, plotFN2, "Undirected graph - scc distribution for file " + file) print "\nComponent size distribution of %s is in: %s\n" % (file, plotFN2) # end of program print "\n\t End of program\n\n"
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
effective_diameter.append(snap.GetBfsEffDiam(graph, 1000)) print("Approximate effective diameter by sampling 1000 nodes:", round(effective_diameter[-1], 4)) print("Approximate effective diameter (mean and variance):", round(get_mean(effective_diameter), 4), ',', round(get_variance(effective_diameter), 4), sep="") 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:",
def main(): # Number of nodes n = int(raw_input("Please enter the number of nodes")) # Probability of an edge between nodes p = float( raw_input( "Please enter the value of probability of an edge between nodes")) # Random Input of x pairs of nodes x = int(raw_input("Please enter the number of random, x pairs of nodes:")) # Empty graph and add nodes ERM = Empty_graph(n) # Add edges to the graph using personal Erdos Renyi Model Erdos_Renyi(ERM, p) # Erdos Renyi Clustering Coeffecient print("Clustering Coeffecient: ", clustering_coffecient(ERM)) # Diameter diameter_ERM = snap.GetBfsEffDiamAll(ERM, 10, False) print(diameter_ERM[2]) # Largest Strongly Connected Component print("Largest Strongly Connected Component - Maximum size:", snap.GetMxSccSz(Small_world)) # Largest Size of Graph ERM_size = snap.GetMxScc(ERM).GetEdges() print(ERM_size) # Plot of Degree Distribution snap.PlotOutDegDistr(ERM, "ERMGraph", "ERM Degree Distribution") # Add Small World Network Small_world = Empty_graph(n) first_edges(Small_world) second_edges(Small_world) random_edges(Small_world, x) # Small World Clustering Coeffecient print("Clustering Coeffecient: ", clustering_coffecient(Small_world)) # Diameter diameter_Small_world = snap.GetBfsEffDiamAll(Small_world, 10, False) print(diameter_Small_world[2]) # Largest Strongly Connected Component print("Largest Strongly Connected Component - Maximum size:", snap.GetMxSccSz(Small_world)) # Largest Size of Graph Small_world_size = snap.GetMxScc(Small_world).GetEdges() print(Small_world_size) # Plot of Degree Distribution snap.PlotOutDegDistr(Small_world, "SmallWorldGraph", "Small World Degree Distribution") # Add Collaboration Network Collaboration_Network = snap.LoadEdgeList(snap.PUNGraph, "CA-HepTh.txt", 0, 1) snap.DelSelfEdges(Collaboration_Network) snap.PrintInfo(Collaboration_Network, "Graph Statistics", "info.txt", False) # Collaboration Network Clustering Coeffecient print("Clustering Coeffecient: ", clustering_coffecient(Collaboration_Network)) # Diameter diameter_Collaboration_Network = snap.GetBfsEffDiamAll( Collaboration_Network, 10, False) print(diameter_Collaboration_Network[2]) # Largest Strongly Connected Component print("Largest Strongly Connected Component - Maximum size:", snap.GetMxSccSz(Collaboration_Network)) # Largest Size of Graph Collaboration_Network_size = snap.GetMxScc( Collaboration_Network).GetEdges() print(Collaboration_Network_size) # Plot of Degree Distribution snap.PlotOutDegDistr(Collaboration_Network, "CollaborationNetworkGraph", "Collaboration Network Degree Distribution")
round(diam, 4)) i *= 10 average += diam 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))
def main(): Component = snap.TIntPrV() #loading the real world graph realWorld = snap.LoadEdgeList(snap.PUNGraph, "CA-HepTh.txt", 0, 1) #deleting the self-edges from the graph snap.DelSelfEdges(realWorld) #calling the function wikiVotingNetwork() #Taking number of nodes in a graph from real world network n = realWorld.GetNodes() #Generating an Undirected Graph G = snap.TUNGraph.New() #Taking number of edges in a graph from user e = int(raw_input('Enter the number of Random Edges : ')) p = float( raw_input('Enter the Probability of Edges between Nodes from 0-1 : ')) #Generating Number of Nodes for i in range(n): #Adding Nodes into the graph G.AddNode(i) #calling the function erdosRenyi(G, p) #Printing the Clustering print 'Erdos Renyi Clustering Co-efficient: ', clustCoefficient(G) diam = snap.GetBfsFullDiam(G, 9877, False) #printing the diameter print 'Erdos Renyi Diameter: ', diam #plotting the graph snap.PlotOutDegDistr(G, "Erdos-Renyi", "Un-Directed graph - Out-Degree Distribution") snap.GetSccSzCnt(G, Component) for comp in Component: #printing number of strongly connected components with size print "Size: %d - Number of Connected Component in Erdos-Renyi: %d" % ( comp.GetVal1(), comp.GetVal2()) #printing fraction of nodes and edges print "Fraction of Nodes and Edges in Erdos Renyi: ", snap.GetMxSccSz(G) #Drawing a Erdos Renyi Graph snap.DrawGViz(G, snap.gvlDot, "erdosRenyi1.png", "Erdos Renyi") #calling the function smallWorldRandomNetwork(G, e) #printing the clustering coefficient print 'Small World Random Network Clustering Co-efficient: ', clustCoefficient( G) diam = snap.GetBfsFullDiam(G, 9877, False) #printing the diameter print 'Small World Random Network Diameter: ', diam snap.GetSccSzCnt(G, Component) for comp in Component: #printing number of strongly connected components with size print "Size: %d - Number of Connected Component in Small World: %d" % ( comp.GetVal1(), comp.GetVal2()) #fraction of nodes and edges in small world print "Fraction of Nodes and Edges in Small World: ", snap.GetMxSccSz(G) #plotting the graph snap.PlotOutDegDistr(G, "Small-World", "Un-Directed graph - Out-Degree Distribution") #drawinf the graph snap.DrawGViz(G, snap.gvlDot, "smallWorld1.png", "Small World Random Network") #calculating the clustering co-efficient print 'Real World Random Network Clustering Co-efficient: ', clustCoefficient( realWorld) diam = snap.GetBfsFullDiam(G, 9877, False) print 'Real World Random Network Diameter: ', diam snap.GetSccSzCnt(realWorld, Component) for comp in Component: #printing number of strongly connected components with size print "Size: %d - Number of Weekly Connected Component in Real World: %d" % ( comp.GetVal1(), comp.GetVal2()) #printing fraction of nodes and edges print "Fraction of Nodes and Edges in Small World: ", snap.GetMxSccSz( realWorld) #plotting the real world network graph snap.PlotOutDegDistr(realWorld, "real-World", "Un-Directed graph - Out-Degree Distribution") #Drawing Real WOrld Graph snap.DrawGViz(realWorld, snap.gvlDot, "realWorld.png", "Real World Random Network")