def test_shortest_path5(self): g1 = weighted_graph.WeightedGraph(2, [(0, 1, 228), (1, 0, 1)], True) self.assertEqual(g1.dijkstra(0, 1), (228, [0, 1])) self.assertEqual(g1.ford_bellman(0, 1), (228, [0, 1])) g2 = weighted_graph.WeightedGraph(2, [(0, 1, 228), (1, 0, 1)], False) self.assertEqual(g2.dijkstra(0, 1), (1, [0, 1])) self.assertEqual(g2.ford_bellman(0, 1), (1, [0, 1])) g3 = weighted_graph.WeightedGraph(2, [(0, 1, 228), (1, 0, -1)], False) self.assertEqual(g3.dijkstra(0, 1), None) self.assertEqual(g3.ford_bellman(0, 1), None) g4 = weighted_graph.WeightedGraph(2, [(0, 1, 228), (1, 0, -1)], True) self.assertEqual(g4.dijkstra(0, 1), None) self.assertEqual(g4.ford_bellman(0, 1), (228, [0, 1]))
def generate_worst_graph(n): edges = list() for i in range(n - 1): edges.append((i, i + 1, 0)) for j in range(i + 1, n): edges.append((i, j, n - i)) return weighted_graph.WeightedGraph(n, edges, False)
def generate_best_graph(n): edges = list() for i in range(n): for j in range(i): if j == 0 and i == 1: edges.append((j, i, 1)) else: edges.append((j, i, 2)) return weighted_graph.WeightedGraph(n, edges, False)
def measure_floyd(): f = open("floyd.txt", "w") for i in range(1, 100): print(i) f.write( str(i) + " " + "%.6f" % measurement.test( 20, weighted_graph.WeightedGraph( i, generators.generate_graph(i, True, True), True).floyd) + "\n") f.close()
def measure_random(): f = open("ford_bellman_random.txt", "w") for i in range(2, 100, 2): print(i) f.write( str(i) + " " + "%.6f" % measurement.test( 20, weighted_graph.WeightedGraph( i, generators.generate_connected_graph(i, True, True), False).ford_bellman, random.randint(0, i - 1), random.randint(0, i - 1)) + "\n") f.close()
def setUp(self): # Note: This test method tests add_edge(), add_node() and # the WeightedGraph constructor. self.empty_graph = weighted_graph.WeightedGraph() self.linear_graph = weighted_graph.WeightedGraph() for each_integer in range(0, 10): self.linear_graph.add_node(each_integer) # range(0, 10) gives 0 though 9 and len(that) gives 10 for each_index in range(1, len(self.linear_graph.node_list)): self.linear_graph.add_edge((each_index - 1), each_index) self.circular_graph = weighted_graph.WeightedGraph() for each_integer in range(0, 10): self.circular_graph.add_node(each_integer) # range(0, 10) gives 0 though 9 and len(that) gives 10 for each_index in range(1, len(self.circular_graph.node_list)): self.circular_graph.add_edge((each_index - 1), each_index) # Tie the graph chain together at the ends: self.circular_graph.add_edge(0, (len(self.circular_graph.node_list)-1))
def test(): graph = weighted_graph.WeightedGraph(5) graph.add_edge(0, 1, -1) graph.add_edge(0, 2, 4) graph.add_edge(1, 2, 3) graph.add_edge(1, 3, 2) graph.add_edge(1, 4, 2) graph.add_edge(3, 2, 5) graph.add_edge(3, 1, 1) graph.add_edge(4, 3, -3) bf = BellmanFord(graph, 0) bf.find_spt() print(bf.dist_to)
def generate_worst_graph(n): edges = list() for i in range(n): for j in range(i): edges.append((j, i, random.randint(-10000000, 10000000))) return weighted_graph.WeightedGraph(n, edges, False)
def measure_dijkstra_random(): f = open("dijkstra_random.txt", "w") for i in range(1, 500, 2): print(i) f.write(str(i) + " " + "%.6f" % measurement.test(20, weighted_graph.WeightedGraph(i, generators.generate_connected_graph(i, True), False).dijkstra, 0, i - 1) + "\n") f.close()
def test_both_traversals(self): # Random graphs have all the properties of predictable graphs, # and over a sufficiently large number of iterations they will # also test all the reasonable permutations of predictable graphs, # including ones with isolated vertices and webs. # This random graph code is demonstrated wordily by printing for each_pass in range(0, 100): random_graph = weighted_graph.WeightedGraph() random_node_count = random.randint(10, 100) # -1 to allow for single-node WeightedGraphs random_edge_count = random.randint(((random_node_count // 2) - 1), (random_node_count - 1)) for each_node_count in range(0, random_node_count): random_graph.add_node(each_node_count) for each_edge_count in range(0, random_edge_count): # This graph is by no means guaranteed to be fully connected. two_random_nodes = random.sample(random_graph.node_list, 2) # Note to self: add_edge() takes values, not Nodes. random_graph.add_edge(two_random_nodes[0].value, two_random_nodes[1].value) random_edge = random.sample(random_graph.edge_list, 1)[0] random_connected_node = random_edge.alpha_node deep_path = random_graph.depth_first_traversal( random_connected_node.value) broad_path = random_graph.breadth_first_traversal( random_connected_node.value) # Ensure the paths are full of ints: assert isinstance(deep_path[0], int) assert isinstance(broad_path[0], int) # Ensure the paths are no longer than the number of nodes: assert len(deep_path) <= random_node_count assert len(broad_path) <= random_node_count # Ensure paths are nonzero: assert len(deep_path) > 0 assert len(broad_path) > 0 # If the length of each path is equal to the length of # itself after duplicates have been removed, # it does not have any repeated values, # meaning it visited each node at most only once. assert len(set(deep_path)) == len(deep_path) assert len(set(broad_path)) == len(broad_path) with self.assertRaises(Exception): random_graph.breadth_first_traversal("g") random_graph.depth_first_traversal("g") random_graph.breadth_first_traversal(-3) random_graph.depth_first_traversal(-3) # Note that this is only guaranteed for len(path) and higher: random_graph.breadth_first_traversal(len(broad_path)) random_graph.depth_first_traversal(len(deep_path))
def test_shortest_path4(self): g = weighted_graph.WeightedGraph(4, [(0, 1, 1), (0, 2, 100000000), (2, 1, 1000000000), (2, 3, -1), (3, 2, -1)], True) self.assertEqual(g.dijkstra(0, 1), None) self.assertEqual(g.ford_bellman(0, 2), None)
def test_shortest_path2(self): g = weighted_graph.WeightedGraph(2, [(0, 1, -2), (1, 0, 1)], True) self.assertEqual(g.dijkstra(0, 1), None) self.assertEqual(g.ford_bellman(0, 1), None)
def test_shortest_path1(self): g = weighted_graph.WeightedGraph(3, [(0, 1, 3), (0, 2, 1), (2, 1, 1)], True) self.assertEqual(g.dijkstra(0, 1), (2, [0, 2, 1])) self.assertEqual(g.ford_bellman(0, 1), (2, [0, 2, 1]))