Lecture 25: Shortest Paths| CS 61B Data Structures, Spring 2019

Categories

  • Summary So Far: DFS vs. BFS
  • Dijkstra’s Algorithm
  • Dijkstra’s Correctness and Runtime
  • A*
  • A* Heuristics (188 preview)

在这里插入图片描述
demo1
demo2
在这里插入图片描述
在这里插入图片描述>As we discussed last time, BFS would not be a good choice for a google maps style navigation application.因为其只考虑哪条路径有最少的edge,而忽略了每条edge的具体长度。

Dijkstra’s Algorithm

Problem: Single Source Shortest Paths

  • Goal: Find the shortest paths from source vertex s to every other vertex.
    在这里插入图片描述

  • Observation: Solution will always be a tree.

    • Can think of as the union of the shortest paths to all vertices.

寻找图中某一点 A到其他节点的最短路径,需要该图中的以A点为root 的Shortest Paths Tree ,即可找到从该SPT的root到其树上某个节点的最短距离。

SPT Edge Count、

在这里插入图片描述

2 Incrrect Algorithm

Bad algorithm #1: Perform a depth first search. When you visit v:

Bad algorithm #2: Perform a depth first search. When you visit v:

在这里插入图片描述

Dijkstra’s Algorithm Demo

Intuitive

在这里插入图片描述

Algorithm
  • Insert all vertices into fringe PQ, storing vertices in order of distance from source.
  • Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.

closest:那条路径的距离最短,则选择哪一条路径。
在这里插入图片描述
在这里插入图片描述
relax all edges pointing from v.不包括已经在SPT树上的节点。

For each edge from v to w, add edge to the SPT only if that edge yields better distance.

Dijkstra’s Correctness and Runtime

Dijkstra’s:

  • PQ.add(source, 0)
  • For other vertices v, PQ.add(v, infinity)
  • While PQ is not empty:
    • p = PQ.removeSmallest()
    • Relax all edges from p

Relaxing an edge p → q with weight w:

  • If distTo[p] + w < distTo[q]:
    • distTo[q] = distTo[p] + w
    • edgeTo[q] = p
    • PQ.changePriority(q, distTo[q])

distTo[p]:途中某一点粉红色的数字,代表从出发点到该节点的距离大小,在不考虑已经在SPT树中节点的情况下,将所有p的邻居一 一进行Relaxing.

Key invariants:

  • edgeTo[v] is the best known predecessor of v.
  • distTo[v] is the best known total distance from source to v.
  • PQ contains all unvisited vertices in order of distTo.

Important properties:

  • Always visits vertices in order of total distance from source.
  • Relaxation always fails on edges to visited (white) vertices.
Guaranteed Optimality

Dijkstra’s Algorithm:

  • Visit vertices in order of best-known distance from source. On visit, relax every edge from the visited vertex.

Dijkstra’s is guaranteed to return a correct result if all edges are non-negative.

Dijkstra’s is guaranteed to be optimal so long as there are no negative edges.

  • Proof relies on the property that relaxation always fails on edges to visited (white) vertices.

Proof sketch: Assume all edges have non-negative weights.

  • At start, distTo[source] = 0, which is optimal.
  • After relaxing all edges from source, let vertex v1 be the vertex with minimum weight, i.e. that is closest to the source. Claim: distTo[v1] is optimal, and thus future relaxations will fail. Why?
    • distTo[p] ≥ distTo[v1] for all p, therefore
    • distTo[p] + w ≥ distTo[v1]
  • Can use induction to prove that this holds for all vertices after dequeuing.
    在这里插入图片描述
Dijkstra’s Algorithm Runtime

在这里插入图片描述

A*

The Problem with Dijkstra’s

Dijkstra’s will explore every place within nearly two thousand miles of Denver before it locates NYC.

Dijkstra’s 会漫无目的的全覆盖式的探索整张地图,构建包括与源节点相连接的包括所有节点的Shortest Path Tree,对于仅仅寻找两个点之间的路径,有很大浪费

Introducing A*

Simple idea:

  • Visit vertices in order of d(Denver, v) + h(v, goal), where h(v, goal) is an estimate(估计值) of the distance from v to our goal NYC.(该估计值由AI得出)

d(Denver, v) is just as same as d(source, v) in Dijkstra’s

  • In other words, look at some location v if:
    • We know already know the fastest way to reach v.
    • AND we suspect that v is also the fastest way to NYC taking into account the time to get to v.
A* Demo, with s = 0, goal = 6.

在这里插入图片描述

在这里插入图片描述

A* Heuristic Example

How do we get our estimate?(通过机器学习得到)

  • Estimate is an arbitrary heuristic h(v, goal).
  • heuristic: “using experience to learn and improve”
  • Doesn’t have to be perfect!
A* vs. Dijkstra’s Algorithm

Note, if edge weights are all equal (as here), Dijkstra’s algorithm is just breadth first search.

This is a good tool for understanding distinction between order in which nodes are visited by the algorithm vs. the order in which they appear on the shortest path.
Unless you’re really lucky, vastly more nodes are visited than exist on the shortest path.

Sammary

在这里插入图片描述

Graph Problems so far

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值