Challenge - 5 Problems
Dijkstra Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dijkstra's Algorithm on a Simple Graph
What is the output distances object after running Dijkstra's algorithm from node 'A' on the given graph?
DSA Javascript
const graph = {
A: { B: 4, C: 1 },
B: { A: 4, C: 2, D: 5 },
C: { A: 1, B: 2, D: 8, E: 10 },
D: { B: 5, C: 8, E: 2, Z: 6 },
E: { C: 10, D: 2, Z: 3 },
Z: { D: 6, E: 3 }
};
function dijkstra(graph, start) {
const distances = {};
const visited = new Set();
const nodes = Object.keys(graph);
nodes.forEach(node => distances[node] = Infinity);
distances[start] = 0;
while (visited.size !== nodes.length) {
let closestNode = null;
let shortestDistance = Infinity;
nodes.forEach(node => {
if (!visited.has(node) && distances[node] < shortestDistance) {
shortestDistance = distances[node];
closestNode = node;
}
});
if (closestNode === null) break;
visited.add(closestNode);
const neighbors = graph[closestNode];
for (const neighbor in neighbors) {
const newDist = distances[closestNode] + neighbors[neighbor];
if (newDist < distances[neighbor]) {
distances[neighbor] = newDist;
}
}
}
return distances;
}
const result = dijkstra(graph, 'A');
console.log(result);Attempts:
2 left
💡 Hint
Trace the shortest paths step by step from node 'A' updating distances.
✗ Incorrect
Starting from 'A', the shortest path to 'B' is through 'C' with distance 3 (A->C->B). Similarly, distances to other nodes are updated accordingly.
🧠 Conceptual
intermediateUnderstanding Priority in Dijkstra's Algorithm
In Dijkstra's algorithm, which data structure is best suited to efficiently select the next node with the smallest tentative distance?
Attempts:
2 left
💡 Hint
Think about how to always pick the node with the smallest distance quickly.
✗ Incorrect
A priority queue (min-heap) allows efficient retrieval of the node with the smallest tentative distance, which is essential for Dijkstra's algorithm.
🔧 Debug
advancedIdentify the Bug in Dijkstra's Algorithm Implementation
What error will the following code produce when running Dijkstra's algorithm on a graph with disconnected nodes?
DSA Javascript
function dijkstra(graph, start) {
const distances = {};
const visited = new Set();
const nodes = Object.keys(graph);
nodes.forEach(node => distances[node] = Infinity);
distances[start] = 0;
while (visited.size !== nodes.length) {
let closestNode = null;
let shortestDistance = Infinity;
nodes.forEach(node => {
if (!visited.has(node) && distances[node] < shortestDistance) {
shortestDistance = distances[node];
closestNode = node;
}
});
if (closestNode === null) break;
visited.add(closestNode);
const neighbors = graph[closestNode];
for (const neighbor in neighbors) {
const newDist = distances[closestNode] + neighbors[neighbor];
if (newDist < distances[neighbor]) {
distances[neighbor] = newDist;
}
}
}
return distances;
}Attempts:
2 left
💡 Hint
Consider what happens when closestNode is null.
✗ Incorrect
If the graph has disconnected nodes, closestNode can become null, then accessing graph[closestNode] causes a TypeError.
🚀 Application
advancedApplying Dijkstra's Algorithm to Find Shortest Path Distance
Given the graph below, what is the shortest distance from node 'X' to node 'Y' using Dijkstra's algorithm?
Graph edges with weights:
X -> A (2), X -> B (5), A -> B (1), A -> Y (7), B -> Y (2)
Attempts:
2 left
💡 Hint
Consider all paths from X to Y and sum their weights.
✗ Incorrect
Shortest path is X->A->B->Y with distances 2 + 1 + 2 = 5.
❓ Predict Output
expertOutput of Dijkstra's Algorithm with Negative Edge Weight
What will be the output or error when running Dijkstra's algorithm on the following graph with a negative edge weight?
DSA Javascript
const graph = {
A: { B: 1, C: 4 },
B: { C: -3, D: 2 },
C: { D: 3 },
D: {}
};
function dijkstra(graph, start) {
const distances = {};
const visited = new Set();
const nodes = Object.keys(graph);
nodes.forEach(node => distances[node] = Infinity);
distances[start] = 0;
while (visited.size !== nodes.length) {
let closestNode = null;
let shortestDistance = Infinity;
nodes.forEach(node => {
if (!visited.has(node) && distances[node] < shortestDistance) {
shortestDistance = distances[node];
closestNode = node;
}
});
if (closestNode === null) break;
visited.add(closestNode);
const neighbors = graph[closestNode];
for (const neighbor in neighbors) {
const newDist = distances[closestNode] + neighbors[neighbor];
if (newDist < distances[neighbor]) {
distances[neighbor] = newDist;
}
}
}
return distances;
}
const result = dijkstra(graph, 'A');
console.log(result);Attempts:
2 left
💡 Hint
Dijkstra's algorithm does not work correctly with negative edge weights.
✗ Incorrect
Dijkstra's algorithm assumes all edge weights are non-negative. Negative weights can cause incorrect shortest path calculations.
