0
0
DSA Cprogramming~20 mins

Dijkstra's Algorithm Single Source Shortest Path in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dijkstra Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dijkstra's Algorithm on a Simple Graph

What is the shortest distance array output after running Dijkstra's algorithm from source node 0 on the following graph?

Graph edges with weights:

  • 0 -> 1 (4)
  • 0 -> 2 (1)
  • 2 -> 1 (2)
  • 1 -> 3 (1)
  • 2 -> 3 (5)
DSA C
int graph[4][4] = {
  {0, 4, 1, 0},
  {0, 0, 0, 1},
  {0, 2, 0, 5},
  {0, 0, 0, 0}
};

// Run Dijkstra from node 0
// Output shortest distances array
A[0, 2, 1, 3]
B[0, 4, 1, 5]
C[0, 3, 1, 4]
D[0, 3, 2, 4]
Attempts:
2 left
💡 Hint

Remember to update distances when a shorter path is found through a neighbor.

🧠 Conceptual
intermediate
1:30remaining
Understanding Priority Queue Role in Dijkstra's Algorithm

Why is a priority queue used in Dijkstra's algorithm?

ATo always pick the next node with the smallest tentative distance to process
BTo store all nodes in alphabetical order
CTo keep track of visited nodes only
DTo reverse the order of nodes before processing
Attempts:
2 left
💡 Hint

Think about how the algorithm decides which node to explore next.

🔧 Debug
advanced
2:30remaining
Identify the Error in Dijkstra's Algorithm Implementation

What error will this code cause when running Dijkstra's algorithm on a graph with 3 nodes?

int dist[3] = {0, INT_MAX, INT_MAX};
int visited[3] = {0, 0, 0};

for (int i = 0; i < 3; i++) {
  int u = -1;
  for (int j = 0; j < 3; j++) {
    if (!visited[j] && (u == -1 || dist[j] < dist[u])) {
      u = j;
    }
  }
  visited[u] = 1;
  for (int v = 0; v < 3; v++) {
    if (graph[u][v] && dist[u] + graph[u][v] < dist[v]) {
      dist[v] = dist[u] + graph[u][v];
    }
  }
}
ANo error, code runs correctly
BRuntime error due to accessing dist[-1] if all nodes visited
CSyntax error due to missing semicolon
DLogic error: distances never updated
Attempts:
2 left
💡 Hint

What happens if all nodes are visited before the loop ends?

Predict Output
advanced
2:00remaining
Output of Dijkstra's Algorithm on a Graph with Zero-Weight Edges

What is the shortest distance array after running Dijkstra's algorithm from source node 0 on this graph?

Graph edges:

  • 0 -> 1 (0)
  • 1 -> 2 (2)
  • 0 -> 2 (4)
  • 2 -> 3 (1)
  • 1 -> 3 (5)
DSA C
int graph[4][4] = {
  {0, 0, 4, 0},
  {0, 0, 2, 5},
  {0, 0, 0, 1},
  {0, 0, 0, 0}
};

// Run Dijkstra from node 0
// Output shortest distances array
A[0, 0, 2, 5]
B[0, 0, 4, 5]
C[0, 2, 4, 5]
D[0, 0, 2, 3]
Attempts:
2 left
💡 Hint

Zero-weight edges can reduce distances without cost.

🧠 Conceptual
expert
1:30remaining
Why Dijkstra's Algorithm Fails with Negative Edge Weights

Why does Dijkstra's algorithm not work correctly if the graph has negative edge weights?

ABecause it assumes once a node's shortest distance is found, it cannot be improved later
BBecause it cannot handle graphs with cycles
CBecause it uses a stack instead of a queue internally
DBecause it requires all edges to have the same weight
Attempts:
2 left
💡 Hint

Think about how the algorithm finalizes distances.