You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

160 lines
4.3 KiB

//==============================================================================
//
// Copyright (c) 2002-
// Authors:
// * Christian von Essen <christian.vonessen@imag.fr> (Verimag, Grenoble)
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham/Oxford)
//
//------------------------------------------------------------------------------
//
// This file is part of PRISM.
//
// PRISM is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PRISM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PRISM; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//==============================================================================
package explicit;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Deque;
import java.util.function.IntPredicate;
import prism.PrismComponent;
import prism.PrismException;
/**
* Tarjan's SCC algorithm operating on a Model object.
*/
public class SCCComputerTarjan extends SCCComputer
{
/* The model to compute (B)SCCs for */
private Model model;
/* Number of nodes (model states) */
private int numNodes;
/* Next index to give to a node */
private int index = 0;
/* Stack of nodes */
private Deque<Integer> stack = new ArrayDeque<Integer>();
/* Nodes currently on the stack. */
private BitSet onStack;
/** The lowlink information for the nodes (states) */
private int[] nodeLowlink;
/** The index information for the nodes (states) */
private int[] nodeIndex;
/** Should we filter trivial SCCs? */
private boolean filterTrivialSCCs;
private IntPredicate restrict;
/**
* Build (B)SCC computer for a given model.
*/
public SCCComputerTarjan(PrismComponent parent, Model model, SCCConsumer consumer) throws PrismException
{
super(parent, consumer);
this.model = model;
this.numNodes = model.getNumStates();
nodeLowlink = new int[numNodes];
Arrays.fill(nodeLowlink, -1);
nodeIndex = new int[numNodes];
Arrays.fill(nodeIndex, -1);
onStack = new BitSet();
}
// Methods for SCCComputer interface
@Override
public void computeSCCs(boolean filterTrivialSCCs, IntPredicate restrict) throws PrismException
{
this.filterTrivialSCCs = filterTrivialSCCs;
consumer.notifyStart(model);
this.restrict = restrict;
tarjan();
consumer.notifyDone();
}
// SCC Computation
/**
* Execute Tarjan's algorithm. Determine maximal strongly connected components
* (SCCS) for the graph of the model and stored in {@code sccs}.
*/
public void tarjan() throws PrismException
{
for (int i = 0; i < numNodes; i++) {
if (restrict != null && !restrict.test(i))
continue; // skip state if not one of the relevant states
if (nodeLowlink[i] == -1)
tarjan(i);
}
}
private void tarjan(final int i) throws PrismException
{
nodeIndex[i] = index;
nodeLowlink[i] = index;
index++;
stack.push(i);
onStack.set(i);
boolean hadSelfloop = false;
SuccessorsIterator it = model.getSuccessors(i);
while (it.hasNext()) {
final int e = it.nextInt();
if (e == i) {
hadSelfloop = true;
continue;
}
if (restrict != null && !restrict.test(e)) {
continue; // ignore edge to state that is not relevant
}
if (nodeIndex[e] == -1) {
tarjan(e);
nodeLowlink[i] = Math.min(nodeLowlink[i], nodeLowlink[e]);
} else if (onStack.get(e)) {
nodeLowlink[i] = Math.min(nodeLowlink[i], nodeIndex[e]);
}
}
if (nodeLowlink[i] == nodeIndex[i]) {
// this is a singleton SCC if the top of the stack equals i
boolean singletonSCC = (stack.peek() == i);
if (singletonSCC && filterTrivialSCCs) {
if (!hadSelfloop) { // singleton SCC & no selfloop -> trivial
stack.pop();
onStack.set(i, false);
return;
}
}
int n;
consumer.notifyStartSCC();
do {
n = stack.pop();
onStack.set(n, false);
consumer.notifyStateInSCC(n);
} while (n != i);
consumer.notifyEndSCC();
}
}
}