diff --git a/Algorithms_and_Data_Structures/Graph/Bipartite_Graph.py b/Algorithms_and_Data_Structures/Graph/Bipartite_Graph.py new file mode 100644 index 0000000000..7c8f6ca70e --- /dev/null +++ b/Algorithms_and_Data_Structures/Graph/Bipartite_Graph.py @@ -0,0 +1,43 @@ +from collections import deque + +def is_bipartite(adj_matrix, V): + # Initialize colors array with -1 (uncolored) + color = [-1] * V + # Process each component of the graph + for start in range(V): + # If the vertex is already colored, skip it + if color[start] != -1: + continue + # Start BFS from this node + q = deque([start]) + color[start] = 0 # Color the starting vertex with 0 + while q: + node = q.popleft() + + for neighbor in range(V): + # Check if there's an edge between node and neighbor + if adj_matrix[node][neighbor] == 1: + # If the neighbor hasn't been colored, color it with the opposite color + if color[neighbor] == -1: + color[neighbor] = 1 - color[node] + q.append(neighbor) + # If the neighbor has the same color as the current node, the graph is not bipartite + elif color[neighbor] == color[node]: + return False + # If we successfully colored the graph, it's bipartite + return True + +def main(): + V = int(input("Enter the number of vertices: ")) + adj_matrix = [[0] * V for _ in range(V)] + print("Enter the adjacency matrix:") + for i in range(V): + adj_matrix[i] = list(map(int, input().split())) + + if is_bipartite(adj_matrix, V): + print("The graph is bipartite.") + else: + print("The graph is not bipartite.") + +if __name__ == "__main__": + main() diff --git a/Project-Structure.md b/Project-Structure.md index 432dcd473d..27fb1fb7f9 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -102,6 +102,7 @@ * [Unique-Paths](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/unique-paths.py) * Graph * [Adjacency Matrix](Algorithms_and_Data_Structures/Graph/Adjacency_Matrix.py) + * [Bipartite Graph](Algorithms_and_Data_Structures/Graph/Bipartite_Graph.py) * [Graph Cloning](Algorithms_and_Data_Structures/Graph/Graph_Cloning.py) * [Menu Driven Code Bfs Traversal](Algorithms_and_Data_Structures/Graph/Menu_driven_code_BFS_Traversal.py) * [Menu Driven Code Dfs Traversal](Algorithms_and_Data_Structures/Graph/Menu_driven_code_DFS_Traversal.py)