Breadth-First Search 广度优先搜索

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

Breadth-First Search 广度优先搜索_第1张图片

  • Some details:

    • White:represented “undiscovered” vertices
    • Gray:represented “discovered” but not “processed” vertices
    • Black:represented “processed” vertices
  • Given a graph G = , 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
    Breadth-First Search 广度优先搜索_第2张图片

  • step 1: initialization
    Breadth-First Search 广度优先搜索_第3张图片

  • step 2
    Breadth-First Search 广度优先搜索_第4张图片

  • step 3
    Breadth-First Search 广度优先搜索_第5张图片

  • step 4
    Breadth-First Search 广度优先搜索_第6张图片

  • step 5
    Breadth-First Search 广度优先搜索_第7张图片

  • step 6
    Breadth-First Search 广度优先搜索_第8张图片

  • step 7
    Breadth-First Search 广度优先搜索_第9张图片

  • step 8
    Breadth-First Search 广度优先搜索_第10张图片

  • step 9
    Breadth-First Search 广度优先搜索_第11张图片

4. What does BFS produce

Breadth-First Search 广度优先搜索_第12张图片

5. Our goal

Breadth-First Search 广度优先搜索_第13张图片

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

你可能感兴趣的:(图论)