Ds 3
Ds 3
Ds 3
Aim:
Program:
import heapq
import time
import sys
V = len(graph)
dist = [sys.maxsize] * V
dist[src] = 0
visited = [False] * V
for _ in range(V):
u = min_distance(dist, visited)
visited[u] = True
for v in range(V):
if graph[u][v] > 0 and not visited[v] and dist[u] + graph[u][v] < dist[v]:
return dist
min_val = sys.maxsize
min_index = -1
for v in range(len(dist)):
min_val = dist[v]
min_index = v
return min_index
V = len(graph)
dist = [sys.maxsize] * V
dist[src] = 0
while pq:
current_dist, u = heapq.heappop(pq)
continue
return dist
def compare_performance(V):
import random
random.seed(42)
matrix = [[0 if i == j else random.randint(1, 10) if random.random() < 0.5 else 0 for j in range(V)] for i in
range(V)]
for i in range(V):
for j in range(V):
if matrix[i][j] != 0:
adj_list[i].append((j, matrix[i][j]))
src = 0
start = time.time()
dijkstra_matrix(matrix, src)
start = time.time()
dijkstra_list(adj_list, src)
# Example usage:
compare_performance(750)