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QTVJG'OQT[VTCFGOCTMUCPFPGXGTUJQWNFDGWUGFKPVGZVQTFKURNC[ EQR[;QWPGXGTUJQWNFCVVGORVVQTGPFGTVJG'OQT[NQIQD[V[RKPIVJGNGVVGTUKPCYQTFRTQEGUUKPIQT RCIGNC[QWVRTQITCO0QPGQHVJGNQIQUKUCV[RGFYQTFDWVTCVJGTKUURGEKƂECNN[FGUKIPGFXGEVQTCTV 'OQT[VKGTQPGNQIQUUJQWNFTGRTQFWEGQPN[KP'OQT[DNWG 2/5 DNCEMQTYJKVG6JGUJKGNFJGKIJV UJQWNFTGRTQFWEGCVqQTNCTIGT#IGPGTCNTWNGHQTURCEKPICTQWPFCP'OQT[NQIQKUVQKPVGITCVGCP QDXKQWUXKUWCNUGRCTCVKQPtPQFGUKIPGNGOGPVQTVGZVUJQWNFDGPGUVGFYKVJ'OQT[NQIQU 'OQT[oURTKOCT[EQNQTUCTG'OQT[DNWG 2/5 CPF[GNNQY 2/5 'OQT[7PKXGTUKV[YQTFOCTMU ECPDGTGRTQFWEGFKP'OQT[DNWG 2/5 DNCEMQTYJKVGQPCP'OQT[DNWGQTFCTMDCEMITQWPF YYYKFGPVKV[GOQT[GFW JVVRYGDIWKFGGOQT[GFW Topological Sort Data Structures and Algorithms Emory University Jinho D. Choi -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle No cycle -
Cycle Detection 2 0 1 2 3 4 No incoming
edge No cycle Cycle! No cycle -
Cycle Detection 3 public boolean containsCycle() { Deque<Integer> notVisited =
new ArrayDeque<>(); for (int i=0; i<size(); i++) notVisited.add(i); while (!notVisited.isEmpty()) { if (containsCycleAux(notVisited.poll(), notVisited, new HashSet<>())) return true; } return false; } -
Cycle Detection 3 public boolean containsCycle() { Deque<Integer> notVisited =
new ArrayDeque<>(); for (int i=0; i<size(); i++) notVisited.add(i); while (!notVisited.isEmpty()) { if (containsCycleAux(notVisited.poll(), notVisited, new HashSet<>())) return true; } return false; } boolean containsCycleAux(int target, Deque<Integer> notVisited, Set<Integer> visited) -
Cycle Detection 4 private boolean containsCycleAux(int target, Deque<Integer> notVisited, Set<Integer>
visited) { notVisited.remove(target); visited.add(target); for (Edge edge : getIncomingEdges(target)) { if (visited.contains(edge.getSource())) return true; if (containsCycleAux(edge.getSource(), notVisited, new HashSet<>(visited))) return true; } return false; } -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Topological Sort 5 Sort vertices in a linear order such
that for each vertex, all vertices associated with its incoming edges must appear first. 0 1 2 3 4 5 6 7 Incoming scores Depth-First -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 7 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 7 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 7 -
Sort by Incoming Scores 6 0 1 2 3 4
5 6 7 Incoming score of each vertex = Σ of all source vertices 0 1 2 3 Keep adding vertices with no incoming edge. 4 5 6 7 -
public List<Integer> sort(Graph graph) { Deque<Integer> global = graph.getVerticesWithNoIncomingEdges(); Deque<Edge>[]
outgoingEdgesAll = graph.getOutgoingEdges(); List<Integer> order = new ArrayList<Integer>(); while (!global.isEmpty()) { Deque<Integer> local = new ArrayDeque<>(); int vertex = global.poll(); order.add(vertex); Deque<Edge> outgoingEdges = outgoingEdgesAll[vertex]; while (!outgoingEdges.isEmpty()) { Edge edge = outgoingEdges.poll(); List<Edge> incomingEdges = graph.getIncomingEdges(edge.getTarget()); incomingEdges.remove(edge); if (incomingEdges.isEmpty()) local.add(edge.getTarget()); } while (!local.isEmpty()) global.addLast(local.removeFirst()); } if (!graph.isEmpty()) throw new IllegalArgumentException("Cyclic graph."); return order; } 7 -
public List<Integer> sort(Graph graph) { Deque<Integer> global = graph.getVerticesWithNoIncomingEdges(); Deque<Edge>[]
outgoingEdgesAll = graph.getOutgoingEdges(); List<Integer> order = new ArrayList<Integer>(); while (!global.isEmpty()) { Deque<Integer> local = new ArrayDeque<>(); int vertex = global.poll(); order.add(vertex); Deque<Edge> outgoingEdges = outgoingEdgesAll[vertex]; while (!outgoingEdges.isEmpty()) { Edge edge = outgoingEdges.poll(); List<Edge> incomingEdges = graph.getIncomingEdges(edge.getTarget()); incomingEdges.remove(edge); if (incomingEdges.isEmpty()) local.add(edge.getTarget()); } while (!local.isEmpty()) global.addLast(local.removeFirst()); } if (!graph.isEmpty()) throw new IllegalArgumentException("Cyclic graph."); return order; } 7 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 6 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 6 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 6 -
Sort by Depth-First 8 0 1 2 3 4 5
6 7 0 1 2 3 Keep adding vertices that are illegible. 4 5 6 7 -
public List<Integer> sort(Graph graph) { Deque<Integer> global = graph.getVerticesWithNoIncomingEdges(); Deque<Edge>[]
outgoingEdgesAll = graph.getOutgoingEdges(); List<Integer> order = new ArrayList<Integer>(); while (!global.isEmpty()) { Deque<Integer> local = new ArrayDeque<>(); int vertex = global.poll(); order.add(vertex); Deque<Edge> outgoingEdges = outgoingEdgesAll[vertex]; while (!outgoingEdges.isEmpty()) { Edge edge = outgoingEdges.poll(); List<Edge> incomingEdges = graph.getIncomingEdges(edge.getTarget()); incomingEdges.remove(edge); if (incomingEdges.isEmpty()) local.add(edge.getTarget()); } while (!local.isEmpty()) global.addLast(local.removeFirst()); } if (!graph.isEmpty()) throw new IllegalArgumentException("Cyclic graph."); return order; } 9 -
public List<Integer> sort(Graph graph) { Deque<Integer> global = graph.getVerticesWithNoIncomingEdges(); Deque<Edge>[]
outgoingEdgesAll = graph.getOutgoingEdges(); List<Integer> order = new ArrayList<Integer>(); while (!global.isEmpty()) { Deque<Integer> local = new ArrayDeque<>(); int vertex = global.poll(); order.add(vertex); Deque<Edge> outgoingEdges = outgoingEdgesAll[vertex]; while (!outgoingEdges.isEmpty()) { Edge edge = outgoingEdges.poll(); List<Edge> incomingEdges = graph.getIncomingEdges(edge.getTarget()); incomingEdges.remove(edge); if (incomingEdges.isEmpty()) local.add(edge.getTarget()); } while (!local.isEmpty()) global.addLast(local.removeFirst()); } if (!graph.isEmpty()) throw new IllegalArgumentException("Cyclic graph."); return order; } 9 global.addFirst(local.removeLast());