Dijkstra's Algorithm (Single Source)

beginner
12 min

Dijkstra's Algorithm (Single Source)

Welcome to our deep dive into Dijkstra's Algorithm! This powerful tool is a key component in many real-world applications, helping you find the shortest path between nodes in a graph. Let's get started! šŸš€

What is Dijkstra's Algorithm? šŸ¤”

Dijkstra's Algorithm is an efficient pathfinding algorithm that solves the single-source shortest path problem for a graph. It was developed by the Dutch computer scientist, Edsger W. Dijkstra, in 1956.

šŸ’” Pro Tip: This algorithm is particularly useful in navigation systems, social networks, and data communication networks.

Why Dijkstra's Algorithm? šŸ¤“

  1. Efficient: Dijkstra's Algorithm guarantees to find the shortest path from a single source node to all other nodes in the graph.
  2. Works with weighted and unweighted graphs: The algorithm can handle both weighted (where edges have costs) and unweighted (where all edges have the same cost) graphs.
  3. Applicable in real-world scenarios: As mentioned earlier, Dijkstra's Algorithm has numerous real-world applications, such as finding the shortest route between cities in a map or the most efficient way to distribute resources.

How Dijkstra's Algorithm Works? 🧩

  1. Initialize: Start with the source node and set its distance to 0. Mark all other nodes as unvisited and set their distances to infinity.

  2. Iterate through vertices: In each iteration, select the unvisited node with the minimum distance and mark it as visited. Update the distances of its adjacent nodes if a shorter path to that node is found.

  3. Repeat until finished: Continue the process until all nodes are visited. The shortest distance to each node from the source will be calculated.

šŸ“ Note: Dijkstra's Algorithm uses a priority queue (min-heap) to maintain nodes based on their distances.

Practical Example šŸ”§

Let's consider the following graph:

A - 4 - B | | | 9 D - 7 - C
  • Start at node A.
  • Update distances: A(0), B(4), C(āˆž), D(āˆž).
  • Visit B (minimum distance): B(0), A(0), C(āˆž), D(āˆž).
  • Update distances: C(11) (via A-B-C with total cost 0+4+7), D(10) (via A-D with total cost 0+7).
  • Visit C: C(11), B(4), A(0), D(10).
  • Update distances (since we've reached C and D): A(3) (via C-A with total cost 11+3), B(5) (via C-B with total cost 11+2).
  • The algorithm stops here, as all nodes have been visited. We have found the shortest path from A to all other nodes: A(0), B(5), C(11), D(10).
Quick Quiz
Question 1 of 1

What is the shortest path from node A to B in the given graph?

Implementing Dijkstra's Algorithm in Python šŸ

Here's a simple Python implementation of Dijkstra's Algorithm.

python
import heapq def dijkstra(graph, start): distances = {node: float('inf') for node in graph} distances[start] = 0 priority_queue = [(0, start)] while priority_queue: current_distance, current_node = heapq.heappop(priority_queue) if current_distance > distances[current_node]: continue for neighbor, weight in graph[current_node].items(): distance = current_distance + weight if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(priority_queue, (distance, neighbor)) return distances

šŸ“ Note: The graph variable should be a Python dictionary, where keys represent nodes and values are dictionaries containing adjacent nodes and their weights.

Wrapping Up šŸŽÆ

Dijkstra's Algorithm is a powerful tool for finding the shortest path in a graph from a single source node. By understanding its concepts and implementing it, you'll be well-equipped to tackle a wide range of real-world problems.

šŸ’” Pro Tip: Practice implementing Dijkstra's Algorithm on various graphs to solidify your understanding.

Happy coding! šŸš€šŸ’»šŸ’»šŸš€