class TSPth(unittest.TestCase): """ (a) test case: to check the result of the shortest distance of the shortest path using Tarjan modification with heap. (b) test data: graphs generated from main.py/ graph_generator.py (c) expected result: results from built-in networkx_dijkstra_path_length function (d) actual result: program output """ s_txt_file = "../data/heap/dense_stream_arcs.txt" a_txt_file = "../data/array/dense_weighted_matrix.txt" to_test_graph = convert_to_adj_list(s_txt_file) nx_graph = nx.from_numpy_matrix(np.loadtxt(a_txt_file, skiprows=2)) source = 30 target = 56 def setUp(self): source = self.source target = self.target self.to_test_result = tarjanstpath(self.to_test_graph, str(source), str(target))[0] self.nx_result = nx.dijkstra_path_length(self.nx_graph, source, target) def testDistance(self): self.assertEqual(float(self.to_test_result), float(self.nx_result))
def heap_sparse(root): txt = "../data/heap/sparse_stream_arcs.txt" graph = convert_to_adj_list(txt) start_time = time.time() tarjanspathtree(graph, root) end_time = time.time() run_time = end_time - start_time return run_time
def main(): print("Shortest path program") print("1. Implementation of algorithms for large graph \n2. Solve Ballyskate problem \n" "3. Solve Padgett Marriage Alliances \n4. Quit") user_choice = int(input("Please select your choice: 1, 2 or 3 \n")) while user_choice != 4: if user_choice == 1: print("You need to create a graph first.") graph_generator() print("Based on time complexity, we use an array data structure for the dense graph," "and a min heap for the sparse graph.") while True: structure_choice = input("Input type of data structure (array, heap) you want to use: ") if structure_choice.lower() == "array": DjikstraSthTree() break elif structure_choice.lower() == "heap": TarjanSPathTree() break else: print("Please choose 'array' or 'heap'.") structure_choice = input("Input type of data structure (array, heap) you want to use: ") elif user_choice == 2: print("Solution for Ballyskate problem: ") file = "data/heap/ballyskate_layout.txt" graph = convert_to_adj_list(file) print("Distance and the shortest path from node 1 to node p is: ", tarjanstpath(graph, '1', 'p')) elif user_choice == 3: print("Solution for Padgett Marriage Alliances problem:") FloretineMarriage() else: print("Please choose 1, 2, or 3.") print("Shortest path program") print("1. Implementation of algorithms for large graph \n2. Solve Ballyskate problem \n" "3. Solve Padgett Marriage Alliances \n4. Quit") user_choice = int(input("Please select your choice: 1, 2, or 3.\n")) print("Thank you!")
def TarjanSPathTree(): """Implementation of Shortest path tree using Tarjain method for a sparse graph.""" file = "data/heap/sparse_stream_arcs.txt" graph = convert_to_adj_list(file) print("Our nodes in the graph is ", graph.keys()) initial_node = input("Input node which you want to start with: ") while initial_node: if initial_node in graph.keys(): spanning_tree = tarjanspathtree(graph, initial_node) print("----------Solution----------") for node, value in spanning_tree.items(): print("Distance and path from node", initial_node, "to node", node, "is: ", value) choice = input("Do you want to choose another starting node? (y/n): ") while choice.lower() != 'n': if choice == "y": TarjanSPathTree() else: print("Please choose y or n") choice = input("Do you want to choose another starting node? (y/n): ") break else: print("Please input a proper starting node.") initial_node = input("Input node which you want to start with: ")
def FloretineMarriage(): """Calculate the centrality measures of each nodes in Padgett data of Floretine Marriage Alliances.""" txt_input_file = "data/padgett.txt" adj_list = convert_to_adj_list(txt_input_file) graph = convert_to_graph(txt_input_file) print( "Centrality measures available: (1) degree, (2) eigenvector, (3) closeness, (4) and betweeness." ) measure_choice = int( input("Please choose measures you want to calculate (from 1 to 4) \n")) while measure_choice: if measure_choice == 1: deg_dict = degree(graph) for node in deg_dict.keys(): print("Degree centrality of node ", node, " is: ", "{:.3f}".format(deg_dict[node])) choice = input("Do you want to calculate other measures?? (y/n): ") while choice.lower() != 'n': if choice == "y": FloretineMarriage() else: print("Please choose y or n") choice = input( "Do you want to calculate other measures?? (y/n): ") break if measure_choice == 2: eig_dict = eigen_vector(graph) for node in eig_dict.keys(): print("Eigenvector centrality of node ", node, " is: ", "{:.3f}".format(eig_dict[node])) choice = input("Do you want to calculate other measures?? (y/n): ") while choice.lower() != 'n': if choice == "y": FloretineMarriage() else: print("Please choose y or n") choice = input( "Do you want to calculate other measures?? (y/n): ") break if measure_choice == 3: closeness_dict = closeness(adj_list) for node in closeness_dict.keys(): print("Closeness centrality of node ", node, " is: ", "{:.3f}".format(closeness_dict[node])) choice = input("Do you want to calculate other measures?? (y/n): ") while choice.lower() != 'n': if choice == "y": FloretineMarriage() else: print("Please choose y or n") choice = input( "Do you want to calculate other measures?? (y/n): ") break if measure_choice == 4: bw_dict = betweeness(graph) for node in bw_dict.keys(): print("Betweeness centrality of node ", node, " is: ", "{:.3f}".format(bw_dict[node])) choice = input("Do you want to calculate other measures?? (y/n): ") while choice.lower() != 'n': if choice == "y": FloretineMarriage() else: print("Please choose y or n") choice = input( "Do you want to calculate other measures?? (y/n): ") break else: print("Please choose from 1 to 4") measure_choice = int( input( "Please choose measures you want to calculate (from 1 to 4) \n" ))