Algorithm Design & Analysis: Dijkstra Algorithm - Proof of Correctness

本文介绍了Dijkstra算法,一种用于寻找图中源顶点到所有其他顶点最短路径的贪心算法,并提供了该算法正确性的数学证明。
摘要由CSDN通过智能技术生成

Hi peers,

Dijkstra algorithm is a greedy algorithm that finds the shortest paths from a given source vertex to all other vertices in a graph. In this short essay, I will review the pseudocode for Dijkstra’s algorithm and also provide a proof of correctness for it. For the best viewing experience, you may refer to the attached PDF version of this essay.

Pseudocode for Dijkstra:
Setup:
G (V, E) is a non-negative weighted graph
V: {v1, v2, …, vn} is the set of all vertices in G
E: {e1, e2, …, em} is a set of all edges in G
S denotes the set of visited vertices
D denotes an array that stores the distance from the source node to other nodes
n denotes the total number of vertices in G
m denotes the total number of edges in G
s denotes the source vertex index
c denotes the index of the closest to vertex s
l denotes the distance between vertex c and vertex s
A: {a1, a2, … ak} denotes the set of adjacent nodes of vertex c

Initialization:
S= ∅;
D[n]=∅;
D[s]=0;
∀ i ≠s,i∈{1,2,…,n}, D[i]= ∞;

Steps:

While (S≠V){
                    s=index of the node that has the smallest value in D;
                    Add s into S;
					For every element, ai, in A:
                       If (D[s] + l < D[ai]): D[ai] = D[s] + l;
}

Proof of correctness:

Given a source vertex s and an arbitrary vertex v in G, the Dijkstra algorithm returns a distance α between the s and v. Now, we prove the distance α is the smallest distance between s and v.

We prove by induction.
Base case: when |S| = 1 (there is one node in the visited set), D[s] = 0, which is the closest distance (obviously, the closest distance between a node and itself is 0!).
Inductive Hypothesis (I.H.): When |S| = t (there are t nodes in the visited set), the algorithm correctly gives the closest distance between the node vi (vi ∈S) and the source node s.

S’ denotes the visited set of nodes when |S| = t. S’’ denotes the visited set of nodes when |S| = t + 1. r denotes the index of the next node to be added. S’’=S’∪{r}.

Now we have to prove that, when |S| = t + 1, the distance calculated by the algorithm, D[r], between node r and source node s is the closest distance between node s and node r.

We prove D[r] is the closest distance by contradiction. Assume there exists a path, P’_sr, from source node s to node r such that the distance of the path, d’, is smaller than D[r]. P_sr denotes the path, which has a length of D[r], from node s to node r.

We know that P’_sr must start in S’ (the source node s is included in S’ at very first). However, when P’_sr extends itself to reach out to node r, P’_sr must leave S’ at some node.

Let v_x denote the node where P’_sr leaves S’. P’_sx denotes the path before P’_sr leaves S’.

Let v_y denotes the first node in the P’_sr after it leaves S’. Then, P’_yr denotes the path after P’_sr leaves S’.

From the inductive hypothesis, we know that the smallest distance between node s and node x is D[x]. Let d’_sx denote the length of P’_sx. Thus,
D[x] ≤ d’_sx

Remember node x ∈S’ and node y is adjacent to node x. This fact implies that the algorithm already calculates the value of D[y]. (Recall from the pseudocode, during such calculation, D[y] remains unchanged with its original value or updated with a new value D[x]+l_xy, whichever is smaller). Let l_xy denote the distance between node x and y. This gives us the following equation.
D[y] ≤D[x]+l_xy

Combining this with the inequality d’_sx ≥ D[x], we have
D[y]≤l_xy+d’_sx

However, another important fact is that the node y is not in the visited set S’ and the node r is neither in the visited set S’. However, node r is the next node to be added in S’. This implies that
D[r]≤D[y]≤D[x]+l_xy

Recall that D[x]+l_xy is the length of the path P’_sy, which is a sub-path of P’_sr. Now, the above inequality tells us that P’_sy is not the optimal path from node s to node y. There exists another path that is shorter than P’_sy. This is equivalent to say that P’sr is not the shortest path between the node s and node r. The contradiction appears.

Thus, we can conclude that there does not exist another path that is shorter than P_sr. P_sr is the shortest path between the node s and node r. By induction, we complete our proof. Q.E.D.

I hope you enjoy the proof. Bon Appétit!

Best,

Ben

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值