def cube_algorithm(G_complete): """ Use cube algorithm to build an approximation for the 2-MBST problem on G: 1. Add edges to G to build complete graph G_complete 2. Build an MST T of G_complete 3. Build the the cube of T 4. find a Hamiltonian path in the cube of T :param G : (nx.Graph()) """ T = nx.minimum_spanning_tree(G_complete, weight="weight") T_cube = nx.Graph() T_cube.add_nodes_from(T.nodes(data=True)) shortest_paths = nx.shortest_path_length(T) for source, lengths_dict in shortest_paths: for target in lengths_dict: if lengths_dict[target] <= 3: T_cube.add_edge(source, target, weight=G_complete.get_edge_data( source, target)["weight"]) ham_path = hamiltonian_path(T_cube.to_directed()) result = nx.Graph() result.add_nodes_from(G_complete.nodes(data=True)) for idx in range(len(ham_path) - 1): result.add_edge(ham_path[idx], ham_path[idx + 1], weight=G_complete.get_edge_data( ham_path[idx], ham_path[idx + 1])['weight']) return result
def test_hamiltonian_cycle(self): """TestData that :func:`networkx.tournament.hamiltonian_path` returns a Hamiltonian cycle when provided a strongly connected tournament. """ G = DiGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) path = hamiltonian_path(G) assert len(path) == 4 assert all(v in G[u] for u, v in zip(path, path[1:])) assert path[0] in G[path[-1]]
def test_hamiltonian_cycle(self): """Tests that :func:`networkx.tournament.hamiltonian_path` returns a Hamiltonian cycle when provided a strongly connected tournament. """ G = DiGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) path = hamiltonian_path(G) assert_equal(len(path), 4) assert_true(all(v in G[u] for u, v in zip(path, path[1:]))) assert_true(path[0] in G[path[-1]])
def test_path_is_hamiltonian(self): G = DiGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) path = hamiltonian_path(G) assert len(path) == 4 assert all(v in G[u] for u, v in zip(path, path[1:]))
def test_path_is_hamiltonian(self): G = DiGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) path = hamiltonian_path(G) assert_equal(len(path), 4) assert_true(all(v in G[u] for u, v in zip(path, path[1:])))
# g) Maximum matching print("g) M =", nx.max_weight_matching(G)) print() # h) Minimum vertex cover print("h) R =", vertex_cover.min_weighted_vertex_cover(G)) print() # i) Minimum edge cover print("i) F =", covering.min_edge_cover(G)) print() # j) Shortest closed path (circuit) that visits every vertex print("j) W =", end=" ") for i in range(len(tournament.hamiltonian_path(D))): print(tournament.hamiltonian_path(D)[i], "-", end=" ") print(tournament.hamiltonian_path(D)[0]) print() # k) Shortest closed path (circuit) that visits every edge H = nx.eulerize(G) c = list(n for (n, d) in nx.eulerian_circuit(H)) print("k) U =", end=" ") for i in range(len(c)): print(c[i], "-", end=" ") print(c[0]) print() # m) 2-edge-connected components print("m) ", list(edge_kcomponents.bridge_components(G)))
#g print("Maximum matching") print(nx.max_weight_matching(g2, maxcardinality=True, weight=0)) #i print("Minimum edge cover") print(covering.min_edge_cover(g2)) #ищем минимальное реберное покрытие #j print("Hanging vertices") print([u for v, u in g3.edges() if len(g3[u]) == 1]) #узнаем висячие вершины g3.remove_nodes_from([u for v, u in g3.edges() if len(g3[u]) == 1]) #убираем висячие ребра print("The shortest closed path that visits every vertex") print(tournament.hamiltonian_path( g3)) #ищем гамильтонов путь и добавляем в него высячие ребра #k print("The shortest closed path that visits every edge") print(list(euler.eulerian_circuit( g3))) #ищем эйлеров путь и добавляем в него ребра-мосты к висячим вершинам #m print("2-edge-connected components") print(list(edge_kcomponents.bridge_components(g))) #o mstree = nx.Graph(mst.minimum_spanning_tree(g2)) print("MST") print(mstree.edges) print("Weight of MST")
def test_hamiltonian_empty_graph(): path = hamiltonian_path(DiGraph()) assert len(path) == 0