org.jgrapht.alg.cycle.TiernanSimpleCycles Maven / Gradle / Ivy
/* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* 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.
*/
/* -------------------------
* TiernanSimpleCycles.java
* -------------------------
* (C) Copyright 2013, by Nikolay Ognyanov
*
* Original Author: Nikolay Ognyanov
* Contributor(s) :
*
* $Id$
*
* Changes
* -------
* 06-Sep-2013 : Initial revision (NO);
*/
package org.jgrapht.alg.cycle;
import java.util.*;
import org.jgrapht.*;
/**
* Find all simple cycles of a directed graph using the Tiernan's algorithm.
*
* See:
* J.C.Tiernan An Efficient Search Algorithm Find the Elementary Circuits of a
* Graph., Communications of the ACM, vol.13, 12, (1970), pp. 722 - 726.
*
* @param the vertex type.
* @param the edge type.
*
* @author Nikolay Ognyanov
*/
public class TiernanSimpleCycles
implements DirectedSimpleCycles
{
private DirectedGraph graph;
/**
* Create a simple cycle finder with an unspecified graph.
*/
public TiernanSimpleCycles()
{
}
/**
* Create a simple cycle finder for the specified graph.
*
* @param graph - the DirectedGraph in which to find cycles.
*
* @throws IllegalArgumentException if the graph argument is
* null
.
*/
public TiernanSimpleCycles(DirectedGraph graph)
{
if (graph == null) {
throw new IllegalArgumentException("Null graph argument.");
}
this.graph = graph;
}
/**
* {@inheritDoc}
*/
@Override public DirectedGraph getGraph()
{
return graph;
}
/**
* {@inheritDoc}
*/
@Override public void setGraph(DirectedGraph graph)
{
if (graph == null) {
throw new IllegalArgumentException("Null graph argument.");
}
this.graph = graph;
}
/**
* {@inheritDoc}
*/
@Override public List> findSimpleCycles()
{
if (graph == null) {
throw new IllegalArgumentException("Null graph.");
}
Map indices = new HashMap();
List path = new ArrayList();
Set pathSet = new HashSet();
Map> blocked = new HashMap>();
List> cycles = new LinkedList>();
int index = 0;
for (V v : graph.vertexSet()) {
blocked.put(v, new HashSet());
indices.put(v, index++);
}
Iterator vertexIterator = graph.vertexSet().iterator();
if (!vertexIterator.hasNext()) {
return cycles;
}
V startOfPath = null;
V endOfPath = null;
V temp = null;
int endIndex = 0;
boolean extensionFound = false;
endOfPath = vertexIterator.next();
path.add(endOfPath);
pathSet.add(endOfPath);
// A mostly straightforward implementation
// of the algorithm. Except that there is
// no real need for the state machine from
// the original paper.
while (true) {
// path extension
do {
extensionFound = false;
for (E e : graph.outgoingEdgesOf(endOfPath)) {
V n = graph.getEdgeTarget(e);
int cmp =
indices.get(n).compareTo(indices.get(path.get(0)));
if ((cmp > 0)
&& !pathSet.contains(n)
&& !blocked.get(endOfPath).contains(n))
{
path.add(n);
pathSet.add(n);
endOfPath = n;
extensionFound = true;
break;
}
}
} while (extensionFound);
// circuit confirmation
startOfPath = path.get(0);
if (graph.containsEdge(endOfPath, startOfPath)) {
List cycle = new ArrayList();
cycle.addAll(path);
cycles.add(cycle);
}
// vertex closure
if (path.size() > 1) {
blocked.get(endOfPath).clear();
endIndex = path.size() - 1;
path.remove(endIndex);
pathSet.remove(endOfPath);
--endIndex;
temp = endOfPath;
endOfPath = path.get(endIndex);
blocked.get(endOfPath).add(temp);
continue;
}
// advance initial index
if (vertexIterator.hasNext()) {
path.clear();
pathSet.clear();
endOfPath = vertexIterator.next();
path.add(endOfPath);
pathSet.add(endOfPath);
for (V vt : blocked.keySet()) {
blocked.get(vt).clear();
}
continue;
}
// terminate
break;
}
return cycles;
}
}
// End TiernanSimpleCycles.java