def createLinkedList(n): graph = WeightedGraph() if n > 0: graph.addNode(1) i = 2 while i <= n: graph.addNode(i) graph.addWeightedEdge(i - 1, i, 1) i = i + 1 return graph
def createWeightedLinkedList(node_count=10, weight=1): self = WeightedGraph() for i in range(node_count): self.addNode(i) if i > 0: self.nodes[i - 1].addEdge(self.nodes[i], 1, weight) return self
def createRandomCompleteWeightedGraph(n: int) -> WeightedGraph: graph = WeightedGraph() for i in range(n): graph.addNode(i) nodes = graph.getAllNodes() for node in nodes: for _node in nodes: if node is not _node: graph.addWeightedEdge(node, _node, randint(0, 2000)) return graph
def createRandomCompleteWeightedGraph(n): graph = WeightedGraph() for i in range(n): graph.addNode(i) weightUpperbound = n**2 for node in graph.getAllNodes(): for i in range(n): if node != i: graph.addWeightedEdge(node, i, random.randint(1, weightUpperbound)) return graph
def createLinkedList(n): linkedGraph = WeightedGraph() for i in range(0, n, 1): linkedGraph.addNode(i) if i == 0: continue else: linkedGraph.addWeightedEdge(i - 1, i, 1) return linkedGraph
def createLinkedList(n): graph = WeightedGraph() for i in range(n): graph.addNode(i) for i in range(n - 1): graph.addWeightedEdge(i, i + 1, 1) return graph
def _createLinkedList(n: int) -> WeightedGraph: graph = WeightedGraph() if not n: return Graph prev = graph.addNode(0) for i in range(1, n): curr = graph.addNode(i) graph.addWeightedEdge(prev, curr, randint(0, 2000)) prev = curr return graph
def createLinkedList(n): newGraph = WeightedGraph() for i in range(n): newGraph.addNode(i) # If graph is not empty if i != 0: # Link the previous node to the last node newGraph.addWeightedEdge(newGraph.vertices[i - 1], newGraph.vertices[i], 1) return newGraph
def createRandomCompleteWeightedGraph(node_count=10): self = WeightedGraph() for n in range(node_count): self.addNode(n) for node in self.nodes: # Connect to every other node: for connect in self.nodes: if node is not connect: # If not self self.addWeightedEdge( node.val, connect.val, random.randrange(0, int(node_count / 3)) ) # Creates edge with random weight in between 0 and one third the number of nodes return self
def createRandomCompleteWeightedGraph(n): graph = WeightedGraph() for i in range(0, n, 1): graph.addNode(i) for node in graph.allNodes: for num in range(0, n, 1): if node == num: continue else: graph.addWeightedEdge(node, num, randint(1, n**2)) return graph
def createRandomCompleteWeightedGraph(n): # Create and populate the graph newGraph = WeightedGraph() for k in range(n): newGraph.addNode(k) # Create edges between all the nodes in the graph for currIndex in range(len(newGraph.vertices)): for secondaryIndex in range(len(newGraph.vertices)): if currIndex != secondaryIndex: newGraph.addWeightedEdge(newGraph.vertices[currIndex], newGraph.vertices[secondaryIndex], random.randint(1, 100)) return newGraph
def createRandomCompleteWeightedGraph(n): graph = WeightedGraph() i = 1 while i <= n: graph.addNode(i) i = i + 1 l = 1 while l <= n: nodeVal = 1 + l while nodeVal <= n: w = random.randrange(1, 10, 1) graph.addWeightedEdge(l, nodeVal, w) nodeVal = nodeVal + 1 l = l + 1 #if (l != nodeVal): #return return graph
def runDijkstras(graph: WeightedGraph, start: int) -> Dict: print("--- Dijkstras From Node", start, "---") nodes = graph.getAllNodes() list_of_nodes = list(nodes) list_of_nodes.sort(key=lambda x: x.nodeVal) return dijkstras(list_of_nodes[start])
def testSP(): nodes = [] for name in range(6): nodes.append(Node('ノード' + str(name))) g = WeightedGraph() for n in nodes: g.addNode(n) g.addEdge(WeightedEdge(nodes[0], nodes[1], weight=100.0)) g.addEdge(WeightedEdge(nodes[0], nodes[2], weight=110.0)) g.addEdge(WeightedEdge(nodes[1], nodes[3], weight=140.0)) g.addEdge(WeightedEdge(nodes[2], nodes[3], weight=120.0)) g.addEdge(WeightedEdge(nodes[0], nodes[4], weight=30.0)) g.addEdge(WeightedEdge(nodes[4], nodes[5], weight=50.0)) g.addEdge(WeightedEdge(nodes[5], nodes[3], weight=10.0)) sp, weight = weightedBFS(g, nodes[0], nodes[3], toPrint=True) print('重みづけされたグラフに対するBFSで発見された最短経路の重み合計:', weight) print('重みづけされたグラフに対するBFSで発見された最短経路:', printWeightedPath(sp))
if __name__ == "__main__": start = time.perf_counter() # Graph: graph = Graph() graph.addNode('0') graph.addNode('1') graph.addNode('2') graph.addNode('3') # Directed Graph: dir_graph = createRandomDAGIter() print(dir_graph) # Weighted Graph: weighted_graph = WeightedGraph() weighted_graph = createRandomCompleteWeightedGraph(10) print(weighted_graph) # Weighted Linked List: wll = createWeightedLinkedList(10, 2) print(wll) # Random unweighted graph: print("Random Unwighted graph:") graph = createRandomUnweightedGraphIter(200) print(graph) # Gridgraph: print("Grid Graph:") gGraph = createRandomGridGraph(8)