org.jgrapht.experimental.GraphTests Maven / Gradle / Ivy
/* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package org.jgrapht.experimental;
import java.util.*;
import org.jgrapht.*;
public final class GraphTests
{
private GraphTests()
{
}
public static boolean isEmpty(Graph g)
{
return g.edgeSet().isEmpty();
}
public static boolean isComplete(Graph g)
{
int n = g.vertexSet().size();
return g.edgeSet().size()
== (n * (n - 1) / 2);
}
public static boolean isConnected(Graph g)
{
int numVertices = g.vertexSet().size();
int numEdges = g.edgeSet().size();
if (numEdges < (numVertices - 1)) {
return false;
}
if ((numVertices < 2)
|| (numEdges > ((numVertices - 1) * (numVertices - 2) / 2)))
{
return true;
}
Set known = new HashSet();
LinkedList queue = new LinkedList();
V v = g.vertexSet().iterator().next();
queue.add(v); // start with node 1
known.add(v);
while (!queue.isEmpty()) {
v = queue.removeFirst();
for (
Iterator it = Graphs.neighborListOf(g, v).iterator();
it.hasNext();)
{
v = it.next();
if (!known.contains(v)) {
known.add(v);
queue.add(v);
}
}
}
return known.size() == numVertices;
}
public static boolean isTree(Graph g)
{
return isConnected(g)
&& (g.edgeSet().size() == (g.vertexSet().size() - 1));
}
public static boolean isBipartite(Graph g)
{
if ((4 * g.edgeSet().size())
> (g.vertexSet().size() * g.vertexSet().size()))
{
return false;
}
if (isEmpty(g)) {
return true;
}
Set unknown = new HashSet(g.vertexSet());
LinkedList queue = new LinkedList();
V v = unknown.iterator().next();
Set odd = new HashSet();
queue.add(v);
while (!unknown.isEmpty()) {
if (queue.isEmpty()) {
queue.add(unknown.iterator().next());
}
v = queue.removeFirst();
unknown.remove(v);
for (
Iterator it = Graphs.neighborListOf(g, v).iterator();
it.hasNext();)
{
V n = it.next();
if (unknown.contains(n)) {
queue.add(n);
if (!odd.contains(v)) {
odd.add(n);
}
} else if (!(odd.contains(v) ^ odd.contains(n))) {
return false;
}
}
}
return true;
}
}
// End GraphTests.java