4

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?

  • 1
    @NursultanZarlyk. From The book: **The running time of this algorithm is easy to analyze. As shown in Section 22.4, the topological sort of line 1 takes O(V + E) time. The call of INITIALIZESINGLE-SOURCE in line 2 takes O(V) time. The for loop of lines 3–5 makes one iteration per vertex. Altogether, the for loop of lines 4–5 relaxes each edge exactly once. (We have used an aggregate analysis here.) Because each iteration of the inner for loop takes O(1) time, the total running time is O(V+E), which is linear in the size of an adjacency-list representation of the graph.** –  May 16 '16 at 14:03
  • 2
    Both right. If the graph is a dag where each predecessor is linked to all of its successors, then the first vertex will relax V-1 edges, the second will relax V-2 edges, etc until the last vertex. In this kind of graph, then E ~ V^2, so O(V^2 + E) = O(V + E) = O(V^2) – T. Claverie May 16 '16 at 14:07

1 Answers1

3

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.

T. Claverie
  • 11,380
  • 1
  • 17
  • 28
  • Can you please enumerate efficient algorithms for dense graphs. –  May 16 '16 at 15:20
  • There is no absolute rule for this. First, the adjacency matrix is more suited to represent dense graphs. Then, from the literature, Dijkstra seems to be a nice choice for the single-source all paths problem in dense graph. For the all-pairs shortest paths, Floyd-Warshall is preferred. – T. Claverie May 16 '16 at 15:31