Introduction to Algorithms (Dijkstra)

Relaxation is Safe

Lemma: The relaxation algorithm maintains the invariant that d[v] ≥ δ(s, v) for all v ∈ V.

Proof: By induction on the number of steps.

Consider RELAX(u, v, w). By induction d[u] ≥ δ(s, u). By the triangle inequality, δ(s, v) ≤ δ(s, u) + δ(u, v). This means that δ(s, v) ≤ d[u] + w(u, v), since d[u] ≥ δ(s, u) and w(u, v) ≥ δ(u, v). So setting d[v] = d[u] + w(u, v) is safe.

DAGS

Can’t have negative cycles because there are no cycles!

  1. Topologically sort the DAG. The path from u to v implies that u is before v in the linear ordering.
  2. One pass over vertices in topologically sorted order relaxing each edge that leaves each vertex. Θ(V + E) time

Dijkstra's Algorithm

For each edge (u, v) \epsilon E, assume w(u, v) ≥ 0, maintain a set S of vertices whose final shortest path weights have been determined. Repeatedly select u \epsilon V − S with minimum shortest path estimate, add u to S, relax all edges out of u.

Strategy: Dijkstra is a greedy algorithm: choose the closest vertex in V − S to add to set S.

Correctness: The key observation is that each time a vertex u is added to set S, we have d[u] = δ(s, u). 

Dijkstra(G, W, s):
    Initialize(G, s)
    S ← φ
    Q ← V[G] //Insert into Q
    while Q != φ
        do u ← EXTRACT-MIN(Q)
        S = S ∪ {u}
        for each vertex v in Adj[u]
            do RELAX(u, v, w)

Dijkstra Complexity

Θ(v) inserts into priority queue

Θ(v) EXTRACT-MIN operations

Θ(E) DECREASE KEY operations

Array impl:

Θ(v) time for extra min

Θ(1) for decrease key

Total: Θ(V.V + E.1) = Θ(V^2 + E) = Θ(V^2)​​​​​​

Binary min-heap:

Θ(lgV) for extract min

Θ(lg V ) for decrease key

Total: Θ(V lg V + E lg V )

Fibonacci heap:

Θ(lgV) for extract min

Θ(1) for decrease key amortized cost

Total: Θ(V lg V + E)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值