def testOneRoute(self): adjList = AdjacencyList() addingRoute = Route(orig = 1, dest = 2, distance = 20) adjList.add(addingRoute) queue = adjList.getPQ() onlyRoute = queue.get() self.assertEqual(addingRoute, onlyRoute) self.assertTrue(queue.empty())
def testAddFirstRoute(self): adjList = AdjacencyList() newRoute = Route(orig = 1, dest = 2, distance = 567) self.assertIsNone(adjList.findRoutePair(newRoute.orig, newRoute.dest)) self.assertTrue(adjList.add(newRoute)) routeFoundInList = adjList.findRoutePair(newRoute.orig, newRoute.dest) self.assertIsNotNone(routeFoundInList) self.assertEqual(routeFoundInList.orig, newRoute.orig) self.assertEqual(routeFoundInList.dest, newRoute.dest) self.assertEqual(routeFoundInList.distance, newRoute.distance)
def main(): covid19 = covid19_Simulation() al = DLList.DLList() for population in range(covid19.people): person = Person() al.append(person) #With lockdown g1 = AdjacencyList.AdjacencyList(covid19.people) #Without lockdown g2 = AdjacencyList.AdjacencyList(covid19.people) covid19.getInteractionGraph(g1, al) covid19.initialInfectedNodes(g1, al) covid19.simulation(g1, 0, al) matplot.show()
def __init__(self): self.bookCatalog = ArrayList.ArrayList() self.shoppingCart = ArrayQueue.ArrayQueue() self.indexKey = ChainedHashTable.ChainedHashTable() self.indexSortedPrefix = BinarySearchTree.BinarySearchTree() self.bookSortedCatalog = ArrayList.ArrayList() self.similaGraph = AdjacencyList.AdjacencyList(0)
def get_max_branching(self, root, adj_list): reverse_adj = adj_list.get_reverse_list() if reverse_adj.get_adjacent[root] is not None: reverse_adj.get_adjacent[root] = [] out_edges = AdjacencyList() for n in reverse_adj: in_edges = reverse_adj.get_adjacent(n) if len(in_edges) == 0: continue max_weight = in_edges[0] for e in in_edges: if e.w > max_weight.w: max_weight = e out_edges.add_edge(max_weight.t, max_weight.f, max_weight.w)
def readRouteData(fileObject): adjList = AdjacencyList() queue = PriorityQueue() while True: s = fileObject.readline() if not s: fileObject.close() break s = s[0:-1] split = re.split("\s+", s) try: adjList.add(Route(int(split[0]), int(split[1]), float(split[2]))) adjList.add(Route(int(split[1]), int(split[0]), float(split[2]))) queue.put(Route(int(split[0]), int(split[1]), float(split[2]))) except: fileObject.close() break fileObject.close() return adjList, queue
def testAddBeyondFirst(self): adjList = AdjacencyList() newRoute1 = Route(orig = 1, dest = 2, distance = 567) adjList.add(newRoute1) newRoute2 = Route(orig = 2, dest = 3, distance = 784) adjList.add(newRoute2) self.assertEqual(adjList.findRoutePair(newRoute1.orig, newRoute1.dest), newRoute1) self.assertEqual(adjList.findRoutePair(newRoute2.orig, newRoute2.dest), newRoute2)
def testMultipleNeighbors(self): adjList = AdjacencyList() newRoute1 = Route(orig = 1, dest = 2, distance = 567) newRoute2 = Route(orig = 1, dest = 77, distance = 99) newRoute3 = Route(orig = 1, dest = 3222, distance = 0.54) adjList.add(newRoute1) adjList.add(newRoute2) adjList.add(newRoute3) self.assertEqual(adjList.findAllNeighbors(1), [2, 77, 3222])
def load(cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ): file = open(fileName, 'r+') numberOfCities = int(file.readline()) #read how many cities there are cities = [] cityMap = {} cityMapInvert = {} list = AdjacencyList() for i in range (1, numberOfCities+1): #read the names of the cities s = file.readline()[0:-1] cities.append(s) cityMap[s] = int(i) cityMapInvert[i] = s distPQ = DistPQ() pricePQ = PricePQ() while True: #read the route information s = file.readline() if not s: file.close() break s = s[0:-1] split = re.split('\s+', s) try: list.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) list.add(Route(int(split[1]), int(split[0]), float(split[2]), float(split[3]))) distPQ.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) pricePQ.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) except: file.close() break return cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ
def testMatchingRoute(self): adjList = AdjacencyList() route1 = Route(orig = 1, dest = 2, distance = 20) route2 = Route(orig = 1, dest = 3, distance = 40) adjList.add(route1) adjList.add(route2) adjList = adjList.removeFromList(orig = 1, dest = 2) self.assertIsNone(adjList.findRoutePair(orig = 1, dest = 2)) self.assertEqual(adjList.findRoutePair(orig = 1, dest = 3), route2)
def test_graph(self): points = 0.5 q = AdjacencyMatrix.AdjacencyMatrix(4) try: q.remove_edge(1, 2) q.has_edge(2, 3) q.add_edge(0, 1) q.add_edge(1, 2) q.add_edge(2, 3) q.add_edge(3, 0) q.add_edge(0, 2) self.assertAlmostEqual(q.has_edge(0, 1), True) self.assertAlmostEqual(q.has_edge(1, 3), False) print("AdjacencyMatrix in_edges(2)", q.in_edges(2)) print("AdjacencyMatrix out_edges(0)", q.out_edges(0)) points += 0.5 except: print("AdjacencyMatrix is not correct") q = AdjacencyList.AdjacencyList(4) try: q.remove_edge(1, 2) q.has_edge(2, 3) q.add_edge(0, 1) q.add_edge(1, 2) q.add_edge(2, 3) q.add_edge(3, 0) q.add_edge(0, 2) self.assertAlmostEqual(q.has_edge(0, 1), True) self.assertAlmostEqual(q.has_edge(1, 3), False) print("AdjacencyList in_edges(2)", q.in_edges(2)) print("AdjacencyList out_edges(0)", q.out_edges(0)) points += 0.5 print("BFS(0)") q.bfs(0) print("DFS(0)") q.dfs(0) points += 0.5 except: print("AdjacencyList is not correct") print(f"Graph: {points} Points")
def loadCatalog(self, fileName: str): #Graphs self.indexKeys = ChainedHashTable.ChainedHashTable() self.bookCatalog = DLList.DLList() #or arraylist with open(fileName, encoding='utf-8') as f: start_time = time.time() count = 0 #line number for line in f: (key, title, group, rank, similar) = line.split("^") b = Book.Book(key, title, group, rank, similar) self.bookCatalog.append(b) self.indexKeys.add(b.key, count) #self.indexKeys.add(key, line) count += 1 elapsed_time = time.time() - start_time print( f"Loaded {self.bookCatalog.size()} books into bookCatalog in {elapsed_time} seconds" ) #self.similar_graph() #def similar_graph(self,k : int): self.similarGraph = AdjacencyList.AdjacencyList( self.bookCatalog.size()) with open(fileName, encoding='utf-8') as f: start_time = time.time() count = 0 for line in f: (key, title, group, rank, similar) = line.split("^") l = similar.split() for k in range(1, len(l)): j = self.indexKeys.find(l[k]) #print(j) if j != None: #is not self.similarGraph.add_edge(count, j) count += 1 elapsed_time = time.time() - start_time print( f"Loaded {self.similarGraph.n} books into Graph in {elapsed_time} seconds" )
def testMoreExpensivePath(self): numCities = 3 menu = Menu() cities = ["Honolulu", "Tokyo", "Los Angeles"] cityMap = {'Honolulu': 1, "Tokyo": 2, "Los Angeles": 3} cityMapInvert = {1: "Honolulu", 2: "Tokyo", 3: "Los Angeles"} choice = 3 #2 is distance, 3 is segments queue = PriorityQueue() adjList = AdjacencyList() #Note HNL-Tokyo is 500, while HNL-LA-Tokyo is 450 hnlToTokyo = Route(orig = 1, dest = 2, distance = 500) hnlToLA = Route(orig = 1, dest = 3, distance = 350) LAtoTokyo = Route(orig = 3, dest = 2, distance = 100) queue.put(hnlToTokyo) queue.put(hnlToLA) queue.put(LAtoTokyo) adjList.add(hnlToTokyo) adjList.add(hnlToLA) adjList.add(LAtoTokyo) self.assertEqual(dijkstra(numCities, cities, cityMap, queue, choice, cityMapInvert, adjList, menu, userInput = ["Honolulu", "Tokyo"]), ["Honolulu", "Tokyo"])
def testMultipleRoutes(self): adjList = AdjacencyList() addRoute1 = Route(orig = 1, dest = 2, distance = 20) addRoute2 = Route(orig = 2, dest = 3, distance = 50) addRoute3 = Route(orig = 2, dest = 4, distance = 30) #Sorted by ascending distance routesByDist = [addRoute1, addRoute3, addRoute2] adjList.add(addRoute1) adjList.add(addRoute2) adjList.add(addRoute3) queue = adjList.getPQ() #Match the routes and ordering of those routes for i in range(0, len(routesByDist)): pqEntry = queue.get() self.assertEqual(pqEntry, routesByDist[i]) #There should be any additional routes in the PQ. self.assertTrue(queue.empty())
print(a) ''' ''' print(c) algorithms.quick_sort(c) print(c) ''' #Lab 7 Larry Delgado import AdjacencyMatrix import AdjacencyList import ArrayQueue q = ArrayQueue.ArrayQueue() q.add(0) print(q) al = AdjacencyList.AdjacencyList(5) al.add_edge(1, 2) al.add_edge(2, 3) al.add_edge(3, 4) al.add_edge(4, 1) al.add_edge(1, 3) print(al.out_edges(1)) ''' am = AdjacencyMatrix.AdjacencyMatrix(5) am.add_edge(1,2) am.add_edge(2,3) am.add_edge(3,4) am.add_edge(4,1) am.add_edge(1,3) print(am.has_edge(1,2))
def __init__(self): self.graph = AdjacencyList(True) self.graphPrepare()
class TopologicalSorting: def __init__(self): self.graph = AdjacencyList(True) self.graphPrepare() def graphPrepare(self): self.graph.addVertex(1) self.graph.addVertex(2) self.graph.addVertex(3) self.graph.addVertex(4) self.graph.addVertex(5) self.graph.addVertex(6) self.graph.addVertex(7) self.graph.addEdge(1,3) self.graph.addEdge(2,3) self.graph.addEdge(3,5) self.graph.addEdge(2,4) self.graph.addEdge(5,6) self.graph.addEdge(4,6) self.graph.addEdge(6,7) def initSorting(self): self.visited = {} self.reversedTopologicalArray = [] self.topologicalArray = [] self.allVertexes = self.graph.vertexs.keys() for key in self.allVertexes: try: self.visited[key] continue except: self.visited[key] = True self.dfs(key, 0) self.reversedTopologicalArray.append(key) self.topologicalArray = self.reversedTopologicalArray.copy() self.topologicalArray.reverse() def dfs(self, vertex, number=0): try: while True: value = self.graph.vertexs[vertex][number] try: self.visited[value] number = number + 1 except: self.visited[value] = True self.dfs(value, 0) self.reversedTopologicalArray.append(value) break except: return None
def testNoRoutes(self): adjList = AdjacencyList() self.assertTrue(adjList.removeFromList(orig = 1, dest = 2).empty())
def testNoRoutes(self): adjList = AdjacencyList() queue = adjList.getPQ() self.assertTrue(queue.empty())
class GraphColoring: def __init__(self, startVertex=1): self.graph = AdjacencyList(False) self.graphPrepare() self.startVertex = startVertex def graphPrepare(self): # self.graph.addVertex(1) # self.graph.addVertex(2) # self.graph.addVertex(3) # self.graph.addVertex(4) # self.graph.addVertex(5) # self.graph.addVertex(6) # self.graph.addVertex(7) # self.graph.addVertex(8) # self.graph.addVertex(9) # self.graph.addVertex(10) # self.graph.addEdge(1,2) # self.graph.addEdge(1,3) # self.graph.addEdge(1,4) # self.graph.addEdge(2,4) # self.graph.addEdge(3,4) # self.graph.addEdge(5,6) # self.graph.addEdge(4,5) # self.graph.addEdge(4,6) # self.graph.addEdge(6,8) # self.graph.addEdge(2,7) # self.graph.addEdge(7,8) # self.graph.addEdge(3,9) # self.graph.addEdge(9,10) # self.graph.addEdge(2,10) # self.graph.addVertex(1) # self.graph.addVertex(2) # self.graph.addVertex(3) # self.graph.addVertex(4) # self.graph.addVertex(5) # self.graph.addVertex(6) # self.graph.addEdge(1,2) # self.graph.addEdge(1,3) # self.graph.addEdge(1,4) # self.graph.addEdge(1,6) # self.graph.addEdge(2,3) # self.graph.addEdge(2,4) # self.graph.addEdge(2,5) # self.graph.addEdge(3,4) # self.graph.addEdge(3,5) # self.graph.addEdge(4,5) # self.graph.addEdge(5,6) self.graph.addVertex(1) self.graph.addVertex(2) self.graph.addVertex(3) self.graph.addVertex(4) self.graph.addVertex(5) self.graph.addVertex(6) self.graph.addVertex(7) self.graph.addVertex(8) self.graph.addVertex(9) self.graph.addEdge(1, 2) self.graph.addEdge(1, 4) self.graph.addEdge(2, 5) self.graph.addEdge(2, 3) self.graph.addEdge(3, 6) self.graph.addEdge(4, 5) self.graph.addEdge(4, 7) self.graph.addEdge(5, 6) self.graph.addEdge(5, 8) self.graph.addEdge(6, 9) self.graph.addEdge(7, 8) self.graph.addEdge(8, 9) def colorGraph(self): self.colorObj = {} self.totalColor = 1 self.reachedVertex = {} self.queue = Queue() self.queue.push(self.startVertex) self.reachedVertex[self.startVertex] = True self.colorObj[self.startVertex] = { "parent": None, "color": self.totalColor } self.colorAssigner() def colorAssigner(self): value = self.queue.pop() if value == None: return parentColor = self.colorObj[value]["color"] foundColor = {} for val in self.graph.vertexs[value]: try: color = self.colorObj[val]["color"] foundColor[color] = True if color == parentColor: for i in range(1, self.totalColor + 2): try: foundColor[i] except: parentColor = self.colorObj[value]["color"] = i if self.totalColor < i: self.totalColor = i break except: color = 1 if parentColor == 1: color = 2 if self.totalColor < 2: self.totalColor = 2 self.colorObj[val] = {"parent": value, "color": color} foundColor[color] = True self.queue.push(val) self.colorAssigner()
def testNonMatchingRoute(self): adjList = AdjacencyList() adjList.add(Route(orig = 1, dest = 2, distance = 20)) self.assertEqual(adjList.removeFromList(orig = 3, dest = 4), adjList)
def testAddNone(self): adjList = AdjacencyList() self.assertFalse(adjList.add(None))
def testNoNeighbors(self): adjList = AdjacencyList() self.assertEqual(adjList.findAllNeighbors(1), [])
distPQ.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) pricePQ.add(Route(int(split[0]), int(split[1]), float(split[2]), float(split[3]))) except: file.close() break return cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ #MAIN STARTS HERE #---------------------------------------------------------------------------------------- fileName = "routes.txt" cities = [] cityMap = {} cityMapInvert = {} list = AdjacencyList() numberOfCities = 0 distPQ = DistPQ() pricePQ = PricePQ() menuMax = 12 cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ = load(cities, cityMap, cityMapInvert, numberOfCities, fileName, list, distPQ, pricePQ) menu = menuInput(menuMax) while menu > 0: if menu == 1: #print the route list list.printList(cityMapInvert) elif menu == 2: #minimum spanning tree kruskalMST(numberOfCities, cityMapInvert, distPQ)
def __init__(self, startVertex=1): self.graph = AdjacencyList(False) self.graphPrepare() self.startVertex = startVertex
def testOneNeighbor(self): adjList = AdjacencyList() newRoute = Route(orig = 1, dest = 2, distance = 567) adjList.add(newRoute) self.assertEqual(adjList.findAllNeighbors(1), [2])