Backend Engineer Guide: Graph Search, Time-Dependent Shortest-Path Routing & Algorithm Optimization in Python

When backend APIs slow down under combinatorial load—such as multi-leg transit routing (like our production MRT Jakarta Schedule platform), logistics dispatch, or dependency resolution—throwing more cloud CPUs at an $O(N^2)$ or $O(2^N)$ algorithm only inflates your cloud bill. As a Senior Backend Engineer, I optimize hot paths using time-dependent Dijkstra / A* search, dynamic programming, bitmask state compression, and vectorized C/Rust extensions.

1. Time-Dependent Dijkstra for Transit & Logistics Routing in Python

Unlike static road networks where edge weights are constant, rail and logistics schedules have time-dependent edge weights: the wait time at station $u$ depends on the exact arrival timestamp $t$. Below is the production $O((V + E) \log V)$ priority-queue implementation using Python's `heapq`:

import heapq
from bisect import bisect_left
from typing import Dict, List, Tuple

# Edge: (destination_node, sorted_departure_minutes, travel_duration_minutes)
TransitGraph = Dict[str, List[Tuple[str, List[int], int]]]

def earliest_arrival_route(
    graph: TransitGraph, start: str, target: str, start_minute: int
) -> Tuple[int, List[str]]:
    best_arrival: Dict[str, int] = {start: start_minute}
    parent: Dict[str, str] = {}
    pq: List[Tuple[int, str]] = [(start_minute, start)]

    while pq:
        curr_time, u = heapq.heappop(pq)
        if u == target:
            break
        if curr_time > best_arrival.get(u, 10**9):
            continue

        for v, departures, duration in graph.get(u, []):
            idx = bisect_left(departures, curr_time)
            if idx == len(departures):
                continue  # No further train/dispatch today on this edge
            next_dep = departures[idx]
            arr_time = next_dep + duration
            if arr_time < best_arrival.get(v, 10**9):
                best_arrival[v] = arr_time
                parent[v] = u
                heapq.heappush(pq, (arr_time, v))

    if target not in best_arrival:
        return -1, []

    path = [target]
    while path[-1] != start:
        path.append(parent[path[-1]])
    path.reverse()
    return best_arrival[target], path

2. Profiling & Eliminating Algorithmic Bottlenecks in Production APIs

3. Related Backend & Full Stack Engineering Guides


⚡ Hire Aldian Fazrihady — Senior Cloud, GCP, Backend, AI Application & Full Stack Engineer

Need production cloud architecture, GCP/AWS infrastructure, high-throughput backend systems, AI agents, or full stack web engineering? Book a 1-Day Programming Sprint for $100 USD (International Card / Apple Pay) or Rp 1,000,000 (Indonesian QRIS / Bank Virtual Account), or hire directly on Upwork:

Book $100 / Rp 1M 1-Day Sprint → Hire on Upwork (100% Job Success) →


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs