[LeetCode] Is Graph Bipartite? 是二分圖麼?
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]
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] == 0) { colors[a] = -1 * colors[t]; q.push(a); } else { if (colors[a] == colors[t]) return false; } } } } return true; } };
參考資料: