Homework13 Finding the Cheapest Path查找最便宜路径_美国留学生作业assignment



python find cheapest path查找成本最低路径
python find cheapest path查找成本最低路径

Problem Setting

You are given a graph (𝑉,𝐸)(V,E), where each vertex 𝑥𝑉x∈V has a set of successors 𝑠(𝑥)s(x). There is one goal vertex 𝑔𝑉g∈V, that one must try to reach. The cost of visiting a vertex 𝑥𝑉x∈V is 𝑐(𝑥)>0c(x)>0.

The goal of the homework is to find, from every graph vertex 𝑥𝑉x∈V, the path to reach the goal 𝑔𝑉g∈V with minumum cost, and to compute for each vertex 𝑥𝑉x∈V its value 𝑣(𝑥)v∗(x), which corresponds to the minimum cost of reaching the goal.

You can compute 𝑣(𝑥)v∗(x) for every 𝑥𝑉x∈V via dynamic programming. First, you set:

𝑣(𝑥)={0+if 𝑥=𝑔;if 𝑥𝑔.(1)v(x)={0if x=g;+∞if x≠g.(1)

Basically, this encodes the fact that if you are at 𝑔g, you are at the goal, and you pay nothing. If you are not at 𝑔g, as we have not yet explored any path, the cost is infinite, as you don’t know how to go to 𝑔g (yet) .

Then, for every 𝑥𝑔x≠g, you update the cost 𝑣(𝑥)v(x) of 𝑥x via:


This because, to reach 𝑔g from 𝑥𝑔x≠g, one must first pay the price 𝑐(𝑥)c(x) of being at 𝑥x, and then one must go to a successor 𝑦y of 𝑔g. If the cost of then reaching 𝑔g from 𝑦y is 𝑣(𝑦)v(y), the total cost 𝑣(𝑥)v(x) from 𝑥x is 𝑐(𝑥)+𝑣(𝑦)c(x)+v(y). If 𝑥x has many successors, it is convenient to choose the successor with minimum cost: hence the minmin in the above equation.

So one way of solving the problem is this.

Initially, set 𝑣(𝑥)v(x) via (1). Then, repeat:

  • Update the value 𝑣(𝑥)v(x) at each 𝑥𝑔x≠g via (2)

Until nothing changes when the update is done.

The last part of the problem consists in finding a minimum-cost path to the goal. But this is easy to do: all you need to do is, when you update the costs 𝑣(𝑥)v(x) of a vertex via (2), you remember which successor 𝑦𝑠(𝑥)y∈s(x) gave you the minimum value (if there is more than one 𝑦𝑠(𝑥)y∈s(x) that gives you the minimum value, pick one of them at random). Call this the optimal successor 𝑏(𝑥)b(x) (for, “best at 𝑥x“) of 𝑥x:


Then, to reach 𝑔g with minimum cost, you simply take the edge from 𝑥x to 𝑦1=𝑏(𝑥)y1=b(x), then to 𝑦2=𝑏(𝑦1)y2=b(y1), and so forth, until you reach 𝑔g. That is, you do not need to remember from each state its path to 𝑔g. You just need to remember the best successor of every state: following this chain of best successors will lead you to 𝑔g with minimum cost.

Here is the representation of a vertex, including its cost 𝑐()c(⋅), value 𝑣()v(⋅), and best successor 𝑏()b(⋅).

class Vertex(object):
    """This represents a vertex of the graph."""

    def __init__(self, cost, name=""):
        assert cost > 0
        assert len(name) > 0
        self.cost = cost
        self.name = name
        # This will be computed later
        self.value = None
        self.best_successor = None

    def __hash__(self):
        return hash(self.name)

    def __repr__(self):
        return "{}(c={},v={})".format(self.name, self.cost, self.value)

We define infinity, to implement (1). Yes, you can represent infinity in Python.

INFINITE = float("inf")

Question 1.

Here is our definition of the graph. In it, you need to complete the compute_values and best_path methods.

  • The compute_values method should perform the computation of the minimum cost for reaching the goal from each vertex, and should update x.value and x.best_successor for every vertex.
  • The best_path method should return the best path from a vertex to the goal, including both the initial vertex, and the goal vertex.
from collections import defaultdict

class Graph(object):

    def __init__(self):
        self.vertices = set()
        self.successors = defaultdict(set)
        self.goal = None # We will set this later. 

    def add_vertex(self, x, successors):
        """Adds a vertex x, with a specified set of successors."""
        # First, we want to make sure that both x and its successors are in the set
        # of graph vertices. 
        # Then, we want to keep track of the successors of x. 
        self.successors[x] = successors

    def add_goal(self, x):
        """Adding a goal is similar to adding a vertex, except that the goal has 
        no successors (you have already arrived!). """
        self.goal = x
        self.add_vertex(x, set())

    def compute_values(self):
        """This function should compute the values x.value for each vertex 
        x of the graph, along with the best successor x.best_successor of each 
        # This can be done in about a dozen lines of code.
        ### YOUR CODE HERE

    def best_path(self, x):
        """This function should output a list of vertices, starting at x, 
        and ending at the goal vertex, that consists of a minimum-cost path
        to the goal."""
        # This can be done in 5-6 lines of code. 
        ### YOUR CODE HERE

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » Homework13 Finding the Cheapest Path查找最便宜路径_美国留学生作业assignment


李兴球博客 风火轮编程主页