Dijkstra Algorithm Calculator

Dijkstra's Algorithm Shortest Path Calculator

Enter one edge per line. Example: A B 5 means a path from A to B with weight 5.


Understanding Dijkstra's Algorithm

Dijkstra's Algorithm is a fundamental graph search algorithm conceived by computer scientist Edsger W. Dijkstra in 1956. It is used to find the shortest paths between nodes in a graph, which may represent, for example, road networks or computer communication lines.

How the Algorithm Works

The algorithm maintains a set of visited nodes and a set of unvisited nodes. It starts at a designated source node and iteratively selects the unvisited node with the smallest tentative distance, "relaxes" its neighbors (updates their distances), and marks the node as visited. This process continues until the destination node is reached or all reachable nodes are processed.

  • Initialization: Set the distance to the source node to 0 and all other nodes to infinity.
  • Selection: Choose the unvisited node with the lowest distance value.
  • Relaxation: For the current node, consider all of its unvisited neighbors and calculate their tentative distances through the current node. If the new distance is smaller than the previously assigned value, update it.
  • Finalization: Mark the current node as visited. A visited node will never be checked again.

Real-World Applications

Dijkstra's logic powers much of our modern digital infrastructure:

  1. GPS and Navigation: Finding the fastest driving route from your home to a destination.
  2. Network Routing: Open Shortest Path First (OSPF) uses Dijkstra to route data packets efficiently across the internet.
  3. Social Networks: Suggesting connections based on the "degrees of separation" between users.
  4. Supply Chain Logic: Optimizing the movement of goods between warehouses and retail hubs to minimize cost or time.

Example Calculation

Imagine a graph with three nodes: A, B, and C. The edges are:

  • A to B (Weight: 10)
  • B to C (Weight: 20)
  • A to C (Weight: 35)

If you calculate the path from A to C, the algorithm will compare the direct path (Weight 35) with the path via B (10 + 20 = 30). Dijkstra's Algorithm will identify A → B → C as the shortest path with a total weight of 30.

function calculateDijkstra() { var edgesRaw = document.getElementById('edgesInput').value.trim(); var startNode = document.getElementById('startNode').value.trim(); var endNode = document.getElementById('endNode').value.trim(); var resultDiv = document.getElementById('dijkstraResult'); if (!edgesRaw || !startNode || !endNode) { showError("Please fill in all fields (Edges, Start, and End)."); return; } var graph = {}; var lines = edgesRaw.split('\n'); for (var i = 0; i < lines.length; i++) { var parts = lines[i].trim().split(/\s+/); if (parts.length < 3) continue; var u = parts[0]; var v = parts[1]; var w = parseFloat(parts[2]); if (isNaN(w)) { showError("Invalid weight detected on line " + (i + 1)); return; } if (!graph[u]) graph[u] = {}; if (!graph[v]) graph[v] = {}; // Assuming directed graph for this calculator; add reverse for undirected graph[u][v] = w; } if (!graph[startNode]) { showError("Start node '" + startNode + "' does not exist in the graph data."); return; } var distances = {}; var prev = {}; var unvisited = []; var nodes = Object.keys(graph); for (var j = 0; j 0) { // Sort to find node with smallest distance unvisited.sort(function(a, b) { return distances[a] – distances[b]; }); var current = unvisited.shift(); if (distances[current] === Infinity) break; if (current === endNode) break; var neighbors = graph[current]; for (var neighbor in neighbors) { var alt = distances[current] + neighbors[neighbor]; if (alt < distances[neighbor]) { distances[neighbor] = alt; prev[neighbor] = current; } } } if (distances[endNode] === Infinity) { showError("No path exists between " + startNode + " and " + endNode); } else { var path = []; var u = endNode; while (u !== null) { path.unshift(u); u = prev[u]; } resultDiv.style.display = "block"; resultDiv.style.backgroundColor = "#e8f5e9"; resultDiv.style.border = "1px solid #c8e6c9"; resultDiv.innerHTML = '

Shortest Path Found!

' + 'Total Weight (Distance): ' + distances[endNode] + " + 'Path: ' + path.join(' → ') + "; } } function showError(msg) { var resultDiv = document.getElementById('dijkstraResult'); resultDiv.style.display = "block"; resultDiv.style.backgroundColor = "#ffebee"; resultDiv.style.border = "1px solid #ffcdd2″; resultDiv.innerHTML = 'Error: ' + msg + "; }

Leave a Reply

Your email address will not be published. Required fields are marked *