26,292 views
75 75 votes

Let $G(V,E)$ be an undirected graph with positive edge weights. Dijkstra’s single source shortest path algorithm can be implemented using the binary heap data structure with time complexity:

  1. $O\left(|V|^2\right)$

  2. $O\left(|E|+|V|\log |V|\right)$

  3. $O\left(|V|\log|V|\right)$

  4. $O\left(\left(|E|+|V|\right)\log|V|\right)$

3 Answers

Best answer
93 93 votes
Option $(D)$ :  Binary heap. $|E|$ decrease key operations and each taking $O\left(\log|V|\right)$ time $+$ $|V|$ extract-min operations each taking $O\left(\log|V|\right)$.

Option $(B)$ :  Fibonacci heap. $|E|$ decrease key operations and each taking $O(1)$ time $+$ $|V|$ extract-min operations each taking $O\left(\log|V|\right)$.

Option $(A)$ :  Array. Finding min-vertex in each iteration takes $O(V)$ and this needs to be done $|V|$ times.

Binomial Heap is same as Binary heap here, as the critical operations are decrease key and extract-min.

Correct Answer: $D$
• edited by
8 8 votes
  1. Complexity Analysis:

    • Heap Insertion: Inserting all vertices into the heap initially takes O(V) time.
    • Extract-Min Operation: This operation, which finds and removes the vertex with the smallest tentative distance, is performed once per vertex. Since the extract-min operation on a binary heap is O(log⁡V), and it is performed V times, the total time for all extract-min operations is O(Vlog⁡V).
    • Decrease-Key Operation: This operation is performed each time an adjacency list is explored and the distance to a vertex is updated. This operation, which effectively updates the heap, takes O(log⁡V) per update. Since every edge may potentially lead to an update (in the worst case), and there are E edges, the total time for all decrease-key operations is O(Elog⁡V).

Combining these, the total time complexity for Dijkstra's algorithm using a binary heap becomes:

O(Vlog⁡V+Elog⁡V)=O((V+E)log⁡V)

2. Fibonacci Heap:

  • Time Complexity: When using a Fibonacci heap in Dijkstra's algorithm, the extract-min operation still requires O(log⁡V) time, but the decrease-key operations are more efficient. The decrease-key operations in a Fibonacci heap can be done in amortized O(1) time.
  • With the Fibonacci heap, the total time complexity breaks down as:
    • O(Vlog⁡V) for all the extract-min operations (since each vertex is extracted exactly once).
    • O(E) for all the decrease-key operations due to the amortized O(1) time per operation.
  • Combining these gives a total of O(Vlog⁡V+E) time complexity.
• edited by
1 1 vote
dijkstra(G, S):
    for each node in V:
        disance[v] = ∞
    distance[S] = 0;
    
    Q = Heap();
    
    while Q is not empty:
        v = extractMIN(Q)
        
        for each adjacent u in V:
            if distance[u] < distance[v] + w:
                distance[u] = distance[u] + w
                parent[u] = v

 

Time Complexity of Dijkstra's algorithm here depends on two things 

  1. To extract minimum from Heap: removing the minimum from Heap takes $O(logn)$ time.
  2. Update for distance in Heap : each time we update the distance of key we have to check the position of node in Heap and reposition it can $O(logn)$ time
$T(n)$ =  $|V| ($remove minimum from Heap$) + deg(|V|)* ($update key$)$

 

$T(n)$ =  $|V| (log|V|) + 2|E|(log|V|)$

 

$T(n)$ =  $ |V|(log|V|) + |E|(log|V|)$

 

$T(n)$ =  $ O((|V|+ |E|) log|V|)$
 
• edited by
Answer:
Position:
Show:

Related questions

200 200 votes
9 answers 9 answers
79.2k
79.2k views
Kathleen asked Sep 22, 2014
79,175 views
A $5$ stage pipelined CPU has the following sequence of stages:IF – instruction fetch from instruction memoryRD – Instruction decode and register readEX – Execute: ALU op...
60 60 votes
6 answers 6 answers
12.0k
12.0k views
go_editor asked Nov 14, 2016
11,961 views
Let $s$ and $t$ be two vertices in a undirected graph $G=(V,E)$ having distinct positive edge weights. Let $[X,Y]$ be a partition of $V$ such that $s \in X$ and $t \in Y$...
55 55 votes
11 answers 11 answers
22.4k
22.4k views
Kathleen asked Sep 22, 2014
22,354 views
Let $s$ and $t$ be two vertices in a undirected graph $G=(V,E)$ having distinct positive edge weights. Let $[X,Y]$ be a partition of $V$ such that $s \in X$ and $t \in Y$...
32 32 votes
3 answers 3 answers
15.5k
15.5k views
gatecse asked Sep 21, 2014
15,512 views
Let $f(x)$ be the continuous probability density function of a random variable $x$, the probability that $a < x \leq b$, is :$f(b-a)$$f(b) - f(a)$$\int\limits_a^b f(x) dx...