def strongly_connected_components(filepath): """ Method to calculate the strongly connected components in the graph and write it in an output file Expects: filepath (filename is sufficient if file is in the same directory else absolute file path is required) Effects: Generates the output file with name output_filename in the same directory """ generator = GraphGenerator(filepath) graph = generator.graph_from_text_file() dfs_1 = DFS(graph) dfs_1.dfs() # graph after first run of dfs with vertices ordered by increasing # finishing time. dfs_1.dfs_graph_from_call_stack() # graph after transpose of the original graph dfs_1_trans_graph = dfs_1.dfs_graph.transpose() # # Second run of dfs on transposed graph dfs_2 = DFS(dfs_1_trans_graph) dfs_2.dfs() dfs_2.dfs_graph_from_call_stack() filename = filepath.split("/")[-1] file_obj = open("output_" + filename, "w") write_dfs_components(file_obj, dfs_2.dfs_forest) file_obj.close()
def run_tests(filepath): """ runs all the tests for the program """ generator = GraphGenerator(filepath) graph = generator.graph_from_text_file() # test for object references in original graph print "TEST: Testing object reference on original graph" print object_references(graph) dfs_1 = DFS(graph) dfs_1.dfs() # graph after first run of dfs with vertices ordered by increasing # finishing time. dfs_1.dfs_graph_from_call_stack() # test for object reference after first dfs run print "TEST: Testing object reference on dfs 1 graph" print object_references(dfs_1.dfs_graph) print "Printing DFS 1 Graph" print dfs_1.dfs_graph print "*****************************************" # graph after transpose of the original graph dfs_1_trans_graph = dfs_1.dfs_graph.transpose() print "Printing transpose of DFS 1 Graph" print dfs_1_trans_graph print "*****************************************" # # Second run of dfs on transposed graph dfs_2 = DFS(dfs_1_trans_graph) dfs_2.dfs() dfs_2.dfs_graph_from_call_stack() # test for object reference after first dfs run print "TEST: Testing object reference on dfs 2 graph" print object_references(dfs_2.dfs_graph) print "Printing DFS 2 Graph" print dfs_2.dfs_graph print "*****************************************" print "Printing DFS components" print "*****************************************" print_dfs_components(dfs_2.dfs_forest)
def main(): g = MyGraph(100) graph = g.generate() # graph = g.static() d = DFS() v = d.dfs(g.graph, 1) print v g.render()
class TestDFS(unittest.TestCase): def setUp(self): self.dfs = DFS() self.graph = Graph() self.graph.insert_edge(create_edge(0, 1, 10)) self.graph.insert_edge(create_edge(1, 3, 10)) self.graph.insert_edge(create_edge(0, 2, 20)) self.graph.insert_edge(create_edge(0, 3, 30)) self.graph.insert_edge(create_edge(2, 3, 60)) self.graph.insert_edge(create_edge(3, 4, 120)) def test_dfs(self): result = self.dfs.dfs(0, self.graph) expected = [0, 1, 3, 2, 4] self.assertEqual(expected, result)
def main(win, width): ROWS = 40 grid = make_grid(ROWS, width) start = None end = None run = True started = False while run: draw(win, grid, ROWS, width) for event in pygame.event.get(): # Close the window if event.type == pygame.QUIT: run = False if started: continue # Left Mouse Click if pygame.mouse.get_pressed()[0]: pos = pygame.mouse.get_pos() row, col = get_clicked_pos(pos, ROWS, width) spot = grid[row][col] if not start and spot != end: start = spot start.make_start() elif not end and spot != start: end = spot end.make_end() elif spot != end and spot != start: spot.make_barrier() elif pygame.mouse.get_pressed()[2]: pos = pygame.mouse.get_pos() row, col = get_clicked_pos(pos, ROWS, width) spot = grid[row][col] spot.reset() if spot == start: start = None elif spot == end: end = None if event.type == pygame.KEYDOWN: if start and end: started = True for row in grid: for spot in row: spot.update_neighbors(grid) if event.key == pygame.K_a: pygame.display.set_caption( 'DFS Path Finding algorithm') DFS.dfs(lambda: draw(win, grid, ROWS, width), grid, start, end) elif event.key == pygame.K_s: pygame.display.set_caption( 'BFS Path Finding algorithm') BFS.bfs(lambda: draw(win, grid, ROWS, width), grid, start, end) elif event.key == pygame.K_d: pygame.display.set_caption('A* Path Finding algorithm') ASTAR.astar(lambda: draw(win, grid, ROWS, width), grid, start, end) started = False if event.key == pygame.K_c: start = None end = None grid = make_grid(ROWS, width) pygame.quit()