def challenge_2(file: str, vertex_a: str, vertex_b: str) -> Graph: graph, verticies, edges_str = graph_from_file(file) # fill graph instance with edges and verticies fill(graph, verticies, edges_str) # getting parent dict dict_ = graph.breadth_first_search(vertex_a, vertex_b) parent = dict_[vertex_b] output = list() # append vertex_b output.append(vertex_b) # walking backwards in "parent" dict while parent != vertex_a: output.append(parent) parent = dict_[parent] # prepending vertex_a output.append(vertex_a) output = output[::-1] print(f"Vertices in shortest path: {output}") print(f"Number of edges in shortest path: {len(output)-1}")
def challenge_5(file: str) -> Graph: graph, verticies, edges_str = graph_from_file(file) # fill graph instance with edges and verticies fill(graph, verticies, edges_str) bool_ = graph.is_eulerian() print(f"This graph is Eulerian: {bool_}")
def challenge_3(file: str, vertex_a: str, vertex_b: str) -> Graph: graph, verticies, edges_str = graph_from_file(file) # fill graph instance with edges and verticies fill(graph, verticies, edges_str) # get distance and previous dicts distance, previous = graph.min_weight_path(vertex_a, vertex_b) print( f"The weight of the minimum weight path between vertex {vertex_a} and {vertex_b} is: {distance[vertex_b]}" )
def challenge_3(file: str, vertex_a: str, vertex_b: str) -> Graph: graph, verticies, edges_str = graph_from_file(file) # fill graph instance with edges and verticies fill(graph, verticies, edges_str) # using recursion, set used for dfs set_ = set() visited_list = list() tuple_ = graph.dfs_recursive(vertex_a, vertex_b, visited_list, set_, print) print( f"There exists a path between vertex {vertex_a} and {vertex_b}: {tuple_[0]}" ) print(f"Vertices in the path: {tuple_[1]}")
def challenge_1(file: str) -> Graph: graph, verticies, edges_str = graph_from_file(file) # fill graph instance with edges and verticies fill(graph, verticies, edges_str) # print vertices print(f"Verticies: {len(graph.get_vertices())}") # print edges print(f"Edges: {graph.num_edges}") # print edge list for vertex in graph.vert_dict: vert = graph.get_vertex(vertex) for neighbour, weight in vert.neighbours.items(): print(f"({vert.id}, {neighbour.id}, {weight})")
def chapter_1(filename: str) -> Graph: """ Builds graph according to data text file and returns graph to be manipulated """ graph, verticies, edges_str = graph_from_file(filename) # fill graph instance with edges and verticies return fill(graph, verticies, edges_str)
def airplane_modelling(file_name: str, city_1: str, city_2: str) -> Graph: graph, verticies, edges_str = graph_from_file(file_name) # fill graph instance with edges and verticies fill(graph, verticies, edges_str) most_connected_city = graph.most_connected_vertex() least_connected_city = graph.least_connected_vertex() distance, previous = graph.min_weight_path(city_1, city_2) is_eulerian = graph.is_eulerian() print(f"The most connected city is: {most_connected_city.id}") print(f"The least connected city is: {least_connected_city.id}") print( f"Can you travel to every airport from any airport in the graph?: {is_eulerian}" ) print( f"The shortest path for a message between {city_1} & {city_2} is {distance[city_2]}" )