General Problem Solving With Search Algorithms: 11/24/20 (C) Debasis Mitra 1
General Problem Solving With Search Algorithms: 11/24/20 (C) Debasis Mitra 1
Input:
1 2 3
4 5
7 8 6 Goal:
1 2 3
4 5 6
7 8
2 possible moves:
1 2 1 2 3
4 5 3 4 5
7 8 6 7 8 6
1 2 3
4 5 6
7 8
1 2
Input:
4 5 3
7 8 6
1 2 1 2 3
4 5 3 4 5
7 8 6 7 8 6
b=3 b=3
1, R 2, L 5, U 3, D 5, R 6, U
1 2 3
4 5 6
11/24/20 (C) Debasis Mitra
Goal 7 8
Problem Solving = Graph Search
1 2
• Think of input data structure. Goal
4 5 3
1 2 3
7 8 6
• Input Size n= 4 5 6
• 8-Puzzle: 3x3 tiles, n= 9 7 8
• 15-Puzzle: 4x4 tiles, n=16
• …
1 2
Input:
4 5 3
7 8 6 Black arrows show the structure of the tree
Red arrows show control flow
1 2 1 2 3
4 5 3 4 5
7 8 6 7 8 6
b=3 b=3
1, R 2, L 5, U 3, D 5, R 6, U
What is the typical data structure for BFS? Queue or Stack? Goal
11/24/20 (C) Debasis Mitra 9
Breadth First Search
Code it!
11/24/20 (C) Debasis Mitra 10
DFS: 8-PUZZLE PROBLEM SOLVING
1 2
Input:
4 5 3
7 8 6
Red and green arrows are control flow
1 2 1 2 3
4 5 3 4 5
7 8 6 7 8 6
b=3 b=3
1, R 2, L 5, U 3, D 5, R 6, U
• DFS:
• Infinite search is possible, in the worst case
• May get stuck on an infinite depth in a branch,
• Even if the goal may be at a low level on a different branch
• Linear memory growth, depth-wise WHY?
• but, go to the point number 1 above memory may explode for large depth!
• Stack for implementation (equivalent to recursive implementation)
• Time complexity: Worst case for both, all nodes searched: O(bd)
b branching factor, d depth of the goal
• Memory:
• BFS O(bd) remember all nodes
Goal
• DFS O(bd), only one set of children at each level, up to goal depth
• But the depth may be very high, up to infinity
• DFS <forgets> previously explored branches
Goal
• Complete Algorithm
– Goal <<will>> be found when l = = d, goal depth
• Memory:
– IDS vs DFS: same O(bd)
– IDS vs BFS: O(bd) vs O(bd)
g (actual distance)
v
h (heuristic distance)
Algorithm ucfs(v)
GOAL
1.Enqueue start node s on a min-priority queue Q; // distance g(s)= 0
2.While Q ≠ empty do
3. v = pop(Q);
4. If v is a goal, return success;
5. Operate on v (e.g., display board);
6. For each child w of v,
7. Insert w in Q such that cost(w) is the lowest;
// cost(w) is path cost from the child node w to a goal node,
// replacing cost(w) with g(w), distance of w from source, makes it
Djikstra’s algorithm
// Q is a priority-queue to keep lowest cost-node in front
Algorithm ucs(v)
1.Enqueue start node s on a min-priority queue Q; // distance h(s)= 0
2.While Q ≠ empty do
3. v = pop(Q); // lowest path-cost node
4. If v = = a goal return success;
5. operate on v (e.g., display board);
6. For each child w of v, if w is not-visited before,
7. Insert w in Q such that g(w) is the lowest; // use priority-que or heap for Q
80 99 80 99
Status of Q: 310
117 310
Q: 80, 99 177 Goal
278 278
Q: 99, 177
310, Success, but continue until finished
Q: 177, 278
Q: 278
278, Success, and finished all nodes
We may stop if just finding a Goal is enough,
And shortest path is not needed,
e.g., in 8-puzzle problem
11/24/20 (C) Debasis Mitra 21
Two properties of search algorithms
• Best-first strategy:
• Out of all frontier nodes pick up n’ that has minimum h(n’)
• Use heap
• Example:
• g(n): how far have you traveled
• h(n): how far does the Eiffel tower appear to be!
• Go toward smallest f = g+h
• least f = least path to goal
• Why least path, rather than any path?
• Because: a larger path may be infinite!
• Why not use true distance for f? Then, who needs a search :)
• Heuristic ≡ You are just trying to guess!
11/24/20 (C) Debasis Mitra 25
2) Consistent Heuristic for A* ≡ Guides only toward correct
direction
• For every pair of nodes n, n’,
such that n’ is a child of n on a path from start to goal node
Goal
Note: heuristic function is f(n) = g(s to n) + h(estimated from n to goal)
and, d(n, n’) = actual distance in input between n and n’
11/24/20 (C) Debasis Mitra 26
IGNORE THIS SLIDE: A* vs. Branch-and-Bound
Problem objective: optimum path to the Problem objective: Optimum cost over
goal (e.g., path finding) all valid leaf nodes (e.g., 0-1 knapsack
(ks) max profit)
Purpose of algorithm: find best path Purpose of algorithm: prune branches
start
d(s,n)=90
d(n,n’’)=50, d(n’’,g)=250
d(n,n’)=50 h(n’’)=240, and 250>240 is true
h(n)=300 f(n’’) = 90+50+240=380
d(n’,g)=280, h(n’)=270, and 280>270 is true But, True distance to Goal
f(n’)=(90+50)+270=310 h(n’)=270 is (90+50) + 250
Search flows this way
Goal
But, True distance to Goal on this path is (90+50) + 280
only to find later that a better path existed via n’’
Backtracks => more time
11/24/20 (C) Debasis Mitra
Optimality of A*
• The book has a bit of confusion on finding the goal vs.
finding “a” best path to the goal
• It is still by, f = g + h
2. Pattern Database:
• Human experts use patterns in memory!
• IBM Blue- series stored and matched successful patterns
• Google search engine evolved toward this
• Database indexing becomes the key problem, for fast matching
• Possibilities:
– Non-determinism in state space (graph)
• Examples:
• n-queens problem (on chess board, no queen to attack another)
• Search space: a node – n queens placed on n columns
• VLSI circuit layout
• (possibly!) n elements connected, optimize total area
• Or, given a fixed area, make needed connections
11/24/20 (C) Debasis Mitra 40
LOCAL SEARCH ALGORITHMS
• How do you know your next optimum is better than the last one?
• Why do you have to forget everything?
• Just remember the last best optimum and update if necessary!
• Still not guaranteed to find <<Global>> optimum, but f^ converges
• Local search:
• Typically, start coordinate and the resulting path is not important
• Newton-Raphson:
• 1D: x x – f(x)/f’(x), if analytic solution exists
• Quadratic Programming
• If-else on nodes,
• condition dependent
• You do not know what will show up on a node – plan ahead!
• Example: Mars rover
• On-line: Robots
• Compute-act-sense-compute-…..
• State space develops as-we-go:
– not “thinking” algorithm, actually make the move, no way to un-commit
• Not much different
• Strategy, heuristics,
• but no set of nodes, may be finite/infinite