# BFS revisited by nyut545e2

VIEWS: 6 PAGES: 17

• pg 1
```									                                                                                 1

BFS revisited

Algorithm bfs(G)                        Algorithm bfsFromVertex(G, v)
1.   Initialise Boolean array visited   1.   visited [v] = true
by setting all entries to false
2.   Q.enqueue(v)
2.   Initialise Queue Q
3.   while not Q.isEmpty() do
3.   for all v ∈ V do
4.      v ← Q.dequeue()
4.      if visited [v] = false then
5.      for all w adjacent to v do
5.         bfsFromVertex(G, v)
6.         if visited [w] = false then
7.            visited [w] = true
8.            Q.enqueue(w)
2

Analysis of BFS
G = (V, E) graph with n vertices and m edges

• bfs without time spent in bfsFromVertex: Θ(n)

• Overall time spent in bfsFromVertex:
– Lines 1 and 2: O(n)
– Lines 3–8: executed once for each vertex v, requires time Θ(out-degree(v))

• Thus

Tbfs(n, m) = Θ(n) + O(n) +         Θ(out-degree(v)) = Θ(n + m).
v∈V
3

Shortest Paths

G = (V, E) graph, v, w ∈ V

• A shortest path from v to w is a path from v to w of minimum length (i.e.,
with the minimum number of edges).

• The distance from v to w is the length of a shortest path from v to w.

• For every vertex v there is a shortest path tree rooted at v, i.e., a tree whose
vertices are all vertices of G reachable from v that has the property that all
paths in this tree from v to a vertex w are shortest paths from v to w.
4

Shortest Path Problems
Single Pair Shortest Path (SPSP)

• Input: Graph G = (V, E), vertices v, w ∈ V
• Problem: Compute shortest path from v to w

Single Source Shortest Paths (SSSP)

• Input: Graph G = (V, E), vertex v ∈ V
• Problem: Compute shortest paths from v to all other vertices

All Pairs Shortest Paths (APSP)

• Input: Graph G = (V, E)
• Problem: Compute shortest paths between all pairs of vertices
5

Representations of Distances and Shortest Paths

Distances

• Single Source: Distance array
• All Pairs: Distance matrix

Shortest path trees

• Single Source: Parent array
• All Pairs: Parent matrix
6

Single Source Shortest Paths with BFS
Algorithm bfsSSSP(G, v)
1.   Initialise array distance by setting all entries to ∞
2.   Initialise array parent by setting all entries to nil
3.   Initialise Queue Q
4.   distance[v] = 0
5.   Q.enqueue(v)
6.   while not Q.isEmpty() do
7.       v ← Q.dequeue()
8.       for all w adjacent to v do
9.           if distance[w] = ∞ then
10.               parent[w] = v
11.               distance[w] = distance[v] + 1
12.               Q.enqueue(w)
13.   return parent
7

Analysis

• bfsSSSP:
Θ(n + m)

• Variant for all pairs shortest paths:

Θ(n(n + m)) ≤ O(n3)
8

A matrix-based approach to APSP
G = (V, E) graph with V = {0, 1, . . . , n − 1}

Distance Matrix: D = (dij )0≤i,j≤n−1 with

length of shortest path from i to j   if j is reachable from i
dij =
∞                                     otherwise

Our algorithms will only compute the distance matrix, a parent matrix can be
computed in time Θ(n3) from the distance matrix.
Lemma: For all i, j,
dij ≤ n − 1.
9

Distances up to k
For k ≥ 0, let D ≤k = (d≤k )0≤i,j≤n−1, where
ij

dij    if dij ≤ k,
d≤k =
ij
∞      otherwise.

Then

• D≤1 is the adjacency matrix with 0 replaced by ∞ and 0s on the diagonal.
• D≤n−1 = D.
• For k ≥ 1,
≤(k−1)
d≤k =
ij       min     di        + d1j .
0≤ ≤n−1
10

A simple algorithm for computing a distance matrix

Algorithm simpleDist(G)
1.   Compute D ≤1
2.   for k = 2 to n − 1 do
3.      for i = 0 to n − 1 do
4.         for j = 0 to n − 1 do
5.             d≤k ← ∞
ij
6.             for = 0 to n − 1 do
≤(k−1)
7.                 if di      + d≤1 < d≤k then
j    ij
≤(k−1)
8.                   d≤k ← di
ij              + d≤1
j
9.   return D ≤n−1

Time Complexity: Θ(n4)
11

The Floyd-Warshall Algorithm
(k)
For k ≥ 0, let D (k) = (dij )0≤i,j≤n−1, where

minimum length of a path from i to j

(k)
dij    =      whose interior vertices are all in {0, . . . , k}     if such a path exists,

∞                                                        otherwise.


Then

• D(−1) = D≤1
• D(n−1) = D.
• For k ≥ 0,
(k)              (k−1)        (k−1)      (k−1)
dij = min       dij       ,   dik      + dkj
12

Example

                         
0       1                0   ∞   1   ∞   1 1 ∞

   1   0   ∞   ∞   ∞ ∞ ∞ 


   ∞   1   0   ∞   ∞ 1 ∞ 

2   3   D(−1)   =
   ∞   1   ∞   0   ∞ ∞ 1 


   1   ∞   ∞   ∞   0 1 ∞ 

   ∞   ∞   ∞   ∞   ∞ 0 ∞ 
4   5   6                ∞   ∞   ∞   1   ∞ 1 0
13

Example (cont’d)

                     
0   ∞ 1 ∞ 1 1 ∞

   1   0 2 ∞ 2 2 ∞ 

   ∞   1 0 ∞ ∞ 1 ∞ 
D(0)
                   
=
   ∞   1 ∞ 0 ∞ ∞ 1 


   1   ∞ 2 ∞ 0 1 ∞ 

   ∞   ∞ ∞ ∞ ∞ 0 ∞ 
∞   ∞ ∞ 1 ∞ 1 0
                    
0   ∞ 1   ∞ 1 1 ∞

   1   0 2   ∞ 2 2 ∞ 

   2   1 0   ∞ 3 1 ∞ 
D(1)
                     
=
   2   1 3   0 3 3 1 


   1   ∞ 2   ∞ 0 1 ∞ 

   ∞   ∞ ∞   ∞ ∞ 0 ∞ 
∞   ∞ ∞   1 ∞ 1 0
14

Example (cont’d)

                       
0 2 1   ∞ 1 1   ∞

   1 0 2   ∞ 2 2   ∞   

   2 1 0   ∞ 3 1   ∞   
D(2)
                       
=
   2 1 3   0 3 3   1   


   1 3 2   ∞ 0 1   ∞   

   ∞ ∞ ∞   ∞ ∞ 0   ∞   
∞ ∞ ∞   1 ∞ 1   0
                       
0 2 1   ∞ 1 1   ∞

   1 0 2   ∞ 2 2   ∞   

   2 1 0   ∞ 3 1   ∞   
D(3)
                       
=
   2 1 3   0 3 3   1   


   1 3 2   ∞ 0 1   ∞   

   ∞ ∞ ∞   ∞ ∞ 0   ∞   
3 2 4   1 4 1   0
15

Example (cont’d)

D(5) = D(4) = D(3)

                        
0 2 1      ∞ 1 1 ∞

   1 0 2      ∞ 2 2 ∞ 

   2 1 0      ∞ 3 1 ∞ 
D(6)
                      
=
   2 1 3      0 3 2 1 


   1 3 2      ∞ 0 1 ∞ 

   ∞ ∞ ∞      ∞ ∞ 0 ∞ 
3 2 4      1 4 1 0
16

The Floyd-Warshall Algorithm

Algorithm ﬂoydWarshall(G)
1.   Compute D (−1)
2.   for k = 0 to n − 1 do
3.      for i = 0 to n − 1 do
4.         for j = 0 to n − 1 do
(k)    (k−1)
5.             dij ← dij
(k−1)      (k−1)      (k−1)
6.            if dik      + dkj      ≥ dij      then
(k)      (k−1)
7.               dij ← dij
8.            else
(k)  (k−1)    (k−1)
9.               dij ← dik    + dkj
10.   return D (n−1)

Time Complexity: Θ(n3)
17

Algorithms and Data Structures in CS2
1. Analysing Algorithms
2. Asymptotic Growth Rates and the Big-O Notation
3. Sequential Data Structures
4. Hash Tables
5. Balanced Search Trees
6. Priority Queues and Heaps
7. Mergesort and the Divide-and-Conquer Technique
8. Heapsort and Quicksort
9. Graphs
10. Depth-First Search and Topological Sorting
11. Breadth-First Search and Shortest Paths

```
To top