def count_possible_astronaut_pairs(total_astronauts, astronauts_connections): """ Get number of possible astronaut pairs where both of them are from different countries """ graph = UndirectedGraph() for astronaut in range(0, total_astronauts): graph.add_vertex(astronaut) for connection in astronauts_connections: graph.add_edge(connection[0], connection[1], 0) country_by_astronaut = vertices_to_components(graph) count_by_country = {} for astronaut, country in country_by_astronaut.items(): count_by_country[country] = count_by_country.get(country, 0) + 1 total = 0 pairs = 0 for country, count in count_by_country.items(): pairs += total * count total += count return pairs
def test_returns_true_for_graph_without_edges(self): g = UndirectedGraph() g.add_vertex(1) g.add_vertex(2) g.add_vertex(3) self.assertTrue(is_bipartite(g))
def test_returns_true_for_graph_with_single_vertex(self): g = UndirectedGraph() g.add_vertex(1) self.assertTrue(is_bipartite(g))