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 containi
or duplicate values.- The graph is undirected: if any element
j
is ingraph[i]
, theni
will be ingraph[j]
.
這道題博主在最開始做的時候,看了半天,愣是沒弄懂輸出數據的意思,博主開始以為給的是邊,后來發現跟圖對應不上,就懵逼了,后來是通過研究論壇上大神們的解法,才總算搞懂了題目的意思,原來輸入數組中的 graph[i],表示頂點i所有相鄰的頂點,比如對於例子1來說,頂點0和頂點1,3相連,頂點1和頂點0,2相連,頂點2和結點1,3相連,頂點3和頂點0,2相連。這道題讓我們驗證給定的圖是否是二分圖,所謂二分圖,就是可以將圖中的所有頂點分成兩個不相交的集合,使得同一個集合的頂點不相連。為了驗證是否有這樣的兩個不相交的集合存在,我們采用一種很機智的染色法,大體上的思路是要將相連的兩個頂點染成不同的顏色,一旦在染的過程中發現有兩連的兩個頂點已經被染成相同的顏色,說明不是二分圖。這里我們使用兩種顏色,分別用1和 -1 來表示,初始時每個頂點用0表示未染色,然后遍歷每一個頂點,如果該頂點未被訪問過,則調用遞歸函數,如果返回 false,那么說明不是二分圖,則直接返回 false。如果循環退出后沒有返回 false,則返回 true。在遞歸函數中,如果當前頂點已經染色,如果該頂點的顏色和將要染的顏色相同,則返回 true,否則返回 false。如果沒被染色,則將當前頂點染色,然后再遍歷與該頂點相連的所有的頂點,調用遞歸函數,如果返回 false 了,則當前遞歸函數的返回 false,循環結束返回 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 && !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; } };
我們再來看一種迭代的解法,整體思路還是一樣的,還是遍歷整個頂點,如果未被染色,則先染色為1,然后使用 BFS 進行遍歷,將當前頂點放入隊列 queue 中,然后 while 循環 queue 不為空,取出隊首元素,遍歷其所有相鄰的頂點,如果相鄰頂點未被染色,則染成和當前頂點相反的顏色,然后把相鄰頂點加入 queue 中,否則如果當前頂點和相鄰頂點顏色相同,直接返回 false,循環退出后返回 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) continue; colors[i] = 1; queue<int> q{{i}}; while (!q.empty()) { int t = q.front(); q.pop(); for (auto a : graph[t]) { if (colors[a] == colors[t]) return false; if (colors[a] == 0) { colors[a] = -1 * colors[t]; q.push(a); } } } } return true; } };
其實這道題還可以使用並查集 Union Find 來做,所謂的並查集,簡單來說,就是歸類,將同一集合的元素放在一起。我們開始遍歷所有結點,若當前結點沒有鄰接結點,直接跳過。否則就要開始進行處理了,並查集方法的核心就兩步,合並跟查詢。我們首先進行查詢操作,對當前結點和其第一個鄰接結點分別調用 find 函數,如果其返回值相同,則意味着其屬於同一個集合了,這是不合題意的,直接返回 false。否則我們繼續遍歷其他的鄰接結點,對於每一個新的鄰接結點,我們都調用 find 函數,還是判斷若返回值跟原結點的相同,return false。否則就要進行合並操作了,根據敵人的敵人就是朋友的原則,所有的鄰接結點之間應該屬於同一個組,因為就兩個組,我所有不爽的人都不能跟我在一個組,那么他們所有人只能都在另一個組,所以需要將他們都合並起來,合並的時候不管是用 root[parent] = y 還是 root[g[i][j]] = y 都是可以,因為不管直接跟某個結點合並,或者跟其祖宗合並,最終經過 find 函數追蹤溯源都會返回相同的值,參見代碼如下:
解法三:
class Solution { public: bool isBipartite(vector<vector<int>>& graph) { vector<int> root(graph.size()); for (int i = 0; i < graph.size(); ++i) root[i] = i; for (int i = 0; i < graph.size(); ++i) { if (graph[i].empty()) continue; int x = find(root, i), y = find(root, graph[i][0]); if (x == y) return false; for (int j = 1; j < graph[i].size(); ++j) { int parent = find(root, graph[i][j]); if (x == parent) return false; root[parent] = y; } } return true; } int find(vector<int>& root, int i) { return root[i] == i ? i : find(root, root[i]); } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/785
類似題目:
參考資料:
https://leetcode.com/problems/is-graph-bipartite/