CST 370 Week 5 Learning Journal Entry

Topological Order - Khan's Algorithm

This week, we learned how to topologically sort graphs using Khan's Algorithm. To be able to topologically sort a graph, we first need to determine that a graph has no cycles; in other words, it is a directed acyclic graph. If the graph has a cycle, we cannot determine a topological order. This process is similar to the depth-first search, except our topological order is reversed. The process is as follows: 

  1. Calculate the in-degree of each vertex. The in-degree is the number of incoming edges each vertex has. 
  2. Push the vertices with an in-degree of zero (no incoming edges) into a queue. 
  3. Remove the vertex from the queue, which removes all edges coming from that vertex. 
  4. Decrement of the in-degree for the neighboring vertices of the vertex that were removed from the queue. 
  5. Push any neighboring vertices with an in-degree of zero into the queue
  6. Repeat steps 3-5 for the next vertex in the queue until the queue is empty.
Below is a diagram of the first few steps of the process. It shows the beginning in-degree array for the given graph and the beginning queue, and it then shows how the in-degree array changes when a vertex is removed from the queue. 





Comments

Popular posts from this blog

CST 334 Week 1 Journal Entry

CST 311 Week 8 Journal Entry

CST 311 Week 5 Journal Entry