Breadth-First Search 广度优先搜索

版权声明:欢迎转载和交流。 https://blog.csdn.net/Hachi_Lin/article/details/88252376

1. The shortest-paths problem

  • Distance δ(s, v) : The length of the shortest path from s to v. For example δ(s, c)=2.
  • The problem:
    • Input: A graph G = (V, E) and a source vertex s∈V
    • Question: A shortest path from s to each vertex v ∈V and the distance δ(s, v) .

2. Algorithm

在这里插入图片描述

  • Some details:

    • White:represented “undiscovered” vertices
    • Gray:represented “discovered” but not “processed” vertices
    • Black:represented “processed” vertices
  • Given a graph G = <V,E>, the BFS return:

    • d[v]:proved to be the shortest distance from s to v
    • π[v]:the predecessor of v in the search, which can be used to derive a shortest path from s to vertex v
    • BFS acturally returns a shortest path tree in which the unique simple path from s to node v is a shortest path from s to v in the original graph.

3. Example of Breadth-First Search

  • vertex s and a graph G
    在这里插入图片描述

  • step 1: initialization
    在这里插入图片描述

  • step 2
    在这里插入图片描述

  • step 3
    在这里插入图片描述

  • step 4
    在这里插入图片描述

  • step 5
    在这里插入图片描述

  • step 6
    在这里插入图片描述

  • step 7
    在这里插入图片描述

  • step 8
    在这里插入图片描述

  • step 9
    在这里插入图片描述

4. What does BFS produce

在这里插入图片描述

5. Our goal

在这里插入图片描述

  • δ(s,v) = d[v]

猜你喜欢

转载自blog.csdn.net/Hachi_Lin/article/details/88252376