13
1
A bipartite graph is a graph whose vertices can be divided into two disjoint set, such that no edge connects two vertices in the same set. A graph is bipartite if and only if it is 2-colorable.
Challenge
Your task is to, given the adjacency matrix of an undirected simple graph, determine whether it is a bipartite graph. That is, if an edge connects vertices i and j, both (i, j) and (j, i) entry of the matrix are 1.
Since the graph is undirected and simple, its adjacency matrix is symmetric and contains only 0 and 1.
Specifics
You should take an N-by-N matrix as input (in any form, e.g. list of lists, list of strings, C-like int**
and size, flattened array, raw input, etc.).
The function/program should return/output a truthy value if the graph is bipartite, and falsy otherwise.
Test Cases
['00101',
'00010',
'10001',
'01000',
'10100'] : False
['010100',
'100011',
'000100',
'101000',
'010000',
'010000'] : True (divide into {0, 2, 4, 5} and {1, 3})
['00',
'00'] : True
Scoring
Builtins that compute the answer directly are banned.
This is code-golf, so the shortest program (in bytes) by the end of this month wins!
Related, and in fact borderline dupe, because being bipartite is equivalent to having no odd cycles, and most of the answers to that question work by enumerating all cycles and examining their lengths. – Peter Taylor – 2017-11-17T08:22:46.030
@PeterTaylor Yeah, but there are simpler ways to solve this problem. – Colera Su – 2017-11-17T16:27:08.113
@ColeraSu Instead of truthy / falsy, can we return
-1
for falsy and any non-negative integer for truthy? – Mr. Xcoder – 2017-11-17T19:10:59.897@MishaLavrov
0
-> Falsy,>0
-> Truthy is generally allowed by standard truthy/falsy rules.-1
and≥ 0
is not that common, that's why I asked. – Mr. Xcoder – 2017-11-17T20:28:59.090@Mr.Xcoder It's okay. – Colera Su – 2017-11-19T04:01:07.493