Page 3
ALGORITHM
SEARCH INTRO
What is Search?
Page 4
ALGORITHM
SEARCH INTRO
1 3 4 6 7 8 10 13 14
When Dataset is Sorted
Page 5
ALGORITHM
SEARCH INTRO
Binary Search
Page 6
ALGORITHM
SEARCH INTRO
Dataset not Sorted
4 3 8 6 7 13 10 14 9
Page 7
ALGORITHM
SEARCH INTRO
Search Process can Produce additional Data
4 3 8
6
7 13 10 14 9
1 11
3
Page 8
ALGORITHM
SEARCH INTRO
State space Tree
Page 9
ALGORITHM
SEARCH INTRO
Three Things to Decide
STATE STRUCTURE
SEARCH ORDER
ENDCONDITION
Page 10
ALGORITHM
SEARCH DFS
DEPTHFIRST
SEARCH
Page 11
ALGORITHM
SEARCH DFS
S
E
Is there way out?
Page 12
ALGORITHM
SEARCH DFS
S
E
Is there way out?
STATE STRUCTURE
x y
Page 13
ALGORITHM
SEARCH DFS
S
E
Is there way out?
SEARCHORDER
DFS
Page 14
ALGORITHM
SEARCH DFS
S
E
Is there way out?
ENDCONDITION
(X,Y) = E
Page 15
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
Page 16
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
Page 17
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
2,2
Page 18
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
2,2
3,2
Page 19
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
2,2
3,2
3,3
Page 20
ALGORITHM
SEARCH DFS
S
E
Is there way out?
3,3
3,4
Page 21
ALGORITHM
SEARCH DFS
S
E
Is there way out?
3,3
3,4
3,5
Page 22
ALGORITHM
SEARCH DFS
S
E
Is there way out?
3,3
3,4
3,5
4,5
Page 23
ALGORITHM
SEARCH DFS
S
E
Is there way out? YES
3,3
3,4
3,5
4,5
E
Page 24
ALGORITHM
SEARCH DFS
S
E
Is there way out? YES
S
1,2
2,2
3,2
3,3
2,1
Page 25
ALGORITHM
SEARCH DFS
Simple DFS prototype logic
function start
EndCondition
child function call
child function call
…
Return
Page 26
ALGORITHM
SEARCH DFS
function start
EndCondition
Child function call
child function call
…
Return (x,y)==E or found
process(x+1,y)
process(x,y+1)
process(x-1,y)
process(x,y-1)
Simple DFS prototype logic
Page 27
ALGORITHM
SEARCH DFS
Simple DFS prototype logic
loop start(Every possible start Node)
function
loop end condition
Search Complete
Page 28
ALGORITHM
SEARCH DFS
Simple DFS prototype code
Page 29
ALGORITHM
SEARCH BFS
BREADTHFIRST
SEARCH
Page 30
ALGORITHM
SEARCH BFS
S
E
How many Shortest Path to End?
Page 31
ALGORITHM
SEARCH BFS
S
E
How many Shortest Path to End?
STATE STRUCTURE
x ystep
Page 32
ALGORITHM
SEARCH BFS
S
E
SEARCHORDER
BFS
How many Shortest Path to End?
Page 33
ALGORITHM
SEARCH BFS
S
E
ENDCONDITION
(X,Y) = E
How many Shortest Path to End?
Page 34
ALGORITHM
SEARCH BFS
S
E
How many Shortest Path to End?
S
1,1,2
Page 35
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
Page 36
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
2,2,2
Page 37
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
2,2,2 2,2,2
Page 38
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
2,2,2 2,2,2
2,3,2 2,3,2
Page 39
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
2,2,2 2,2,2
3,3,2 3,3,2
4,4,2 4,3,3 4,4,2 4,3,3
Page 40
ALGORYTHM
SEARCH BFS
How many Shortest Path to End?
S
E
4,4,2 4,3,3
5,5,25,4,3 4,4,35,3,45,4,1
Page 41
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
E
4,4,2 4,3,3
5,5,25,4,3 5,4,35,3,45,4,1
6,4,3
Page 42
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
E
4,4,2 4,3,3
5,5,25,4,3 5,4,35,3,45,4,1
6,4,3
7,3,57,4,5
Page 43
ALGORITHM
SEARCH BFS
How many Shortest Path to End? 8, 2
S
E
4,4,2 4,3,3
5,5,25,4,3 5,4,35,3,45,4,1
6,4,3
7,3,57,4,5
8,E 8,1,5
Page 44
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
loop start
EndCondition
Enqueue Start Node
Search Complete
Dequeue
Process
Page 45
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
loop start
EndCondition
Enqueue Start Node
Queue process end
Dequeue
Process
queue is Empty & found
1 2 3 4 5
Process
Page 46
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
process start
Enqueuecondition
enqueue
Enqueue loop start
Make new node
process end
Page 47
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
process start
Enqueuecondition
enqueue
Enqueue loop start
Make new node
process end
1 2 3 4 5
Process
Visited? End of map?
n1 n2 n3 n4
n2 n4
n2n4
Page 48
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
loop start(Every possible start Node)
Queue Process
loop end condition
Search Complete
Page 49
ALGORITHM
SEARCH BFS
queue for BFS(left), BFS prototype code(right)