Skip to content
Snippets Groups Projects
Commit 61b0c71d authored by Johannes Mey's avatar Johannes Mey
Browse files

sync submodule

parent 7e6217fc
No related branches found
No related tags found
No related merge requests found
import java.util.Collections; import java.util.*;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
aspect Naviagation {
inh DependencyGraph Component.dg();
eq DependencyGraph.getComponent().dg() = this;
}
aspect Reachability { aspect Reachability {
// syn Set<Component> Component.successors() circular [new HashSet<>()] { // old, dependency-based algorithm for SCC
// Set<Component> result = new HashSet<>();
// for (Component c: getFromList()) {
// result.add(c);
// result.addAll(c.successors());
// }
// return result;
// }
// syn Set<Component> Component.predecessors() circular [new HashSet<>()] { // coll Set<Component> Component.successors() circular [new HashSet<>()] with add root DependencyGraph;
// Set<Component> result = new HashSet<>(); // Component contributes this to Component.successors() for each getToList();
// for (Component c: getToList()) { // Component contributes each this.successors() to Component.successors() for each getToList();
// result.add(c);
// result.addAll(c.predecessors());
// }
// return result;
// }
// inh DependencyGraph Component.dg(); // coll Set<Component> Component.predecessors() [new HashSet<>()] with add root DependencyGraph;
// eq DependencyGraph.getComponent().dg() = this; // Component contributes this to Component.predecessors() for each successors();
//
// syn lazy Map<Component,Set<Component>> DependencyGraph.componentPredecessorMap() {
// Map<Component,Set<Component>> result = new HashMap<>();
// for (Component c: getComponentList()) {
// result.put(c, new HashSet<>());
// }
//
// for (Component from: getComponentList()) {
// for (Component to: from.successors()){
// result.get(to).add(from);
// }
// }
// return result;
// }
syn lazy Set<Component> Component.predecessors() = new HashSet<>(); // coll HashSet<Component> Component.SCC() with add root DependencyGraph;
syn lazy Set<Component> Component.successors() = new HashSet<>(); // Component contributes each predecessors() when (successors().contains(this)) to Component.SCC() for this;
syn lazy Set<Component> Component.SCC() = new HashSet<>();
// syn lazy Set<Component> Component.SCC() {
// Set<Component> result = new HashSet<>(successors());
// result.retainAll(predecessors());
// return result;
// }
// coll HashSet<Set<Component>> DependencyGraph.SCC() with add root DependencyGraph; // coll HashSet<Set<Component>> DependencyGraph.SCC() with add root DependencyGraph;
// Component contributes SCC() when SCC().size() > 0 to DependencyGraph.SCC(); // Component contributes SCC() when SCC().size() > 0 to DependencyGraph.SCC();
syn Set<Set<Component>> DependencyGraph.SCC() { //Kosaraju's algorithm
System.out.println("Kosaraju's algorithm"); // stubs for tests that use old algorithm
syn lazy Set<Component> Component.predecessors() = new HashSet<>();
syn lazy Set<Component> Component.successors() = new HashSet<>();
syn lazy Set<Component> Component.SCC() = new HashSet<>();
/**
* Kosaraju's algorithm
*/
syn Set<Set<Component>> DependencyGraph.SCC() {
// System.out.println("Kosaraju's algorithm");
Map<Component, Integer> visited = new HashMap<>(); Map<Component, Integer> visited = new HashMap<>();
Deque<Component> locked = new LinkedList<>(); Deque<Component> locked = new LinkedList<>();
//Visit nodes forward //Visit nodes forward
long startVisit = System.nanoTime(); // long startVisit = System.nanoTime();
for (Component n : getComponentList()) for (Component n : getComponentList())
visit(n, visited, locked); visit(n, visited, locked);
//Assign nodes to SCCs backward //Assign nodes to SCCs backward
long startAssign = System.nanoTime(); // long startAssign = System.nanoTime();
int scc = 0; int scc = 0;
for (Component n : locked) { for (Component n : locked) {
assign(n, visited, scc); assign(n, visited, scc);
scc++; scc++;
} }
long stop = System.nanoTime(); // long stop = System.nanoTime();
//Map visited Map[Node,int]-> result Map[int,Set[Node]] //Map visited Map[Node,int]-> result Map[int,Set[Node]]
System.out.println("visit : "+(startAssign-startVisit)); // System.out.println("visit : " + (startAssign - startVisit));
System.out.println("assign: "+(stop-startAssign)); // System.out.println("assign: " + (stop - startAssign));
System.out.println("sum: "+(stop-startVisit)); // System.out.println("sum: " + (stop - startVisit));
Map<Integer,Set<Component>> result=visited.entrySet().stream() Map<Integer, Set<Component>> result = visited.entrySet().stream().collect(
.collect(Collectors.groupingBy(e->e.getValue(), Collectors.groupingBy(
Collectors.mapping(e->e.getKey(), e -> e.getValue(),
Collectors.toSet()))); Collectors.mapping(e -> e.getKey(), Collectors.toSet())
)
);
return result.values().stream().collect(Collectors.toSet()); return result.values().stream().collect(Collectors.toSet());
} }
public void DependencyGraph.visit(Component n, Map<Component, Integer> visited, Deque<Component> locked){ private void DependencyGraph.visit(Component n, Map<Component, Integer> visited, Deque<Component> locked) {
if (visited.containsKey(n)) return; if (visited.containsKey(n)) return;
visited.put(n, -1); visited.put(n, -1);
for (Component s : n.getFromList()) for (Component s : n.getFromList())
...@@ -92,7 +71,7 @@ aspect Reachability { ...@@ -92,7 +71,7 @@ aspect Reachability {
locked.addFirst(n); locked.addFirst(n);
} }
public void DependencyGraph.assign(Component n, Map<Component, Integer> visited, int root) { private void DependencyGraph.assign(Component n, Map<Component, Integer> visited, int root) {
if (visited.get(n) > -1) return; if (visited.get(n) > -1) return;
visited.put(n, root); visited.put(n, root);
for (Component p : n.getToList()) for (Component p : n.getToList())
......
Subproject commit 1b26decca78b3cdbfa3a9ba9666fbd75e2bdd436 Subproject commit bab01c7e6df0d3e6aa2a0bc0ba7dc9f6f2ee3184
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment