Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CS253: Topological Sort (2019)

Avatar for Jinho D. Choi Jinho D. Choi
November 11, 2019

CS253: Topological Sort (2019)

Avatar for Jinho D. Choi

Jinho D. Choi

November 11, 2019
Tweet

More Decks by Jinho D. Choi

Other Decks in Business

Transcript

  1. Emory University Logo Guidelines #PKPUVKVWVKQPCUNCTIGCPFXCTKGFCU'OQT[TGSWKTGUCEQPUKUVGPVXKUWCNKFGPVKV[VJCVWPKƂGUKVUXCTKQWU CHƂNKCVGU'OQT[oUEWTTGPVUVCPFCTFUYJKEJJCXGDGGPKPWUGUKPEGTGKPHQTEGVJGWPKSWGEJCTCEVGT CPFSWCNKV[QHGCEJCECFGOKECPFCFOKPKUVTCVKXGWPKVYJKNGUKOWNVCPGQWUN[OCMKPIKVENGCTVJCV'OQT[ UVCPFUDGJKPFGCEJQHVJGO +PCFFKVKQPVQVJGOCKP7PKXGTUKV[ITCRJKEKFGPVKƂGTUOQUVUEJQQNUCPFOCLQTWPKVUJCXGVJGKTQYP EQORNGOGPVCT[UGVQHKFGPVKV[ITCRJKEUHQTRTKPVCPFYGDYJKEJYGTGFGXGNQRGFKPECTGHWNEQPUWNVC-

    VKQPYKVJFGCPUCPFWPKVJGCFU&QYPNQCFCDNGNQIQUCPFYQTFOCTMUHQT'OQT[7PKXGTUKV[VJGUEJQQNU CPFOCLQTWPKVUECPDGHQWPFQPVJGYGDCVJVVRKFGPVKV[GOQT[GFW RTKPVITCRJKEUVCPFCTFU CPF JVVRYGDIWKFGGOQT[GFW YGDITCRJKEUVCPFCTFU 6JG'/14;YQTFOCTMKUCHGFGTCNN[TGIKUVGTGF VTCFGOCTM#UCPEVKQPGFKFGPVKƂGTQHVJG7PKXGTUKV[tCUEJQQNQTOCLQTWPKVNQIQVJCVKPENWFGUVJG UJKGNFU[ODQNCPFVJGYQTFOCTM'/14;tUJQWNFCRRGCTQPGCEJRWDNKECVKQP+H[QWYKUJVQJCXGCP KFGPVKƂGT WPKVUKIPCVWTG ETGCVGFURGEKƂECNN[HQT[QWTRTQITCOQTFGRCTVOGPVRNGCUGEQPVCEVVJG1HƂEG QH$TCPF/CPCIGOGPVCVQTUVCPKUMQFOCP"GOQT[GFW 6JGV[RGHCEG)QWF[KUTGUGTXGFHQTVJG'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
  2. Cycle Detection 2 0 1 2 3 4 No incoming

    edge No cycle Cycle! No cycle
  3. 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; }
  4. 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)
  5. 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; }
  6. Topological Sort 5 Sort vertices in a linear order such

    that for each vertex, all vertices associated with its incoming edges must appear first.
  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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Sort by Incoming Scores 6 0 1 2 3 4

    5 6 7 Keep adding vertices with no incoming edge.
  27. 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.
  28. 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.
  29. 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.
  30. 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.
  31. 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.
  32. 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.
  33. 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.
  34. 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.
  35. 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.
  36. 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.
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 Keep adding vertices that are illegible.
  51. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 Keep adding vertices that are illegible.
  52. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 Keep adding vertices that are illegible.
  53. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 Keep adding vertices that are illegible.
  54. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 Keep adding vertices that are illegible.
  55. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 Keep adding vertices that are illegible.
  56. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 Keep adding vertices that are illegible.
  57. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 Keep adding vertices that are illegible.
  58. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible.
  59. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible.
  60. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible.
  61. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible. 5
  62. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible. 5
  63. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible. 5
  64. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 3 Keep adding vertices that are illegible. 5
  65. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 2 3 Keep adding vertices that are illegible. 5
  66. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 2 3 Keep adding vertices that are illegible. 5
  67. Sort by Depth-First 8 0 1 2 3 4 5

    6 7 0 1 2 3 Keep adding vertices that are illegible. 5
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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());