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)