ALGORITHM
SEARCH INTRO
What is Search?
ALGORITHM
SEARCH INTRO
1 3 4 6 7 8 10 13 14
When Dataset is Sorted
ALGORITHM
SEARCH INTRO
Binary Search
ALGORITHM
SEARCH INTRO
Dataset not Sorted
4 3 8 6 7 13 10 14 9
ALGORITHM
SEARCH INTRO
Search Process can Produce additional Data
4 3 8
6
7 13 10 14 9
1 11
3
ALGORITHM
SEARCH INTRO
State space Tree
ALGORITHM
SEARCH INTRO
Three Things to Decide
STATE STRUCTURE
SEARCH ORDER
ENDCONDITION
ALGORITHM
SEARCH DFS
DEPTHFIRST
SEARCH
ALGORITHM
SEARCH DFS
S
E
Is there way out?
ALGORITHM
SEARCH DFS
S
E
Is there way out?
STATE STRUCTURE
x y
ALGORITHM
SEARCH DFS
S
E
Is there way out?
SEARCHORDER
DFS
ALGORITHM
SEARCH DFS
S
E
Is there way out?
ENDCONDITION
(X,Y) = E
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
2,2
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
2,2
3,2
ALGORITHM
SEARCH DFS
S
E
Is there way out?
S
1,2
2,2
3,2
3,3
ALGORITHM
SEARCH DFS
S
E
Is there way out?
3,3
3,4
ALGORITHM
SEARCH DFS
S
E
Is there way out?
3,3
3,4
3,5
ALGORITHM
SEARCH DFS
S
E
Is there way out?
3,3
3,4
3,5
4,5
ALGORITHM
SEARCH DFS
S
E
Is there way out? YES
3,3
3,4
3,5
4,5
E
ALGORITHM
SEARCH DFS
S
E
Is there way out? YES
S
1,2
2,2
3,2
3,3
2,1
ALGORITHM
SEARCH DFS
Simple DFS prototype logic
function start
EndCondition
child function call
child function call
…
Return
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
ALGORITHM
SEARCH DFS
Simple DFS prototype logic
loop start(Every possible start Node)
function
loop end condition
Search Complete
ALGORITHM
SEARCH DFS
Simple DFS prototype code
ALGORITHM
SEARCH BFS
BREADTHFIRST
SEARCH
ALGORITHM
SEARCH BFS
S
E
How many Shortest Path to End?
ALGORITHM
SEARCH BFS
S
E
How many Shortest Path to End?
STATE STRUCTURE
x ystep
ALGORITHM
SEARCH BFS
S
E
SEARCHORDER
BFS
How many Shortest Path to End?
ALGORITHM
SEARCH BFS
S
E
ENDCONDITION
(X,Y) = E
How many Shortest Path to End?
ALGORITHM
SEARCH BFS
S
E
How many Shortest Path to End?
S
1,1,2
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
2,2,2
ALGORITHM
SEARCH BFS
How many Shortest Path to End?
S
1,1,2 1,2,1
S
E
2,2,2 2,2,2
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
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
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
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
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
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
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
loop start
EndCondition
Enqueue Start Node
Search Complete
Dequeue
Process
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
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
process start
Enqueuecondition
enqueue
Enqueue loop start
Make new node
process end
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
ALGORITHM
SEARCH BFS
Simple BFS prototype logic
loop start(Every possible start Node)
Queue Process
loop end condition
Search Complete
ALGORITHM
SEARCH BFS
queue for BFS(left), BFS prototype code(right)