aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/scc/SCC.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/scc/SCC.java')
-rw-r--r--subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/scc/SCC.java143
1 files changed, 143 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/scc/SCC.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/scc/SCC.java
new file mode 100644
index 00000000..de070839
--- /dev/null
+++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/scc/SCC.java
@@ -0,0 +1,143 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2012, Tamas Szabo, Istvan Rath and Daniel Varro
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v. 2.0 which is available at
5 * http://www.eclipse.org/legal/epl-v20.html.
6 *
7 * SPDX-License-Identifier: EPL-2.0
8 *******************************************************************************/
9
10package tools.refinery.viatra.runtime.rete.itc.alg.misc.scc;
11
12import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
13import tools.refinery.viatra.runtime.rete.itc.igraph.IGraphDataSource;
14
15import java.util.*;
16
17/**
18 * Efficient algorithms to compute the Strongly Connected Components in a directed graph.
19 *
20 * @author Tamas Szabo
21 *
22 * @param <V>
23 * the type parameter of the nodes in the graph
24 */
25public class SCC<V> {
26
27 private SCC() {/*Utility class constructor*/}
28
29 public static long sccId = 0;
30
31 /**
32 * Computes the SCCs for the given graph and returns them as a multiset. (Iterative version of Tarjan's algorithm)
33 *
34 * @param g
35 * the directed graph data source
36 * @return the set of SCCs
37 */
38 public static <V> SCCResult<V> computeSCC(IGraphDataSource<V> g) {
39 int index = 0;
40 Set<Set<V>> ret = new HashSet<Set<V>>();
41
42 // stores the lowlink and index information for the given node
43 Map<V, SCCProperty> nodeMap = CollectionsFactory.createMap();
44
45 // stores all target nodes of a given node - the list will be modified
46 Map<V, Set<V>> targetNodeMap = CollectionsFactory.createMap();
47
48 // stores those target nodes for a given node which have not been visited
49 Map<V, Set<V>> notVisitedMap = CollectionsFactory.createMap();
50
51 // stores the nodes during the traversal
52 Deque<V> nodeStack = new ArrayDeque<V>();
53
54 // stores the nodes which belong to an scc (there can be many sccs in the stack at the same time)
55 Deque<V> sccStack = new ArrayDeque<V>();
56
57 boolean sink = false, finishedTraversal = true;
58
59 // initialize all nodes with 0 index and 0 lowlink
60 Set<V> allNodes = g.getAllNodes();
61 for (V n : allNodes) {
62 nodeMap.put(n, new SCCProperty(0, 0));
63 }
64
65 for (V n : allNodes) {
66 // if the node has not been visited yet
67 if (nodeMap.get(n).getIndex() == 0) {
68 nodeStack.push(n);
69
70 while (!nodeStack.isEmpty()) {
71 V currentNode = nodeStack.peekLast();
72 sink = false;
73 finishedTraversal = false;
74 SCCProperty prop = nodeMap.get(currentNode);
75
76 if (nodeMap.get(currentNode).getIndex() == 0) {
77 index++;
78 sccStack.addLast(currentNode);
79 prop.setIndex(index);
80 prop.setLowlink(index);
81
82 notVisitedMap.put(currentNode, new HashSet<V>());
83
84 // storing the target nodes of the actual node
85 if (g.getTargetNodes(currentNode) != null) {
86 Set<V> targets = g.getTargetNodes(currentNode).distinctValues();
87 targetNodeMap.put(currentNode, CollectionsFactory.createSet(targets));
88 }
89 }
90
91 if (targetNodeMap.get(currentNode) != null) {
92
93 // remove node from stack, the exploration of its children has finished
94 if (targetNodeMap.get(currentNode).size() == 0) {
95 targetNodeMap.remove(currentNode);
96
97 nodeStack.removeLast();
98
99 for (V targetNode : g.getTargetNodes(currentNode).distinctValues()) {
100 if (notVisitedMap.get(currentNode).contains(targetNode)) {
101 prop.setLowlink(Math.min(prop.getLowlink(), nodeMap.get(targetNode).getLowlink()));
102 } else if (sccStack.contains(targetNode)) {
103 prop.setLowlink(Math.min(prop.getLowlink(), nodeMap.get(targetNode).getIndex()));
104 }
105 }
106
107 finishedTraversal = true;
108 } else {
109 V targetNode = targetNodeMap.get(currentNode).iterator().next();
110 targetNodeMap.get(currentNode).remove(targetNode);
111 // if the targetNode has not yet been visited push it to the stack
112 // and mark it in the notVisitedMap
113 if (nodeMap.get(targetNode).getIndex() == 0) {
114 notVisitedMap.get(currentNode).add(targetNode);
115 nodeStack.addLast(targetNode);
116 }
117 }
118 }
119 // if currentNode has no target nodes
120 else {
121 nodeStack.removeLast();
122 sink = true;
123 }
124
125 // create scc if node is a sink or an scc has been found
126 if ((sink || finishedTraversal) && (prop.getLowlink() == prop.getIndex())) {
127 Set<V> sc = new HashSet<V>();
128 V targetNode = null;
129
130 do {
131 targetNode = sccStack.removeLast();
132 sc.add(targetNode);
133 } while (!targetNode.equals(currentNode));
134
135 ret.add(sc);
136 }
137 }
138 }
139 }
140
141 return new SCCResult<V>(ret, g);
142 }
143}