def preproc_graph(filename): ''' get connected graph I beleive this is done after we remap the nodes to consecutive order in map_nodes_new.py ''' print "Working on %s \n" % filename print "Generating graph from edge list..." # laod edge list into snap Graph0 = snap.LoadEdgeList(snap.PUNGraph, filename, 0, 1, '\t') # get edges V0 = Graph0.GetNodes() # delete zero degree nodes snap.DelZeroDegNodes(Graph0) print "Done generating graph!" # get max weakly connected component print "Generating connected graph..." Graph = snap.GetMxWcc(Graph0) V = Graph.GetNodes() E = Graph.GetEdges() print "Done generating graph with V = %i, E = %i!, V0 = %i" % (V, E, V0) # get nodes included in weakly connected graph (which could be # a proper subset of original set) # Find one edge in graph and find all connected nodes for EI in Graph.Edges(): conn_node = EI.GetSrcNId() # start with one edge break # only need one edge since connected CnCom = snap.TIntV() snap.GetNodeWcc(Graph, conn_node, CnCom) conn_node_ids = sort(array([node for node in CnCom])) return Graph, conn_node_ids, V, E, V0
def calcCnctComponents(graph): lenComps = {} CnCom = snap.TIntV() for node in graph.Nodes(): nodeid = node.GetId() if nodeid in lenComps: continue snap.GetNodeWcc(graph, nodeid, CnCom) l = CnCom.Len() lenComps[nodeid] = l for conNode in CnCom: lenComps[conNode] = l return lenComps
def labelConnectedComponents(graph): components = {} CnCom = snap.TIntV() component = 1 for node in graph.Nodes(): nodeid = node.GetId() if nodeid in components: continue snap.GetNodeWcc(graph, nodeid, CnCom) if len(CnCom) == 1: components[nodeid] = 0.0 else: for cnid in CnCom: components[cnid] = component component += 1 return components
# test if the graph is connected or weakly connected print("IsConnected(G) =", snap.IsConnected(G)) print("IsWeaklyConnected(G) =", snap.IsWeaklyConn(G)) # get the weakly connected component counts WccSzCnt = snap.TIntPr64V() snap.GetWccSzCnt(G, WccSzCnt) #print (WccSzCnt[0],WccSzCnt[0].Val1,WccSzCnt[0].Val2) for i in range(0, WccSzCnt.Len()): print("WccSzCnt[%d] = (%d, %d)" % (i, WccSzCnt[i].Val1.Val, WccSzCnt[i].Val2.Val)) # return nodes in the same weakly connected component as node 1 CnCom = snap.TInt64V() snap.GetNodeWcc(G, 1, CnCom) print("CnCom.Len() = %d" % (CnCom.Len())) # get nodes in weakly connected components WCnComV = snap.TCnComV() snap.GetWccs(G, WCnComV) for i in range(0, WCnComV.Len()): print("WCnComV[%d].Len() = %d" % (i, WCnComV[i].Len())) for j in range(0, WCnComV[i].Len()): print("WCnComV[%d][%d] = %d" % (i, j, WCnComV[i][j])) # get the size of the maximum weakly connected component MxWccSz = snap.GetMxWccSz(G) print("MxWccSz = %.5f" % (MxWccSz)) # get the graph with the largest weakly connected component
def get_size_of_conn_comp(G, n): CnCom = snap.TIntV() return len(snap.GetNodeWcc(G, n, CnCom))
import snap Graph = snap.GenRndGnm(snap.PNGraph, 100, 1000) CnCom = snap.TInt64V() snap.GetNodeWcc(Graph, 0, CnCom) print("Nodes in the same connected component as node 0:") for node in CnCom: print(node)
def nodesInWcc(self, returnNodes=True): nodes = snap.TIntV() snap.GetNodeWcc(self.parentGraph.rawGraph, self.id, nodes) return SnapUtil.rawComponentToNodeSet(nodes, self.parentGraph, returnNodes)