package kreisbrecher_ws1718; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** Beispielimplementierung eines Graphen (da Graph ja nur ein Interface ist). */ public class GraphTest implements Graph { private Map nodes; // index (of node) -> node private Map> edges; // index (of node) -> list of indizes of destination nodes public GraphTest() { nodes = new HashMap<>(); edges = new HashMap<>(); } @Override public Node getNode(int nodeIndex) { return nodes.get(nodeIndex); } @Override public Set getEdges(int fromNode) { return edges.get(fromNode); } @Override public void removeEdge(int from, int to) { Set dest = getEdges(from); if (dest != null) dest.remove(to); } private void addNode(int index, Node n) { if (nodes.containsKey(index)) throw new RuntimeException("Knoten " + index + " existiert bereits."); nodes.put(index, n); edges.put(index, new HashSet<>()); } public void insert(int index, int[] edgesTo) { addNode(index, new Node()); for (int t : edgesTo) addEdge(index, t); } private void addEdge(int from, int to) { Set dest = getEdges(from); dest.add(to); } /* Beispiel-Test aus der Angabe: */ public static void main(String[] args) { GraphTest g = new GraphTest(); g.insert(0, new int[]{1,3,6}); g.insert(1, new int[]{}); g.insert(2, new int[]{}); g.insert(3, new int[]{2}); g.insert(4, new int[]{3,6}); g.insert(5, new int[]{4}); g.insert(6, new int[]{5,7}); g.insert(7, new int[]{}); g.insert(8, new int[]{5}); CycleBreaker cb1 = new CycleBreaker(g, 0); new Thread(cb1).start(); CycleBreaker cb2 = new CycleBreaker(g, 6); new Thread(cb2).start(); CycleBreaker cb3 = new CycleBreaker(g, 8); new Thread(cb3).start(); } }