Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7702403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:13:08+00:00 2026-05-31T23:13:08+00:00

I am implementing an algorithm to determine whether an undirected graph is bipartite or

  • 0

I am implementing an algorithm to determine whether an undirected graph is bipartite or not. Based on this pseudo-code made ​​my implementation, which works for graphs connected, but when it is disconnected simply the program indicates a wrong answer. I think if its not connected, then one more loop is needed for every disjoint sub-graph. But im stuck with this. How I can solve my code for me to print a correct answer?

#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;

#define MAX 1000

int numberVertex, numberEdges;
int particion[MAX], visited[MAX];
vector< int > adjacencyMatrix[MAX];

bool bfs()
{
    int i, origin, destination, begin;
    queue< int > queueVertex;
    begin = 0;
    queueVertex.push(begin);
    particion[begin] = 1; // 1 left,
    visited[begin] = 1; // set adjacencyMatrixray

    while(!queueVertex.empty())
    {
        origin = queueVertex.front(); queueVertex.pop();
        for(i=0; i < adjacencyMatrix[origin].size(); i++)
        {
            destination = adjacencyMatrix[origin][i];
            if(particion[origin] == particion[destination])
            {
                return false;
            }
            if(visited[destination] == 0)
            {
                visited[destination] = 1;
                particion[destination] = 3 - particion[origin]; // alter 1 and 2 subsets
                queueVertex.push(destination);
            }
        }
    }
    return true;
}

int main()
{
 freopen("tarea2.in", "r", stdin);
    int i,j, nodeOrigin, nodeDestination;
    scanf("%d %d", &numberVertex, &numberEdges);
    for(i=0; i<numberEdges; i++)
    {
        scanf("%d %d", &nodeOrigin, &nodeDestination);
        adjacencyMatrix[nodeOrigin].push_back(nodeDestination);
        adjacencyMatrix[nodeDestination].push_back(nodeOrigin);
    }
    if(bfs()) {

        printf("Is bipartite\n");
          for (j=0; j<numberVertex; j++){
        cout<<j<<" "<<particion[j]<<endl;
        }

    }
    else {printf("Is not bipartite\n");}





    return 0;
}

For example for this input

6 4
3 0
1 0
2 5
5 4

the output should be:

Is bipartite
0 1
1 2
2 1
3 2
4 1
5 2

Instead throws me the output:

0 1
1 2
2 0
3 2
4 0
5 0

This happens because the graph is not a connected graph, ie, has two connected components.I hope you can help me because I’ve been stuck with this for several days.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:13:10+00:00Added an answer on May 31, 2026 at 11:13 pm

    You should run bfs on every connected component. Simplest way to do this is to iterate over all vertices and if they weren’t visited, then just call bfs on them.

    bool is_bipartite()
    {
        for(int i = 0; i < numberVertex; i++)
        {
           if (visited[i] == 0 && !bfs(i)) {
               return false;
           }
        } 
        return true;
    }
    

    It is still linear because, you run bfs on every connected component once.

    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <iostream>
    using namespace std;
    
    #define MAX 1000
    
    int numberVertex, numberEdges;
    int particion[MAX], visited[MAX];
    vector< int > adjacencyMatrix[MAX];
    
    bool bfs(int begin)
    {
        int i, origin, destination;
        queue< int > queueVertex;
        queueVertex.push(begin);
        particion[begin] = 1; // 1 left,
        visited[begin] = 1; // set adjacencyMatrixray
    
        while(!queueVertex.empty())
        {
            origin = queueVertex.front(); queueVertex.pop();
            for(i=0; i < adjacencyMatrix[origin].size(); i++)
            {
                destination = adjacencyMatrix[origin][i];
                if(particion[origin] == particion[destination])
                {
                    return false;
                }
                if(visited[destination] == 0)
                {
                    visited[destination] = 1;
                    particion[destination] = 3 - particion[origin]; // alter 1 and 2 subsets
                    queueVertex.push(destination);
                }
            }
        }
        return true;
    }
    
    bool is_bipartite()
    {
        for(int i=0; i< numberVertex; i++)
        {
           if (visited[i] == 0 && !bfs(i)) {
               return false;
           }
        } 
        return true;
    }
    
    int main()
    {
        //freopen("tarea2.in", "r", stdin);
        int i,j, nodeOrigin, nodeDestination;
        scanf("%d %d", &numberVertex, &numberEdges);
        for(i=0; i<numberEdges; i++)
        {
            scanf("%d %d", &nodeOrigin, &nodeDestination);
            adjacencyMatrix[nodeOrigin].push_back(nodeDestination);
            adjacencyMatrix[nodeDestination].push_back(nodeOrigin);
        }
        if(is_bipartite()) {
    
            printf("Is bipartite\n");
              for (j=0; j<numberVertex; j++){
            cout<<j<<" "<<particion[j]<<endl;
            }
    
        }
        else {printf("Is not bipartite\n");}
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble implementing a python algorithm which does the following: (This is
When implementing a graph search algorithm, I needed a priority queue which allowed for
I am implementing this algorithm for a directed graph. But the interesting thing about
I'm implementing Kruskal's algorithm and I'd like to utilize threads. However I am not
I'm implementing an algorithm in which I need manipulate a mesh, adding and deleting
I'm trying different data structures for implementing Prim's algorithm. So I made a class
For fun, I've been implementing the DES algorithm in java. (Well, it's not that
I'm having some problems implementing an algorithm to read a foreign process' memory. Here
I'm implementing the Euclidian algorithm for finding the GCD (Greatest Common Divisor) of two
I had some troubles implementing Lawler's algorithm but thanks to SO and a bounty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.