《算法图解》第七章练习答案

《算法图解》第七章练习答案

7.1 使用狄克斯特拉算法

广度优先算法可以找出段数最少的路径,狄克斯特拉算法可以找出最快路径

步骤:

(1) 找出“最便宜”的节点,即可在最短时间内到达的节点。

(2) 更新该节点的邻居的开销。

(3) 重复这个过程,直到对图中的每个节点都这样做。

(4) 计算最终路径。

7.5 实现

练习:在下面的各个图中,从起点到终点的最短路径的总权重分别是多少?

《算法图解》第七章练习答案_第1张图片

程序后半段都没有差别,只有在一开始创建散列表时有区别,所以就习题A给出完整代码。

graph和parents散列表就按图的信息正常创建即可;costs中,与起点一步相连的正常赋值,其余赋“infinity”。

Exe A

# python
# Create graph Table
graph = {}
graph['start'] = {}
graph['start']['a'] = 5
graph['start']['b'] = 2

graph['a'] = {}
graph['a']['c'] = 4
graph['a']['d'] = 2

graph['b'] = {}
graph['b']['a'] = 8
graph['b']['d'] = 7

graph['c'] = {}
graph['c']['d'] = 6
graph['c']['fin'] = 3

graph['d'] = {}
graph['d']['fin'] = 1

graph['fin'] = {}  # fin doesn't have neighbour

# Create Costs Table
infinity = float('inf')
costs = {}
costs['a'] = 5
costs['b'] = 2
costs['c'] = infinity
costs['d'] = infinity
costs['fin'] = infinity


# Create Parents Table
parents = {}
parents['a'] = ['start', 'b']
parents['b'] = 'start'
parents['c'] = 'a'
parents['d'] = ['a', 'b', 'c']
parents['fin'] = None

processed = []


def find_lowest_cost_node(costs):
    lowest_cost = float('inf')
    lowest_cost_node = None
    # go through each node
    for node in costs:
        cost = costs[node]
        # If it's the lowest cost so far and hasn't been processed yet
        if cost< lowest_cost and node not in processed:
            # set it as the new lowest-cost node
            lowest_cost = cost
            lowest_cost_node = node
    return lowest_cost_node

# Find the lowest-cost node that you haven't processed yet
node = find_lowest_cost_node(costs)
# If you've processed all the nodes, this while loop is done
while node is not None:
    cost = costs[node]
    # Go through all the neighbors of this node
    neighbors = graph[node]
    for n in neighbors.keys():
        new_cost = cost + neighbors[n]
        # If it's cheaper to get to this neighbor by going through this node
        if costs[n] > new_cost:
            # update the cost for this node
            costs[n] = new_cost
            # This node becomes the new parent for this neighbor
            parents[n] = node
    # Mark the node as processed
    processed.append(node)
    node = find_lowest_cost_node(costs)

print("Cost from the start to each node:")
print(costs)

Exe B

# Creat graph Table
graph = {}
graph['start'] = {}
graph['start']['a'] = 10

graph['a'] = {}
graph['a']['b'] = 20

graph['b'] = {}
graph['b']['c'] = 1
graph['b']['fin'] = 30

graph['c'] = {}
graph['c']['a'] = 1

graph['fin'] = {}

# Create costs Table
infinity = float('inf')
costs = {}
costs['a'] = 10
costs['b'] = infinity
costs['c'] = infinity
costs['fin'] = infinity

# Create parents Table
parents = {}
parents['a'] = ['start', 'c']
parents['b'] = 'a'
parents['c'] = 'b'
parents['fin'] = None

processed = []

...

Exe C

# Create graph Table
graph = {}
graph['start'] = {}
graph['start']['a'] = 2
graph['start']['b'] = 2

graph['a'] = {}
graph['a']['c'] = 2
graph['a']['fin'] = 2

graph['b'] = {}
graph['b']['a'] = 2

graph['c'] = {}
graph['c']['b'] = -1
graph['c']['fin'] = 2

graph['fin'] = {}

# Create costs Table
infinity = float('inf')
costs = {}
costs['a'] = 2
costs['b'] = 2
costs['c'] = infinity
costs['fin'] = infinity

# Create parents Table
parents = {}
parents['a'] = ['start', 'b']
parents['b'] = ['start', 'c']
parents['c'] = 'a'
parents['fin'] = ['a', 'c']

processed = []

你可能感兴趣的:(《算法图解》,python,算法)