Program to find how long it will take to reach messages in a network in Python

In network communication, messages propagate through nodes with varying transmission times. This problem finds the minimum time needed for a message starting at node 0 to reach all nodes in an undirected network graph.

Given n nodes labeled 0 to n, and edges in the form (a, b, t) where t is the transmission time between nodes a and b, we need to find how long it takes for every node to receive the message.

Problem Analysis

If the input is n = 3 and edges = [[0, 1, 3],[1, 2, 4],[2, 3, 2]], the output will be 9. The message travels: 0 ? 1 ? 2 ? 3, taking 3 + 4 + 2 = 9 time units to reach the last node.

Algorithm

We use Dijkstra's algorithm with a priority queue to find the shortest path from node 0 to all other nodes ?

  1. Build an adjacency list representation of the graph
  2. Use a min-heap to track (time, node) pairs
  3. Process nodes in order of shortest time from source
  4. Return the time when all nodes are visited

Solution

import heapq
from collections import defaultdict

class Solution:
    def solve(self, n, edges):
        graph = self.build_graph(edges)
        visited = set()
        heap = [(0, 0)]  # (time, node)
        
        while heap:
            current_total_time, node = heapq.heappop(heap)
            
            if node in visited:
                continue
                
            visited.add(node)
            
            # All nodes visited
            if len(visited) == (n + 1):
                return current_total_time
            
            # Add neighbors to heap
            for neighbor, time in graph[node]:
                if neighbor not in visited:
                    heapq.heappush(heap, (current_total_time + time, neighbor))
        
        return -1  # Not all nodes reachable
    
    def build_graph(self, edges):
        graph = defaultdict(set)
        for src, dest, time in edges:
            graph[src].add((dest, time))
            graph[dest].add((src, time))
        return graph

# Test the solution
solution = Solution()
n = 3
edges = [[0, 1, 3], [1, 2, 4], [2, 3, 2]]
result = solution.solve(n, edges)
print(f"Time to reach all nodes: {result}")
Time to reach all nodes: 9

How It Works

  1. Graph Building: Create adjacency list with (neighbor, time) pairs
  2. Priority Queue: Min-heap ensures we process closest nodes first
  3. Dijkstra's Algorithm: Finds shortest path from source to all nodes
  4. Termination: When all n+1 nodes are visited, return the maximum time

Time Complexity

The time complexity is O(E log V) where E is the number of edges and V is the number of vertices, due to the heap operations in Dijkstra's algorithm.

Conclusion

This solution uses Dijkstra's algorithm to find the minimum time for a message to propagate through all nodes in a network. The key insight is that we need the maximum of all shortest path distances from the source node.

Updated on: 2026-03-25T13:28:33+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements