Kahn's Algorithm analysis [00PF]

How long does Kahn's Algorithm take?

  • Creating T, Z, and \mathit {in} each take \Theta (1).
  • Initializing \mathit {in} (the first \mathsf {for} loop) takes \Theta (V+E) since we loop over all vertices and then look at every edge once.
  • Initializing Z (the second \mathsf {for} loop) takes \Theta (V), assuming we can add to Z in \Theta (1) time.
  • The main \mathsf {while} loop will execute \Theta (V) times; in each loop we do some \Theta (1) operations (remove from Z, append to T), and do \Theta (1) work for each of several edges. Again, we will look at each edge once in total, so this main loop takes \Theta (V+E) time.
Overall, then, this algorithm is \Theta (1) + \Theta (V+E) + \Theta (V) + \Theta (V+E) = \Theta (V+E).