import heapq def dijkstra(graph, start, end): # Priority queue: (cost, current_node) queue = [(0, start)] distances = {node: float('inf') for node in graph} distances[start] = 0 predecessors = {node: None for node in graph} while queue: current_cost, current_node = heapq.heappop(queue) if current_node == end: break for neighbor, cost in graph[current_node].items(): new_cost = current_cost + cost if new_cost < distances[neighbor]: distances[neighbor] = new_cost predecessors[neighbor] = current_node heapq.heappush(queue, (new_...
Popular posts from this blog
from collections import Counter def count_word_frequency(file_path, top_n): try: words = open(file_path, encoding='utf-8').read().lower().split() words = [''.join(c for c in word if c.isalnum()) for word in words] print(Counter(words).most_common(top_n)) except Exception as e: print(f"Error: {e}") # Usage count_word_frequency("example.txt", 10)






Comments
Post a Comment