联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2024-10-06 01:22


Programming Task 1: Graphs in Python [10% of your final mark]

Deadline: Sunday 6 October 2024, 23:59 Wuxi Time

This is your first programming task of this module is about graphs and implementing Dijkstra’s algorithm. You will submit a SINGLE PYTHON FILE (main.py) found in the Task1.zip file on Moodle. Your job is to modify the main.py file only. Importing any libraries that are not already imported in the main.py file is STRICTLY FORBIDDEN and will be considered as cheating.

We operate a strict zero tolerance policy on cheating and plagiarism and late penalties apply. Any deviation from the format specified above will result in you receiving the mark of zero.

THE TASK

What Are Graphs?

A weighted graph is defined as ?? = (??, ??, ??) where ?? is the set of nodes, ?? ? ?? × ?? is the set of edges where (??1, ??2) ∈ ?? if and only if (??2,??1) ∈ ??. Finally, ??: ?? → N is the weighting function thatspecifiesthelengthofeachedge.Further,wehave??(??1,??2)= ??(??2,??1).

For example, below we have the representation of a graph ?? = (??, ??, ??) where:

? ?? = {0,1,2},

? ??={(0,1),(1,0),(1,2),(2,1)},

? ??(0,1) = ??(1,0) = 3 and ??(1,2) = ??(2,1) = 4.

What is an adjacency matrix?

An adjacency matrix of a weighted graph ?? = (??, ??, ??) is a |??| by |??| matrix ?? such that ?????? = 0 for ?? < |??| and for ?? < ?? < |??| we have

          0

3

1

4

2

  ??(??,??) ???? (?? ,?? ) ∈ ?? ????

m???? = {

∞ ????h????????????

For example, the graph above has the below adjacency matrix:

03∞ ??=(3 0 4)

∞40

Your Python Implementation

Open the main.py file from Task1.zip. THIS IS THE ONLY FILE YOU SHOULD CHANGE ANF UPLOAD TO MOODLE. ONLY UPLOAD THIS FILE AND DO NOT ADD ANY NEW IMPORT STATEMENTS TO IT. In order to test your score, simply run the main.py file.

(1) In order to obtain the first five marks:

Usingtheinf importedfrommath,modifythe__init__,addEdge,andgetAdjacencyMatrix methods so that the initialisation method creates a graph with noOfNodes nodes, assume they are called [0, 1, ..., noOfNodes-1] and no edges. The addEdge method adds an edge between x and y of length weight. For example, you would write the below code to create the graph on the previous page.

And then the below code

g.getAdjacencyMatrix() would result in

[[0, 3, inf], [3, 0, 4], [inf, 4, 0]]

(2) In order to obtain the second five marks:

Modify the shortestRoute method so that it returns a dictionary with the key distance corresponding to the value of the total distance of the shortest route between the nodes x and y and the key path corresponding to the nodes in that route in order. You may find it useful to search for Dijkstra’s algorithm. For example, the g.shortestRoute(2,0) call for the g above would result in:

{"distance":7, "path":[2,1,0]}

    g = Graph(3)

g.addEdge(0,1,3) g.addEdge(1,2,4)

   


相关文章

【上一篇】:到头了
【下一篇】:没有了

版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp