You are on page 1of 37

INTRODUCTION TO

ARTIFICIAL INTELLIGENCE
FOR IT & NON-IT PROFESSIONALS
DFS VS BFS

• Depth-First Search DFS vs BFS Breadth-First Search


• Consider Simple Search Algorithm
• It will give you an idea about the sort of data structures used during
search, and the stop criteria for your search.

2
DFS VS BFS

• Let S be the Start State, ques called Q and Visited


1. Initialize Q with the start node Q=(S) as the only entry;
set Visited =(S)
2. If Q is empty, fail. Else pick node X from Q
3. If X is a goal, return X, we’ve reached the goal

3
DFS VS BFS

4. (Otherwise) Remove X from Q


5. Find all the children of state/node X that are not in Visited
6. Add these to Q; Add Children of X to Visited
7. Go to Step 2

4
DFS VS BFS

• Simple Search Algorithm


Applied to DFS Search
• Depth First Search dives into a tree deeper and deeper to find the
goal state.

5
DFS VS BFS

• Use the same Simple Search Algorithm to implement DFS by defining


our priority function as
• P(n) = 1/height(n)

6
DFS VS BFS

• The following sequence of diagrams will show you how DFS works on
a tree using the Simple Search Algorithm.

7
DFS: Example
S

A B

C D E F

G H

Q Visited
1
2
3
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2
3
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
DFS VS BFS

• BFS explores the breadth of the tree first and progresses downward
level by level.
• Now, we use the same Simple Search Algo to implement BFS by
keeping our priority function as
P(n) = height(n)
• Select node with Min value of P(n)

16
DFS VS BFS

• In BFS, we still give priority to the node/ element with minimum P(n)
• Node with the largest value of height will be at the maximum priority to
be picked from Q.
• Greater the depth/height greater the priority.
• Sequence of diagrams illustrate BFS using the Simple Search
Algorithm.

17
BFS: Example
S

A B

C D E F

G H

Q Visited
1
2
3
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2
3
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5
BFS: Example
S

A B

C D E F

G H

Q Visited
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6
BFS: Example
S

A B

C D E F

G H
Q Visited
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7
BFS: Example S

A B

C D E F

G H

Q Visited
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8
BFS: Example S

A B

C D E F

G H

Q Visited
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9
BFS: Example S

A B

C D E F

G H

Q Visited
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10
BFS: Example S

A B

C D E F

G H
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H

6 E,F,G,H S,A,B,C,D,E,F,G,H

7 F,G,H S,A,B,C,D,E,F,G,H

8 G,H S,A,B,C,D,E,F,G,H

9 H S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
DFS VS BFS

Problems with DFS and BFS


• DFS has small space requirements (linear in depth) but has major
problems:
DFS can run forever in search spaces with infinite length paths
DFS does not guarantee finding the shallowest goal

29
DFS VS BFS

• BFS guarantees finding the shallowest path even in presence of


infinite paths, but it has one great problem
• BFS requires a great deal of space (exponential in depth)
Progressive deepening.

30
DFS VS BFS

• Heuristically Informed Searches


• The basic idea of a heuristic search is instead of trying all possible
search paths, try and focus on paths that seem to be getting us closer
to your goal state using a “guide”.
• Heuristics are not always guaranteed to succeed, & provide correct
solution

31
DFS VS BFS

• Recall the mouse search for cheese


• Consider another example diagram shown
• The graph shows a map in which the numbers on the edges are the
distances between cities,
• For example, the distance between city S and city D is 3 and between
B and E is 4.

32
DFS VS BFS

33
DFS VS BFS

• Our goal is to reach city G starting from city S.

34
DFS VS BFS

35
DFS VS BFS

• Conclusion: Heuristics do help us reduce the search space, but it is


not guaranteed that we’ll always find a solution.
• The key lies in the fact that how do we use the heuristic.
• Consider a heuristic function.
• A heuristic function takes as input the heuristic and returns a number
corresponding to that heuristic.

36
DFS VS BFS

• Depending on our application we might give priority to either larger


numbers or smaller numbers.

37

You might also like