In a directed graph (digraph), edges have a direction (A → B ≠ B → A). In a weighted graph, each edge carries a numeric cost (distance, time, capacity). Combining both models real systems where relationships are one-way and have a cost.
In a directed graph (digraph), edges have a direction (A → B ≠ B → A). In a weighted graph, each edge carries a numeric cost (distance, time, capacity). Combining both models real systems where relationships are one-way and have a cost.
Weighted directed graph:
A --5--> B
A --2--> C
C --1--> B
Adjacency list with weights:
A: [(B,5), (C,2)]
B: []
C: [(B,1)]
graph = {
'A': [('B', 5), ('C', 2)], # directed, weighted edges
'B': [],
'C': [('B', 1)],
}
# Shortest A->B is A->C->B = 2 + 1 = 3, not the direct edge 5.
| Domain | Vertices | Weighted directed edges |
|---|---|---|
| Maps / GPS | intersections | one-way roads + travel time |
| Networks | routers | links + latency/bandwidth |
| Task scheduling | tasks | dependencies + durations |
| Finance | currencies | exchange rates |
| Goal | Algorithm | Complexity |
|---|---|---|
| Shortest path (non-negative weights) | Dijkstra | O((V+E) log V) |
| Shortest path (negative edges) | Bellman-Ford | O(VE) |
| Ordering with dependencies | Topological sort | O(V+E) |
| Detect cycles / reachability | DFS | O(V+E) |
Direction and weight are what turn an abstract graph into a faithful model of routing, scheduling, and dependency problems that drive real applications.
The choice of algorithm hinges on these properties — for example, negative weights rule out Dijkstra and require Bellman-Ford.