Finding Optimal Path: By: Dr. Anjali Diwan
Finding Optimal Path: By: Dr. Anjali Diwan
Finding Optimal Path: By: Dr. Anjali Diwan
► The brute force algorithm tries out all the possibilities till a satisfactory
solution is not found.
► Brute force search is the most common search algorithm as it does not require
any domain knowledge.
► All that is required is a state description, legal operators, the initial state
and the description of a goal state.
► It does not improve the performance and completely relies on the computing
power to try out possible combinations.
► The brute force algorithm searches all the positions in the text between 0 and
n-m, whether the occurrence of the pattern starts there or not.
► After each attempt, it shifts the pattern to the right by exactly 1 position.
The time complexity of this algorithm is O(m*n).
► If we are searching for n characters in a string of m characters, then it will
take n*m tries.
One example is to make an attempt to break the 5 digit password; then brute
force may take up to 105 attempts to crack the code.
► A brute force approach is an approach that finds all the possible solutions to
find a satisfactory solution to a given problem.
► Algorithm may have two approaches Optimizing and Satisficing.
► Often Brute force algorithms require exponential time. Various heuristics and
optimization can be used:
► Heuristic: A rule of thumb that helps you to decide which possibilities we should
look at first.
► Optimization: A certain possibilities are eliminated without exploring all of them.
Example
► Brute force search considers each and every state of a
tree, and the state is represented in the form of a node.
As far as the starting position is concerned, we have two
choices, i.e., A state and B state. We can either generate
state A or state B. In the case of B state, we have two
states, i.e., state E and F.
► In the case of brute force search, each state is
considered one by one. As we can observe in the above
tree that the brute force search takes 12 steps to find
the solution.
Advantages and Disadvantages of a
brute-force algorithm
► Advantages of the brute-force algorithm:
► This algorithm finds all the possible solutions, and it also guarantees that it finds the
correct solution to a problem.
► This type of algorithm is applicable to a wide range of domains.
► It is mainly used for solving simpler and small problems.
► It can be considered a comparison benchmark to solve a simple problem and does not
require any particular domain knowledge.
► Disadvantages of a brute-force algorithm:
► It is an inefficient algorithm as it requires solving each and every state.
► It is a very slow algorithm to find the correct solution as it solves each state without
considering whether the solution is feasible or not.
► The brute force algorithm is neither constructive nor creative as compared to other
algorithms.
Basic Concept of this
Branch and bound algorithm have four steps:
Step 3: If the top node of the stack is a goal node, then stop and return success.
Step 4: Else POP the node from the stack. Process it and find all its successors.
Find out the path containing all its successors as well as predecessors and then
PUSH the successors which are belonging to the minimum or shortest path.
Step 5: Go to step 5.
Step 6: Exit.
STEP1: Consider node A as root node. STEP2: Now the stack will be
Find it’s successor C,F,B------- A
Calculate the distance form the root: As Bis on top of the satck calculate neighbours of
B = 0+5 B
F = 0+9 D = 0+5+4
C = 0+7 E = 0+5+6
C = 0+5+4+8
F = 0+5+4+3
C,f….. D
The least distance is F from D and it is our Goal node. So stop and return
success.
Hence the searching path will be A-B -D-F
Algorithm: Best first search
• Step 1: Put the initial node x0 and its heuristic value H(x0) to the
open list.
• Step 2: Take a node x from the top of the open list. If the open list is
empty, stop with failure. If x is the target node, stop with success.
• Step 3: Expand x and get a set S of child nodes. Add x to the closed
list.
• Step 4: For each x’ in S but not in the closed list, estimate its
heuristic value H. If x’ is not in the open list, put x’ along with the
edge (x,x’) and H into the open list; otherwise, if H is smaller than
the old value H(x’), update x’ with the new edge and the new
heuristic value.
• Step 5: Sort the open list according to the heuristic values of the
nodes, and return to Step 2.
Example 2.6 p. 25
0 {(0,0),4} --
► The algorithm makes its decisions is by taking the f-value into account. The
algorithm selects the smallest f-valued cell and moves to that cell. This
process continues until the algorithm reaches its goal cell.
Example
► The initial node is A and the goal node is E.
The A* Algorithm
• Step 1: Put the initial node x0 and its cost F(x0)=H(x0) to
the open list.
• Step 2: Get a node x from the top of the open list. If the
open list is empty, stop with failure. If x is the target node,
stop with success.
• Step 3: Expand x to get a set S of child nodes. Put x to
the closed list.
The A* Algorithm
• Step 4: For each x’ in S, find its cost
𝐹 = 𝐹(𝑥) + 𝑑(𝑥, 𝑥’) + [𝐻(𝑥’) − 𝐻(𝑥)]
– If x’ is in the closed list but the new cost is smaller than the old
one, move x’ to the open list and update the edge (x,x’) and the
cost.
– Else, if x’ is in the open list, but the new cost is smaller than the
old one, update the edge (x,x’) and the cost.
– Else (if x’ is not in the open list nor in the closed list), put x’ along
with the edge (x,x’) and the cost F to the open list.
• Step 5: Sort the open list according to the costs of the
nodes, and return to Step 2.
Example 2.7 p. 27
1 2 1 0
(0,2) (1,2) (2,2) (0,2) (1,2) (2,2)
2 1 1
3 3 2 1
(0,1) (1,1) (2,1) (0,1) (1,1) (2,1)
2 3
5 4 3 2
1
(0,0) (1,0) (2,0) (0,0) (1,0) (2,0)
• it has the optimal characteristics of A* to find the shortest path but it uses less
memory than A*.
Pros and Cons
► Pros:
• It will always find the optimal solution provided that it exist’s and that if a
heuristic is supplied it must be admissible.
• Heuristic is not necessary, it is used to speed up the process.
• Various heuristics can be integrated to the algorithm without changing the
basic code.
• The cost of each move can be tweaked into the algorithms as easily as the
heuristic
• Uses a lot less memory which increases linearly as it doesn’t store and forgets
after it reaches a certain depth and start over again.
► Cons:
• Doesn’t keep track of visited nodes and thus explores already explored nodes
again.
• Slower due to repeating the exploring of explored nodes.
• Requires more processing power and time than A*.
Steps of Algorithm
• The Depth first search and Breadth first search given earlier for OR trees or
graphs can be easily adopted by AND-OR graph.
• The main difference lies in the way termination conditions are determined,
since all goals following an AND nodes must be realized;
• Where as a single goal node following an OR node will do. So for this purpose
we are using AO* algorithm.
► Like A* algorithm here we will use two arrays and one heuristic function.
► OPEN: It contains the nodes that has been traversed but yet not been marked
solvable or unsolvable.
► CLOSE: It contains the nodes that have already been processed.
Algorithm:
► Step 1: Place the starting node into OPEN.
► Step 3: Select a node n that is both on OPEN and a member of T0. Remove it from OPEN and
place it in
► CLOSE
► Step 4: If n is the terminal goal node then leveled n as solved and leveled all the ancestors of
n as solved. If the starting node is marked as solved then success and exit.
► Step 5: If n is not a solvable node, then mark n as unsolvable. If starting node is marked as
unsolvable, then return failure and exit.
► Step 6: Expand n. Find all its successors and find their h (n) value, push them into OPEN.
► Step 8: Exit.
► Advantages:
►
► It is an optimal algorithm.
►
► If traverse according to the ordering of nodes. It can be used for both OR and
AND graph.
►
► Disadvantages:
►
► Sometimes for unsolvable nodes, it can’t find the optimal path. Its complexity
is than other algorithms.
►