Chapter 3 - Solving Problems by Searching
Chapter 3 - Solving Problems by Searching
Computer Science
CHAPTER 3
Solving Problems
by
By Samuel M.
Searching DEC. 2022
Learning objectives: at the end of the class, you should be able
to:
•What is a Problem?
• Problem types with examples
•Solving a problem
• Solution and optimal solution
• Components of problem
•Searching
• Searching Algorithm Evaluation
• Search Strategies
2
What is a Problem?
• It is a gap between what actually is and what is desired.
• A problem exists when an individual becomes aware of the
existence of an obstacle which makes it difficult to achieve a
desired goal or objective.
• A number of problems are addressed towards designing
intelligent agent:
• Toy problems: are problems that are useful to test and
demonstrate methodologies and can be used by researchers to
compare the performance of different algorithms.
• May require little or no ingenuity, good for games design
• e.g. 8-puzzle, n-queens, vacuum cleaner world, towers of Hanoi, river
crossing…
• Real-life problems: problems that have much greater
commercial/economic impact if solved.
• Such problems are more difficult and complex to solve, and there is no
single agreed-upon description.
• E.g. Route finding, Traveling sales person, etc.
3
Some more Real-world Problems
Route finding Problem
Traveling Salesman Problem(TSP)
VLSI Layout
Assembly Sequencing
Robot Navigation
Route Finding Problem
• Route Finding Problem - shortest path problem
• Defined in terms of locations and transitions along links between
them.
• Applications: automated travel advisory systems, airline
travel planning systems, military operations planning,
routing in computer networks,
General Route Finding Algorithm
1. Identify initial state as origin: Initial State
2. Expand to All Possible Locations: Successors
3. Choose Location with smallest cost/fastest route
4. Test Goal Function, is it the destiny? Goal Test
5. if yes, return location: Goal State
else, return to 2
Cont’d
• Route Finding Problem: Entire States - State Space
6
Cont’d
• Route Finding Problem:
• A problem can be defined formally by five components
• Initial State: The initial state that the agent starts in. For
example, the initial state for our agent in Romania might be
described as In(Arad)
7
• Traveling Salesperson Problems(TSP)
Cont…
• Visit each city on the map exactly once and returns to the
origin city.
• Needs information about the visited cities
• The aim is used to find the shortest possible route that visits
every city exactly once and return to the starting point.
• Applications: vehicle routing
• VLSI Layout Cont…
• Place cells on a chip so they don’t overlap and there is room for
connecting wires to be placed between the cells
• A VLSI Layout problem requires positioning millions of
components and connections on a chip to minimize area,
minimize circuit delays, minimize stray capacitances, and
maximize manufacturing yield.
Solving a Problem
10
Solving a Problem
Problem Solving: is a process of generating solutions from
observed/or given data. Use direct or indirect(Model based) methods
A problem is characterized by - a stet of goals, objects & operations
operations.
To build a system, to solve a particular problem, we need to
• Define the problem precisely
Find input situations as well as final situations for acceptable solution to
the problem
• Analyze the problem
Find few important features that may have impact on the appropriateness
of various possible techniques for solving the problem.
• Isolate and represent task necessary to solve the problem
• Choose the best problem solving technique(s) and apply to
the particular problem
11
Solving a problem…
Solution and Optimal Solution
•Solution is set of actions that leads from initial state to goal
state,
12
Example: The 8 puzzle problem
1 2 3 1 2 3
4 8 4 5 6
7 6 5 7 8
14
Exercise: The 8 puzzle problem
• This is the problem of arranging the tiles so that all the tiles are in
the correct positions. You do this by moving tiles or space up,
down, left, or right, so long as the following conditions are met:
• a) there's no other tile blocking you in the direction of the movement; and
• b) you're not trying to move outside of the boundaries/ edges.
1 2 3 1 2 3
8 4 5 8 4
7 6 7 6 5
15
Exercise : River Crossing Puzzles
Goat, Wolf and Cabbage Problem
• A farmer returns from the market, where he bought a goat,
a cabbage and a wolf. On the way home he must cross a
river. His boat is small and unable to transport more
than one of his purchases. He cannot leave the goat alone
with the cabbage (because the goat would eat it), nor he can leave
the goat alone with the wolf (because the goat would be eaten). How
can the farmer get everything safely on the other side?
1. Identify the set of possible states and operators
2. Construct the state space of the problem using suitable
representation
17
Steps in Problem Solving
• Goal Formulation
• is a step that specifies exactly what the agent is trying to achieve
• This step narrows down the scope that the agent has to look at
• Problem Formulation
• Is a step that puts down the actions and states that the agent has to
consider given a goal (avoiding any redundant states), like:
• the initial state
• the allowable actions etc…
• Search
• is the process of looking for the various sequence of actions that
lead to a goal state, evaluating them and choosing the optimal
sequence.
• Execute
• is the final step that the agent executes the chosen sequence of
actions to get it to the solution/goal
19
Example: Path Finding Problem
Initial
Arad to Bucharest
State
• Formulate goal:
• be in Bucharest (Romania)
• state: be in a city
(20 world states)
• Find solution:
• sequence of cities leading from start to goal
state, e.g., Arad, Sibiu, Fagaras, Bucharest
• Execution
• drive from Arad to Bucharest according to
the solution
Route Finding Problems
• Basic idea:
• Simulated exploration
of state space by
generating successors
of already explored
states (AKA expanding
states)
• Basic idea:
• Simulated exploration of
state space by
generating successors of
already explored states
(AKA expanding states)
• Completeness
• Optimality
• Time complexity
• Space complexity
Cont…
• Completeness
• Guarantees finding a solution whenever one exists.
• Think about the density of solutions in space and evaluate whether
the searching technique guaranteed to find all solutions or not.
• Optimality
• is the algorithm finding an optimal/best solution; i.e. the one
with minimize cost or maximize profit
• If a solution is found, is it guaranteed to be an optimal one? That
is, is it the one with minimum cost?
• how good is our solution?
Time complexity: how long does it take to find a solution
- Usually measured in terms of the number of nodes expanded
(time taken)
Space complexity: how much space is used by the algorithm
Types of search algorithms:
Based on the search problems we can classify the search algorithms into
uninformed (Blind search) and informed search (Heuristic search) algorithms:
Uninformed/Blind Search:
The uninformed search does not contain any domain
knowledge such as closeness, the location of the goal.
It operates in a brute-force (trial-and-error) way as it only
includes information about how to traverse the tree and
how to identify leaf and goal nodes.
Uninformed search applies a way in which search tree is
searched without any information about the search space
like initial state operators and test for the goal, so it is also
called blind search.
It examines each node of the tree until it achieves the goal
node
It can be divided into five main types:
Breadth-first search
Uniform cost search
Depth-first search
Iterative deepening depth-first search
Bidirectional Search
Informed Search:
• BFS: S->B->C->D->E->G
Depth-First Search (DFS)
Expand one of the node at the
deepest level of the tree.
Only when the search hits a non-goal dead end
does the search go back and expand nodes at
shallower levels
It is called the DFS because it starts
from the root node & follows each
path to its greatest depth node before
moving to the next path.
will follow the order as:
Root node--->Left node ----> right node.
Depth-First Search (DFS)…
Space Complexity: DFS algorithm needs to store only single path from the
root node, hence space complexity of DFS is equivalent to the size of the
fringe set, which is O(bm).
Exercise
• Apply DFS to find an optimal path from Start Node (S)
to Goal Node(G).
Depth Limited Search (DLS)
• Depth Limited Search is similar to Depth First Search with a
predetermined limit ℓ.
• Depth-limited search can solve the drawback of the infinite path
in the Depth-first search.
• The node at the depth limit will treat as it has no successor nodes
further.
• Depth-limited search can be terminated with two Conditions of
failure:
Standard failure value: It indicates that problem does not have any
solution.
Cutoff failure value: It defines no solution for the problem within a given
depth limit.
Example of DLS
Consider a case where depth-
limit d=2
Depth limit ℓ = 2
S-->A-->C-->D--->B-->I-->J
…cont.
• Advantages:
• Memory efficient.
• Disadvantages:
• incompleteness.
• may not be optimal if problem has more than one solution.
Goal
Exercise
• Apply DLS to find an optimal path from Start Node (S)
to Goal Node (G) with depth limit ℓ = 2
Iterative Deepening Search (IDS)
The Iterative Deepening Depth-First Search (IDS) algorithm,
repeatedly applies depth-limited search with increasing limits.
It gradually increases limits from 0,1,...d, until the goal node is
found.
E F G
H
Level Iteration Path Returned using IDS
I J K
Depth
0 1
1 2
2 3
3 4
Uniform Cost Search
• The goal of this technique is to find the shortest path to the
goal in terms of cost.
• It modifies the BFS by always expanding least-cost unexpanded
node
• Implementation: nodes in list keep track of total path length
from start to that node
• List kept in priority queue ordered by path cost
A
S S S
1 10 S
5 B 5
S G 0 A B C A B C A B C
1 5 15 5 15 15
G G G
15 5
11 11 10
C
• Properties:
• This strategy finds the cheapest solution provided the cost of a
path must never decrease as we go along the path successor(n))
≥ g(n), for every node n.
• Takes space since it keeps every node in memory
Example of UCS
• niform-Cost Search is mainly used in Artificial Intelligence.
U
…Cont’d
Cost: S-->A: 1, A-->D: 2, D-->G: 3
S---> A---> D---> G
A B C
3 9
7 4 5
D E G
Exercise:
Apply Uninformed Search Strategies to find optimal
path. Initial State = S, Goal State = G, ℓ = 1
Find Optimal Path
S
1.BFS? 1 8
5
2.DFS?
3.IDS? A B C
9
4.DLS: ℓ = 2? 3
7 4
5
5.UCS? D E G
Comparing Uninformed Search Strategies
Time Space
Strategies Complete Optimal Complexity Complexity
• Implementation:
• expand 1st the node closest to the goal state, i.e. with evaluation
function f(n) = h(n)
• h(n) = 0 if node n is the goal state
• Otherwise h(n) ≥ 0; an estimated cost of the cheapest path from
the state at node n to a goal state
Example
Initial state
S
8
1 5 8
A B C
8 4 3
3 9
7 4 5
D E G
0
goal state
…Cont’d
• Best First (Greedy) Search Algorithm:
• Step 1: Place the starting node into the OPEN list.
• Step 2: If the OPEN list is empty, Stop and return failure.
• Step 3: Remove the node n, from the OPEN list which has the
lowest value of h(n), and places it in the CLOSED list.
• Step 4: Expand the node n, and generate the successors of node n.
• Step 5: Check each successor of node n, and find whether any node
is a goal node or not. If any successor node is goal node, then
return success and terminate the search, else proceed to Step 6.
• Step 6: For each successor node, algorithm checks for evaluation
function f(n), and then check if the node has been in either OPEN
or CLOSED list. If the node has not been in both list, then add it to
the OPEN list.
• Step 7: Return to Step 2.
…Cont’d
• Advantages:
• switch between BFS and DFS (gaining the advantages of both).
• more efficient than BFS and DFS algorithms.
• Disadvantages:
• behave as unguided depth-first search in worst case scenario.
• get stuck in a loop as DFS.
• not optimal.
…Cont’d
• Example of Greedy Search
• Consider the below search problem, and we will traverse it using
greedy best-first search. At each iteration, each node is
expanded using evaluation function f(n)=h(n) , which is given in
the below table.
A* Search Algorithm
• It considers both estimated cost of getting from n to the goal
node h(n), and cost of getting from initial node to node n, g(n)
• Apply three functions over every nodes
• g(n): Cost of path found so far from initial state to n (path cost)
• h(n): Estimated cost of shortest path from n to z (heuristic value)
• f(n): Estimated total cost of shortest path from a to z via n
• Evaluation function f(n) = h(n) + g(n)
• Implementation: Expand the node for which the evaluation
function f(n) is lowest
• Rank nodes by f(n) that goes from the start node to goal node via
given node
• NB:
• At each point in the search space, only those node is expanded
which have the lowest value of f(n), and the algorithm
terminates when the goal node is found.
…Cont’d
Algorithm of A* search:
• Step 1: Place the starting node in the OPEN list.
• Step 2: Check if the OPEN list is empty or not, if the list is empty
then return failure and stops.
• Step 3: Select the node from the OPEN list which has the
smallest value of evaluation function (g+h), if node n is goal node
then return success and stop, otherwise
• Step 4: Expand node n and generate all of its successors, and put
n into the closed list. For each successor n', check whether n' is
already in the OPEN or CLOSED list, if not then compute
evaluation function for n' and place into Open list.
• Step 5: Else if node n' is already in OPEN and CLOSED, then it
should be attached to the back pointer which reflects the lowest
g(n') value.
• Step 6: Return to Step 2.
…Cont’d
• Example of A* Search:
• Traverse the given graph using the A* algorithm. The heuristic
value of all states is given in the below table. Calculate f(n) of
each state using the formula f(n)= g(n) + h(n), where g(n) is the
cost to reach any node from start state.
• Here we will use OPEN and CLOSED list.
…Cont’d
Heuristic Search Methods
Algorithm Time Space Optimal Complete
Comp. Comp.
Greedy O(bm) O(bm) No No
Solution:
Greedy: f(n) = h(n)
A*: f(n) = g(n) + h(n)
Solution: Search Space
• Informed Search (Heuristic Search) Strategies
• BFS (Greedy): f(n) = h(n)
1. S: f(n) = 13
2. S => A: f(n) = 12
S => B: f(n) = 4 (Lowest h, expand B)
3. S => B => E: f(n) = 8
S => B => F: f(n) = 2 (Lowest h, expand F)
4. S => B => F => I: f(n) = 9
S => B => F => G: f(n) = 0 (Lowest h, Reached Goal State)
Solution: Search Space
• Informed Search (Heuristic Search) Strategies
• A* Search: f(n) = g(n) + h(n)
1. S: f(n) = 0 + 13 = 13
2. S => A: f(n) = 3 + 12 = 15
S => B: f(n) = 2 + 4 = 6 (Lowest f-cost, expand via B)
3. S => B => E: f(n) = (2 + 3) + 8 = 13
S => B => F: f(n) = (2 + 1) + 2 = 5 (Lowest f-cost, expand via F)
4. S => B => F => I: f(n) = (2 + 1 + 2 + 3) + 9 = 17
S => B => F => G: f(n) = (2 + 1 + 3) + 0 = 6 (Lowest f-cost, Reached Goal)
Questions?