{"id":613,"date":"2018-07-19T11:42:06","date_gmt":"2018-07-19T11:42:06","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=613"},"modified":"2018-12-21T10:46:39","modified_gmt":"2018-12-21T10:46:39","slug":"topological-sorting-as-an-application-of-dfs","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/topological-sorting-as-an-application-of-dfs\/","title":{"rendered":"Topological Sorting as an Application of DFS"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/WA52Sg-Gah0\" 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 as an Application of DFS<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 To understand the concept of Topological Ordering as an Application of DFS\r\n\r\n\u2022\u00a0 To discuss the complexity of Topological Sorting\r\n\r\n\u2022\u00a0 To discuss the Proof of Correctness\r\n\r\n&nbsp;\r\n\r\n<strong>38.1 Topological Sort<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appears in the sequence before its predecessor. A directed graph G is a dag (directed acyclic graph) if it does not have any cycle. A <strong><em>directed acyclic graph<\/em><\/strong> or <strong><em>dag<\/em><\/strong> is a directed graph with no directed cycles. Any vertex in a dag that has no incoming vertices is called a <strong><em>source<\/em><\/strong>; any vertex with no outgoing edges is called a <strong><em>sink<\/em><\/strong>. Every dag has at least one source and one sink, but may have more than one of each.<\/p>\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. <strong>Topological sort<\/strong>: An ordering of the tasks that conforms with the given dependencies<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Examples:\u00a0 Scheduling<\/strong>:\u00a0 When\u00a0 scheduling<strong>\u00a0 <\/strong><em>task\u00a0 graphs<\/em><strong>\u00a0 <\/strong>in\u00a0 distributed\u00a0 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 example is during compilation to order modules\/libraries.<\/p>\r\n&nbsp;\r\n\r\n<strong>The 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 we 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. A directed acyclic graph (DAG) is a digraph that has no directed cycles. A topological ordering of a digraph is a numbering<\/p>\r\n<strong><em>v<\/em><\/strong>1<strong><em> , \u2026, v<\/em><\/strong><strong><em>n\u00a0<\/em><\/strong>\r\n\r\nof the vertices such that for every edge (<strong><em>v<\/em><\/strong><strong><em>i<\/em><\/strong> <strong><em>, v<\/em><\/strong><strong><em>j<\/em><\/strong>), we have <strong><em>i<\/em><\/strong> &lt; <strong><em>j<\/em><\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">In order for the problem to be solvable, there can\u2019t be a cyclic set of constraints. In the example of a directed acyclic graph given in Figure 38.1 the vertices A and B have to be processed before vertex C can be processed. Similarly vertex B and C have to be processed before vertex D can be processed and D has to be processed before vertex E can be processed. Therefore one topological sorting of DAG is A,B,C,D and E shown here as numbered vertices (Figure .38.1).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-616 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418.png\" alt=\"\" width=\"677\" height=\"561\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Basically topological sort can be viewed as a modified version of DFS where we run a DFS but add the fact that at the end of the recursive function, add the node the DFS was called with to the end of the topological sort.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us argue that a directed graph G is acyclic iff a DFS of G yields no back edges which is implied by the fact that a back edge implies a cycle. The contrapositive is that if G has a cycle \u00de that is there exists a back edge. Now let v be the vertex on the cycle first discovered, and <em>u<\/em> be the predecessor of <em>v<\/em> on the cycle. When <em>v<\/em> is discovered, whole cycle is visited since all vertices reachable from <em>v<\/em> must be visited before returning from DFS-Visit(). Therefore the path from u\u00aev is a back edge.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us have the small discussion on DFS and DAGs. Argue that a directed graph G is acyclic iff a DFS of G yields no back edges and Forward: if G is acyclic, will be no\u00a0<span style=\"font-size: 1em;text-align: initial\">back edges. Of course a back edge implies a cycle and backward: if no back edges, G is acyclic. In other words if G has a cycle \u00de $ a back edge<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>38.2 Depth First Search Algorithm and Edge Classification<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module we will be discussing about Topological sorting, but as an application of Depth First Search(DFS). In digraphs we traverse edges only along their direction. In the directed DFS algorithm, we can distinguish four types of edges as discovery edges, back edges, forward edges and cross edges (Figure 38.3). A directed DFS starting a vertex <strong><em>s<\/em><\/strong> determines the vertices reachable from <strong><em>s.<\/em><\/strong><\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-617 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-419.png\" alt=\"\" width=\"348\" height=\"269\" \/>\r\n\r\n&nbsp;\r\n\r\nEdge (<strong>u<\/strong>,<strong>v<\/strong>) of <strong>G<\/strong> is classified as a:\r\n\r\n&nbsp;\r\n\r\n(1)\u00a0 <strong>Tree <\/strong>edge iff<strong> u <\/strong>discovers<strong> v <\/strong>during the DFS:<strong> Predecessor [v<\/strong>] =<strong> u <\/strong>If (<strong>u<\/strong>,<strong>v<\/strong>) is NOT a tree edge then it is a:\r\n\r\n(2) <strong>Forward <\/strong>edge iff<strong> u <\/strong>is an ancestor of<strong> v <\/strong>in the DFS tree\r\n\r\n(3) <strong>Back <\/strong>edge iff<strong> u <\/strong>is a descendant of<strong> v <\/strong>in the DFS tree\r\n\r\n(4) <strong>Cross <\/strong>edge iff<strong> u <\/strong>is neither an ancestor nor a descendant of<strong> v<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the example given in Figure 38.4, the tree edges in green correspond to the sequence of nodes in the DFS path, forward edges are in purple and correspond to descendents in the DFS path that are not tree edges. Back edges are in red and correspond to backward ancestors in the DFS path. All other edges that do not form part of the DFS path are called cross edges and are given in orange. Note that the edge classification depends on the particular path of the DFS tree.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-618 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420.png\" alt=\"\" width=\"609\" height=\"205\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>38.3 Topological Sorting Algorithm using DFS<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>G <\/strong>is called a Directed Acyclic Graph, or just a<strong> DAG,<\/strong>\r\n\r\n&nbsp;\r\n\r\nTOPOLOGICAL-SORT(G):\r\n\r\n&nbsp;\r\n\r\n1) call DFS(G) to compute finishing times f[v] for each vertex v\r\n\r\n2) as each vertex is finished, insert it onto the front of a linked list\r\n\r\n3) return the linked list of vertices\r\n\r\n&nbsp;\r\n\r\nNote that the result is just a list of vertices in order of decreasing finish times f[].\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can use a DFS in our algorithm to determine a topological sort. The idea is: When you do a DFS on a directed acyclic graph, eventually you will reach a node with no outgoing edges. This is because if this never happened, you hit a cycle, because the number of nodes is finite. This node that you reach in a DFS is \u201csafe\u201d to place at the end of the topological sort. Think of the getting ready example. Now what we find is that if we have added each of the vertices \u201cbelow\u201d a vertex into our topological sort, it is safe then to add this one in. If we added in leaving for work at the end, then we can surely add taking a shower.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">First call DFS(G) to compute <strong>finishing<\/strong> times <strong>f<\/strong>[<strong>v<\/strong>] for each vertex <strong>v and then as<\/strong> each vertex is finished, insert it onto the <strong>front<\/strong> of a linked list and return the linked list of vertices. We have to note that the result is just a list of vertices in order of <strong>decreasing <\/strong>finish times<strong> f<\/strong>[]. Now let us look at how we classify the edge using DFS, Consider an edge E(u,v) of G which has to be classified as,<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Tree <\/strong>edge iff<strong> u <\/strong>discovers<strong> v <\/strong>during the DFS:<strong> P<\/strong>[<strong>v<\/strong>] =<strong> u. <\/strong>If (<strong>u<\/strong>,<strong>v<\/strong>) is NOT a tree edge then it is a: <strong>Forward<\/strong> edge iff <strong>u<\/strong> is an ancestor of <strong>v<\/strong> in the DFS tree, <strong>Back<\/strong> edge iff <strong>u<\/strong> is a descendant of <strong>v<\/strong> in the DFS tree and <strong>Cross<\/strong> edge iff <strong>u<\/strong> is neither an ancestor nor a descendant of <strong>v.<\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong>38.3.2 Time stamp of a Vertex<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Each vertex v has two time-stamps namely d[v] records when v is first discovered and f[v] records when the search finishes examining its adjacency list. For every vertex u d[u] &lt; f[u] .<\/p>\r\n&nbsp;\r\n\r\n<strong>38.4 Walkthrough of the Topological Algorithm using DFS<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The example shown in Figure 38.5 (a) has been taken from ww.cs.utoronto.ca\/~tabrown\/csc263\/2014W\/week9.ppt<\/p>\r\n&nbsp;\r\n\r\n<strong>38.4.1 Time stamp of a Vertex<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Each vertex v has two time-stamps namely d[v] records when v is first discovered and f[v] records when the search finishes examining its adjacency list. For every vertex u d[u] &lt; f[u] .<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We first initialize the time stamps of d and v values of every node of the graph to \u00a5. Call DFS(G) to compute the finishing times f[v]. Let\u2019s say we start the DFS from the\u00a0<span style=\"font-size: 1em;text-align: initial\">vertex c. At this point the d[c] is set = 1, the time stamp at which node c is first discovered as shown in Figure 38.5 (a).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-619 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421.png\" alt=\"\" width=\"444\" height=\"346\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As each vertex is finished, we insert it onto the front of a linked list. We follow the path from starting vertex c, next we discover vertx d at time stamp 2 i.e d[d]=2, then at next time stamp discover vertexf so d[f]=3 and <strong>f<\/strong> is done hence f[f]=4,since f is finished, and is output. We then move back to d (Figure 38.5 (b)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-620 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422.png\" alt=\"\" width=\"423\" height=\"382\" \/>\r\n\r\nAt timestamp 5 d is done hence f[d] =5 and this is output (Figure 38.5 (c)).\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-621 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423.png\" alt=\"\" width=\"421\" height=\"383\" \/>\r\n<div>\r\n\r\nWe then move back to c (Figure 38.5 (d)).\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-622 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424.png\" alt=\"\" width=\"423\" height=\"382\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Next that is at time stamp 6 we discover the vertex e so d[e] =6. Now <strong>e<\/strong> is done since both edges e are back edges. Hence at timestamp 7 e is output and we move back to <strong>c\u00a0<\/strong>(Figure 38.5 (e)).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-623 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425.png\" alt=\"\" width=\"441\" height=\"394\" \/>\r\n<div>\r\n<p style=\"text-align: justify\"><strong>Now c <\/strong>is done as well and it is output. Note that if there was (<strong>c<\/strong>,<strong>f<\/strong>) edge in the graph, it would be classified as a <strong>forward edge<\/strong> (in this particular DFS run) (Figure 38.5 (f)).<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-624 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426.png\" alt=\"\" width=\"430\" height=\"395\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The f[c] = 8. Let\u2019s now call DFS visit from the vertex a at time stamp 9 and hence d[a]=9. At this time stamp we discover the vertex c, but c was already processed =&gt; (a,c) is a cross edge. Next at time stamp 10 we discover vertex b (Figure 35.8 (g)).<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-625 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427.png\" alt=\"\" width=\"448\" height=\"392\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we set d[b] = 10 and at time stamp 11 we find b is done as (b,d) is a cross edge and hence f[b] = 11 and we output it (Figure 35.8 (h)). Now we go back to c.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-626 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428.png\" alt=\"\" width=\"420\" height=\"382\" \/>\r\n\r\n&nbsp;\r\n\r\nAt time stamp 12 we find a also is done (Figure 38.5 (i)).\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-627 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429.png\" alt=\"\" width=\"452\" height=\"392\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We now set f[a] = 12 and output it. <strong>WE HAVE THE RESULT! -<\/strong> return the linked list of vertices (Figure 38.5 (j)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-628 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430.png\" alt=\"\" width=\"441\" height=\"401\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The linked list is sorted in decreasing order of finishing times f[] (Figure 38.5 (k)). This is true for any different vertex order for DFS visit. Please note that if we redraw the graph so that all vertices are in a line ordered by a valid topological sort, then all edges point from left to right.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-629 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431.png\" alt=\"\" width=\"420\" height=\"374\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>38.5 Time Complexity<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Running time of topological sort is \u0398(n + m) where n=|V| and m=|E|. This is because Depth first search takes \u0398(n + m) time in the worst case, and inserting into the front of a linked list takes \u0398(1) time<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We never visited a vertex more than one time . For each vertex, we had to examine all outgoing edges that is <em>\u03a3<\/em> <em>outdegree(v) = m. This is<\/em> summed over all vertices, not per vertex so, our running time is exactly O(n + m).<\/p>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Topological Sorting as an Application of DFS<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/WA52Sg-Gah0\" 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<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Discussed the concept of Topological Ordering as an Application of DFS<\/li>\r\n \t<li>Explained the complexity of Topological Sorting<\/li>\r\n \t<li>Discussed the Proof of Correctness<\/li>\r\n<\/ul>\r\n<img class=\"size-full wp-image-630 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432.png\" alt=\"\" width=\"582\" height=\"549\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/WA52Sg-Gah0\" 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 as an Application of DFS<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 To understand the concept of Topological Ordering as an Application of DFS<\/p>\n<p>\u2022\u00a0 To discuss the complexity of Topological Sorting<\/p>\n<p>\u2022\u00a0 To discuss the Proof of Correctness<\/p>\n<p>&nbsp;<\/p>\n<p><strong>38.1 Topological Sort<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appears in the sequence before its predecessor. A directed graph G is a dag (directed acyclic graph) if it does not have any cycle. A <strong><em>directed acyclic graph<\/em><\/strong> or <strong><em>dag<\/em><\/strong> is a directed graph with no directed cycles. Any vertex in a dag that has no incoming vertices is called a <strong><em>source<\/em><\/strong>; any vertex with no outgoing edges is called a <strong><em>sink<\/em><\/strong>. Every dag has at least one source and one sink, but may have more than one of each.<\/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. <strong>Topological sort<\/strong>: An ordering of the tasks that conforms with the given dependencies<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Examples:\u00a0 Scheduling<\/strong>:\u00a0 When\u00a0 scheduling<strong>\u00a0 <\/strong><em>task\u00a0 graphs<\/em><strong>\u00a0 <\/strong>in\u00a0 distributed\u00a0 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 example is during compilation to order modules\/libraries.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The 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 we 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. A directed acyclic graph (DAG) is a digraph that has no directed cycles. A topological ordering of a digraph is a numbering<\/p>\n<p><strong><em>v<\/em><\/strong>1<strong><em> , \u2026, v<\/em><\/strong><strong><em>n\u00a0<\/em><\/strong><\/p>\n<p>of the vertices such that for every edge (<strong><em>v<\/em><\/strong><strong><em>i<\/em><\/strong> <strong><em>, v<\/em><\/strong><strong><em>j<\/em><\/strong>), we have <strong><em>i<\/em><\/strong> &lt; <strong><em>j<\/em><\/strong><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">In order for the problem to be solvable, there can\u2019t be a cyclic set of constraints. In the example of a directed acyclic graph given in Figure 38.1 the vertices A and B have to be processed before vertex C can be processed. Similarly vertex B and C have to be processed before vertex D can be processed and D has to be processed before vertex E can be processed. Therefore one topological sorting of DAG is A,B,C,D and E shown here as numbered vertices (Figure .38.1).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-616 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418.png\" alt=\"\" width=\"677\" height=\"561\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418.png 677w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418-300x249.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418-65x54.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418-225x186.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-418-350x290.png 350w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Basically topological sort can be viewed as a modified version of DFS where we run a DFS but add the fact that at the end of the recursive function, add the node the DFS was called with to the end of the topological sort.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us argue that a directed graph G is acyclic iff a DFS of G yields no back edges which is implied by the fact that a back edge implies a cycle. The contrapositive is that if G has a cycle \u00de that is there exists a back edge. Now let v be the vertex on the cycle first discovered, and <em>u<\/em> be the predecessor of <em>v<\/em> on the cycle. When <em>v<\/em> is discovered, whole cycle is visited since all vertices reachable from <em>v<\/em> must be visited before returning from DFS-Visit(). Therefore the path from u\u00aev is a back edge.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us have the small discussion on DFS and DAGs. Argue that a directed graph G is acyclic iff a DFS of G yields no back edges and Forward: if G is acyclic, will be no\u00a0<span style=\"font-size: 1em;text-align: initial\">back edges. Of course a back edge implies a cycle and backward: if no back edges, G is acyclic. In other words if G has a cycle \u00de $ a back edge<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>38.2 Depth First Search Algorithm and Edge Classification<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module we will be discussing about Topological sorting, but as an application of Depth First Search(DFS). In digraphs we traverse edges only along their direction. In the directed DFS algorithm, we can distinguish four types of edges as discovery edges, back edges, forward edges and cross edges (Figure 38.3). A directed DFS starting a vertex <strong><em>s<\/em><\/strong> determines the vertices reachable from <strong><em>s.<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-617 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-419.png\" alt=\"\" width=\"348\" height=\"269\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-419.png 348w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-419-300x232.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-419-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-419-225x174.png 225w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Edge (<strong>u<\/strong>,<strong>v<\/strong>) of <strong>G<\/strong> is classified as a:<\/p>\n<p>&nbsp;<\/p>\n<p>(1)\u00a0 <strong>Tree <\/strong>edge iff<strong> u <\/strong>discovers<strong> v <\/strong>during the DFS:<strong> Predecessor [v<\/strong>] =<strong> u <\/strong>If (<strong>u<\/strong>,<strong>v<\/strong>) is NOT a tree edge then it is a:<\/p>\n<p>(2) <strong>Forward <\/strong>edge iff<strong> u <\/strong>is an ancestor of<strong> v <\/strong>in the DFS tree<\/p>\n<p>(3) <strong>Back <\/strong>edge iff<strong> u <\/strong>is a descendant of<strong> v <\/strong>in the DFS tree<\/p>\n<p>(4) <strong>Cross <\/strong>edge iff<strong> u <\/strong>is neither an ancestor nor a descendant of<strong> v<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the example given in Figure 38.4, the tree edges in green correspond to the sequence of nodes in the DFS path, forward edges are in purple and correspond to descendents in the DFS path that are not tree edges. Back edges are in red and correspond to backward ancestors in the DFS path. All other edges that do not form part of the DFS path are called cross edges and are given in orange. Note that the edge classification depends on the particular path of the DFS tree.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-618 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420.png\" alt=\"\" width=\"609\" height=\"205\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420.png 609w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420-300x101.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420-225x76.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-420-350x118.png 350w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>38.3 Topological Sorting Algorithm using DFS<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>G <\/strong>is called a Directed Acyclic Graph, or just a<strong> DAG,<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>TOPOLOGICAL-SORT(G):<\/p>\n<p>&nbsp;<\/p>\n<p>1) call DFS(G) to compute finishing times f[v] for each vertex v<\/p>\n<p>2) as each vertex is finished, insert it onto the front of a linked list<\/p>\n<p>3) return the linked list of vertices<\/p>\n<p>&nbsp;<\/p>\n<p>Note that the result is just a list of vertices in order of decreasing finish times f[].<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can use a DFS in our algorithm to determine a topological sort. The idea is: When you do a DFS on a directed acyclic graph, eventually you will reach a node with no outgoing edges. This is because if this never happened, you hit a cycle, because the number of nodes is finite. This node that you reach in a DFS is \u201csafe\u201d to place at the end of the topological sort. Think of the getting ready example. Now what we find is that if we have added each of the vertices \u201cbelow\u201d a vertex into our topological sort, it is safe then to add this one in. If we added in leaving for work at the end, then we can surely add taking a shower.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">First call DFS(G) to compute <strong>finishing<\/strong> times <strong>f<\/strong>[<strong>v<\/strong>] for each vertex <strong>v and then as<\/strong> each vertex is finished, insert it onto the <strong>front<\/strong> of a linked list and return the linked list of vertices. We have to note that the result is just a list of vertices in order of <strong>decreasing <\/strong>finish times<strong> f<\/strong>[]. Now let us look at how we classify the edge using DFS, Consider an edge E(u,v) of G which has to be classified as,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Tree <\/strong>edge iff<strong> u <\/strong>discovers<strong> v <\/strong>during the DFS:<strong> P<\/strong>[<strong>v<\/strong>] =<strong> u. <\/strong>If (<strong>u<\/strong>,<strong>v<\/strong>) is NOT a tree edge then it is a: <strong>Forward<\/strong> edge iff <strong>u<\/strong> is an ancestor of <strong>v<\/strong> in the DFS tree, <strong>Back<\/strong> edge iff <strong>u<\/strong> is a descendant of <strong>v<\/strong> in the DFS tree and <strong>Cross<\/strong> edge iff <strong>u<\/strong> is neither an ancestor nor a descendant of <strong>v.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>38.3.2 Time stamp of a Vertex<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Each vertex v has two time-stamps namely d[v] records when v is first discovered and f[v] records when the search finishes examining its adjacency list. For every vertex u d[u] &lt; f[u] .<\/p>\n<p>&nbsp;<\/p>\n<p><strong>38.4 Walkthrough of the Topological Algorithm using DFS<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The example shown in Figure 38.5 (a) has been taken from ww.cs.utoronto.ca\/~tabrown\/csc263\/2014W\/week9.ppt<\/p>\n<p>&nbsp;<\/p>\n<p><strong>38.4.1 Time stamp of a Vertex<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Each vertex v has two time-stamps namely d[v] records when v is first discovered and f[v] records when the search finishes examining its adjacency list. For every vertex u d[u] &lt; f[u] .<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We first initialize the time stamps of d and v values of every node of the graph to \u00a5. Call DFS(G) to compute the finishing times f[v]. Let\u2019s say we start the DFS from the\u00a0<span style=\"font-size: 1em;text-align: initial\">vertex c. At this point the d[c] is set = 1, the time stamp at which node c is first discovered as shown in Figure 38.5 (a).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-619 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421.png\" alt=\"\" width=\"444\" height=\"346\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421.png 444w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421-300x234.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421-225x175.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-421-350x273.png 350w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As each vertex is finished, we insert it onto the front of a linked list. We follow the path from starting vertex c, next we discover vertx d at time stamp 2 i.e d[d]=2, then at next time stamp discover vertexf so d[f]=3 and <strong>f<\/strong> is done hence f[f]=4,since f is finished, and is output. We then move back to d (Figure 38.5 (b)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-620 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422.png\" alt=\"\" width=\"423\" height=\"382\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422.png 423w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422-300x271.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422-225x203.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-422-350x316.png 350w\" sizes=\"auto, (max-width: 423px) 100vw, 423px\" \/><\/p>\n<p>At timestamp 5 d is done hence f[d] =5 and this is output (Figure 38.5 (c)).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-621 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423.png\" alt=\"\" width=\"421\" height=\"383\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423.png 421w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423-300x273.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423-225x205.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-423-350x318.png 350w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/p>\n<div>\n<p>We then move back to c (Figure 38.5 (d)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-622 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424.png\" alt=\"\" width=\"423\" height=\"382\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424.png 423w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424-300x271.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424-225x203.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-424-350x316.png 350w\" sizes=\"auto, (max-width: 423px) 100vw, 423px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Next that is at time stamp 6 we discover the vertex e so d[e] =6. Now <strong>e<\/strong> is done since both edges e are back edges. Hence at timestamp 7 e is output and we move back to <strong>c\u00a0<\/strong>(Figure 38.5 (e)).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-623 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425.png\" alt=\"\" width=\"441\" height=\"394\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425.png 441w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425-300x268.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425-225x201.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-425-350x313.png 350w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\"><strong>Now c <\/strong>is done as well and it is output. Note that if there was (<strong>c<\/strong>,<strong>f<\/strong>) edge in the graph, it would be classified as a <strong>forward edge<\/strong> (in this particular DFS run) (Figure 38.5 (f)).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-624 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426.png\" alt=\"\" width=\"430\" height=\"395\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426.png 430w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426-300x276.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426-65x60.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426-225x207.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-426-350x322.png 350w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The f[c] = 8. Let\u2019s now call DFS visit from the vertex a at time stamp 9 and hence d[a]=9. At this time stamp we discover the vertex c, but c was already processed =&gt; (a,c) is a cross edge. Next at time stamp 10 we discover vertex b (Figure 35.8 (g)).<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-625 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427.png\" alt=\"\" width=\"448\" height=\"392\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427.png 448w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427-300x263.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427-65x57.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427-225x197.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-427-350x306.png 350w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we set d[b] = 10 and at time stamp 11 we find b is done as (b,d) is a cross edge and hence f[b] = 11 and we output it (Figure 35.8 (h)). Now we go back to c.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-626 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428.png\" alt=\"\" width=\"420\" height=\"382\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428.png 420w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428-300x273.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428-225x205.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-428-350x318.png 350w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>At time stamp 12 we find a also is done (Figure 38.5 (i)).<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-627 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429.png\" alt=\"\" width=\"452\" height=\"392\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429.png 452w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429-300x260.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429-65x56.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429-225x195.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-429-350x304.png 350w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We now set f[a] = 12 and output it. <strong>WE HAVE THE RESULT! &#8211;<\/strong> return the linked list of vertices (Figure 38.5 (j)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-628 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430.png\" alt=\"\" width=\"441\" height=\"401\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430.png 441w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430-300x273.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430-225x205.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-430-350x318.png 350w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The linked list is sorted in decreasing order of finishing times f[] (Figure 38.5 (k)). This is true for any different vertex order for DFS visit. Please note that if we redraw the graph so that all vertices are in a line ordered by a valid topological sort, then all edges point from left to right.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-629 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431.png\" alt=\"\" width=\"420\" height=\"374\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431.png 420w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431-300x267.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431-225x200.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-431-350x312.png 350w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>38.5 Time Complexity<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Running time of topological sort is \u0398(n + m) where n=|V| and m=|E|. This is because Depth first search takes \u0398(n + m) time in the worst case, and inserting into the front of a linked list takes \u0398(1) time<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We never visited a vertex more than one time . For each vertex, we had to examine all outgoing edges that is <em>\u03a3<\/em> <em>outdegree(v) = m. This is<\/em> summed over all vertices, not per vertex so, our running time is exactly O(n + m).<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Topological Sorting as an Application of DFS<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/WA52Sg-Gah0\" 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><strong>Summary<\/strong><\/p>\n<ul>\n<li>Discussed the concept of Topological Ordering as an Application of DFS<\/li>\n<li>Explained the complexity of Topological Sorting<\/li>\n<li>Discussed the Proof of Correctness<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-630 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432.png\" alt=\"\" width=\"582\" height=\"549\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432.png 582w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432-300x283.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432-65x61.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432-225x212.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-432-350x330.png 350w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/p>\n","protected":false},"author":3,"menu_order":38,"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-613","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\/613","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\/613\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/613\/revisions\/992"}],"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\/613\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=613"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=613"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}