def solve(self): """Solve the problem Note: O(n) (runtime) and O(n) (space) solution uses the breadth first traversal to traverse nodes and copy node neighbors. Args: Returns: GraphNode Raises: None """ print("Solving {} problem ...".format(self.PROBLEM_NAME)) # Create a queue for BFS nodes_queue = deque() nodes_queue.append(self.input_graph_node) # Root node to return for the cloned graph cloned_graph_node = GraphNode(self.input_graph_node.data) # Bookkeeping visited nodes -- contains the cloned nodes visited_dict = {self.input_graph_node: cloned_graph_node} while len(nodes_queue) > 0: current_node = nodes_queue.popleft() copied_node = visited_dict[current_node] # iterate current node's neighbors for node in current_node.neighbors: # if the node is already not visited if node not in visited_dict: # append the node to queue nodes_queue.append(node) # create a copy node for the neighbor neighbor_node = GraphNode(node.data) # add neighbor and update visited copied_node.add_neighbor(neighbor_node) visited_dict[node] = neighbor_node else: # if the node is already visited neighbor_node = visited_dict[node] # Add the neighbor node to the copied node's neighbors if it's not added if neighbor_node not in copied_node.neighbors: copied_node.add_neighbor(neighbor_node) return cloned_graph_node
def test_solve(self): """Test solve Args: self: TestCloneGraphDepthFirst Returns: None Raises: None """ # Given ''' Note : All the edges are Undirected Given Graph: 1--2 | | 4--3 ''' node1 = GraphNode(1) node2 = GraphNode(2) node3 = GraphNode(3) node4 = GraphNode(4) node1.add_neighbor(node2) node1.add_neighbor(node4) node2.add_neighbor(node3) node2.add_neighbor(node1) node3.add_neighbor(node4) node3.add_neighbor(node2) node4.add_neighbor(node1) node4.add_neighbor(node3) clone_graph_problem = CloneGraphDepthFirst(node1) # When cloned_graph_node = clone_graph_problem.solve() # Then cloned_list = [] Graph.depth_first_traversal(cloned_graph_node, cloned_list) input_list = [] Graph.depth_first_traversal(node1, input_list) self.assertEqual(cloned_list, input_list)
def test_solve_invalid(self): """Test solve (invalid) Args: self: TestIsGraphBipartite Returns: None Raises: None """ # Given ''' Note : All the edges are Undirected Given Graph: 1----2 | \ | | \ | 4----3 ''' node1 = GraphNode(1) node2 = GraphNode(2) node3 = GraphNode(3) node4 = GraphNode(4) node1.add_neighbor(node2) node1.add_neighbor(node4) node1.add_neighbor(node3) node2.add_neighbor(node3) node2.add_neighbor(node1) node3.add_neighbor(node4) node3.add_neighbor(node2) node3.add_neighbor(node1) node4.add_neighbor(node1) node4.add_neighbor(node3) is_bipartite_problem = IsGraphBipartite(node1) # When result = is_bipartite_problem.solve() # Then self.assertFalse(result)
def test_breadth_first_traversal(self): """Test for graph node (breadth_first_traversal) Args: self: TestGraph Returns: None Raises: None """ # Given ''' Given Graph: 1--2 | | 4--3 ''' node1 = GraphNode(1) node2 = GraphNode(2) node3 = GraphNode(3) node4 = GraphNode(4) node1.add_neighbor(node2) node1.add_neighbor(node4) node2.add_neighbor(node3) node2.add_neighbor(node1) node3.add_neighbor(node4) node3.add_neighbor(node2) node4.add_neighbor(node1) node4.add_neighbor(node3) # When result = [] Graph.breadth_first_traversal(node1, result) # Then self.assertEqual(result, [1, 2, 4, 3])