MA214 Algorithms and Data Structures 2023/24 Exercises 9Python

Java Python MA214 Algorithms and Data Structures

2023/24

Exercises 9

(Kruskal’s algorithm, Dijkstra’s algorithm)

Exercise 9.1.  Kruskal’s algorithm                                                                                5 pts

Implement Kruskal’s minimum spanning tree algorithm in Python. The pseudocode for the algorithm is given in the course textbook. Differently from Prim’s algorithm, Kruskal’s algorithm returns the set of the edges of a minimum spanning tree. There-fore, will not use the predecessor links to capture the tree. The implementation of a graph data structure, Graph .py, is given on the course Moodle page. Import that file without changing the contents. Your submissions will be used together with a local copy of that file.

In this implementation, you will need to represent each edge as a triple (u, v, w), where (u, v) is the edge and w = w (u, v) is the weight of that edge. You may refresh on the use of tuples in Python at the follow MA214 Algorithms and Data Structures 2023/24 Exercises 9Python ing link: Tuples. You may also find sorting lists of tuples acording to a particular tuple entry useful. The examples at the following link show how to do that: Sorting.

A template file for you to complete is provided on Moodle. Please submit a single file MSTKruskal .py atthe submission link for this question.

Exercise 9.2.  Dijkstra’s algorithm                                                                               5 pts

(a) Run Dijkstra’s algorithm on the directed graph below, first using vertex s as the source and then using vertex z as the source. Use the examples from the lecture as a template.

(b) Suppose we change line 11 of Dijkstra’s algorithm to the following:

11                  while   len ( Q )  >  1

This change causes the while loop to execute |V| − 1 = n − 1 times instead of |V| times. Is this proposed algorithm correct?

Submit your solutions to this question as a PDF-file at the submission link for this question         

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值