private List<Vertex<T>> traverse(DirectedGraph<T> graph, Vertex<T> start, Visitor<T> visitor) { List<Vertex<T>> visited = new ArrayList<>(); List<Vertex<T>> stack = new ArrayList<>(); Set<Vertex<T>> seen = new HashSet<>(visited); stack.add(start); seen.add(start); do { // mark as visited Vertex<T> next = stack.remove(stack.size() - 1); visited.add(next); if (!visitor.visit(next)) { return visited; } // add all non-visited adjacent vertices to the stack Set<Vertex<T>> adjacentVertices = graph.getAdjacentVertices(next); for (Vertex<T> v : adjacentVertices) { seen.add(v); stack.add(v); } } while (!stack.isEmpty()); return visited; }
public List<Vertex<T>> sort(DirectedGraph<T> dag) throws CycleException { // perform the sort over the entire graph, calculating roots and references for all children Map<Vertex<T>, AtomicInteger> vertexMap = new HashMap<>(); List<Vertex<T>> roots = new ArrayList<>(); // first pass over the graph to collect vertex references and root vertices Set<Vertex<T>> vertices = dag.getVertices(); for (Vertex<T> v : vertices) { int incoming = dag.getIncomingEdges(v).size(); if (incoming == 0) { roots.add(v); } else { AtomicInteger count = new AtomicInteger(); count.set(incoming); vertexMap.put(v, count); } } // perform the sort return sort(dag, vertexMap, roots); }
/** * Performs the sort. * * @param dag the DAG to sort * @param vertices map of vertices and references * @param roots roots in the graph * @return the total ordering calculated by the topological sort * @throws CycleException if a cycle is detected */ private List<Vertex<T>> sort(DirectedGraph<T> dag, Map<Vertex<T>, AtomicInteger> vertices, List<Vertex<T>> roots) throws CycleException { List<Vertex<T>> visited = new ArrayList<>(); int num = vertices.size() + roots.size(); while (!roots.isEmpty()) { Vertex<T> v = roots.remove(roots.size() - 1); visited.add(v); List<Vertex<T>> outgoing = dag.getOutgoingAdjacentVertices(v); for (Vertex<T> child : outgoing) { AtomicInteger count = vertices.get(child); if (count.decrementAndGet() == 0) { // add child to root list as all parents are processed roots.add(child); } } } if (visited.size() != num) { throw new CycleException(); } return visited; }
public boolean hasCycles(DirectedGraph<T> graph) { for (Vertex<T> vertex : graph.getVertices()) { if (isCycle(graph, vertex)) { return true; } } return false; }
private boolean isCycle(DirectedGraph<T> graph, Vertex<T> from) { Set<Edge<T>> edges = graph.getOutgoingEdges(from); for (Edge<T> edge : edges) { Vertex<T> opposite = edge.getOppositeVertex(from); if (isPath(graph, opposite, from)) { // cycle found return true; } } return false; }
public List<Cycle<T>> findCycles(DirectedGraph<T> graph) { List<Cycle<T>> cycles = new ArrayList<>(); for (Edge<T> edge : graph.getEdges()) { List<Vertex<T>> path = getPath(graph, edge.getSink(), edge.getSource()); if (!path.isEmpty()) { Cycle<T> cycle = searchCycle(cycles, edge); if (cycle == null) { cycle = new Cycle<>(); cycle.setOriginPath(path); cycles.add(cycle); } else { cycle.setBackPath(path); } } } return cycles; }
/** * Performs the sort. * * @param dag the DAG to sort * @param vertices map of vertices and references * @param roots roots in the graph * @return the total ordering calculated by the topological sort * @throws CycleException if a cycle is detected */ private List<Vertex<T>> sort(DirectedGraph<T> dag, Map<Vertex<T>, AtomicInteger> vertices, List<Vertex<T>> roots) throws CycleException { List<Vertex<T>> visited = new ArrayList<>(); int num = vertices.size() + roots.size(); while (!roots.isEmpty()) { Vertex<T> v = roots.remove(roots.size() - 1); visited.add(v); List<Vertex<T>> outgoing = dag.getOutgoingAdjacentVertices(v); for (Vertex<T> child : outgoing) { AtomicInteger count = vertices.get(child); if (count.decrementAndGet() == 0) { // add child to root list as all parents are processed roots.add(child); } } } if (visited.size() != num) { throw new CycleException(); } return visited; }
public boolean hasCycles(DirectedGraph<T> graph) { for (Vertex<T> vertex : graph.getVertices()) { if (isCycle(graph, vertex)) { return true; } } return false; }
private boolean isCycle(DirectedGraph<T> graph, Vertex<T> from) { Set<Edge<T>> edges = graph.getOutgoingEdges(from); for (Edge<T> edge : edges) { Vertex<T> opposite = edge.getOppositeVertex(from); if (isPath(graph, opposite, from)) { // cycle found return true; } } return false; }
public List<Cycle<T>> findCycles(DirectedGraph<T> graph) { List<Cycle<T>> cycles = new ArrayList<>(); for (Edge<T> edge : graph.getEdges()) { List<Vertex<T>> path = getPath(graph, edge.getSink(), edge.getSource()); if (!path.isEmpty()) { Cycle<T> cycle = searchCycle(cycles, edge); if (cycle == null) { cycle = new Cycle<>(); cycle.setOriginPath(path); cycles.add(cycle); } else { cycle.setBackPath(path); } } } return cycles; }
public List<Vertex<T>> sort(DirectedGraph<T> dag) throws CycleException { // perform the sort over the entire graph, calculating roots and references for all children Map<Vertex<T>, AtomicInteger> vertexMap = new HashMap<Vertex<T>, AtomicInteger>(); List<Vertex<T>> roots = new ArrayList<Vertex<T>>(); // first pass over the graph to collect vertex references and root vertices Set<Vertex<T>> vertices = dag.getVertices(); for (Vertex<T> v : vertices) { int incoming = dag.getIncomingEdges(v).size(); if (incoming == 0) { roots.add(v); } else { AtomicInteger count = new AtomicInteger(); count.set(incoming); vertexMap.put(v, count); } } // perform the sort return sort(dag, vertexMap, roots); }
/** * Performs the sort. * * @param dag the DAG to sort * @param vertices map of vertices and references * @param roots roots in the graph * @return the total ordering calculated by the topological sort * @throws CycleException if a cycle is detected */ private List<Vertex<T>> sort(DirectedGraph<T> dag, Map<Vertex<T>, AtomicInteger> vertices, List<Vertex<T>> roots) throws CycleException { List<Vertex<T>> visited = new ArrayList<Vertex<T>>(); int num = vertices.size() + roots.size(); while (!roots.isEmpty()) { Vertex<T> v = roots.remove(roots.size() - 1); visited.add(v); List<Vertex<T>> outgoing = dag.getOutgoingAdjacentVertices(v); for (Vertex<T> child : outgoing) { AtomicInteger count = vertices.get(child); if (count.decrementAndGet() == 0) { // add child to root list as all parents are processed roots.add(child); } } } if (visited.size() != num) { throw new CycleException(); } return visited; }
public boolean hasCycles(DirectedGraph<T> graph) { for (Vertex<T> vertex : graph.getVertices()) { if (isCycle(graph, vertex)) { return true; } } return false; }
private boolean isCycle(DirectedGraph<T> graph, Vertex<T> from) { Set<Edge<T>> edges = graph.getOutgoingEdges(from); for (Edge<T> edge : edges) { Vertex<T> opposite = edge.getOppositeVertex(from); if (isPath(graph, opposite, from)) { // cycle found return true; } } return false; }
public List<Cycle<T>> findCycles(DirectedGraph<T> graph) { List<Cycle<T>> cycles = new ArrayList<Cycle<T>>(); for (Edge<T> edge : graph.getEdges()) { List<Vertex<T>> path = getPath(graph, edge.getSink(), edge.getSource()); if (!path.isEmpty()) { Cycle<T> cycle = searchCycle(cycles, edge); if (cycle == null) { cycle = new Cycle<T>(); cycle.setOriginPath(path); cycles.add(cycle); } else { cycle.setBackPath(path); } } } return cycles; }
private List<Vertex<T>> traverse(DirectedGraph<T> graph, Vertex<T> start, Visitor<T> visitor) { List<Vertex<T>> visited = new ArrayList<>(); List<Vertex<T>> stack = new ArrayList<>(); Set<Vertex<T>> seen = new HashSet<>(visited); stack.add(start); seen.add(start); do { // mark as visited Vertex<T> next = stack.remove(stack.size() - 1); visited.add(next); if (!visitor.visit(next)) { return visited; } // add all non-visited adjacent vertices to the stack Set<Vertex<T>> adjacentVertices = graph.getAdjacentVertices(next); for (Vertex<T> v : adjacentVertices) { seen.add(v); stack.add(v); } } while (!stack.isEmpty()); return visited; }
public List<Vertex<T>> sort(DirectedGraph<T> dag) throws CycleException { // perform the sort over the entire graph, calculating roots and references for all children Map<Vertex<T>, AtomicInteger> vertexMap = new HashMap<>(); List<Vertex<T>> roots = new ArrayList<>(); // first pass over the graph to collect vertex references and root vertices Set<Vertex<T>> vertices = dag.getVertices(); for (Vertex<T> v : vertices) { int incoming = dag.getIncomingEdges(v).size(); if (incoming == 0) { roots.add(v); } else { AtomicInteger count = new AtomicInteger(); count.set(incoming); vertexMap.put(v, count); } } // perform the sort return sort(dag, vertexMap, roots); }