def preprocessGraphs(Graphs): v = [] # appends node set for all graphs to v. for g in range(0, len(Graphs)): a = [] for n in Graphs[g].Nodes(): a.append(n.GetId()) v.append(set(a)) # take the intersection, giving us all common nodes. u = list(set.intersection(*v)) print >> sys.stderr, "nodes: " + str(len(u)) # converts to a snap-vector w = snap.TIntV() for i in u: w.Add(i) # update graph list with subgraph induced by common nodes. for g in range(0, len(Graphs)): Graphs[g] = snap.GetSubGraph(Graphs[g], w) ng = 0 print >> sys.stderr, "edges:" for g in range(0, len(Graphs)): ng = ng + Graphs[g].GetEdges() print >> sys.stderr, " " + str(Graphs[g].GetEdges()) print >> sys.stderr, "total edges: " + str(ng)
def saveGraphs(): g = snap.LoadEdgeList(snap.PUNGraph, sys.argv[1],0,1) Graphs = [] a = snap.TIntV() for file in sys.argv[2:]: Graphs.append(snap.LoadEdgeList(snap.PUNGraph, file, 0, 1)) folder = str(file).split("/")[0] if folder != "data": sys.exit("comparison should be made with file in 'data' folder") data = sys.argv[2].split("/")[1] path = "/" if "DCS_LP" in sys.argv[1]: path = "results/snap/"+data+"DCS_LP/" else :# DCS_GREEDY path = "results/snap/"+data+"DCS_GREEDY/" if not os.path.exists(path) : os.makedirs(path) for i in g.Nodes() : a.Add(i.GetId()) for i in range(0,len(Graphs)): Graphs[i] = snap.GetSubGraph(Graphs[i],a) filename = raw_input('Enter a snapgraph name: ') print "saving graphs in: " + path +" under names "+filename+"i.txt" for i in range(0,len(Graphs)): snap.SaveEdgeList(Graphs[i],path+filename+str(i+1)+".txt","Minimal graph from" + sys.argv[1])
def printStats(): g = snap.LoadEdgeList(snap.PUNGraph, sys.argv[1],0,1) for i in range(2,len(sys.argv)): cName,cExt = os.path.splitext(sys.argv[i]) a = snap.TIntV() gi = snap.LoadEdgeList(snap.PUNGraph, sys.argv[i], 0, 1) print "Printing stats..." for n in g.Nodes(): a.Add(n.GetId()) g0 = snap.GetSubGraph(gi,a) n = g0.GetNodes() e = g0.GetEdges() print "---" print "Stats from : " + str(cName) print "Nodes : "+str(n) print "Edges : "+str(e) print "Density : "+ str( e / float(n))
def fsg(nodes): g = -1 Graphs = [] names = [] for file in sys.argv[1:]: Graphs.append(snap.LoadEdgeList(snap.PUNGraph, file, 0, 1)) lpName, lpExt = os.path.splitext(file) names.append(lpName) a = snap.TIntV() for i in nodes: a.Add(i) density = sys.maxint for i in range(0, len(Graphs)): Graphs[i] = snap.GetSubGraph(Graphs[i], a) newD = Graphs[i].GetEdges() / float(Graphs[i].GetNodes()) if newD < density: density = newD g = i print "Smallest graph was: " + str(names[g]) print "Density: " + str(density) return (names[g], Graphs[g], density)
def findSmallestGraph(): g = snap.LoadEdgeList(snap.PUNGraph, sys.argv[1],0,1) Graphs = [] names = [] for file in sys.argv[2:]: Graphs.append(snap.LoadEdgeList(snap.PUNGraph, file, 0, 1)) lpName,lpExt = os.path.splitext(file) names.append(lpName) a = snap.TIntV() for i in g.Nodes() : a.Add(i.GetId()) density = 500 for i in range(0,len(Graphs)): Graphs[i] = snap.GetSubGraph(Graphs[i],a) newD = Graphs[i].GetEdges() / float(Graphs[i].GetNodes()) if newD < density : density = newD g = i print "Smallest graph was: "+ str(names[i]) print "Density: "+str(density) return (names[i],Graphs[g])
def findSmallestGraph(nodes, Graphs): a = snap.TIntV() for i in nodes: a.Add(i) density = 500 smallest = -1 for i in range(0, len(Graphs)): graph = snap.GetSubGraph(Graphs[i], a) try: newD = graph.GetEdges() / float(graph.GetNodes()) except ZeroDivisionError: print "Div zero. setting density=0" newD = 0 if newD < density: density = newD smallest = graph #print "Smallest graph was: "+ str(g) #print "Density: "+str(density) return ("find smallest", smallest, density)
def getSubgraph(target_graph, graph): a = snap.TIntV() for i in graph.Nodes(): a.Add(i.GetId()) return snap.GetSubGraph(target_graph, a)
import sys from lib import snap from lib import snapGraphCopy from lib import triangleDensity def loadGraphs(locations): graphs = [] for arg in locations: graphs.append(snap.LoadEdgeList(snap.PUNGraph, arg, 0, 1)) return graphs graphs = loadGraphs(sys.argv[1:-1]) subgraphNodes = open(sys.argv[-1]).read() subgraphNodes = subgraphNodes.split("\n") nodes = snap.TIntV() for i in range(0,len(subgraphNodes)-1): if(subgraphNodes[i][0] != '#'): nodes.Add(int(subgraphNodes[i])) leastDense = 0 smallestNumberOfEdges = sys.maxint for g in range(0,len(graphs)): graphs[g] = snap.GetSubGraph(graphs[g],nodes) edges = graphs[g].GetEdges() if (smallestNumberOfEdges>edges): leastDense = g smallestNumberOfEdges = edges print ("number of edges: " + str(smallestNumberOfEdges)) print ("number of nodes: " + str(len(nodes))) print ("density: " + str(smallestNumberOfEdges*1.0/len(nodes))) print "calculating diameter"
def getSubgraph(target_graph, nodes): a = snap.TIntV() for i in nodes: a.Add(i) return snap.GetSubGraph(target_graph, a)
def printMoreGraphs(Graphs): nodes = [] allEdges = [] allEdgeCons = [] ##### preprocessing start ##### # TODO: clean up the preprocessing, possibly make it into a new function v = [] # appends node set for all graphs to v. for g in range(0, len(Graphs)): a = [] for n in Graphs[g].Nodes(): a.append(n.GetId()) v.append(set(a)) # take the intersection, giving us all common nodes. u = list(set.intersection(*v)) # converts to a snap-vector w = snap.TIntV() for i in u: w.Add(i) print >> sys.stderr, str(len(u)) # update graph list with subgraph induced by common nodes. for g in range(0, len(Graphs)): Graphs[g] = snap.GetSubGraph(Graphs[g], w) ng = 0 print >> sys.stderr, "edges:\n" for g in range(0, len(Graphs)): ng = ng + Graphs[g].GetEdges() print >> sys.stderr, Graphs[g].GetEdges() print >> sys.stderr, ng ##### preprocessing end ##### for g in range(0, len(Graphs)): edges = [] edgeCons = [] for e in Graphs[g].Edges(): src = e.GetSrcNId() dst = e.GetDstNId() xij = "x" + str(g) + "_" + str(src) + "#" + str(dst) yi = "y" + str(src) yj = "y" + str(dst) edges.append(xij) edgeCons.append(xij + " - " + yi + " <= 0") edgeCons.append(xij + " - " + yj + " <= 0") for n in Graphs[g].Nodes(): id = n.GetId() yi = "y" + str(id) nodes.append(yi) allEdges.append(edges) allEdgeCons.append(edgeCons) # remove duplicates nodes = list(set(nodes)) print "maximize t" print "\nsubject to" print(" +\n".join(nodes)) + " <= 1" # sum yi <= 1 for e in allEdges: print(" +\n".join(e)) + " - t >= 0" # sum xij >= t for c in allEdgeCons: print " \n".join(c) # xij <= yi and xij <= yj print "\nend"