代做Budget Holiday I调试R语言程序

- 首页 >> CS

Budget Holiday I

This is a regular task. You must submit a PDF, which can be produced using the LATEX template on Moodle, exported from a word processor, hand-written or any other method.

In this task, we’ll look at the following problem:

You are going on a holiday to to Austria, but are on a limited budget. Austria has n towns, each with an airport.

Each town t has a cost of Arrive(t) to enter Austria via that airport, and a cost of Depart(t) to leave from there.

There are also m roads between the towns.

• Each road r has an associated cost Toll(r).

• It is possible to reach any town from any other town by a sequence of roads. In other words, if a graph is constructed with vertices and edges corresponding to towns and roads, the graph is guaranteed to be connected.

A holiday is a path through Austria where you arrive at a town by plane, visit zero or more other towns connected by roads, then leave by plane (possibly from the town where you started). You want to determine the cheapest cost of a holiday to Austria.

In this example, the cheapest holiday is to enter Austria at town a, travel to b then c, and leave via c. The total cost is Arrive(a) + Toll(a ↔ b) + Toll(b ↔ c) + Depart(c) = 2 + 1 + 2 + 1 = 6.

It is very expensive to arrive in or depart from town d, and also to visit it by road. A holiday does not have to include all towns.

(a) A simple way to solve this problem involves applying Dijkstra’s algorithm starting from each town. Explain why this method solves the problem, then analyse the time complexity.

(b) We would like to improve upon the approach from (a). It is possible to construct a new graph and get the solution by running Dijkstra’s algorithm only once. Explain how to construct a graph based on the map of Austria such that the minimum cost of a holiday is equal to the length of the shortest path between two specified vertices. Analyse the time complexity of this approach.

Hint: You might want to add additional edges and vertices to the original graph; you might want to include arrival and departure costs to ensure that paths between your elected vertices are valid holidays.

Constructing a new graph so that we can apply a known algorithm is a common technique, so keep it in mind for future problems!

Rubric.

(a) Briefly explain how we can find the cost of the cheapest holiday using this method. Analyse the time complexity in terms of n (the number of towns) and m (the number of roads).

Expected length: a short paragraph.

(b) Describe how to construct the graph. Clearly identify the vertices, the edges, and the weights of each edge in your graph. Specify two particular vertices, and briefly explain why the shortest path between them represents the cheapest holiday. Analyse the time complexity in terms of m and n.

Expected length: less than half a page.

Notes.

Part (b) will probably be quite challenging for students since we are introducing it before the Flow module. If students are struggling and you would like to provide hints, try to get them to think about the fact that the holiday actually starts at home, not at any of the towns in Austria. This might guide them towards the idea of adding a vertex for starting “at home”, and another vertex for ending “at home”.

Sample Solution.

(a) We can run Dijkstra’s algorithm n times to find the shortest path between every pair of towns, and then iterate over all pairs and choose the pair that minimises Arrive u+d(u, v) + Depart v.

The n applications of Dijkstra’s algorithm will take O(nm log n) time, and iterating over all pairs will take O(n 2 ) time, giving a total time complexity of O(nm log n).

(b) Construct the graph as follows:

• Create a vertex ‘Start’, connected to each town t with an edge with weight Arrive(t)

• Create a vertex ‘End’, connected to each town t with an edge with weight Depart(t)

• Use Dijkstra’s algorithm to find the single-source shortest path from Start to End. The length of this path is the cost of the cheapest holiday.

A path in G from ‘Start’ to ‘End’ is made up of an arrival cost, a sequence of tolls be-tween connecting towns, and then some departure cost. The cost of the cheapest holiday is therefore given by the length of the shortest path from s to e.

The graph will have m + 2n = O(m) edges and n + 2 = O(n) vertices, so the total run-time will be O(m log n) from using Dijkstra’s algorithm.





站长地图