def test_is_not_bipartite(self):
        """Create a graph."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_vertex('E')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'D')
        graph.add_edge('D', 'C')
        graph.add_edge('C', 'A')
        # These two cause the rule to break
        graph.add_edge('C', 'E')
        graph.add_edge('D', 'E')
        self.assertEqual(graph.is_bipartite(), False)

        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('A', 'D')
        # This one causes the rule to break
        graph.add_edge('B', 'D')
        self.assertEqual(graph.is_bipartite(), False)
Exemplo n.º 2
0
    def test_not_bipartite(self):
        """Test that a cycle on 3 vertices is NOT bipartite."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')

        self.assertFalse(graph.is_bipartite())
Exemplo n.º 3
0
    def test_is_bipartite_tree(self):
        """Test that a tree on 4 vertices is bipartite."""
        graph = Graph(is_directed=False)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        vertex_d = graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('A', 'D')

        self.assertTrue(graph.is_bipartite())
Exemplo n.º 4
0
    def test_is_bipartite_cycle(self):
        """Test that a cycle on 4 vertices is bipartite."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'C')
        graph.add_edge('C', 'D')
        graph.add_edge('A', 'D')

        self.assertTrue(graph.is_bipartite())
    def test_is_bipartite(self):
        """Create a graph."""
        graph = Graph(is_directed=True)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('A', 'D')
        self.assertEqual(graph.is_bipartite(), True)

        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_vertex('D')
        graph.add_edge('A', 'B')
        graph.add_edge('B', 'D')
        graph.add_edge('D', 'C')
        graph.add_edge('C', 'A')
        self.assertEqual(graph.is_bipartite(), True)
Exemplo n.º 6
0
    # Or, read a graph in from a file
    # graph = read_graph_from_file('test_files/graph_small_directed.txt')

    # Output the vertices & edges
    # Print vertices
    print(f'The vertices are: {graph.get_vertices()} \n')

    # Print edges
    print('The edges are:')
    for vertex_obj in graph.get_vertices():
        for neighbor_obj in vertex_obj.get_neighbors():
            print(f'({vertex_obj.get_id()} , {neighbor_obj.get_id()})')

    # Find whether the graph is bipartite
    print('Finding bipartiteness...')
    print(graph.is_bipartite())

    # Find The Topological sort of the graph
    print("topological sort")
    print(graph.topological_sort())

    # Get all connected components
    print('Finding connected components...')
    print(graph.get_connected_components())

    # Search the graph
    print('Performing BFS traversal...')
    graph.bfs_traversal('A')

    # Find shortest path
    print('Finding shortest path from vertex A to vertex E...')