You are on page 1of 42

Artificial Intelligence

Presentation Topic:

Graph , DFS and BFS


Presented By

Tahira Yasmeen 04
Kashaf Noreen 40
Introduction
The objective of this Presentation is to provide a basic
introduction about graphs and the commonly used.
Algorithms used for traversing the graph, BFS and DFS.
Breadth First Search (BFS) and Depth First
Search (DFS) are the two popular algorithms asked in
most of the programming interviews.
This Presentation will help any beginner to get some basic
understanding about what graphs are, how
they are represented, graph traversals using BFS and DFS.
What is a Graph?
Graphs are one of the most interesting data structures in computer
science. Graphs and the trees are
somewhat similar by their structure. In fact, tree is derived from the
graph data structure. However there are two important differences
between trees and graphs.
Unlike trees, in graphs, a node can have many parents.
The link between the nodes may have values or weights.
Graphs are good in modeling real world problems like representing
cities which are connected by roads
and finding the paths between cities, modeling air traffic controller
system, etc. These kinds of problems
are hard to represent using simple tree structures.The following
example shows a very simple graph:
Graph

A,B,C,D,E,F
Graph
In the above graph, A,B,C,D,E,F are called nodes
and the connecting lines between these nodes are
called edges. The edges can be directed edges
which are shown by arrows; they can also be
weighted
edges in which some numbers are assigned to
them. Hence, a graph can be a directed/undirected
and weighted/un-weighted graph.
In this presentation, we will discuss undirected and
un-weighted graphs.
Tree searches
A tree search starts at the
1 root and explores nodes
from there, looking for a
goal node
2 3
For some problems, any
goal node is acceptable (10
4 5 6 7 or 13); for other problems,
you want a minimum-depth
goal node, that is, a goal
8 9 10 11 node nearest the root (only
13)
12 13

Goal nodes
Algorithms used for traversing
the graph:
Two most common graph traversal
algorithms are:

Breadth First Search (BFS)


Depth First Search (DFS)
Breadth-First Traversal
Similar to traversing binary tree level-by-
level
Nodes at each level
Visited from left to right
All nodes at any level i
Visited before visiting nodes at level i + one
Search down the levels
Breadth-First Traversal
This is a very different approach for traversing
the graph nodes. The aim of BFS algorithm is to
traverse
the graph as close as possible to the root node.
Queue is used in the implementation of the
breadth first
search. Lets see how BFS traversal works with
respect to the following graph:
Breadth-First Traversal

Level=0

Level=1

Level=2
A B C D E F
Breadth-First Traversal
If we do the breadth first traversal of the above
graph and print the visited node as the output, it
will
print the following output. A B C D E F. The
BFS visits the nodes level by level, so it will start
with
level 0 which is the root node, and then it moves
to the next levels which are B, C and D, then the
last
levels which are E and F.
Breadth-First Traversal
A

B C

D E F G

A B C D E F G
Breadth-First Search
Level=0
Queue:
A

Level=1 B C

Level=2
Current:
D E F G

A B C D E F G
Breadth-First Search
Queue:
A
A
B C

Current:
D E F G
Breadth-First Search
Queue:
A
A
B C

Current:
D E F G
A
Breadth-First Search
Queue:
A

B C

Current:
D E F G
A

A
Breadth-First Search
Queue:
A
B
B C

Current:
D E F G
A

A
Breadth-First Search
Queue:
A
C
B
B C

Current:
D E F G
A

A
Breadth-First Search
Queue:
A
C
B
B C

Current:
D E F G
B

A
Breadth-First Search
Queue:
A
C
B C

Current:
D E F G
B

A B
Breadth-First Search
Queue:
A
D
C
B C

Current:
D E F G
B

A B
Breadth-First Search
Queue:
A
E
D
B C
C
Current:
D E F G
B

A B
Breadth-First Search
Queue:
A
E
D
B C
C
Current:
D E F G
C

A B
Breadth-First Search
Queue:
A
E
D
B C

Current:
D E F G
C

A B C
Breadth-First Search
Queue:
A
F
E
B C
D
Current:
D E F G
C

A B C
Breadth-First Search
Queue:
A
G
F
B C
E
D
D E F G
Current:
C
A B C
Breadth-First Search
Queue:
A
G
F
B C
E
D
D E F G
Current:
D
A B C
Breadth-First Search
Queue:
A
G
F
B C
E

D E F G
Current:
D
A B C D
Breadth-First Search
Queue:
A
G
F
B C
E

D E F G
Current:
E
A B C D
Breadth-First Search
Queue:
A
G
F
B C

D E F G
Current:
E
A B C D E
Breadth-First Search
Queue:
A
G
F
B C

D E F G
Current:
F
A B C D E
Breadth-First Search
Queue:
A
G
B C

D E F G
Current:
F
A B C D E F
Breadth-First Search
Queue:
A
G
B C

D E F G
Current:
G
A B C D E F
Breadth-First Search
Queue:
A

B C

D E F G
Current:
G
A B C D E F G
Breadth-First Search

B C

D E F G

A B C D E F G
Depth First Search (DFS)
The aim of DFS algorithm is to traverse the
graph in such a way that it tries to go far from
the root
node. Stack is used in the implementation of the
depth first search. Lets see how depth first
search
works with respect to the following graph:
Depth-First Traversal
Stack

D
C

ABEFCD F

E
A B E F C D
B
A
Depth First Search (DFS)
As stated before, in DFS, nodes are visited by
going through the depth of the tree from the
starting
node. If we do the depth first traversal of the
above graph and print the visited node, it will be
A B E F C D.
DFS visits the root node and then its children
nodes until it reaches the end node, i.e. E and F
nodes, then moves up to the parent nodes.
Depth-First Traversal in Tree
A

B C

D E F G

A B D E C F G
BFS and DFS
DFS is depth first search
Start down one path
Example 1, 2, 3, 4,5, 6, 7, etc.
BFS is breath first search
Search down the levels
1, 2,7,8, 3,6,9,12
THANK`S
ANY
QUESTION

You might also like