diff --git a/Labs/5/5.odt b/Labs/5/5.odt new file mode 100644 index 0000000..e4a1e77 Binary files /dev/null and b/Labs/5/5.odt differ diff --git a/Labs/5/5.pdf b/Labs/5/5.pdf new file mode 100644 index 0000000..62e0f13 Binary files /dev/null and b/Labs/5/5.pdf differ diff --git a/Labs/5/5.png b/Labs/5/5.png new file mode 100644 index 0000000..4ee6694 Binary files /dev/null and b/Labs/5/5.png differ diff --git a/Labs/5/5.py b/Labs/5/5.py new file mode 100644 index 0000000..58ca219 --- /dev/null +++ b/Labs/5/5.py @@ -0,0 +1,53 @@ +import heapq + +def dijkstra_verbose(graph, start): + distances = {node: float('inf') for node in graph} + distances[start] = 0 + pq = [(0, start)] + step = 0 + + print(f"\n--- Dijkstra's Algorithm from Node {start} ---\n") + print(f"Initial distances: {distances}") + + while pq: + current_distance, current_node = heapq.heappop(pq) + print(f"\nStep {step}: Visiting node {current_node} with current distance {current_distance}") + + if current_distance > distances[current_node]: + print("\tSkipping since a shorter path is already found.") + continue + + for neighbor, weight in graph[current_node].items(): + distance = current_distance + weight + if distance < distances[neighbor]: + old_distance = distances[neighbor] + distances[neighbor] = distance + heapq.heappush(pq, (distance, neighbor)) + print(f"\tUpdating distance to node {neighbor}: {old_distance} -> {distance}") + else: + print(f"\tNo update required for node {neighbor} (current: {distances[neighbor]}, new: {distance})") + + print(f"Distances after step {step}: {distances}") + step += 1 + + print(f"\nFinal shortest distances from node {start}: {distances}") + return distances + +# Compute SSSPT for each node and print the steps +graph = { + 0: {1: 3, 2: 1}, + 1: {3: 2}, + 2: {1: 1, 4: 5, 5: 5}, + 3: {2: 4, 4: 3}, + 4: {5: 1, 6: 3}, + 5: {6: 1, 7: 4}, + 6: {7: 1}, + 7: {} +} + +#Compute SSSPT for each node and print the steps +#for node in graph: +# dijkstra_verbose(graph, node) + +# Compute SSSPT for a specific node and print the steps +dijkstra_verbose(graph, 0) diff --git a/Labs/5/5.xcf b/Labs/5/5.xcf new file mode 100644 index 0000000..60ffc2e Binary files /dev/null and b/Labs/5/5.xcf differ diff --git a/Labs/5/Q1.png b/Labs/5/Q1.png new file mode 100644 index 0000000..534c35e Binary files /dev/null and b/Labs/5/Q1.png differ diff --git a/Labs/5/Q4.png b/Labs/5/Q4.png new file mode 100644 index 0000000..da2091a Binary files /dev/null and b/Labs/5/Q4.png differ