def simple_wgraph(): """Return a simple weighted graph with edges.""" from weighted_graph import WeightedGraph wg = WeightedGraph() for edge in SIMPLE_WGRAPH: wg.add_edge(edge[0], edge[1], edge[2]) return wg
def cyclic(): """Create simple cyclic graph.""" from weighted_graph import WeightedGraph empty_wg = WeightedGraph() empty_wg.add_edge('A', 'B', 5) empty_wg.add_edge('B', 'C', 5) empty_wg.add_edge('C', 'A', 5) return empty_wg
def make_graph_three(): my_graph = WeightedGraph() my_graph.add_node('a') my_graph.add_node('b') my_graph.add_node('c') my_graph.add_edge('a', 'c') my_graph.add_edge('b', 'a') return my_graph
def make_graph(): g = WeightedGraph() g.add_edge('a', 'b') g.add_edge('b', 'c') g.add_edge('c', 'f') g.add_edge('g', 'f') g.add_edge('a', 'd') g.add_edge('d', 'e') g.add_edge('e', 'a') return g
def test_prims_lazy_mst(): w_graph = WeightedGraph(4) w_graph.add_edge(1, 0, -10) w_graph.add_edge(1, 2, -35) w_graph.add_edge(0, 2, -15) w_graph.add_edge(0, 3, -12) w_graph.add_edge(1, 3, -11) w_graph.add_edge(2, 3, -30) prims_lazy = PrimsLazyMst(w_graph) prims_lazy.get_mst()
def non_ref(): """Create simple non referential graph.""" from weighted_graph import WeightedGraph empty_wg = WeightedGraph() empty_wg.add_edge('A', 'B', 5) empty_wg.add_edge('A', 'C', 5) empty_wg.add_edge('B', 'D', 5) empty_wg.add_edge('B', 'E', 5) empty_wg.add_edge('C', 'F', 5) empty_wg.add_edge('C', 'G', 5) return empty_wg
def test_kruskal(): w_graph = WeightedGraph(4) w_graph.add_edge(1, 0, -10) w_graph.add_edge(1, 2, -35) w_graph.add_edge(0, 2, -15) w_graph.add_edge(0, 3, -12) w_graph.add_edge(1, 3, -11) w_graph.add_edge(2, 3, -30) ''' w_graph.add_edge(4,6,-1) w_graph.add_edge(3,8,-2) w_graph.add_edge(8,7,-5) w_graph.add_edge(6,8,-8) w_graph.add_edge(7,6,-17) ''' k = KruskalMst(w_graph) k.get_mst() '''
def make_graph_cycle(): g = WeightedGraph() g.add_edge('a', 'b') g.add_edge('b', 'c') g.add_edge('c', 'a') return g
lowest_cost = math.inf lowest_cost_node = None for node in costs: cost = costs[node] if node not in visited and cost < lowest_cost: lowest_cost = cost lowest_cost_node = node return lowest_cost_node g = WeightedGraph() g.add_node('A') g.add_node('B') g.add_node('C') g.add_node('D') g.add_node('E') g.add_node('F') g.add_edge('A', 'B', 5) g.add_edge('A', 'C', 2) g.add_edge('B', 'C', 8) g.add_edge('B', 'D', 4) g.add_edge('B', 'E', 2) g.add_edge('C', 'E', 7) g.add_edge('D', 'E', 6) g.add_edge('D', 'F', 3) g.add_edge('E', 'F', 1) dijkstra_1(g, 'A')