Vous êtes sur la page 1sur 3

Directed Graph

Greed: Shortest Path Directed graph: G = (V, E) .


■ V = set of vertices or nodes.
■ E ⊆ V × V = set of edges or arcs.
■ n = |V|, m = |E|.
■ Directed path: s - 2 - 3 - 5 - t.
– simple
2 23 3
9
■ Directed cycle: 5 - 4 - 3 - 5.
s 18
14 6
2
6 2 3
30 4 19
11 1
15 5
5
6
20 16 6
4
7 44
t 5

7 8

Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne 2

Networks Shortest Path Network


Shortest path network: (V, E, s, t, c) .
Network Nodes Arcs Flow ■ Directed graph (V, E).
■ Source s ∈ V, sink t ∈ V.
telephone exchanges, cables, fiber optics, voice, video,
communication ■ Arc costs c(v, w).
computers, satellites microwave relays packets Cost of path s - 2 - 3 - 5 - t
■ Cost of path = sum of arc costs in path. = 9 + 23 + 2 + 16
gates, registers,
circuits wires current = 48.
processors
mechanical joints rods, beams, springs heat, energy
reservoirs, pumping 23
hydraulic pipelines fluid, oil 2 3
stations, lakes 9
s 18
financial stocks, currency transactions money 14
2 6
6
freight, 30 4 19
airports, rail yards, highways, railbeds, 5
11
transportation vehicles, 15
5
street intersections airway routes 6
passengers 20 16

7 44
t

3 4
Shortest Path Shortest Path: Existence
Shortest path problem. (CLR 25.1-25.2) Existence. If some path from s to v contains a negative cost cycle,
■ Shortest path network (V, E, s, t, c). there does not exist a shortest path. Otherwise, there exists a shortest
s-v that is simple.
■ Find shortest directed path from s to t.
⇒ If negative cycle, can produce arbitrarily negative path by
traversing cycle enough times.
Assumptions.
■ Network contains directed path from s to every other node.
■ Network does not contain a negative cost cycle. s v
3 C

Application. -6
-4 c(C) < 0
■ Online directions.
5 7 4
⇐ If no negative cycle, can remove cycles without increasing cost.

5 6

Shortest Path: Properties Dijkstra’s Algorithm


Optimal substructure property. All sub-paths of shortest paths are Upon termination.
shortest paths. ■ π(v) = distance of shortest s-v path.
■ Let P1 be x-y sub-path of shortest s-v path P. ■ pred(v) gives shortest path.
■ Let P2 be any x-y path. Dijkstra’s Algorithm
■ c(P1) ≤ c(P2), otherwise P x for each v ∈ V
P1 y
not shortest s-v path. s v π(v) ← ∞
pred(v) ← nil
P2 π(s) ← 0
S ← φ
init(Q)
Triangle inequality. for each v ∈ V
■ Let d*(v, w) be the length of the shortest path from v to w. insert(v, Q)
while (Q ≠ φ)
■ Then, d*(v, w) ≤ d*(v, x) + d*(x, w) v = delete-min(Q)
S ← S ∪ {v}
v w
for each w s.t (v,w) ∈ E
if π(w) > π(v) + c(v,w)
decrease-key π(w) ← π(v) + c(v,w)
x pred(w) ← v
7 8
Dijkstra’s Algorithm: Proof of Correctness Priority Queues and Heaps (CLR 20, 21)
y
Invariant. For each vertex v ∈ S, π(v) = d*(s, v). Heaps
P*
■ Proof by induction on |S|. Operation Linked List Binary Binomial Fibonacci * Relaxed
x
■ Base case: |S| = 0 is trivial. make-heap 1 1 1 1 1
s
insert 1 log N log N 1 1
v
S find-min N 1 log N 1 1
■ Induction step:
delete-min N log N log N log N log N
– suppose Dijkstra’s algorithm adds vertex v to S
union 1 N log N 1 1
– π(v) is the length of the some path from s to v
– if π(v) is not the length of the shortest s-v path, then let P* be a
decrease-key 1 log N log N 1 1
shortest s-v path delete N log N log N log N log N
– P* must use an edge that leaves S, say (x, y) is-empty 1 1 1 1 1
– then π(v) > d*(s, v) assumption
= d*(s, x) + d(x, y) + d*(y, v) optimal substructure
≥ d*(s, x) + d(x, y) nonnegative lengths
Dijkstra n (n) + m(1) = O(n2) n (log n) + m(1) =
= π(x) + d(x, y) inductive hypothesis O(m + n log n)
1 make-heap
≥ π(y) algorithm n (log n) + m(log n) =
n insert
so Dijkstra’s algorithm would have selected y instead of v n delete-min O(m log n)
9
m decrease-key 10

Shortest Path Extensions


Variants of shortest path:
■ Undirected graph.
– O(m + n) using Thorup’s algorithm
■ Negative weights but no negative cycles.
– O(mn) using Bellman-Ford
■ Unit weights.
– O(m + n) using breadth first search
■ Integer weights between 0 and constant C.
■ DAGs.
– O(m + n) using topological sort
■ All-pairs.
– O(n3) using Floyd-Warshall
– O(mn + n log log n) using Pettie’s algorithm

11

Vous aimerez peut-être aussi