def test_find_shortest_path(self): filename = 'test_files/graph_medium_undirected.txt' graph = read_graph_from_file(filename) path_from_A_to_F = graph.find_shortest_path('A', 'F') self.assertEqual(len(path_from_A_to_F), 4)
def test_medium_undirected_graph(self): filename = 'test_files/graph_medium_undirected.txt' graph = read_graph_from_file(filename) vertices = graph.get_vertices() self.assertEqual(len(vertices), 6) self.assertEqual( tuple(sorted(map(lambda vertex: vertex.get_id(), vertices))), ('A', 'B', 'C', 'D', 'E', 'F')) self.assertFalse(graph.is_directed) self.assertEqual(objs_to_ids(graph.get_vertex('A').get_neighbors()), ('B', 'C')) self.assertEqual(objs_to_ids(graph.get_vertex('B').get_neighbors()), ('A', 'C', 'D')) self.assertEqual(objs_to_ids(graph.get_vertex('C').get_neighbors()), ('A', 'B', 'D', 'E')) self.assertEqual(objs_to_ids(graph.get_vertex('D').get_neighbors()), ('B', 'C', 'E', 'F')) self.assertEqual(objs_to_ids(graph.get_vertex('E').get_neighbors()), ( 'C', 'D', 'F', )) self.assertEqual(objs_to_ids(graph.get_vertex('F').get_neighbors()), ('D', 'E'))
def test_simple_scc(self): filename = 'test_files/graph_medium_directed_cyclic.txt' graph = read_graph_from_file(filename) scc = graph.strongly_connected_components() for actual, expected in zip(scc, (('A', 'E'), ('B', 'C', 'F'), ('D', 'G', 'H'))): with self.subTest(expected): self.assertCountEqual(actual, expected)
def test_read_undirected_graph_from_file(self): filename = 'test_files/graph_small_undirected.txt' graph = read_graph_from_file(filename) self.assertEqual(len(graph.get_vertices()), 4) self.assertEqual(len(graph.get_neighbors('1')), 1) self.assertEqual(len(graph.get_neighbors('2')), 2) self.assertEqual(len(graph.get_neighbors('3')), 1) self.assertEqual(len(graph.get_neighbors('4')), 2)
def test_get_all_vertices_n_away(self): filename = 'test_files/graph_medium_undirected.txt' graph = read_graph_from_file(filename) vertices_1_away = graph.find_vertices_n_away('A', 1) self.assertEqual(sorted(vertices_1_away), ['B', 'C']) vertices_2_away = graph.find_vertices_n_away('A', 2) self.assertEqual(sorted(vertices_2_away), ['D', 'E']) vertices_3_away = graph.find_vertices_n_away('A', 3) self.assertEqual(vertices_3_away, ['F'])
def test_read_directed_graph_from_file(self): filename = 'test_files/graph_small_directed.txt' graph = read_graph_from_file(filename) self.assertEqual(len(graph.get_vertices()), 4) vertex1 = graph.get_vertex('1') vertex2 = graph.get_vertex('2') vertex3 = graph.get_vertex('3') vertex4 = graph.get_vertex('4') self.assertEqual(len(vertex1.get_neighbors()), 1) self.assertEqual(len(vertex2.get_neighbors()), 1) self.assertEqual(len(vertex3.get_neighbors()), 1) self.assertEqual(len(vertex4.get_neighbors()), 0)
def test_small_directed_graph(self): filename = 'test_files/graph_small_directed.txt' graph = read_graph_from_file(filename) vertices = graph.get_vertices() self.assertEqual(len(vertices), 4) self.assertEqual(objs_to_ids(vertices), ('1', '2', '3', '4')) self.assertTrue(graph.is_directed) self.assertEqual(objs_to_ids(graph.get_vertex('1').get_neighbors()), ('2', )) self.assertEqual(objs_to_ids(graph.get_vertex('2').get_neighbors()), ('4', )) self.assertEqual(objs_to_ids(graph.get_vertex('3').get_neighbors()), ('4', )) self.assertEqual(objs_to_ids(graph.get_vertex('4').get_neighbors()), ())
def test_improper_graph_type(self): filename = 'test_files/improper_graph_type.txt' with self.assertRaises(ValueError) as error: graph = read_graph_from_file(filename)
# graph.add_vertex('E') # graph.add_vertex('B') # graph.add_vertex('C') # graph.add_vertex('D') # graph.add_vertex('F') # graph.add_vertex('G') # # Add connections # graph.add_edge('A', 'B') # graph.add_edge('B', 'C') # graph.add_edge('B', 'D') # graph.add_edge('D', 'E') # graph.add_edge('F', 'G') # Or, read a graph in from a file graph = read_graph_from_file('test_files/graph_medium_undirected.txt') # Output the vertices & edges # Print vertices print(f'The vertices are: {graph.get_vertices()} \n') # Print edges print('The edges are:') for vertex_obj in graph.get_vertices(): for neighbor_obj in vertex_obj.get_neighbors(): print(f'({vertex_obj.get_id()} , {neighbor_obj.get_id()})') # Search the graph print('Performing BFS traversal...') graph.bfs_traversal('A')
def test_cyclic_topological_sort(self): filename = 'test_files/graph_medium_directed_cyclic.txt' graph = read_graph_from_file(filename) with self.assertRaises(ValueError): graph.topological_sort()
def test_contains_cycle_medium(self): filename = 'test_files/graph_medium_directed_cyclic.txt' graph = read_graph_from_file(filename) self.assertTrue(graph.contains_cycle())
# Driver code if __name__ == '__main__': import sys if len(sys.argv) == 1: # Create the graph graph = Graph(is_directed=True) # Add some vertices vertices = ["A", "E", "B", "C", "D", "H", "G", "F"] for vertex in vertices: graph.add_vertex(vertex) # Add connections edges = [('A', 'B'), ('B', 'C'), ('B', 'D'), ('D', 'E'), ('E', 'F'), ('H', 'G')] for edge in edges: v1, v2 = edge graph.add_edge(v1, v2) # print graph to stdout print_graph(graph) else: filename = sys.argv[1] # Create graph from file graph = read_graph_from_file(filename) # print graph to stdout print_graph(graph)
# graph.add_vertex('B') # graph.add_vertex('C') # graph.add_vertex('D') # graph.add_vertex('F') # graph.add_vertex('G') # # Add connections # graph.add_edge('A', 'B') # graph.add_edge('B', 'C') # graph.add_edge('B', 'D') # graph.add_edge('D', 'E') # graph.add_edge('F', 'G') # graph.add_edge('C', 'A') # Or, read a graph in from a file graph = read_graph_from_file('test_files/graph_small_undirected.txt') # Check if the graph is bipartite print(graph.is_bipartite()) # Show the connected components(Not currently working correctly) # print("Connected components: {}".format(graph.get_connected_components())) # Find the neighbors vert2 = graph.get_vertex('2') neighbors = vert2.get_neighbors() print(neighbors) vert4 = graph.get_vertex('4') neighbors = vert4.get_neighbors() print(neighbors)
def test_improper_graph_type(self): """Read an improper graph from a file (should throw a ValueError).""" filename = 'test_files/improper_graph_type.txt' with self.assertRaises(ValueError) as error: graph = read_graph_from_file(filename)
graph.add_vertex('A') graph.add_vertex('E') graph.add_vertex('B') graph.add_vertex('C') graph.add_vertex('D') graph.add_vertex('F') graph.add_vertex('G') # Add connections graph.add_edge('A', 'B') graph.add_edge('B', 'C') graph.add_edge('B', 'D') graph.add_edge('D', 'E') graph.add_edge('F', 'G') else: graph = read_graph_from_file("test_files\graph_medium_undirected.txt") # Or, read a graph in from a file # graph = read_graph_from_file('test_files/graph_small_directed.txt') # Output the vertices & edges # Print vertices print(f'The vertices are: {graph.get_vertices()} \n') # Print edges print('The edges are:') for vertex_obj in graph.get_vertices(): for neighbor_obj in vertex_obj.get_neighbors(): print(f'({vertex_obj.get_id()} , {neighbor_obj.get_id()})') # Find whether the graph is bipartite