DFS & BFS Graph

26
Identity MD.MAHFUZUL YAMIN ID:141-15-3429 SEC:F DEPARTMENT OF CSE DAFFODIL INTERNATIONAL UNIVERSITY

Transcript of DFS & BFS Graph

Page 1: DFS & BFS Graph

Identity

MD.MAHFUZUL YAMINID:141-15-3429SEC:FDEPARTMENT OF CSE DAFFODIL INTERNATIONAL UNIVERSITY

Page 2: DFS & BFS Graph

Topic

Depth-First SearchThink Stack

Breadth-First SearchThink Queue

Page 3: DFS & BFS Graph

Depth-First Search Search “deeper” in the graph whenever possible Edges are explored out of the most recently

discovered vertex v that still has unexplored edges

• After all edges of v have been explored, the search “backtracks” from the parent of v

• The process continues until all vertices reachable from the original source have been discovered

• If undiscovered vertices remain, choose one of them as a new source and repeat the search from that vertex

• DFS creates a “depth-first forest”

1 2

5 4

3

Page 4: DFS & BFS Graph

Depth-first search: Directed graphs

The algorithm is essentially the same as for undirected graphs, the difference residing in the interpretation of the word "adjacent". 

In a directed graph, node w is adjacent to node v if the directed edge (v, w) exists. 

If (v, w) exists but (w, v) does not, then w is adjacent to v but v is not adjacent to w. 

With this change of interpretation the procedures dfs and search apply equally well in the case of a directed graph.

Page 5: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

sourcevertex

Page 6: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| | |

| |

sourcevertex d f

Page 7: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| | |

2 | |

sourcevertex d f

Page 8: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| | 3 |

2 | |

sourcevertex d f

Page 9: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| | 3 | 4

2 | |

sourcevertex d f

Page 10: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| 5 | 3 | 4

2 | |

sourcevertex d f

Page 11: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| 5 | 63 | 4

2 | |

sourcevertex d f

Page 12: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | | |

| 5 | 63 | 4

2 | 7 |

sourcevertex d f

Page 13: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | 8 | |

| 5 | 63 | 4

2 | 7 |

sourcevertex d f

Page 14: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |

sourcevertex d f

Page 15: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 16: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 | 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 17: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 |12 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 18: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 |12 8 |11 13|

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 19: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 |12 8 |11 13|

14| 5 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 20: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 |12 8 |11 13|

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 21: DFS & BFS Graph

DFS Example

Data Structure and Algorithm

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex d f

Page 22: DFS & BFS Graph

All vertex are visited

Page 23: DFS & BFS Graph

Breadth-first search

23

• In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. • Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

Page 24: DFS & BFS Graph

Adjacency Matrix

Adjacency List

Page 25: DFS & BFS Graph

BFS: Start with Node 5

71

5

4

3

2

6

5 1 2 0 4 3 7 6

0

Page 26: DFS & BFS Graph

That’s all