[go: up one dir, main page]

0% found this document useful (0 votes)
2 views44 pages

Algo Bfs - Dfs (Slide 3)

The document provides an overview of graph search algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). It explains the terminology, implementation, time complexity, and applications of BFS, as well as the process and example of DFS. The time complexity for BFS is O(|V| + |E|), and both algorithms are used in various applications such as web crawling and GPS navigation.

Uploaded by

ksthelion489
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views44 pages

Algo Bfs - Dfs (Slide 3)

The document provides an overview of graph search algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). It explains the terminology, implementation, time complexity, and applications of BFS, as well as the process and example of DFS. The time complexity for BFS is O(|V| + |E|), and both algorithms are used in various applications such as web crawling and GPS navigation.

Uploaded by

ksthelion489
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Algorithm Analysis and Design

Rifat Ahommed
Lecturer
Department of CSE
SEU
Graph Search

BFS
Terminology

● Source

● Parent-child

● Distance

● Path

● Color
1. While
2. Gray
3. Black
Breadth-First Search: Example

r s t u

   

   
v w x y
Breadth-First Search: Example

r s t u

 0  

   
v w x y

Q: s
Breadth-First Search: Example

r s t u

1 0  

 1  
v w x y

Q: w r
Breadth-First Search: Example

r s t u

1 0 2 

 1 2 
v w x y

Q: r t x
Breadth-First Search: Example

r s t u

1 0 2 

2 1 2 
v w x y

Q: t x v
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 
v w x y

Q: x v u
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: v u y
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: u y
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: y
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: Ø
Breadth-First Search Algorithm
Implementation

1. Make Adjacency List or Adjacency matrix


2. Take four Array
i. Color
ii. Distance
iii. Parent
iv. Queue

3. Make a Function named BFS and send the


graph and source vertex as parameter
Example
Example
Example
Example
Example
Example
Finally
Complexity
• V is the number of vertices, E the number of
edges.
• Each vertex is enqueued and dequeued at most
once.
• Scanning for all adjacent vertices
takes O(|E|) time, since sum of lengths of
adjacency lists is |E|.
• Hence The Time Complexity of BFS Gives
a O(|V|+|E|) time complexity.
Applications

• Web crawling
• Social Networking
• Network broadcasting
• Garbage collection
• Model checking
• Solving puzzle & games
• GPS navigation system etc.
Depth-First Search

● Depth-first search is another strategy for


exploring a graph
■ Explore “deeper” in the graph whenever possible
■ Edges are explored out of the most recently
discovered vertex v that still has unexplored edges
■ When all of v’s edges have been explored,
backtrack to the vertex from which v was
discovered
Depth-First Search

● Vertices initially colored white


● Then colored gray when discovered
● Then black when finished
DFS Example
source
vertex
DFS Example
source
vertex
d f
1 | | |

| |

| | |
DFS Example
source
vertex
d f
1 | | |

2 | |

| | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |

3 | 4 5 | 6 |

What is the structure of the grey vertices?


What do they represent?
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|15
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15
DFS Algorithm

DFS(G) DFS_Visit(u)
{ u->color = GREY;
for each vertex u  G->V time = time+1;
u->d = time;
{
for each v  u->Adj[]
u->color = WHITE;
{
}
if (v->color == WHITE)
time = 0;
DFS_Visit(v);
for each vertex u  G->V
}
{
u->color = BLACK;
if (u->color == WHITE)
time = time+1;
DFS_Visit(u);
u->f = time;
}
}
}

You might also like