- Design and Analysis of Algorithms
- Home

- Basics of Algorithms
- DAA - Introduction
- DAA - Analysis of Algorithms
- DAA - Methodology of Analysis
- Asymptotic Notations & Apriori Analysis
- DAA - Space Complexities

- Design Strategies
- DAA - Divide & Conquer
- DAA - Max-Min Problem
- DAA - Merge Sort
- DAA - Binary Search
- Strassen’s Matrix Multiplication
- DAA - Greedy Method
- DAA - Fractional Knapsack
- DAA - Job Sequencing with Deadline
- DAA - Optimal Merge Pattern
- DAA - Dynamic Programming
- DAA - 0-1 Knapsack
- Longest Common Subsequence

- Graph Theory
- DAA - Spanning Tree
- DAA - Shortest Paths
- DAA - Multistage Graph
- Travelling Salesman Problem
- Optimal Cost Binary Search Trees

- Heap Algorithms
- DAA - Binary Heap
- DAA - Insert Method
- DAA - Heapify Method
- DAA - Extract Method

- Sorting Methods
- DAA - Bubble Sort
- DAA - Insertion Sort
- DAA - Selection Sort
- DAA - Quick Sort
- DAA - Radix Sort

- Complexity Theory
- Deterministic vs. Nondeterministic Computations
- DAA - Max Cliques
- DAA - Vertex Cover
- DAA - P and NP Class
- DAA - Cook’s Theorem
- NP Hard & NP-Complete Classes
- DAA - Hill Climbing Algorithm

- DAA Useful Resources
- DAA - Quick Guide
- DAA - Useful Resources
- DAA - Discussion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Dijkstra’s algorithm solves the single-source shortest-paths problem on a directed weighted graph *G = (V, E)*, where all the edges are non-negative (i.e., *w(u, v)* ≥ 0 for each edge *(u, v) Є E*).

In the following algorithm, we will use one function ** Extract-Min()**, which extracts the node with the smallest key.

Algorithm: Dijkstra’s-Algorithm (G, w, s)for each vertex v Є G.V v.d := ∞ v.∏ := NIL s.d := 0 S := Ф Q := G.V while Q ≠ Ф u := Extract-Min (Q) S := S U {u} for each vertex v Є G.adj[u] if v.d > u.d + w(u, v) v.d := u.d + w(u, v) v.∏ := u

The complexity of this algorithm is fully dependent on the implementation of Extract-Min function. If extract min function is implemented using linear search, the complexity of this algorithm is ** O(V^{2} + E)**.

In this algorithm, if we use min-heap on which ** Extract-Min()** function works to return the node from

Let us consider vertex ** 1** and

Vertex | Initial | Step1 V_{1} |
Step2 V_{3} |
Step3 V_{2} |
Step4 V_{4} |
Step5 V_{5} |
Step6 V_{7} |
Step7 V_{8} |
Step8 V_{6} |
---|---|---|---|---|---|---|---|---|---|

1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

2 | ∞ | 5 | 4 | 4 | 4 | 4 | 4 | 4 | 4 |

3 | ∞ | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |

4 | ∞ | ∞ | ∞ | 7 | 7 | 7 | 7 | 7 | 7 |

5 | ∞ | ∞ | ∞ | 11 | 9 | 9 | 9 | 9 | 9 |

6 | ∞ | ∞ | ∞ | ∞ | ∞ | 17 | 17 | 16 | 16 |

7 | ∞ | ∞ | 11 | 11 | 11 | 11 | 11 | 11 | 11 |

8 | ∞ | ∞ | ∞ | ∞ | ∞ | 16 | 13 | 13 | 13 |

9 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 20 |

Hence, the minimum distance of vertex ** 9** from vertex

1→ 3→ 7→ 8→ 6→ 9

This path is determined based on predecessor information.

This algorithm solves the single source shortest path problem of a directed graph ** G = (V, E)** in which the edge weights may be negative. Moreover, this algorithm can be applied to find the shortest path, if there does not exist any negative weighted cycle.

Algorithm: Bellman-Ford-Algorithm (G, w, s)for each vertex v Є G.V v.d := ∞ v.∏ := NIL s.d := 0 for i = 1 to |G.V| - 1 for each edge (u, v) Є G.E if v.d > u.d + w(u, v) v.d := u.d +w(u, v) v.∏ := u for each edge (u, v) Є G.E if v.d > u.d + w(u, v) return FALSE return TRUE

The first **for** loop is used for initialization, which runs in ** O(V)** times. The next

Hence, Bellman-Ford algorithm runs in ** O(V, E)** time.

The following example shows how Bellman-Ford algorithm works step by step. This graph has a negative edge but does not have any negative cycle, hence the problem can be solved using this technique.

At the time of initialization, all the vertices except the source are marked by ∞ and the source is marked by **0**.

In the first step, all the vertices which are reachable from the source are updated by minimum cost. Hence, vertices ** a** and

In the next step, vertices ** a, b, f** and

Following the same logic, in this step vertices ** b, f, c** and

Here, vertices ** c** and

Hence, the minimum distance between vertex **s** and vertex **d** is **20**.

Based on the predecessor information, the path is s→ h→ e→ g→ c→ d

Advertisements