Kahn’s Algorithm Topological Sorting- Find More About It

Topological sorting is a Software engineering activity which involves the presenting of vertices UV in a straight line, thus, for every directed edge, vertex U is put before V while organizing. It is only used for Directed Acyclic Graphs (DAG), that is, graphs that have vertices and edges which depict the flow of activities from one to the other. So what is Kahn’s Algorithm and how is it used in Topological Sorting? Let’s dive into the nitty-gritty of it.

Kahn’s Algorithm Topological Sorting

Kahn’s Algorithm Topological Sorting

Kahn’s Algorithm is a kind of algorithm used for Topological Sorting. A DAG has at least one vertex with 0 in-degree and one with 0 out-degree. The Kahn Algorithm functions by keeping account of the number of incoming edges (in-degree), storing, and deleting edges originating after being stored.

Starbucks Rewards Hacks – Know Mo...
Starbucks Rewards Hacks – Know More

The Underlying Concept of Kahn’s Algorithm

It has been noted that a Directed Acyclic Graphs (DAG) has at least one vertex with in-degree 0 and one vertex with out-degree 0.

Here is how that is proven:

For example, using a directed acyclic graph G. It can be said that G does not contain a cycle because it is acyclic. That it is acyclic implies that no endless loop can be found as a path in the graph. Essentially, this means that all paths in G are of finite length. 

Let A be the longest path on the DAG G. The deductions therein include:

  • A starts from a source U and ends at a destination V. That is, a beginning and an end exist.
  • Having A as the longest path means to the source U, that there is no incoming edge and from the destination V, that no outgoing edge exists. Therefore, A is the longest path only if the in-degree of source U= 0 and the out-degree of destination V= 0.
  • The longest path A will only exist because DAG has only finite paths. This shows that: A DAG will always have at least one vertex with zero in-degree and one with zero out-degree.

3 Basic Functions of Kahn’s Algorithm Topological Sorting

  1. To find: Kahn’s Algorithm finds nodes with zero in-degree, that is, no incoming edge. 
  2. To store: It stores these zero in-degree nodes in a stack and removes them from the main graph.
  3. To delete: It checks and deletes or removes edges springing up from the stored nodes. 

How Does the Kahn Algorithm Work?

Kahn’s Algorithm process seeks to ensure that no vertices or nodes are left to be removed, and a topological ordering of the fixed or directed acyclic graph is obtained. 

The process of understanding how Kahn’s Algorithm works are seen in 9 steps. These steps are highlighted below: 

  1. For all nodes, look out for the indegree.
  2. Take notice of a node which does not have incoming edges.
  3. From the graph, take out that node without incoming edges and add it to the ordering.
  4. The next step is to take the edges of the outgoing node out from the graph. 
  5. From the areas where the attached edges were deleted, decrease the indegree.
  6. Consider the steps from 2 to 5 until you have no nodes left with zero in-degree.
  7. After that, check to see that all aspects of the graph are properly represented and in the right order.
  8. You have the right order if the result of Step 7 is in line or accurate. If so, then no topological ordering exists.
  9. Then you can exit.

Calculation of In-degree

Two methods can be used in the calculation of In-degree. These methods are explained below: 

Method 1

Using an in-degree array to track indegree values, you can find the in-degree of all nodes. For this, the first thing to do is to set up all values in the in-degree array to 0 and then check the edges and increase (increment) the destination vertex or node V of each edge by 1 in the in-degree counter.

The Pseudo Code

For each node in all Node, in-degree[node] = 0 (This loop is for all nodes)

For each edge (source, destination) in Edges, indegree[dest]++ (This loop is for all edges)

Summary: With V as vertices and E as edges, the total complexity of method 1 is O(V+E).

Method 2

In the second method, you can go over each vertex or node on the list with the view that it is the source vertex or node. Doing this, any node attached to it at the edge as a destination vertex or node gets its in-degree incremented by 1.

Pseudo code 

For every vertex or node in allNodes, 

{for each destinationNode in destinationNodesList[node]

indegree[destinationNode]++;}

Note: The inner loop (that is the ones in the bracket) is done E number of times. E here equals the total number of Edges.

While for the outer loop, it would be done V number of times, V here equals the total number of vertices or nodes.

Summary: This shows that the total time complexity for this method 2 will also be O(V+E).

Time Complexity of the Kahn’s Algorithm

This includes factors to consider when calculating the time complexity for Khan’s Algorithm explained above. They are 2 basic factors to be considered and they include: 

  1. In-degree Calculation: This involves the resetting of indegrees to 0 that is done V number of times. The inner loop will be done for every edge, that is E number of times.
  2. Algorithm: This involves adding each vertex to the queue just once (V number of times totally). As it is dequeued, the degree is brought down for all its edges (E times cumulatively).

Uses or Application of Kahn’s Algorithm Topological Sorting

  1. If you are a software developer one into coding, you can use Kahn’s Algorithm to solve complex questions or problems with more ease.
  2. It can be used to determine the right order of executing lots of tasks being put together.
  3. It can be used to detect ineffective operating systems. To know if cycles are present in a graph.
  4. It can be used to schedule jobs, given the dependency some jobs have on others.
  5. You can use it to know what is required of certain jobs or tasks.
  6. Used in educational systems, especially higher institutions to plan out courses.
  7. It is used to sort out symbol reliance in linkers and position or put together time-build dependencies.
  8. If you need to organize your data according to ranks or serially, then Khan’s Algorithm is used.

Conclusion

Asides from Kahn’s Algorithm, there are other algorithms that can be used for Topological Sorting, like the Depth-first search and some parallel algorithms. However, we have thoroughly discussed Kahn’s Algorithm process and have seen how it makes data sorting and arranging less confusing and overwhelming.

Companies, educational sectors and software engineers can take advantage of this algorithm to increase productivity for a task or job.

Kahn’s Algorithm Topological Sorting- Find More About It

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top