{"id":574,"date":"2018-07-19T11:14:39","date_gmt":"2018-07-19T11:14:39","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=574"},"modified":"2018-12-21T10:22:03","modified_gmt":"2018-12-21T10:22:03","slug":"topological-sorting","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/topological-sorting\/","title":{"rendered":"Topological sorting"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/uJtH4WperDs\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss about Topological Sorting.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 To understand the concept of Topological Sorting\r\n\r\n\u2022 To discuss some applications of Topological Sorting\r\n\r\n\u2022 To Illustrate Topological Sorting using a walkthrough example\r\n\r\n&nbsp;\r\n\r\n<strong>37.1 Topological Sort<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We have a <strong>set of tasks<\/strong> and a <strong>set of dependencies (precedence constraints)<\/strong> of form \u201ctask A must be done before task B\u201d.<\/p>\r\n&nbsp;\r\n\r\n<strong>Topological sort<\/strong>: An ordering of the tasks that conforms to the given dependencies.\r\n\r\n&nbsp;\r\n\r\n<strong>Goal<\/strong>: Find a topological sort of the tasks or decide that there is no such ordering\r\n\r\n&nbsp;\r\n\r\n<strong>37.2 Recap of Graph Fundamentals<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>37.2.1 Digraphs<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A digraph is a graph whose edges are all directed. It is a shortened representation of \u201cdirected graph\u201d. Some of the applications of digraphs are representation of the following One-way streets, Flights, Task Scheduling as graphs.<\/p>\r\n&nbsp;\r\n\r\n<strong>37.2.2 Digraph Application<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Scheduling is a typical example of a digraph. Edge (a,b) means that task a must be completed before task b can start. Consider the following graph given in Figure 37.1. The first task that has to be done is ics21 as it does not have any predecessors. Next, we can proceed in sequence to ics22, ics23 and then proceed onto ics161 and end with\u00a0<span style=\"font-size: 1em;text-align: initial\">good life. Or, after ics23, we can do ics52 and ics53 in parallel. Please note that, in order to do ics53, we have to do ics21, ics22 as well as ics23. Similarly, ics51 can be done after ics21. It does not have to wait for ics22 or ics23 as there are no edges from these two to ics51. Another order is ics131 after ics21 as there is an edge from ics21 to ics131. Similarly ics151 can be done after ics51. But ics53 ends there. There are various possible moves from ics21 such as ics131 or ics51 or icsics22. But to reach ics53, ics21, ics22, ics23 and ics51 have to be finished.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-577 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383.png\" alt=\"\" width=\"572\" height=\"249\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>37.2.3 Reachability<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider the example given in Figure 37.2. The list of vertices reachable from any node via directed paths is called reachability.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-578 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384.png\" alt=\"\" width=\"626\" height=\"228\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider node C. The nodes reachable from C are E, A and D. That is, from C we can go to E directly or to A and D through E (Figure 37.2).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Similarly, from B, the nodes reachable are: C, E, A, D and F. While B has direct connectivity to A, C, D and F, E can be reached through C. This is shown in Figure 37.3.<\/p>\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-579 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-385.png\" alt=\"\" width=\"319\" height=\"200\" \/><\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>37.3 DAGs and Topological Ordering<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A directed acyclic graph (DAG) is a digraph that has no directed cycles. Figure 37.4 is an example of a DAG and its topological ordering.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-580 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386.png\" alt=\"\" width=\"643\" height=\"213\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A\u00a0 topological ordering of a digraph is a numbering <strong><em>v<\/em><\/strong>1 <strong><em>, \u2026, v<\/em><\/strong><strong><em>n<\/em><\/strong> of the vertices such that for every edge (<em>v<\/em><em>i<\/em> <em>, v<\/em><em>j<\/em>), we have <em>i<\/em> &lt; <em>j.<\/em> That is i has to be done before j. Example: In a task scheduling digraph, a topological ordering is a task sequence that satisfies the precedence constraints. A digraph admits a topological ordering if and only if it is a DAG. One of the topological ordering for the DAG G, is shown in Figure 37.4.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Initial node can be A or B. Only after A and B are done can we move to C. Only after C and B are done can we move to D. After D is done we can move to E. So, the ordering here is: A,B,C,D and E.<\/p>\r\n&nbsp;\r\n\r\n<strong>37.4 Topological Sorting<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider an example of Topological sorting for a typical student day graph which is given in Figure 37.5.<\/p>\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-581 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387.png\" alt=\"\" width=\"621\" height=\"375\" \/><\/strong>\r\n<div>\r\n<p style=\"text-align: justify\">The first node to be traversed is wake-up. After that, study computer sci.. Then , we can either do eat or nap (in any order). Only after eat and nap, we can do more c.s. There are 3 things to be done before \u201cwrite a c.s. program\u201d: play, more c.s. and work out. Play and work out can be done only after \u201cmore c.s.\u201d. But, after \u201cmore c.s.\u201d, play and work out can be done in any order. Once \u201cwrite a c.s. program is over, \u201cmake cookies for professors\u201d, sleep and then \u201cdream about graphs\u201d can be done in that order. For a directed acyclic graph (DAG) G = (V,E), a topological sort is a linear ordering of all of G\u2019s vertices v1, v2, \u2026, vn such that:<\/p>\r\n&nbsp;\r\n\r\nFormally: for every edge (vi,vk) in <em>E<\/em>, i&lt;k.\r\n\r\nVisually: all arrows are pointing to the right.\r\n\r\nA real-world example for this is getting dressed.\r\n\r\n&nbsp;\r\n\r\n<strong>37.4.1 Examples<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">One of the major applications of topological sorting is scheduling where graphs are used to represent the order of tasks. When scheduling task graphs in distributed systems, usually we first need to sort the tasks topologically and then assign them to resources (the most efficient scheduling is an NP-complete problem). Another application is during compilation to order the modules\/libraries. Consider the graph given in Figure 37.6. In the graph given at the side, we can either start with d or c. Let us assume that we start with c. After c, we can proceed to d. After d, we can do a, g and f. After this, we can do b followed by e.<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-582 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-388.png\" alt=\"\" width=\"350\" height=\"226\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider another example of getting dressed. The dependencies and the topological ordering is shown in Figure 37.7.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-583 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389.png\" alt=\"\" width=\"517\" height=\"274\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The topological ordering may be as follows: we start with Socks, Underwear, Pants, Shoes, then, Shirt, Belt, Tie and Jacket and then Watch. Note that here, the node Watch is independent.<\/p>\r\n&nbsp;\r\n\r\n<strong>37.5 Topological sorting for cyclic graphs?<\/strong>\r\n\r\n<img class=\"size-full wp-image-584 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-390.png\" alt=\"\" width=\"211\" height=\"148\" \/>\r\n<p style=\"text-align: justify\">The topological sorting for cyclic graphs is impossible. Let us consider two vertices v and w on a cycle, there exists paths from v to w <em>and<\/em> from w to v. Consider the cyclic\u00a0<span style=\"font-size: 1em;text-align: initial\">graph (Figure 37.8). To do 1, we have to do 2. To do 2, we have to do 3. To do 3, we have to do 1. Any ordering will contradict one of these paths .<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>37.6 Topological sort more formally<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Is it possible to execute all the tasks in <strong>G<\/strong> in an order that respects all the precedence requirements given by the graph edges? The answer is \"<strong>yes<\/strong>\" <em>if and only if<\/em> the directed graph <strong>G<\/strong> has <strong>no cycle<\/strong>! (Otherwise we have a <strong>deadlock<\/strong>).<\/p>\r\n&nbsp;\r\n\r\n<strong>37.6.1 Topological sort<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Topological sort can be defined as the linearly ordering of the vertices so that the lineorder respects the ordering relations implied by the arcs.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-585 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391.png\" alt=\"\" width=\"361\" height=\"218\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Considering the graph shown in Figure 37.9, the following topological orderings are possible:0,1,2,5,9, and 0,4,5,9 , but 0,6,3,7 is not possible because there is no edge between 3 and 7. There are often many possible topological sorts of a given DAG. Consider the following DAG (Figure 37.10)<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-586 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392.png\" alt=\"\" width=\"431\" height=\"215\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe Topological orders for this DAG are as follows:1,2,5,4,3,6,7 2,1,5,4,7,3,6,2,5,1,4,7,3,6 etc.. Each topological order is a feasible schedule.\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Let us consider the following problem of Getting Ready: <em>A - waking up, B - taking a<\/em> <em>shower, C - eating breakfast and D leaving for work<\/em><\/p>\r\n&nbsp;\r\n\r\nThe constraints would be A before B, A before C, B before D, and C before D\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can model dependencies (or constraints) like these using a <strong><em>Directed Acyclic<\/em><\/strong> <strong><em>Graph. <\/em><\/strong>Given a set of items and constraints, we create the corresponding graph. In this graph each item corresponds to a vertex in the graph and for each constraint where item A must finish before item B, we place a directed edge A \u00e0 B. Following these rules, the DAG would be as shown in<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-587 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-393.png\" alt=\"\" width=\"285\" height=\"219\" \/>\r\n\r\n&nbsp;\r\n\r\nA topological sort would give A, B, C, D.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Another example for topological sort is items of clothing to wear. The items are shown in Figure 37.12.<\/p>\r\n<img class=\"size-full wp-image-588 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394.png\" alt=\"\" width=\"592\" height=\"138\" \/>\r\n\r\nThere is no exact one order to put these items on, but we must adhere to certain restrictions:\r\n<ul>\r\n \t<li>Socks must be put on before Shoes<\/li>\r\n \t<li>Undergarments must be put on before Slacks and Shirt<\/li>\r\n \t<li>Slacks must be put on before Belt<\/li>\r\n \t<li>Slacks must be put on before Shoes<\/li>\r\n<\/ul>\r\nAnother example considers CS classes and their prerequisites (Figure 37.13) :\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-589 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395.png\" alt=\"\" width=\"650\" height=\"275\" \/>\r\n\r\n&nbsp;\r\n\r\nThe goal of a topological sort would be to find an ordering of these classes that you can take.\r\n\r\n&nbsp;\r\n\r\n<strong>37.7 Goal of a Topological Sort:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When given a list of items with dependencies (i.e. item 5 must be completed before item 3, etc.), produce an ordering of the items that satisfies the given constraints. In order for the problem to be solvable, there can\u2019t be a cyclic set of constraints. We can\u2019t have item 5 must be completed before item 3, item 3 must be completed before item 7, and item 7 must be completed before item 5. But however<\/p>\r\n&nbsp;\r\n\r\n<strong>37.8 Topological sort algorithm<\/strong>\r\n\r\n&nbsp;\r\n\r\nAssume that the in-degree is stored with each node (in-degree is the number of incoming edges to the node). Now\r\n\r\nRepeat until no nodes remain:\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 Choose a root and output it.\r\n\r\n\u2022\u00a0 Remove the root and all its edges.\r\n\r\n&nbsp;\r\n\r\nThe performance of the algorithm is of the order O(V\u00b2+E), if linear search is used to find the root.\r\n<p style=\"text-align: justify\">The algorithm is as follows:<\/p>\r\n&nbsp;\r\n\r\n\u2013Scan all nodes, pushing roots onto a stack.\r\n\r\n\u2013Repeat until stack is empty:\r\n\r\n&nbsp;\r\n\r\n1. Pop a root r from the stack and output it.\r\n<p style=\"text-align: justify\">2.For all nodes n such that (r,n) is an edge, decrement n\u2019s in-degree. If the in degree of the node is 0 then push it onto the stack.<\/p>\r\n&nbsp;\r\n\r\nThe performance of the algorithm is O(V+E), so still O(V\u00b2) in worst case, but better for sparse graphs.\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-590 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396.png\" alt=\"\" width=\"632\" height=\"258\" \/><\/strong>\r\n<div>\r\n\r\nSince D and G have in-degree 0, we can start with them. The order would be: D,G,A,B,F,H,J,E,I,C.\r\n\r\n&nbsp;\r\n\r\nThis is shown in Figure 37.15 given below:\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-591 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397.png\" alt=\"\" width=\"662\" height=\"184\" \/>\r\n\r\n&nbsp;\r\n\r\nThe details of the topological sort algorithm is given below:\r\n\r\n&nbsp;\r\n\r\n<strong>Starting point must have zero in-degree<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 If it doesn\u2019t exist, the graph would not be acyclic\r\n\r\n&nbsp;\r\n\r\n<strong>Algorithm<\/strong>\r\n\r\n&nbsp;\r\n\r\n1. A vertex with zero <em>in-degree<\/em> is a task that can start right away. So we can output it first in the linear order\r\n<p style=\"text-align: justify\">2.If a vertex <em>i<\/em> is output, then its outgoing arcs <em>(i, j)<\/em> are no longer useful, since tasks <em>j<\/em> does not need to wait for <em>i<\/em> anymore- so remove all <em>i<\/em>\u2019s outgoing arcs<\/p>\r\n<p style=\"text-align: justify\">3. With vertex <em>i<\/em> removed, the new graph is still a directed acyclic graph. So, repeat step 1-2 until no vertex is left.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>37.8.1 Topological sort algorithm explained with Example<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-592 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398.png\" alt=\"\" width=\"606\" height=\"321\" \/>\r\n<p style=\"text-align: justify\">The algorithm is implemented as a traversal method that visits the vertices in a topological sort order. An array of length |V| is used to record the in-degrees of the vertices. Hence there no need to remove vertices or edges. A priority queue is used to keep track of vertices with in-degree zero that are not yet visited.<\/p>\r\n&nbsp;\r\n\r\n<strong>37.9 Walk Through Example<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Considering the example directed graph shown in Figure 37.16 (a) we will illustrate the steps of the algorithm discussed above. Two data structures are associated with the graph. One is the adjacency list where every vertex stored in an array points to a list of neighbours, it points to that is the neighbours for which this node is the .source node. Another array stores all nodes and their in-degree. Now in this example of topological sorting we start with 0 node (Figure 37.16 (a)). The queue initially has the start node 0 and the output is also 0.<\/p>\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-593 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399.png\" alt=\"\" width=\"599\" height=\"361\" \/><\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we remove the element from the front of the queue (here 0), remove arcs (edges) from 0 and need to adjust the in-degrees of 0\u2019s neighbours that is the in-degrees of nodes 1,4,6 needs to be decremented ((Figure 37.16 (b)).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The in-degree of the nodes (6,1,4) are updated and the node 0 is marked in the in-degree table. Now since the in-degree of nodes 6, 1, 4 after updation is 0 that is they all can be start points, they are enqueued into the priority queue (Figure 37.16 (c)).<\/p>\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-594 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400.png\" alt=\"\" width=\"653\" height=\"374\" \/><\/strong>\r\n<div>\r\n<p style=\"text-align: justify\">Now we dequeue 6, and output it. Then we remove arcs (edges) from 6 and need to adjust in-degree of its neighbours (2,3) (Figure 37.16 (d)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-595 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401.png\" alt=\"\" width=\"658\" height=\"369\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The in-degree of the nodes (2,3) are updated and the node 6 is marked in the in-degree table. Now since the in-degree of nodes 1, 4 and 3 after updation is 0 that is they all can be start points. While nodes 1,4 have already been placed in the priority queue\u00a0<span style=\"font-size: 1em;text-align: initial\">now we enqueue 3., the new start node. Note that the in-degree of node 2 after updation is not zero and hence cannot be a start node at this stage and hence is not enqueued (Figure 37.16 (e)).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-596 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402.png\" alt=\"\" width=\"646\" height=\"342\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 1, and output it. Then we remove arcs (edges) from 1and need to adjust in-degree of its neighbours (2) (Figure 37.16 (f)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-597 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403.png\" alt=\"\" width=\"649\" height=\"337\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The in-degree of the node 2 is updated and the node 1 is marked in the in-degree table. Now since the in-degree of node 2 after updation is 0 that is it can be a start point.\u00a0<span style=\"font-size: 1em;text-align: initial\">While nodes 1,4,3 have already been placed in the priority queue now we enqueue the new start point 2. (Figure 37.16 (g).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-598 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404.png\" alt=\"\" width=\"654\" height=\"372\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 4, and output it. Then we remove arcs (edges) from 4 and need to adjust in-degree of its neighbours (5) (Figure 37.16 (h)).<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-599 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405.png\" alt=\"\" width=\"641\" height=\"354\" \/>\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">The in-degree of the node 5 is updated and the node 4 is marked in the in-degree table. Now due to this updation node 5\u2019s in-degree becomes 1 only and no new node\u2019s in-degree becomes 0, that is no new start point is formed. Hence the queue remains unchanged. (Figure 37.16 (i)).<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-600 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406.png\" alt=\"\" width=\"640\" height=\"370\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 3, and output it. Then we remove arcs (edges) from 3 and need to adjust in-degree of its neighbours (8) (Figure 37.16 (j)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-601 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407.png\" alt=\"\" width=\"641\" height=\"336\" \/>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">The in-degree of the node 8 is updated and the node 3 is marked in the in-degree table. Now due to this updation node 8\u2019s in-degree becomes 1 only and no new node\u2019s in-degree becomes 0, that is no new start point is formed. Hence the queue remains unchanged. (Figure 37.16 (k)).<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-602 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408.png\" alt=\"\" width=\"652\" height=\"348\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 2, and output it. Then we remove arcs (edges) from 2 and need to adjust in-degree of its neighbours (7,5) (Figure 37.16 (l)). Note that after this dequeueing the queue is empty.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-603 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409.png\" alt=\"\" width=\"652\" height=\"340\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">The in-degree of the nodes (5,7) are updated and the node 2 is marked in the in-degree table. Now since the in-degree of nodes 5 &amp; 7 after updation is 0 that is they all can be start points. Now we enqueue 5,7, the new start nodes. (Figure 37.16 (m)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-604 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410.png\" alt=\"\" width=\"634\" height=\"331\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 5, and output it. Then we remove arcs (edges) from 5 and need to adjust in-degree of its neighbours (9) (Figure 37.16 (n).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-605 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411.png\" alt=\"\" width=\"661\" height=\"345\" \/>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The in-degree of the node 9 is updated and the node 5 is marked in the in-degree table.Now since the in-degree of nodes\u00a0 9 after updation is not 0, no new start point is found.(Figure 37.16 (o)).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-606 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412.png\" alt=\"\" width=\"672\" height=\"338\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 7, and output it. Then we remove arcs (edges) from 7 and need to adjust in-degree of its neighbours (8) (Figure 37.16 (p)). Note that after this dequeueing the queue is empty.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-607 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413.png\" alt=\"\" width=\"649\" height=\"329\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">The in-degree of the node 8 is updated and the node 7 is marked in the in-degree table. Now since the in-degree of nodes 8 after updation is 0, we enqueue it. (Figure 37.16 (q)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-608 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414.png\" alt=\"\" width=\"643\" height=\"323\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we dequeue 8, and output it. Then we remove arcs (edges) from 8 and need to adjust in-degree of its neighbour (9) (Figure 37.16 (r)). Note that after this dequeueing the queue is empty.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-609 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415.png\" alt=\"\" width=\"647\" height=\"385\" \/>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">The in-degree of the node 9 is updated and the node 8 is marked in the in-degree table. Now since the in-degree of node 9 after updation is 0, we enqueue it. Now we dequeue 9 and output it. Since it has no neighbours process stops (Figure 37.16 (s)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-610 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416.png\" alt=\"\" width=\"538\" height=\"597\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The final topological sorting of the graph is shown in Figure 37.16 (t). Now we can check whether the topological ordering is correct.<strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><\/p>\r\n\r\n<\/div>\r\nWe start with 0 \u2013 (no dependency since in-degree is 0.\r\n\r\nNext is 6 \u2013 in-degree is 1 and dependent on 0 which has already been processed\r\n\r\nNext is 1\u2013 in-degree is 1 and dependent on 0 which has already been processed\r\n\r\nNext is 4 \u2013 in-degree is 1 and dependent on 0 which has already been processed\r\n\r\nNext is 3\u00a0 \u2013 in-degree is 1 and dependent on 6 which has already been processed\r\n\r\nNext is 2 \u2013 in-degree is 2 and dependent on 1&amp; 6 both of which has already been processed\r\n<p style=\"text-align: justify\">Next is 5- in-degree is 2 and dependent on 2 &amp;4 which has already been processed<\/p>\r\n<p style=\"text-align: justify\">Next is 7\u2013 in-degree is 1 and dependent on 2 which has already been processed<\/p>\r\nNext is 8 \u2013 in-degree is 2 and dependent on 3 &amp; 7 both of which has already been processed\r\n\r\nNext is 9\u2013 in-degree is 2 and dependent on 8 &amp; 5 both of which has already been processed\r\n\r\nHence we see that the topological ordering has been followed.\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the concept of Topological Sorting<\/li>\r\n \t<li>Discussed some applications of Topological Sorting<\/li>\r\n \t<li>Illustrated Topological Sorting using a walkthrough example<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Topological sorting<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/uJtH4WperDs\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<img class=\"size-full wp-image-611 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417.png\" alt=\"\" width=\"647\" height=\"553\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/uJtH4WperDs\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss about Topological Sorting.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 To understand the concept of Topological Sorting<\/p>\n<p>\u2022 To discuss some applications of Topological Sorting<\/p>\n<p>\u2022 To Illustrate Topological Sorting using a walkthrough example<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.1 Topological Sort<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We have a <strong>set of tasks<\/strong> and a <strong>set of dependencies (precedence constraints)<\/strong> of form \u201ctask A must be done before task B\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Topological sort<\/strong>: An ordering of the tasks that conforms to the given dependencies.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Goal<\/strong>: Find a topological sort of the tasks or decide that there is no such ordering<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.2 Recap of Graph Fundamentals<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.2.1 Digraphs<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A digraph is a graph whose edges are all directed. It is a shortened representation of \u201cdirected graph\u201d. Some of the applications of digraphs are representation of the following One-way streets, Flights, Task Scheduling as graphs.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.2.2 Digraph Application<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Scheduling is a typical example of a digraph. Edge (a,b) means that task a must be completed before task b can start. Consider the following graph given in Figure 37.1. The first task that has to be done is ics21 as it does not have any predecessors. Next, we can proceed in sequence to ics22, ics23 and then proceed onto ics161 and end with\u00a0<span style=\"font-size: 1em;text-align: initial\">good life. Or, after ics23, we can do ics52 and ics53 in parallel. Please note that, in order to do ics53, we have to do ics21, ics22 as well as ics23. Similarly, ics51 can be done after ics21. It does not have to wait for ics22 or ics23 as there are no edges from these two to ics51. Another order is ics131 after ics21 as there is an edge from ics21 to ics131. Similarly ics151 can be done after ics51. But ics53 ends there. There are various possible moves from ics21 such as ics131 or ics51 or icsics22. But to reach ics53, ics21, ics22, ics23 and ics51 have to be finished.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-577 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383.png\" alt=\"\" width=\"572\" height=\"249\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383.png 572w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383-225x98.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-383-350x152.png 350w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.2.3 Reachability<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider the example given in Figure 37.2. The list of vertices reachable from any node via directed paths is called reachability.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-578 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384.png\" alt=\"\" width=\"626\" height=\"228\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384.png 626w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384-300x109.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384-225x82.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-384-350x127.png 350w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider node C. The nodes reachable from C are E, A and D. That is, from C we can go to E directly or to A and D through E (Figure 37.2).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Similarly, from B, the nodes reachable are: C, E, A, D and F. While B has direct connectivity to A, C, D and F, E can be reached through C. This is shown in Figure 37.3.<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-579 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-385.png\" alt=\"\" width=\"319\" height=\"200\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-385.png 319w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-385-300x188.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-385-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-385-225x141.png 225w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>37.3 DAGs and Topological Ordering<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A directed acyclic graph (DAG) is a digraph that has no directed cycles. Figure 37.4 is an example of a DAG and its topological ordering.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-580 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386.png\" alt=\"\" width=\"643\" height=\"213\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386.png 643w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386-300x99.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386-225x75.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-386-350x116.png 350w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A\u00a0 topological ordering of a digraph is a numbering <strong><em>v<\/em><\/strong>1 <strong><em>, \u2026, v<\/em><\/strong><strong><em>n<\/em><\/strong> of the vertices such that for every edge (<em>v<\/em><em>i<\/em> <em>, v<\/em><em>j<\/em>), we have <em>i<\/em> &lt; <em>j.<\/em> That is i has to be done before j. Example: In a task scheduling digraph, a topological ordering is a task sequence that satisfies the precedence constraints. A digraph admits a topological ordering if and only if it is a DAG. One of the topological ordering for the DAG G, is shown in Figure 37.4.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Initial node can be A or B. Only after A and B are done can we move to C. Only after C and B are done can we move to D. After D is done we can move to E. So, the ordering here is: A,B,C,D and E.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.4 Topological Sorting<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider an example of Topological sorting for a typical student day graph which is given in Figure 37.5.<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-581 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387.png\" alt=\"\" width=\"621\" height=\"375\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387.png 621w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387-300x181.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-387-350x211.png 350w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/strong><\/p>\n<div>\n<p style=\"text-align: justify\">The first node to be traversed is wake-up. After that, study computer sci.. Then , we can either do eat or nap (in any order). Only after eat and nap, we can do more c.s. There are 3 things to be done before \u201cwrite a c.s. program\u201d: play, more c.s. and work out. Play and work out can be done only after \u201cmore c.s.\u201d. But, after \u201cmore c.s.\u201d, play and work out can be done in any order. Once \u201cwrite a c.s. program is over, \u201cmake cookies for professors\u201d, sleep and then \u201cdream about graphs\u201d can be done in that order. For a directed acyclic graph (DAG) G = (V,E), a topological sort is a linear ordering of all of G\u2019s vertices v1, v2, \u2026, vn such that:<\/p>\n<p>&nbsp;<\/p>\n<p>Formally: for every edge (vi,vk) in <em>E<\/em>, i&lt;k.<\/p>\n<p>Visually: all arrows are pointing to the right.<\/p>\n<p>A real-world example for this is getting dressed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.4.1 Examples<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One of the major applications of topological sorting is scheduling where graphs are used to represent the order of tasks. When scheduling task graphs in distributed systems, usually we first need to sort the tasks topologically and then assign them to resources (the most efficient scheduling is an NP-complete problem). Another application is during compilation to order the modules\/libraries. Consider the graph given in Figure 37.6. In the graph given at the side, we can either start with d or c. Let us assume that we start with c. After c, we can proceed to d. After d, we can do a, g and f. After this, we can do b followed by e.<\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-582 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-388.png\" alt=\"\" width=\"350\" height=\"226\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-388.png 350w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-388-300x194.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-388-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-388-225x145.png 225w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider another example of getting dressed. The dependencies and the topological ordering is shown in Figure 37.7.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-583 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389.png\" alt=\"\" width=\"517\" height=\"274\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389.png 517w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-389-350x185.png 350w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The topological ordering may be as follows: we start with Socks, Underwear, Pants, Shoes, then, Shirt, Belt, Tie and Jacket and then Watch. Note that here, the node Watch is independent.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.5 Topological sorting for cyclic graphs?<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-584 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-390.png\" alt=\"\" width=\"211\" height=\"148\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-390.png 211w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-390-65x46.png 65w\" sizes=\"auto, (max-width: 211px) 100vw, 211px\" \/><\/p>\n<p style=\"text-align: justify\">The topological sorting for cyclic graphs is impossible. Let us consider two vertices v and w on a cycle, there exists paths from v to w <em>and<\/em> from w to v. Consider the cyclic\u00a0<span style=\"font-size: 1em;text-align: initial\">graph (Figure 37.8). To do 1, we have to do 2. To do 2, we have to do 3. To do 3, we have to do 1. Any ordering will contradict one of these paths .<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>37.6 Topological sort more formally<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Is it possible to execute all the tasks in <strong>G<\/strong> in an order that respects all the precedence requirements given by the graph edges? The answer is &#8220;<strong>yes<\/strong>&#8221; <em>if and only if<\/em> the directed graph <strong>G<\/strong> has <strong>no cycle<\/strong>! (Otherwise we have a <strong>deadlock<\/strong>).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.6.1 Topological sort<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Topological sort can be defined as the linearly ordering of the vertices so that the lineorder respects the ordering relations implied by the arcs.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-585 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391.png\" alt=\"\" width=\"361\" height=\"218\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391.png 361w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391-300x181.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-391-350x211.png 350w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Considering the graph shown in Figure 37.9, the following topological orderings are possible:0,1,2,5,9, and 0,4,5,9 , but 0,6,3,7 is not possible because there is no edge between 3 and 7. There are often many possible topological sorts of a given DAG. Consider the following DAG (Figure 37.10)<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-586 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392.png\" alt=\"\" width=\"431\" height=\"215\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392.png 431w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392-225x112.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-392-350x175.png 350w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The Topological orders for this DAG are as follows:1,2,5,4,3,6,7 2,1,5,4,7,3,6,2,5,1,4,7,3,6 etc.. Each topological order is a feasible schedule.<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Let us consider the following problem of Getting Ready: <em>A &#8211; waking up, B &#8211; taking a<\/em> <em>shower, C &#8211; eating breakfast and D leaving for work<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>The constraints would be A before B, A before C, B before D, and C before D<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can model dependencies (or constraints) like these using a <strong><em>Directed Acyclic<\/em><\/strong> <strong><em>Graph. <\/em><\/strong>Given a set of items and constraints, we create the corresponding graph. In this graph each item corresponds to a vertex in the graph and for each constraint where item A must finish before item B, we place a directed edge A \u00e0 B. Following these rules, the DAG would be as shown in<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-587 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-393.png\" alt=\"\" width=\"285\" height=\"219\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-393.png 285w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-393-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-393-225x173.png 225w\" sizes=\"auto, (max-width: 285px) 100vw, 285px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>A topological sort would give A, B, C, D.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Another example for topological sort is items of clothing to wear. The items are shown in Figure 37.12.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-588 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394.png\" alt=\"\" width=\"592\" height=\"138\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394.png 592w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394-300x70.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394-65x15.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394-225x52.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-394-350x82.png 350w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p>There is no exact one order to put these items on, but we must adhere to certain restrictions:<\/p>\n<ul>\n<li>Socks must be put on before Shoes<\/li>\n<li>Undergarments must be put on before Slacks and Shirt<\/li>\n<li>Slacks must be put on before Belt<\/li>\n<li>Slacks must be put on before Shoes<\/li>\n<\/ul>\n<p>Another example considers CS classes and their prerequisites (Figure 37.13) :<\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-589 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395.png\" alt=\"\" width=\"650\" height=\"275\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395.png 650w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395-300x127.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395-225x95.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-395-350x148.png 350w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The goal of a topological sort would be to find an ordering of these classes that you can take.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.7 Goal of a Topological Sort:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When given a list of items with dependencies (i.e. item 5 must be completed before item 3, etc.), produce an ordering of the items that satisfies the given constraints. In order for the problem to be solvable, there can\u2019t be a cyclic set of constraints. We can\u2019t have item 5 must be completed before item 3, item 3 must be completed before item 7, and item 7 must be completed before item 5. But however<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.8 Topological sort algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Assume that the in-degree is stored with each node (in-degree is the number of incoming edges to the node). Now<\/p>\n<p>Repeat until no nodes remain:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 Choose a root and output it.<\/p>\n<p>\u2022\u00a0 Remove the root and all its edges.<\/p>\n<p>&nbsp;<\/p>\n<p>The performance of the algorithm is of the order O(V\u00b2+E), if linear search is used to find the root.<\/p>\n<p style=\"text-align: justify\">The algorithm is as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2013Scan all nodes, pushing roots onto a stack.<\/p>\n<p>\u2013Repeat until stack is empty:<\/p>\n<p>&nbsp;<\/p>\n<p>1. Pop a root r from the stack and output it.<\/p>\n<p style=\"text-align: justify\">2.For all nodes n such that (r,n) is an edge, decrement n\u2019s in-degree. If the in degree of the node is 0 then push it onto the stack.<\/p>\n<p>&nbsp;<\/p>\n<p>The performance of the algorithm is O(V+E), so still O(V\u00b2) in worst case, but better for sparse graphs.<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-590 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396.png\" alt=\"\" width=\"632\" height=\"258\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396.png 632w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396-300x122.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396-65x27.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396-225x92.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-396-350x143.png 350w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/strong><\/p>\n<div>\n<p>Since D and G have in-degree 0, we can start with them. The order would be: D,G,A,B,F,H,J,E,I,C.<\/p>\n<p>&nbsp;<\/p>\n<p>This is shown in Figure 37.15 given below:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-591 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397.png\" alt=\"\" width=\"662\" height=\"184\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397.png 662w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397-300x83.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397-225x63.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-397-350x97.png 350w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The details of the topological sort algorithm is given below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Starting point must have zero in-degree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 If it doesn\u2019t exist, the graph would not be acyclic<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1. A vertex with zero <em>in-degree<\/em> is a task that can start right away. So we can output it first in the linear order<\/p>\n<p style=\"text-align: justify\">2.If a vertex <em>i<\/em> is output, then its outgoing arcs <em>(i, j)<\/em> are no longer useful, since tasks <em>j<\/em> does not need to wait for <em>i<\/em> anymore- so remove all <em>i<\/em>\u2019s outgoing arcs<\/p>\n<p style=\"text-align: justify\">3. With vertex <em>i<\/em> removed, the new graph is still a directed acyclic graph. So, repeat step 1-2 until no vertex is left.<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>37.8.1 Topological sort algorithm explained with Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-592 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398.png\" alt=\"\" width=\"606\" height=\"321\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398.png 606w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-398-350x185.png 350w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/p>\n<p style=\"text-align: justify\">The algorithm is implemented as a traversal method that visits the vertices in a topological sort order. An array of length |V| is used to record the in-degrees of the vertices. Hence there no need to remove vertices or edges. A priority queue is used to keep track of vertices with in-degree zero that are not yet visited.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>37.9 Walk Through Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Considering the example directed graph shown in Figure 37.16 (a) we will illustrate the steps of the algorithm discussed above. Two data structures are associated with the graph. One is the adjacency list where every vertex stored in an array points to a list of neighbours, it points to that is the neighbours for which this node is the .source node. Another array stores all nodes and their in-degree. Now in this example of topological sorting we start with 0 node (Figure 37.16 (a)). The queue initially has the start node 0 and the output is also 0.<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-593 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399.png\" alt=\"\" width=\"599\" height=\"361\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399.png 599w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399-300x181.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-399-350x211.png 350w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we remove the element from the front of the queue (here 0), remove arcs (edges) from 0 and need to adjust the in-degrees of 0\u2019s neighbours that is the in-degrees of nodes 1,4,6 needs to be decremented ((Figure 37.16 (b)).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The in-degree of the nodes (6,1,4) are updated and the node 0 is marked in the in-degree table. Now since the in-degree of nodes 6, 1, 4 after updation is 0 that is they all can be start points, they are enqueued into the priority queue (Figure 37.16 (c)).<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-594 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400.png\" alt=\"\" width=\"653\" height=\"374\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400.png 653w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400-300x172.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400-225x129.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-400-350x200.png 350w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/strong><\/p>\n<div>\n<p style=\"text-align: justify\">Now we dequeue 6, and output it. Then we remove arcs (edges) from 6 and need to adjust in-degree of its neighbours (2,3) (Figure 37.16 (d)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-595 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401.png\" alt=\"\" width=\"658\" height=\"369\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401.png 658w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401-300x168.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401-225x126.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-401-350x196.png 350w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The in-degree of the nodes (2,3) are updated and the node 6 is marked in the in-degree table. Now since the in-degree of nodes 1, 4 and 3 after updation is 0 that is they all can be start points. While nodes 1,4 have already been placed in the priority queue\u00a0<span style=\"font-size: 1em;text-align: initial\">now we enqueue 3., the new start node. Note that the in-degree of node 2 after updation is not zero and hence cannot be a start node at this stage and hence is not enqueued (Figure 37.16 (e)).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-596 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402.png\" alt=\"\" width=\"646\" height=\"342\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402.png 646w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-402-350x185.png 350w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 1, and output it. Then we remove arcs (edges) from 1and need to adjust in-degree of its neighbours (2) (Figure 37.16 (f)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-597 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403.png\" alt=\"\" width=\"649\" height=\"337\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403.png 649w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-403-350x182.png 350w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The in-degree of the node 2 is updated and the node 1 is marked in the in-degree table. Now since the in-degree of node 2 after updation is 0 that is it can be a start point.\u00a0<span style=\"font-size: 1em;text-align: initial\">While nodes 1,4,3 have already been placed in the priority queue now we enqueue the new start point 2. (Figure 37.16 (g).<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-598 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404.png\" alt=\"\" width=\"654\" height=\"372\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404.png 654w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404-300x171.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-404-350x199.png 350w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 4, and output it. Then we remove arcs (edges) from 4 and need to adjust in-degree of its neighbours (5) (Figure 37.16 (h)).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-599 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405.png\" alt=\"\" width=\"641\" height=\"354\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405.png 641w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405-300x166.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-405-350x193.png 350w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">The in-degree of the node 5 is updated and the node 4 is marked in the in-degree table. Now due to this updation node 5\u2019s in-degree becomes 1 only and no new node\u2019s in-degree becomes 0, that is no new start point is formed. Hence the queue remains unchanged. (Figure 37.16 (i)).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-600 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406.png\" alt=\"\" width=\"640\" height=\"370\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406.png 640w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406-300x173.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406-225x130.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-406-350x202.png 350w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 3, and output it. Then we remove arcs (edges) from 3 and need to adjust in-degree of its neighbours (8) (Figure 37.16 (j)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-601 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407.png\" alt=\"\" width=\"641\" height=\"336\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407.png 641w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407-300x157.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407-225x118.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-407-350x183.png 350w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">The in-degree of the node 8 is updated and the node 3 is marked in the in-degree table. Now due to this updation node 8\u2019s in-degree becomes 1 only and no new node\u2019s in-degree becomes 0, that is no new start point is formed. Hence the queue remains unchanged. (Figure 37.16 (k)).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-602 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408.png\" alt=\"\" width=\"652\" height=\"348\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408.png 652w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408-300x160.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408-225x120.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-408-350x187.png 350w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 2, and output it. Then we remove arcs (edges) from 2 and need to adjust in-degree of its neighbours (7,5) (Figure 37.16 (l)). Note that after this dequeueing the queue is empty.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-603 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409.png\" alt=\"\" width=\"652\" height=\"340\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409.png 652w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-409-350x183.png 350w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">The in-degree of the nodes (5,7) are updated and the node 2 is marked in the in-degree table. Now since the in-degree of nodes 5 &amp; 7 after updation is 0 that is they all can be start points. Now we enqueue 5,7, the new start nodes. (Figure 37.16 (m)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-604 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410.png\" alt=\"\" width=\"634\" height=\"331\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410.png 634w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410-300x157.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-410-350x183.png 350w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 5, and output it. Then we remove arcs (edges) from 5 and need to adjust in-degree of its neighbours (9) (Figure 37.16 (n).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-605 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411.png\" alt=\"\" width=\"661\" height=\"345\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411.png 661w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411-300x157.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-411-350x183.png 350w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The in-degree of the node 9 is updated and the node 5 is marked in the in-degree table.Now since the in-degree of nodes\u00a0 9 after updation is not 0, no new start point is found.(Figure 37.16 (o)).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-606 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412.png\" alt=\"\" width=\"672\" height=\"338\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412.png 672w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412-300x151.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-412-350x176.png 350w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 7, and output it. Then we remove arcs (edges) from 7 and need to adjust in-degree of its neighbours (8) (Figure 37.16 (p)). Note that after this dequeueing the queue is empty.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-607 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413.png\" alt=\"\" width=\"649\" height=\"329\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413.png 649w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413-300x152.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413-225x114.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-413-350x177.png 350w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">The in-degree of the node 8 is updated and the node 7 is marked in the in-degree table. Now since the in-degree of nodes 8 after updation is 0, we enqueue it. (Figure 37.16 (q)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-608 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414.png\" alt=\"\" width=\"643\" height=\"323\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414.png 643w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414-300x151.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-414-350x176.png 350w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we dequeue 8, and output it. Then we remove arcs (edges) from 8 and need to adjust in-degree of its neighbour (9) (Figure 37.16 (r)). Note that after this dequeueing the queue is empty.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-609 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415.png\" alt=\"\" width=\"647\" height=\"385\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415.png 647w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-415-350x208.png 350w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">The in-degree of the node 9 is updated and the node 8 is marked in the in-degree table. Now since the in-degree of node 9 after updation is 0, we enqueue it. Now we dequeue 9 and output it. Since it has no neighbours process stops (Figure 37.16 (s)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-610 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416.png\" alt=\"\" width=\"538\" height=\"597\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416.png 538w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416-270x300.png 270w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416-65x72.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416-225x250.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-416-350x388.png 350w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The final topological sorting of the graph is shown in Figure 37.16 (t). Now we can check whether the topological ordering is correct.<strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><\/p>\n<\/div>\n<p>We start with 0 \u2013 (no dependency since in-degree is 0.<\/p>\n<p>Next is 6 \u2013 in-degree is 1 and dependent on 0 which has already been processed<\/p>\n<p>Next is 1\u2013 in-degree is 1 and dependent on 0 which has already been processed<\/p>\n<p>Next is 4 \u2013 in-degree is 1 and dependent on 0 which has already been processed<\/p>\n<p>Next is 3\u00a0 \u2013 in-degree is 1 and dependent on 6 which has already been processed<\/p>\n<p>Next is 2 \u2013 in-degree is 2 and dependent on 1&amp; 6 both of which has already been processed<\/p>\n<p style=\"text-align: justify\">Next is 5- in-degree is 2 and dependent on 2 &amp;4 which has already been processed<\/p>\n<p style=\"text-align: justify\">Next is 7\u2013 in-degree is 1 and dependent on 2 which has already been processed<\/p>\n<p>Next is 8 \u2013 in-degree is 2 and dependent on 3 &amp; 7 both of which has already been processed<\/p>\n<p>Next is 9\u2013 in-degree is 2 and dependent on 8 &amp; 5 both of which has already been processed<\/p>\n<p>Hence we see that the topological ordering has been followed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained the concept of Topological Sorting<\/li>\n<li>Discussed some applications of Topological Sorting<\/li>\n<li>Illustrated Topological Sorting using a walkthrough example<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Topological sorting<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/uJtH4WperDs\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-611 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417.png\" alt=\"\" width=\"647\" height=\"553\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417.png 647w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417-300x256.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417-65x56.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417-225x192.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-417-350x299.png 350w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/p>\n","protected":false},"author":3,"menu_order":37,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-574","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/574\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/574\/revisions\/989"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/574\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=574"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=574"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=574"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}