def randomDijkstra(n, p, wts=[1]): graph = Graph() V = [Vertex(x) for x in range(n)] for v in V: graph.addVertex(v) for v in V: for w in V: if v != w: if random() < p: graph.addDiEdge(v, w, wt=choice(wts)) return graph
def createRoadMap(g, N, obstacles, dMetric): """ Creates the roadmap of the scenario: -g is the graph on which the obstacles are to be stored. -N the number of points. -obstacles is the obstacles' coordinates. -dMetric: determines how near the points should be to one another. """ r.seed() for i in range(N): x = r.uniform(-7.0, 7.0) y = r.uniform(-7.0, 7.0) discard = False for obs in obstacles: if m.sqrt((obs[0] - x)**2 + (obs[1] - y)**2) <= obs[2]: discard = True if not discard: g.addVertex((x, y), dMetric)
def randomAStarGraph(n, p, wts=[1]): graph = Graph() minLat = -20.0 maxLat = 50.0 minLong = -150.0 maxLong = -60.0 V = [] for x in range(n): # generate random latitude and longitude latitude = uniform(minLat, maxLat) longitude = uniform(minLong, maxLong) V.append(AStarVertex(1, latitude, longitude)) for v in V: graph.addVertex(v) for v in V: for w in V: if v != w: if random() < p: graph.addDiEdge(v, w, wt=choice(wts)) return graph
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)
#! /usr/bin/python import graph #set up graph graph = graph.Graph() vertex = {'A', 'B', 'C', 'D', 'E', 'F'} for v in vertex: graph.addVertex(v) edges = {('A', 'B', 1), ('A', 'C', 2), ('B', 'D', 3), ('B', 'E', 2), ('C', 'F', 2)} for e in edges: graph.addUndirectedEdge(e[0], e[1], e[2]) #breadth first +greedy def dijkstras(graph, edges): #set up unvisited = queue.Queue() distance = [] #add each key to the unvisited queue for v in graph: unvisited.add(v) distance[v] = float("inf") current = unvisited.pop distance[current] = 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
prefix = prefix or [] 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')
# 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)