Search code examples
algorithmgraphgraph-theorydijkstra

Dijkstra's algorithm vs relaxing edges in topologically sorted graph for DAG


I was reading Introduction To Algorithms 3rd Edition. There are 3 methods given to solve the problem. My inquiry is about two of them.

The one with no name

The algorithm starts by topologically sorting the dag (see Section 22.4) to impose a linear ordering on the vertices. If the dag contains a path from vertex u to vertex v, then u precedes v in the topological sort. We make just one pass over the vertices in the topologically sorted order. As we process each vertex, we relax each edge that leaves the vertex.

Dijkstra's Algorithm

This is quite well known

As far as the book shows, time complexity of without name one is O(V+E) but of Dijstra's is O(ElogV). We cannot use Dijkstra's on negative weight but we can use the other. What are the advantages of using Dijkstra's Algorithm except it can be used in cyclic ones?


Solution

  • Because the first algorithm you give only works on acyclic graphs, whereas Dijkstra runs on graph with non-negative weight. The limitations are not the same.

    In real-world, many applications can be modelled as graphs with non-negative weights, that's why Dijkstra is so used. Plus, it is very simple to implement. The complexity of Dijkstra is higher because it relies on priority queue, but this does not mean it takes necessarily more time to execute. (nlog(n) time is not that bad, because log(n) is a relatively small number: log(10^80) = 266)

    However, this stand for sparse graphs (low density of edges). For dense graphs, other algorithms may be more efficient.