Processing math: 100%

Testing Bipartiteness solved by 646

Feb. 21, 2014, 4:24 p.m. by Rosalind Team

Topics: Graphs

Problem

Figure 1. The graphs from the dataset

A bipartite graph is a graph G=(V,E) whose vertices can be partitioned into two sets (V=V1V2 and V1V2=) such that there are no edges between vertices in the same set (for instance, if u,vV1, then there is no edge between u and v).

There are many other ways to formulate this property. For instance, an undirected graph is bipartite if and only if it can be colored with just two colors. Another one: an undirected graph is bipartite if and only if it contains no cycles of odd length.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Given: A positive integer k20 and k simple graphs in the edge list format with at most 103 vertices each.

Return: For each graph, output "1" if it is bipartite and "-1" otherwise.

See Figure 1 for visual example from the sample dataset.

Sample Dataset

2

3 3
1 2
3 2
3 1

4 3
1 4
3 1
1 2

Sample Output

-1 1

Please login to solve this problem.