IS CS-2018S1-01

题目来源Problem 1 日期:2024-08-07 题目主题:CS-算法-图论

解题思路

在解决这道题目时,我们需要理解 Dijkstra 算法的工作机制,包括初始化步骤、选择最小路径顶点的过程,以及更新路径长度的方式。通过示例和伪代码,我们可以全面理解该算法的正确性和时间复杂度。

Solution

Question 1

An example where Dijkstra’s algorithm does not find the correct shortest paths is when the graph contains negative edge weights. However, since Dijkstra’s algorithm is not designed to handle negative weights, we will use a non-negative example where it might fail due to not updating paths correctly.

Consider the following graph:

  • Vertices:
  • Edges with weights:

Starting from vertex , Dijkstra’s algorithm will proceed as follows:

  1. Initialize: .
  2. Choose : Update and .
  3. Choose : Dijkstra’s algorithm won’t update to since it already has .

In this example, Dijkstra’s algorithm does not find the correct shortest path due to the presence of a negative edge.

Question 2

To fill in the blank , we need to correctly update the path lengths using the Dijkstra’s algorithm rule. The pseudo-code line should be:

if c[u] > c[v] + d(v, u) then c[u] = c[v] + d(v, u)

So the filled-in pseudo-code is:

Dijkstra( graph G = (V,E), start node s, length d(u,v) of each edge (u,v) ) {
    c = an empty array;    Q = an empty set;
    for ( v ∈ V )
        c[v] = ∞;
    c[s] = 0;
    for ( v ∈ V )
        add v to Q;
    while ( Q ≠ ∅ ) {
        v = a vertex v ∈ Q that minimizes c[v];
        remove v from Q;
        for ( u ∈ {destinations of edges outgoing from v} )
            if c[u] > c[v] + d(v, u) then c[u] = c[v] + d(v, u);
    }
}

Question 3

Let’s apply Dijkstra’s algorithm to the given graph:

  • Start at node .

Initialization:

  • , , , .

Iteration 1:

  • Choose : .
  • Update , ,

Iteration 2:

  • Choose : .
  • Update (since ).

Iteration 3:

  • Choose : .
  • Update (since ).

Iteration 4:

  • Choose : .

Final values:

  • , , , .

Question 4

Time complexity of the code fragments:

(i): Finding the vertex that minimizes takes time for each iteration. Since there are vertices, the total time spent is .

(ii): For each vertex , we consider all outgoing edges . Each edge is considered exactly once, hence the total time spent is .

Question 5

Using a priority queue (binary heap) as :

(i): Extracting the minimum element from the priority queue takes time. Since we perform this operation times, the total time spent is .

(ii): Each edge relaxation operation (updating the distance) involves updating the priority queue, which takes time. Since there are edges, the total time spent is .

知识点

图论Dijkstra算法时间复杂度

解题技巧和信息

  1. Dijkstra 算法的限制:Dijkstra 算法不能处理带有负权边的图。
  2. 复杂度分析:使用普通数组时,Dijkstra 算法的时间复杂度为 ;使用优先队列时,时间复杂度为
  3. 边松弛操作:每次从源顶点到目标顶点更新路径长度时,都需要检查并更新最短路径以及顶点的优先队列。

重点词汇

  1. Shortest path 最短路径
  2. Weighted graph 加权图
  3. Priority queue 优先队列
  4. Edge relaxation 边松弛
  5. Computational complexity 计算复杂度

参考资料

  1. Introduction to Algorithms, 3rd Edition, Chapter 24: Single-Source Shortest Paths