广度优先分析
我有 Cormen 的以下 BFS 函数。
定义从 s 到 v 的最短路径距离 path(s,v) 作为从顶点 s 到顶点 v 的任何路径中的最小边数,否则如果没有从 s 到 v 的路径。长度为 path 的路径从 s 到 v 的 (s,v) 被称为从 s 到 v 的最短路径。
以下是给定的引理
设 G = (V,E) 为有向图或无向图,并设 s 属于 V 为任意顶点。然后,对于任何边 (u, v) E,
path(s,v) <= path(s,u) + 1 。
我的问题是为什么我们在上面的公式中必须有<=,我教过“=”是可以的,谁能告诉我一个场景为什么我们需要<=?
下面是BFS算法
Leema 2 :
设 G = (V,E) 为有向图或无向图,并假设 BFS 在 G 上运行,给定源顶点 s 属于 V。然后在终止时,对于属于 V 的每个顶点 v,值 d[ v] 计算出由BFS满足d[v]>=path(s,v)。
证明:
我们对顶点被放入队列 Q 的次数进行归纳。我们的归纳假设是 d[v] >= path(s,v),因为所有 v 都属于 V。
归纳的基础是紧接着s之后的情况被放入BFS第8行的Q中。
归纳假设在这里成立,因为 d[s] = 0 = path(s, s) 且 d[v] = path (s, v),所有 v 都属于 V - {s}。
我的问题是作者所说的“我们对顶点放置在队列 Q 中的次数使用归纳法”是什么意思?它与归纳假设有何关系?
谢谢!
BFS(G,s)
1 for each vertex u V[G] - {s}
2 do color[u] WHITE
3 d[u]
4 [u] NIL
5 color[s] GRAY
6 d[s] 0
7 [s] NIL
8 Q {s}
9 while Q
10 do u head[Q]
11 for each v Adj[u]
12 do if color[v] = WHITE
13 then color[v] GRAY
14 d[v] d[u] + 1
15 [v] u
16 ENQUEUE(Q,v)
17 DEQUEUE(Q)
18 color[u] BLACK
I have the following BFS function from Cormen.
Definition of the shortest-path distance path(s,v) from s to v as the minimum number of edges in any path from vertex s to vertex v, or else if there is no path from s to v. A path of length path(s,v) from s to v is said to be a shortest path from s to v.
Following is lemma given
Let G = (V,E) be a directed or undirected graph, and let s belongs to V be an arbitrary vertex. Then, for any edge (u, v) E,
path(s,v) <= path(s,u) + 1 .
My question is why we have to have <= in above formula, i taught "=" is ok, can any one tell me one scenrio why we require <= ?
Below is BFS algorithm
Leema 2:
Let G = (V,E) be a directed or undirected graph, and suppose that BFS is run on G from a given source vertex s belongs to V. Then upon termination, for each vertex v belongs to V, the value d[v] computed by BFS satisfies d[v] >= path (s, v).
Proof:
We use induction on the number of times a vertex is placed in the queue Q. Our inductive hypothesis is that d[v] >= path(s,v) for all v belongs to V.
The basis of the induction is the situation immediately after s is placed in Q in line 8 of BFS.
The inductive hypothesis holds here, because d[s] = 0 = path(s, s) and d[v] = path (s, v) for all v belongs to V - {s}.
My question is what does author mean by "We use induction on the number of times a vertex is placed in the queue Q" ? and how it is related to inductive hypothesis?
Thanks!
BFS(G,s)
1 for each vertex u V[G] - {s}
2 do color[u] WHITE
3 d[u]
4 [u] NIL
5 color[s] GRAY
6 d[s] 0
7 [s] NIL
8 Q {s}
9 while Q
10 do u head[Q]
11 for each v Adj[u]
12 do if color[v] = WHITE
13 then color[v] GRAY
14 d[v] d[u] + 1
15 [v] u
16 ENQUEUE(Q,v)
17 DEQUEUE(Q)
18 color[u] BLACK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个问题,考虑一个只有三个顶点的完整图。在此图中,path(s,v) = path(s,u) + 1 是真的吗?
For your first question, consider a complete graph with only three vertices. In this graph is it true that path(s,v) = path(s,u) + 1 ?