(A)
/ \
1/ \4
/ \
(B)-------(C)
\ /
2\ /5
\ /
(D)
Represented as an adjacency list:
A: [(B, 1), (C, 4)]
B: [(C, 2), (D, 2)]
C: [(D, 5)]
D: []
Verification Summary
The algorithm correctly computes
shortest paths from A:
●A → B = 1
●A → C = 3 (via B)
●A → D = 3 (via B)
import heapq
def dijkstra(graph, start):
distances = {node: float('inf') for node in graph}
distances[start] = 0
pq = [(0, start)]
while pq:
current_distance, current_node = heapq.heappop(pq)
if current_distance > distances[current_node]:
continue
for neighbor, weight in graph[current_node]:
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return distances
# Graph
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('C', 2), ('D', 2)],
'C': [('D', 5)],
'D': []
}
print("Dijkstra's Algorithm Output:")
print(dijkstra(graph, 'A'))
Time | Incoming | BufferBefore | Sent |
BufferAfter | Dropped
------------------------------------------------------
------
1| 4| 0| 3| 1|0
2| 6| 1| 3| 4|0
3| 8| 4| 3| 7|2
4| 3| 7| 3| 7|2
5| 5| 7| 3| 7|2
Final buffer drain:
6| 0| -| 3| 4|0
7| 0| -| 3| 1|0
8| 0| -| 1| 0|0
Total Dropped Packets: 4