Question
I need the following code to check if the graph is a bipartite graph or not using the BFS algorithm. But i am getting errors
I need the following code to check if the graph is a bipartite graph or not using the BFS algorithm. But i am getting errors with two parts in the code. One is the print parent and the other is the BFS check itself. Following is my code.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/** * * @author Owner */
import java.util.LinkedList; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.List; import java.util.Queue; public class Graph { List> adjList = null;
private static class Vertex { LinkedList
// Vertex.visited == Boolean Visit Switch // stores level of each vertex in BFS // stores vertex is discovered or not boolean[] discovered = new boolean[N]; int[] level = new int[N];
// mark source vertex as discovered and // set its level to 0 discovered[v] = true; level[v] = 0;
// create a queue to do BFS and enqueue // source vertex in it Queue
// run till queue is not empty while (!q.isEmpty()) { // pop front node from queue and print it v = q.poll();
// do for every edge (v -> u) for (int u : graph.adjList.get(v)) { // if vertex u is explored for first time if (!discovered[u]) { // mark it discovered discovered[u] = true;
// set level as level of parent node + 1 level[u] = level[v] + 1;
// push the vertex into the queue q.add(u); } // if the vertex is already been discovered and // level of vertex u and v are same, then the // graph contains an odd-cycle & is not biparte else if (level[v] == level[u]) return false; } } return true; } // end of bipartite } // end of class Graph
Your help is much appreciated!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started