{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/topologicalSort","result":{"data":{"blog":{"frontmatter":{"title":"TOPOLOGICAL SORT","thumbnail":"blog66","date":"May 24, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1kCxPwtsANxOCKjmLddIJ8wNxwZgWbFib/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Topological sort is focused on ordering of vertices in a directed\n                  acyclic graph [DAG] in which each node comes before all nodes\n                  to which it has outgoing edges.\n                </p>\n                <p>\n                  Every DAG may have one or more topological orderings.\n                  Topological sort is not possible if the graph has a cycle,\n                  since for two vertices v and w on the cycle, v precedes w and\n                  w precedes v.\n                </p>\n                <p>\n                  This algorithm falls under Divide & Conquer Technique which is\n                  divides the problem, finds the solution to smaller problem and\n                  then merges these to get the solution to the original problem.\n                  Let's look at the working of this algorithm.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Note:</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  We will be using 2D arrays(adjacency matrix) for implementing\n                  graph.\n                </li>\n                <li>We shall implement a directed graph.</li>\n                <li>\n                  Make a point that the order of traversing actually can be\n                  anything like ascending or descending or random, but will\n                  stick to traverse each node in ascending order.\n                </li>\n                <li>\n                  The output will differ because it depends on what vertex you\n                  start with and in what order you traverse them.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Inserting new vertices and edges is same as previously done in\n                  <a href=\"/blogs/graphTraversalTechniques\"\n                    >Graph Traversal Techniques</a\n                  >\n                  except that here, it is a directed graph.\n                </li>\n                <li>\n                  Let V be the number of vertices and E be the number of edges\n                  in the graph.\n                </li>\n                <li>\n                  Whenever a new edge is added from u->v, increment the indegree\n                  of v only because it's a directed graph.\n                </li>\n                <li>\n                  <strong>Topological Sort:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Initially push the vertices on to a stack whose indegree\n                      is 0.\n                    </li>\n                    <li>\n                      Loop from i=0 to V\n                      <ol class=\"pl-4\">\n                        <li>\n                          Loop until the stack is not empty i.e top != -1.\n                        </li>\n                        <li>\n                          Pop the top of stack and add it to the topological\n                          order array.\n                        </li>\n                        <li>\n                          Now decrement the indegree of the adjacent vertices of\n                          this popped vertex and if indegree of the adjacent\n                          vertex becomes zero then push it to the stack.\n                        </li>\n                      </ol>\n                    </li>\n                    <li>\n                      If the length of the topological order array is not equal\n                      to V then the graph has a cycle.\n                    </li>\n                    <li>\n                      Else we have the required topological sort in the\n                      topological order array.\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity: O(V+E)</h4>\n            </div>\n\t\t\t        <div class=\"my-2 p-2\">\n                <strong>Similar posts:</strong>\n                  <a href=\"/blogs/graph\">Graph Data Structure</a>\t|\n                  <a href=\"/blogs/graphTraversalTechniques\"\n                      >Graph Traversal Techniques</a\n                  >\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Topological sort is focused on ordering of vertices in a directed\n                  acyclic graph [DAG] in which each node comes before all nodes\n                  to which it has outgoing edges.\n                </p>\n                <p>\n                  Every DAG may have one or more topological orderings.\n                  Topological sort is not possible if the graph has a cycle,\n                  since for two vertices v and w on the cycle, v precedes w and\n                  w precedes v.\n                </p>\n                <p>\n                  This algorithm falls under Divide & Conquer Technique which is\n                  divides the problem, finds the solution to smaller problem and\n                  then merges these to get the solution to the original problem.\n                  Let's look at the working of this algorithm.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Note:</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  We will be using 2D arrays(adjacency matrix) for implementing\n                  graph.\n                </li>\n                <li>We shall implement a directed graph.</li>\n                <li>\n                  Make a point that the order of traversing actually can be\n                  anything like ascending or descending or random, but will\n                  stick to traverse each node in ascending order.\n                </li>\n                <li>\n                  The output will differ because it depends on what vertex you\n                  start with and in what order you traverse them.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Inserting new vertices and edges is same as previously done in\n                  <a href=\"/blogs/graphTraversalTechniques\"\n                    >Graph Traversal Techniques</a\n                  >\n                  except that here, it is a directed graph.\n                </li>\n                <li>\n                  Let V be the number of vertices and E be the number of edges\n                  in the graph.\n                </li>\n                <li>\n                  Whenever a new edge is added from u->v, increment the indegree\n                  of v only because it's a directed graph.\n                </li>\n                <li>\n                  <strong>Topological Sort:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Initially push the vertices on to a stack whose indegree\n                      is 0.\n                    </li>\n                    <li>\n                      Loop from i=0 to V\n                      <ol class=\"pl-4\">\n                        <li>\n                          Loop until the stack is not empty i.e top != -1.\n                        </li>\n                        <li>\n                          Pop the top of stack and add it to the topological\n                          order array.\n                        </li>\n                        <li>\n                          Now decrement the indegree of the adjacent vertices of\n                          this popped vertex and if indegree of the adjacent\n                          vertex becomes zero then push it to the stack.\n                        </li>\n                      </ol>\n                    </li>\n                    <li>\n                      If the length of the topological order array is not equal\n                      to V then the graph has a cycle.\n                    </li>\n                    <li>\n                      Else we have the required topological sort in the\n                      topological order array.\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity: O(V+E)</h4>\n            </div>\n\t\t\t        <div class=\"my-2 p-2\">\n                <strong>Similar posts:</strong>\n                  <a href=\"/blogs/graph\">Graph Data Structure</a>\t|\n                  <a href=\"/blogs/graphTraversalTechniques\"\n                      >Graph Traversal Techniques</a\n                  >\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M31%2095c-5%200-5%200-5%203l2%202c2%200%202%201%202%207%200%207%200%207%202%207%203%200%203%200%203-7s1-8%203-8%202-1%202-2c-1-3-1-3-9-2m12%202c-7%209%200%2023%208%2015%203-4%203-13%201-16-3-3-6-3-9%201m30-2c-2%201-4%206-4%2010%200%2013%2012%2012%2013-1%200-8-3-12-9-9m11%200l-1%2010v9h6c4%200%205%200%206-2v-2l2%202c2%203%206%203%209-1s3-13%200-15c-5-4-10%200-11%207%200%205%200%205-4%205h-3v-7c0-6%200-7-2-7l-2%201m40-1l-1%2012c0%207%200%208%202%208%203%200%203%200%203-3v-2l2%202c3%204%206%204%2010%200%202-2%202-2%201-4-1-1-2-1-3%201l-3%201c-2-1-1-7%200-9%202-1%202-1%202%201%200%203%205%203%205%200%202-8-9-9-13-1l-1%204v-5c0-4%200-5-2-5h-2m27%201l-4%204-4%2014c0%202%205%201%206%200%201-3%204-3%204-1s1%202%209%202c9%200%209%200%209-2%201-4%200-4-3-4s-3%200-3-7c0-6-1-6-2-6-2%200-3%200-3%208-1%2010-1%2011-2%204-2-10-3-13-7-12m46%200c-2%200-4%206-4%209v3l-3-2c-2-2-2-3%200-3s2-1%202-3c0-3-1-4-4-4-7%200-10%207-4%2011%203%202%203%204-1%204s-4%203%201%204c4%200%206-1%208-4l1-2%201%202c2%205%208%205%2010%200%205-8%200-18-7-15m30%200c-6%200-6%200-6%203%200%202%201%202%203%202s2%200%202%207c0%206%200%207%202%207l2-1%201-3v-6c0-4%200-5%202-5l2-2-1-3-7%201M56%2096l-1%209-1%209h2c3%200%203%200%203-3l3-3c6-2%207-6%205-11-1-2-8-3-11-1m57%200c-4%203-5%2013-2%2016s8%202%2010-2%200-8-4-6c-2%200-2%201-2%203v2c-2%200-1-8%201-9h1c0%201%201%202%203%202l2-2c0-5-5-7-9-4m95%201l-1%209c0%207%200%208%202%208s2%200%202-3c0-4%202-4%204%200%201%203%202%204%204%202v-8l1-6c0-3-1-4-6-4s-6%200-6%202m91%209l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%205-17-1-20l-3-2-1-1-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/6bfeb098ace172aeacb4c6f82abaf8bc/ee604/blog66.png","srcSet":"/static/6bfeb098ace172aeacb4c6f82abaf8bc/69585/blog66.png 200w,\n/static/6bfeb098ace172aeacb4c6f82abaf8bc/497c6/blog66.png 400w,\n/static/6bfeb098ace172aeacb4c6f82abaf8bc/ee604/blog66.png 800w,\n/static/6bfeb098ace172aeacb4c6f82abaf8bc/f3583/blog66.png 1200w,\n/static/6bfeb098ace172aeacb4c6f82abaf8bc/e4d72/blog66.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"topologicalSort","thumbnail":"thumbnails/blog66.png"}},"staticQueryHashes":["2987289216","63159454"]}