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:
- Calculate the in-degree of each vertex. The in-degree is the number of incoming edges each vertex has.
- Push the vertices with an in-degree of zero (no incoming edges) into a queue.
- Remove the vertex from the queue, which removes all edges coming from that vertex.
- Decrement of the in-degree for the neighboring vertices of the vertex that were removed from the queue.
- Push any neighboring vertices with an in-degree of zero into the queue
- 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
Post a Comment