判断二分图

LeetCode算法网站算法题

https://leetcode-cn.com/problems/is-graph-bipartite/

一.遍历+颜色标记

算法:

(1)先任选一个节点开始遍历,并标记为红色;

(2)遍历标记完成之后,开始遍历该节点没有被标记的的邻接节点,并把此次遍历的邻接节点的颜色标记为绿色;

(3)然后重复遍历标记以标记点位未标记邻接点的过程,从而遍历整个图,标记点位的颜色由红色和绿色交替标记(每次遍历同一个点的邻接点标记颜色不变)

(4)在遍历的过程中,如果我们通过节点 u 遍历到了节点 v(即 u 和 v 在图中有一条边直接相连),那么会有三种情况:

         未被标记,那么就像上面说的那样去遍历染色;

         已被标记,并且邻接点和当前点位的颜色相同,那么说明该图不是二分图,就直接退出遍历,并且设置标记值为False;

         已被标记,并且邻接点和当前点位的颜色不同,那么什么都不用处理,直接对当前点的下一个邻接点进行判断标记遍历。

(5)还有一点需要注意的是,如果整个图是联通的那么从一个节点开始遍历就可以遍历到整个图,但是如果不连通那么还需要再          对单独的那个节点额外进行一次遍历标记,但是单独的那个节点不会影响到二分图的判断。

1.深度优先遍历

class Solution
{
private:
    vector<int>color;
    bool flag;
    static constexpr int NoColor=0;
    static constexpr int Red=1;
    static constexpr int Green=2;
    void dfs(int node,int c,const vector<vector<int>>&g)
    {
        color[node]=c;
        int next_c=(c==Red?Green:Red);
        for(int i=0;i<g[node].size();i++)
        {
            int neibr=g[node][i];
            if(color[neibr]==NoColor)
            {
                dfs(neibr,next_c,g);
                if(flag==false)
                    return;
            }
            else if(color[neibr]==color[node])
            {
                flag=false;
                return;
            }
        }
    }
public:
    bool isBipartite(vector<vector<int>>& graph)
    {
        flag=true;
        int n=graph.size();
        color.assign(n,NoColor);
        for(int i=0;i<n&&flag;i++)
        {
            if(color[i]==NoColor)
                dfs(i,Red,graph);
        }
        return flag;
    }
};

2.广度优先遍历——由于大体思路是一样的,我直接CV官解

class Solution {
private:
    static constexpr int UNCOLORED = 0;
    static constexpr int RED = 1;
    static constexpr int GREEN = 2;
    vector<int> color;

public:
    bool isBipartite(vector<vector<int>>& graph) {
        int n = graph.size();
        vector<int> color(n, UNCOLORED);
        for (int i = 0; i < n; ++i) {
            if (color[i] == UNCOLORED) {
                queue<int> q;
                q.push(i);
                color[i] = RED;
                while (!q.empty()) {
                    int node = q.front();
                    int cNei = (color[node] == RED ? GREEN : RED);
                    q.pop();
                    for (int neighbor: graph[node]) {
                        if (color[neighbor] == UNCOLORED) {
                            q.push(neighbor);
                            color[neighbor] = cNei;
                        }
                        else if (color[neighbor] != cNei) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
};

 

二.并查集

算法:

遍历图中每个顶点,将当前顶点的所有邻接点进行合并(注意不连接当前顶点),并判断这些邻接点中是否存在某一邻接点已经和当前顶点处于同一个集合中了,若是,则说明不是二分图。

class union_set
{
private:
    vector<int>parents;
public:
    union_set(int len);
    int find_parents(int index);
    void make_union(int index1,int index2);
};
union_set::union_set(int len)
{
    int i;
    for(i=0; i<len; i++)
        parents.push_back(i);
}
int union_set::find_parents(int index)
{
    if(index==parents[index])
        return index;
    parents[index]=find_parents(parents[index]);
    return parents[index];
}
void union_set::make_union(int index1,int index2)
{
    parents[find_parents(index1)]=find_parents(index2);
}



class Solution
{
public:
    bool isBipartite(vector<vector<int>>& graph)
    {
        int n = graph.size();
        union_set u(n);

        for (int i = 0; i < n; ++i)
        {
            //判断当前点位是否和邻接点在连接之前点位的邻接点时候已经连接过了,如过连接过了就返回False
            for (int j=0; j < graph[i].size(); ++j)
            {
                int root = u.find_parents(i);
                if (root == u.find_parents(graph[i][j]))
                    return false;
            }
            //把当前点位的邻接点都连接起来,注意并不连接当前点位
            for (int j=1; j<graph[i].size(); ++j)
            {
                u.make_union(graph[i][j-1], graph[i][j]);
            }
        }

        return true;
    }
};

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值