def parseGraphList(fname): f = open(fname) nTests = int(f.readline()) graphList = [] for i in range(0, nTests): nVerts = int(f.readline()) graphList.append(graph.newGraph(nVerts)) vertsList = [] for j in range(0, nVerts): line = f.readline() coordString = line.split() coords = (int(coordString[0]), int(coordString[1])) vertsList.append(coords) for k in range(0, j): # integer-rounded distance (otherVx, otherVy) = vertsList[k] (vx, vy) = coords (dx, dy) = (vx - otherVx, vy - otherVy) distance = math.sqrt(dx * dx + dy * dy) graph.addEdge(graphList[i], j, k, int(round(distance))) return graphList
def addArrows(G, arrow, img): for a in arrow: cir = getClosestCircle(G, a, img) newI = np.ones_like(img)*255 newI = np.where(img==a,0,newI) newI = cv2.threshold(newI,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1] l = 255 minX = np.min(np.where(newI == l)[0]) maxX = np.max(np.where(newI == l)[0]) minY = np.min(np.where(newI == l)[1]) maxY = np.max(np.where(newI == l)[1]) point = (minX,minY) cir2 = ((maxX-minX)//2,(maxY-minY)//2,0) num = [] poi = [] ims = [] num.append(a) poi.append(point) ims.append(newI[minX:maxX,minY:maxY]) if(G.has_edge(cir,cir)): num = gr.getEdgeNum(G,cir,cir) num.append(a) poi = gr.getEdgePoint(G,cir,cir) poi.append(point) gr.setEdgePoint(G,cir,cir, poi) ims = gr.getEdgeImg(G,cir,cir) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,cir,cir,ims) gr.setEdgeNum(G, cir, cir, num) else: gr.addEdge(G,cir,cir,'',num,poi,ims) gr.addNodeLoop(G,cir,cir2)
def parsefour(filecsv, graph): with open(filecsv) as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"', skipinitialspace=True) for row in reader: graph.addEdge(row[2], row[0])
def buildGraph(projects, depend): graph = Graph() for project in projects: graph.get(project) for a, b in depend: graph.addEdge(a, b) return graph
def parsefootball(filecsv, graph): with open(filecsv) as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"', skipinitialspace=True) parse = ((row[0], int(row[1]), row[2], int(row[3])) for row in reader) for row in parse: if row[1] > row[3]: graph.addEdge(row[2], row[0]) else: graph.addEdge(row[0], row[2])
def randomEdge(graph, nodes=1000): edges = G.getEdges(graph, weight=True) nodes = min(nodes, G.getNodes(graph)) sampleGraph = {} sampleEdges = set() while True: edge = random.choice(edges) if not edge in sampleEdges: G.addEdge(sampleGraph, edge[0], edge[1], edge[2]) sampleEdges.add(edge) if len(G.getNodes(sampleGraph)) == nodes: break return sampleGraph
def sampleRN(graph, nodes=1000): sampleGraph = {} nodesOriginal = G.getNodes(graph) edgesOriginal = G.getEdges(graph, weight=False) nodesSample = set() size = min(nodes, len(nodesOriginal)) while (len(nodesSample) < size): nodesSample.add(random.choice(nodesOriginal)) nodesSample = list(nodesSample) for src in nodesSample: if src in graph.keys(): for dest, weight in graph[src]: if dest in nodesSample: G.addEdge(sampleGraph, src, dest, weight) currentNodes = G.getNodes(sampleGraph) for node in nodesSample: if node not in currentNodes: sampleGraph[node] = [] return sampleGraph
def cross(self, src, dest, weight): self.currentNode = dest self.nodesSample.add(dest) self.nodesSample.add(src) if not G.addEdge(self.sampleGraph, src, dest, weight): #print 'not G.addEdge' self.updateStartAndCurrent() #self.startNode = random.choice(self.nodesOriginal) #self.currentNode = startNode else: #print src, dest,"added to G" #print self.sampleGraph pass
import graph as gw #basic test graph1 = gw.create() gw.addVertex(graph1, 'v1') gw.addVertex(graph1, 'v2') gw.addEdge(graph1, 'v1','v2') gw.addVertex(graph1, 'v3') gw.disp(graph1) graph2 = gw.create() gw.addVertex(graph2, 'v2') gw.addVertex(graph2, 'v3') gw.addEdge(graph2, 'v2','v3') gw.disp(graph2) graph3 = gw.create() graph4 = gw.graphUnion(graph1, graph2, graph3) gw.disp(graph4) gw.disp(graph3)
import graph as gw #super basic test to check the first 5 functions graph = gw.create() gw.addVertex(graph, 'v1') gw.addVertex(graph, 'v2') gw.addEdge(graph, 'v1', 'v2') gw.disp(graph) gw.addEdge(graph, 'v2', 'v3') gw.disp(graph) gw.destroy(graph)
# Delete an inexistent node #tree.delete(50) # Test the graph print "Printing the tree after deleting 10, 2 and 1 " tree.print_subtree(tree.node) print '_' * 10, "Testing the graph", '_' * 10 graph = graph.Graph() # Add vertices for i in range (1, 21): graph.addVertex(i) # Add an existing vertex #graph.addVertex(2) for i in range (1, 11): graph.addEdge(i, i+1) # Create an edge between not existing vertex #graph.addEdge(345, 1000) for i in range (10, 19): graph.addEdge(i, 20-i) for i in range (1, 6): graph.findVertex(i)
def parseSNAP(filename, graph): with open(filename) as f: skipComments = (line for line in f if line[0] != '#') edges = (re.split("\s+", edge) for edge in skipComments) for edge in edges: graph.addEdge(edge[0], edge[1])
def discoverOneArrow(G,img,x,y,imgOrg,objects,label,lps,withLps): minX = np.min(np.where(img == 0)[0]) maxX = np.max(np.where(img == 0)[0]) minY = np.min(np.where(img == 0)[1]) maxY = np.max(np.where(img == 0)[1]) pts = [] #top for i in range(minY,maxY+1): if(img[minX][i] == 0): ptop = (minX,i) pts.append(ptop) break #bottom for i in range(minY,maxY+1): if(img[maxX][i] == 0): pbot = (maxX,i) pts.append(pbot) break #left for i in range(minX,maxX+1): if(img[i][minY] == 0): plef = (i,minY) pts.append(plef) break #right for i in range(minX,maxX+1): if(img[i][maxY] == 0): prig = (i,maxY) pts.append(prig) break size = len(G.nodes()) vec = [0]*size q = qu.PriorityQueue() pos = 0 names = [] for c in G.nodes(): names.append(c) for c in G.nodes(): x0 = gr.getNodeX0(G,c) y0 = gr.getNodeY0(G,c) r = gr.getNodeRadius(G,c) name = c if(x>y): disttop = np.sqrt((x0-ptop[0])**2 +(y0-ptop[1])**2) distbot = np.sqrt((x0-pbot[0])**2 +(y0-pbot[1])**2) q.put((disttop,name,pos)) q.put((distbot,name,pos)) else: distlef = np.sqrt((x0-plef[0])**2 +(y0-plef[1])**2) distrig = np.sqrt((x0-prig[0])**2 +(y0-prig[1])**2) q.put((distlef,name,pos)) q.put((distrig,name,pos)) pos= pos+1 po = [] for i in range(2): d,n,pos = q.get() vec[pos] = vec[pos]+1 po.append(pos) counter = 0 for v in vec: if v == 2: break if v == 1: counter +=1 if counter == 2: obj1 = getObject(objects,po[0]*-1) obj2 = getObject(objects,po[1]*-1) nobj1 = str(obj1[1][0])+str(obj1[1][1]) nobj2 = str(obj2[1][0])+str(obj2[1][1]) if(checkArrow(G,img,x,y, obj1, obj2)): dir = arrowDirection(img) newI = copy.deepcopy(img) newI = cv2.threshold(newI,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1] l = 255 minX = np.min(np.where(newI == l)[0]) maxX = np.max(np.where(newI == l)[0]) minY = np.min(np.where(newI == l)[1]) maxY = np.max(np.where(newI == l)[1]) point = (minX,minY) text = '' num = [] poi = [] ims = [] num.append(label) poi.append(point) ims.append(newI[minX:maxX,minY:maxY]) if(dir == 0): if(obj1[1][0] > obj2[1][0]): if(G.has_edge(nobj1,nobj2)): num = gr.getEdgeNum(G,nobj1,nobj2) num.append(label) poi = gr.getEdgePoint(G,nobj1,nobj2) poi.append(point) gr.setEdgePoint(G,nobj1,nobj2, poi) ims = gr.getEdgeImg(G,nobj1,nobj2) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj1,nobj2,ims) gr.setEdgeNum(G, nobj1, nobj2, num) else: gr.addEdge(G,nobj1,nobj2,text,num,poi,ims) else: if(G.has_edge(nobj2,nobj1)): num = gr.getEdgeNum(G,nobj2,nobj1) num.append(label) poi = gr.getEdgePoint(G,nobj2,nobj1) poi.append(point) gr.setEdgePoint(G,nobj2,nobj1, poi) ims = gr.getEdgeImg(G,nobj2,nobj1) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj2,nobj1,ims) gr.setEdgeNum(G, nobj2, nobj1, num) else: gr.addEdge(G,nobj2, nobj1,text,num,poi,ims) elif(dir == 1): if(obj1[1][0] > obj2[1][0]): if(G.has_edge(nobj2,nobj1)): num = gr.getEdgeNum(G,nobj2,nobj1) num.append(label) poi = gr.getEdgePoint(G,nobj2,nobj1) poi.append(point) gr.setEdgePoint(G,nobj2,nobj1, poi) ims = gr.getEdgeImg(G,nobj2,nobj1) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj2,nobj1,ims) gr.setEdgeNum(G, nobj2, nobj1, num) else: gr.addEdge(G,nobj2, nobj1,text,num,poi,ims) else: if(G.has_edge(nobj1,nobj2)): num = gr.getEdgeNum(G,nobj1,nobj2) num.append(label) poi = gr.getEdgePoint(G,nobj1,nobj2) poi.append(point) gr.setEdgePoint(G,nobj1,nobj2, poi) ims = gr.getEdgeImg(G,nobj1,nobj2) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj1,nobj2,ims) gr.setEdgeNum(G, nobj1, nobj2, num) else: gr.addEdge(G,nobj1,nobj2,text,num,poi,ims) elif(dir == 2): if(obj1[1][1] > obj2[1][1]): if(G.has_edge(nobj1,nobj2)): num = gr.getEdgeNum(G,nobj1,nobj2) num.append(label) poi = gr.getEdgePoint(G,nobj1,nobj2) poi.append(point) gr.setEdgePoint(G,nobj1,nobj2, poi) ims = gr.getEdgeImg(G,nobj1,nobj2) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj1,nobj2,ims) gr.setEdgeNum(G, nobj1, nobj2, num) else: gr.addEdge(G,nobj1,nobj2,text,num,poi,ims) else: if(G.has_edge(nobj2,nobj1)): num = gr.getEdgeNum(G,nobj2,nobj1) num.append(label) poi = gr.getEdgePoint(G,nobj2,nobj1) poi.append(point) gr.setEdgePoint(G,nobj2,nobj1, poi) ims = gr.getEdgeImg(G,nobj2,nobj1) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj2,nobj1,ims) gr.setEdgeNum(G, nobj2, nobj1, num) else: gr.addEdge(G,nobj2, nobj1,text,num,poi,ims) elif(dir == 3): if(obj1[1][1] > obj2[1][1]): if(G.has_edge(nobj2,nobj1)): num = gr.getEdgeNum(G,nobj2,nobj1) num.append(label) poi = gr.getEdgePoint(G,nobj2,nobj1) poi.append(point) gr.setEdgePoint(G,nobj2,nobj1, poi) ims = gr.getEdgeImg(G,nobj2,nobj1) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj2,nobj1,ims) gr.setEdgeNum(G, nobj2, nobj1, num) else: gr.addEdge(G,nobj2,nobj1,text,num,poi,ims) else: if(G.has_edge(nobj1,nobj2)): num = gr.getEdgeNum(G,nobj1,nobj2) num.append(label) poi = gr.getEdgePoint(G,nobj1,nobj2) poi.append(point) gr.setEdgePoint(G,nobj1,nobj2, poi) ims = gr.getEdgeImg(G,nobj1,nobj2) ims.append(newI[minX:maxX,minY:maxY]) gr.setEdgeImg(G,nobj1,nobj2,ims) gr.setEdgeNum(G, nobj1, nobj2, num) else: gr.addEdge(G,nobj1,nobj2,text,num,poi,ims) return 1, imgOrg,label else: pass return 2, imgOrg, 0
import graph as gw g1 = gw.create() gw.disp(g1) g1 = gw.addVertex(g1, 'a') g1 = gw.addVertex(g1, 'b') gw.disp(gw.addEdge(g1, 'a', 'b')) gw.disp(gw.addEdge(g1, 'a', 'c')) g2 = gw.create() g2 = gw.addVertex(g2, 'c') gw.disp(gw.graphUnion(g1, g2, g1)) gw.disp(g1) gw.disp(gw.addVertex(g1, " hello")) gw.disp(gw.addVertex(g1, "[;]")) g3 = gw.create() gw.disp(gw.graphUnion(g1, g2, g3)) gw.destroy(g2) gw.destroy(g1)
import graph as gw g1 = gw.create() g1 = gw.addVertex(g1, 'v1') g1 = gw.addVertex(g1, 'v2') g1 = gw.addVertex(g1, 'v3') g1 = gw.addVertex(g1, 'v4') g1 = gw.addEdge(g1, 'v1', 'v2') g1 = gw.addEdge(g1, 'v1', 'v3') g1 = gw.addEdge(g1, 'v2', 'v1') null1 = gw.addEdge(g1, 'v1', 'v1') # self loop null1 = gw.addEdge(g1, 'v1', 'v2') # edge already exists print('printing g1:') gw.disp(g1) g2 = gw.create() g2 = gw.addVertex(g2, 'u1') g2 = gw.addVertex(g2, 'u2') g2 = gw.addVertex(g2, 'u3') g2 = gw.addVertex(g2, 'u4') g2 = gw.addEdge(g2, 'u1', 'u2') g2 = gw.addEdge(g2, 'u2', 'u1') print('printing g2:') gw.disp(g2) print('addEdge not exist vertex1:') null1 = gw.addEdge(g2, 'v1', 'u2') # edge not exists print('printing null1:') gw.disp(null1) print('addEdge not exist vertex2:') null1 = gw.addEdge(g2, 'u1', 'v2') # edge not exists print('addEdge not exist vertex1 and 2:') null1 = gw.addEdge(g2, 'v1', 'v2') # edge not exists
if m == 0: print(prefix) return for i in range(n): prefix.append(i) permutation_with_repetition(n, m - 1, prefix) prefix.pop() import graph import queue graph = graph.Graph() for i in range(5): graph.addVertex(i) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(1, 5) graph.addEdge(2, 3) graph.addEdge(2, 4) for v in graph: for nbr in v.getConnections(): pass path = list() def bfs(start, graph): start.setColor('Gray') distances = dict()
def discoverLoops(G,circles,imgOrg): lps = [] withLps = [] for i in range(len(circles)): cirA = circles[i][1] x0A = cirA[0] y0A = cirA[1] rA = cirA[2] queA = [] queA.append((x0A,(y0A+rA))) queA.append((x0A,(y0A-rA))) queA.append(((x0A+rA),y0A)) queA.append(((x0A-rA),y0A)) for j in range(i+1,(len(circles))): cirB = circles[j][1] x0B = cirB[0] y0B = cirB[1] rB = cirB[2] loopB = circles[j][2] queB = [] queB.append((x0B,(y0B+rB))) queB.append((x0B,(y0B-rB))) queB.append(((x0B+rB),y0B)) queB.append(((x0B-rB),y0B)) loop = False if(x0B > x0A-rA) and (x0B < x0A+rA) and (y0B+rB > y0A-rA) and (y0B+rB < y0A + rA): loop = True if(x0B > x0A-rA) and (x0B < x0A+rA) and (y0B-rB > y0A-rA) and (y0B-rB < y0A + rA): loop = True if(x0B+rB > x0A-rA) and (x0B+rB < x0A+rA) and (y0B > y0A-rA) and (y0B < y0A + rA): loop = True if(x0B-rB > x0A-rA) and (x0B-rB < x0A+rA) and (y0B > y0A-rA) and (y0B < y0A + rA): loop = True if(x0A > x0B-rB) and (x0A < x0B+rB) and (y0A+rA > y0B-rB) and (y0A+rA < y0B + rB): loop = True if(x0A > x0B-rB) and (x0A < x0B+rB) and (y0A-rA > y0B-rB) and (y0A-rA < y0B + rB): loop = True if(x0A+rA > x0B-rB) and (x0A+rA < x0B+rB) and (y0A > y0B-rB) and (y0A < y0B + rB): loop = True if(x0A-rA > x0B-rB) and (x0A-rA < x0B+rB) and (y0A > y0B-rB) and (y0A < y0B + rB): loop = True #quando o circulo encontrado para o loop esta muito próximo, mas nao tem interseção com o circulo do estado if(not loop): for k in range(0,4): for l in range(0,4): d = np.sqrt(pow(queA[k][0]-queB[l][0],2) + pow(queA[k][1]-queB[l][1],2)) if(d < 10): loop = True nCirA = str(x0A)+str(y0A) nCirB = str(x0B)+str(y0B) if (nCirA in lps): loop = False if (loop): pointA = 0 pointB = 0 num = [] num.append(-1) if (circles[i][2]): if(G.has_edge(nCirB,nCirB)): num = gr.getEdgeNum(G,nCirB,nCirB) num.append(-1) gr.setEdgeNum(G,nCirB,nCirB,num) poi = gr.getEdgePoint(G,nCirB,nCirB) poi.append(circles[j][5]) gr.setEdgePoint(G,nCirB,nCirB, poi) ims = gr.getEdgeImg(G,nCirB,nCirB) ims.append(circles[j][6]) gr.setEdgeImg(G,nCirB,nCirB,ims) else: gr.addEdge(G,nCirB,nCirB,'',num,[],[]) elif(circles[j][2]): if(G.has_edge(nCirA,nCirA)): num = gr.getEdgeNum(G,nCirA,nCirA) num.append(-1) gr.setEdgeNum(G,nCirA,nCirA,num) poi = gr.getEdgePoint(G,nCirA,nCirA) poi.append(circles[j][5]) gr.setEdgePoint(G,nCirA,nCirA, poi) ims = gr.getEdgeImg(G,nCirA,nCirA) ims.append(circles[j][6]) gr.setEdgeImg(G,nCirA,nCirA,ims) else: gr.addEdge(G,nCirA,nCirA,'',num,[],[]) else: if (circles[i][3]): pointA += 2 if (circles[j][3]): pointB += 2 if (circles[i][4] == '' and circles[j][4] != ''): pointB += 1 if (circles[i][4] != '' and circles[j][4] == ''): pointA += 1 if (nCirA in withLps): pointA += 1 if (nCirB in withLps): pointB += 1 if (pointA >= pointB): G.remove_node(nCirB) poi = [] poi.append(circles[j][5]) img = [] img.append(circles[j][6]) num = [] num.append(-1) if(G.has_edge(nCirA,nCirA)): num = gr.getEdgeNum(G,nCirA,nCirA) num.append(-1) gr.setEdgeNum(G,nCirA,nCirA,num) poi = gr.getEdgePoint(G,nCirA,nCirA) poi.append(circles[j][5]) gr.setEdgePoint(G,nCirA,nCirA, poi) ims = gr.getEdgeImg(G,nCirA,nCirA) ims.append(circles[j][6]) gr.setEdgeImg(G,nCirA,nCirA,ims) else: gr.addEdge(G,nCirA,nCirA,'',num,poi,img) gr.addNodeLoop(G,nCirA,cirB) lps.append(nCirB) if(nCirA not in withLps): withLps.append(nCirA) else: G.remove_node(nCirA) poi = [] poi.append(circles[i][5]) img = [] img.append(circles[i][6]) num = [] num.append(-1) if(G.has_edge(nCirB,nCirB)): num = gr.getEdgeNum(G,nCirB,nCirB) num.append(-1) gr.setEdgeNum(G,nCirB,nCirB,num) poi = gr.getEdgePoint(G,nCirB,nCirB) poi.append(circles[i][5]) gr.setEdgePoint(G,nCirB,nCirB, poi) ims = gr.getEdgeImg(G,nCirB,nCirB) ims.append(circles[i][6]) gr.setEdgeImg(G,nCirB,nCirB,ims) else: gr.addEdge(G,nCirB,nCirB,'',num,poi,img) gr.addNodeLoop(G,nCirB,cirA) lps.append(nCirA) if(nCirB not in withLps): withLps.append(nCirB) return G, imgOrg, lps, withLps