1. 程式人生 > >[LeetCode] Is Graph Bipartite? 是二分圖麽?

[LeetCode] Is Graph Bipartite? 是二分圖麽?

body dep ... length pro com -s 一個 public

Given an undirected graph, return true if and only if it is bipartite.

Recall that a graph is bipartite if we can split it‘s set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form: graph[i]

is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn‘t contain any element twice.

Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation: 
The graph looks like this:
0----1
|    |
|    |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation: 
The graph looks like this:
0----1
| \  |
|  \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.

Note:

  • graph will have length in range [1, 100].
  • graph[i] will contain integers in range [0, graph.length - 1]
    .
  • graph[i] will not contain i or duplicate values.
  • The graph is undirected: if any element j is in graph[i], then i will be in graph[j].

這道題博主在最開始做的時候,看了半天,楞是沒弄懂輸出數據的意思,博主開始以為給的是邊,後來發現跟圖對應不上,就懵逼了,後來是通過研究論壇上大神們的解法,才總算搞懂了題目的意思,原來輸入數組中的graph[i],表示頂點i所有相鄰的頂點,比如對於例子1來說,頂點0和頂點1,3相連,頂點1和頂點0,2相連,頂點2和結點1,3相連,頂點3和頂點0,2相連。這道題讓我們驗證給定的圖是否是二分圖,所謂二分圖,就是可以將圖中的所有頂點分成兩個不相交的集合,使得同一個集合的頂點不相連。為了驗證是否有這樣的兩個不相交的集合存在,我們采用一種很機智的染色法,大體上的思路是要將相連的兩個頂點染成不同的顏色,一旦在染的過程中發現有兩連的兩個頂點已經被染成相同的顏色,說明不是二分圖。這裏我們使用兩種顏色,分別用1和-1來表示,初始時每個頂點用0表示未染色,然後遍歷

解法一:

class Solution {
public:
    bool isBipartite(vector<vector<int>>& graph) {
        vector<int> colors(graph.size());
        for (int i = 0; i < graph.size(); ++i) {
            if (colors[i] == 0 && !valid(graph, 1, i, colors)) {
                return false;
            }
        }
        return true;
    }
    bool valid(vector<vector<int>>& graph, int color, int cur, vector<int>& colors) {
        if (colors[cur] != 0) return colors[cur] == color;
        colors[cur] = color;
        for (int i : graph[cur]) {
            if (!valid(graph, -1 * color, i, colors)) {
                return false;
            }
        }
        return true;
    }
};

解法二:

class Solution {
public:
    bool isBipartite(vector<vector<int>>& graph) {
        vector<int> colors(graph.size());
        for (int i = 0; i < graph.size(); ++i) {
            if (colors[i] == 0) colors[i] = 1;
            for (auto a : graph[i]) {
                if (colors[a] == colors[i]) return false;
                colrs[a] = -1 * colors[a];
            }
        }
        return true;
    }
};

參考資料:

https://leetcode.com/problems/is-graph-bipartite/discuss/115487/Java-Clean-DFS-solution-with-Explanation

https://leetcode.com/problems/is-graph-bipartite/discuss/115723/C++-short-iterative-solution-with-comments

LeetCode All in One 題目講解匯總(持續更新中...)

[LeetCode] Is Graph Bipartite? 是二分圖麽?