Professional Documents
Culture Documents
Algorithms - I
Breadth First Search
Returns goal state node which is at the minimum depth
Optimal where step cost is same for all transitions
Goal Test performed on reaching the node
BFS - Algorithm
Breadth First Search -
EXAMPLE
• Problem
• Initial state – 1
• Actions (As per the outgoing edges from the states)
• Transition model – as shown in the edges of the graph
• Path cost – weight of the edges in the graph
• Goal State: 5
BFS Algorithm Working
node
1. Goal test - False
2.Frontier (F) = FIFO(1)
3. Explored (E) = {}
BFS Algorithm Working
node
1. Goal test(initial_state) - False
2.Frontier (F) = FIFO(1)
3. Explored (E) = {}
4. node = pop(frontier)
(F = FIFO()
E = {1})
5. Expand 1 (perform all actions and for each
action
a. perform goal test
b. add child to frontier if it is not present in
frontier or explored
F = FIFO(3,2)
BFS Algorithm Working
node
4. node = pop(frontier)
(F = FIFO()
E = {1})
5. Expand 1 (perform all actions and for each action a.
perform goal test b.add child to frontier if it is not present in
frontier or explored
F = FIFO(3,2)
(Repeating 4,5)
6. node = pop(frontier) = 3
F = FIFO(2)
E = {1, 3}
7. Expand 3
F = FIFO(2, 4)
BFS Algorithm Working
node
4. node = pop(frontier)
(F = FIFO()
E = {1})
5. Expand 1 (perform all actions and for each action a.
perform goal test b.add child to frontier if it is not present in
frontier or explored
F = FIFO(3,2)
(Repeating 4,5)
(Repeating 4,5)
8. node = pop(frontier) = 2
6. node = pop(frontier) = 3
F = FIFO(4)
F = FIFO(2)
E = {1, 3, 2}
E = {1, 3}
9. Expand 2
7. Expand 3
Child – 5 – goal test – true – Solution(5) – returned by the algorithm
F = FIFO(2, 4)
F = FIFO(4)
BFS Algorithm Working
node
4. node = pop(frontier)
(F = FIFO()
E = {1})
5. Expand 1 (perform all actions and for each action a.
perform goal test b.add child to frontier if it is not present in
frontier or explored
Solution(5)
F = FIFO(3,2)
Node
1. F = PQ(1:0)
2. E = {}
Uniform Cost Search - Working
1. F = PQ(1:0)
2. E = {}
3. Node = pop(F)
F = PQ()
E = {1}
4. GoalTest(Node) is false
5. Expand 1
For all actions at 1 add child to F if not present in F or E
F = PQ(3:1, 2:5)
(Repeat steps 3 to 5 when F is not empty)
Uniform Cost Search - Working
1. F = PQ(1:0)
2. E = {}
3. Node = pop(F)
F = PQ()
E = {1}
4. GoalTest(Node) is false
5. Expand 1
For all actions at 1 add child to F if not present in F or E
F = PQ(3:1, 2:5)
(Repeat steps 3 to 5 when F is not empty)
6.Node = pop(F)
F = PQ(2:5)
E = {1, 3}
7. Goal test(Node) is false
8. Expand 3
For all actions at 3 add child to F if not present in F or E
F = PQ(2:5, 4:2)
(Repeat steps 3 to 5 when F is not empty)
Uniform Cost Search - Working
1. F = PQ(1:0)
2. E = {}
3. Node = pop(F)
F = PQ()
E = {1}
4. GoalTest(Node) is false
5. Expand 1
For all actions at 1 add child to F if not present in F or E
F = PQ(3:1, 2:5)
(Repeat steps 3 to 5 if F not empty)
9. Node = pop(F)
6. Node = pop(F) F = PQ(2:5)
F = PQ(2:5) E = {1, 3, 4}
E = {1, 3} 10. Goal test(Node) is false
7. Goal test(Node) is false 11. Expand 4
8. Expand 3 For all actions at 1 add child to F if not present in F or E
For all actions at 3 add child to F if not present in F or E F = PQ(2:5, 5:3)
F = PQ(2:5, 4:2) (Repeat steps 3 to 5 if F not empty)
(Repeat steps 3 to 5 if F not empty)
Uniform Cost Search - Working
1. F = PQ(1:0)
2. E = {}
3. Node = pop(F)
F = PQ()
E = {1}
4. GoalTest(Node) is false
5. Expand 1
For all actions at 1 add child to F if not
present in F or E
F = PQ(3:1, 2:5)
(Repeat steps 3 to 5 if F not empty)
6. Node = pop(F)
F = PQ(2:5) 9. Node = pop(F)
E = {1, 3} F = PQ(2:5) 12. Node = pop(F)
7. Goal test(Node) is false E = {1, 3, 4} F = PQ(2:5)
8. Expand 3 10. Goal test(Node) is false 13. Goal test(Node) is True
For all actions at 3 add child to F 11. Expand 4 Return Solution(5- Reached on exploring 4)
if not present in F or E For all actions at 4 add child
F = PQ(2:5, 4:2) to F if not present in F or E
(Repeat steps 3 to 6 if F not F = PQ(2:5, 5:3)
empty) (Repeat steps 3 to 6 if F not
empty)
Uniform Cost Search - Working
1. F = PQ(1:0)
2. E = {}
3. Node = pop(F)
F = PQ()
E = {1}
4. GoalTest(Node) is false
5. Expand 1
For all actions at 1 add child to F if not
present in F or E
F = PQ(3:1, 2:5)
(Repeat steps 3 to 5 if F not empty)
6. Node = pop(F)
F = PQ(2:5) 9. Node = pop(F) 12. Node = pop(F)
E = {1, 3} F = PQ(2:5) F = PQ(2:5)
7. Goal test(Node) is false E = {1, 3, 4} 13. Goal test(Node) is True
8. Expand 3 10. Goal test(Node) is false Return Solution(5- Reached on exploring 4)
For all actions at 3 add child to F 11. Expand 4
if not present in F or E For all actions at 4 add child
F = PQ(2:5, 4:2) to F if not present in F or E
(Repeat steps 3 to 6 if F not F = PQ(2:5, 5:3)
empty) (Repeat steps 3 to 6 if F not
empty)
Uniform Cost Search - Performance
• Uniform-cost search is guided by path costs rather than depths, so its complexity
is not easily characterized in terms of b and d.
• Instead, let C be the cost of the optimal solution, and assume that every action
costs at least e
• Then the algorithm’s worst-case time and space complexity is O(b1+floor(C/e)), which
can be much greater than bd.
• This is because uniform cost search can explore large trees of small steps before
exploring paths involving large and perhaps useful steps.
• When all step costs are equal, b 1+C/d is just bd+1. When all step costs are the same,
uniform-cost search is similar to breadth-first search, except that the latter stops as
soon as it generates a goal, whereas uniform-cost search examines all the nodes at
the goal’s depth to see if one has a lower cost; thus uniform-cost search does
strictly more work by expanding nodes at depth d unnecessarily.
• For the problem whose states and transitions are as per the
graph given , find the path from Chicago to Sault Ste Marie
Exercise using (a) BFS (b) UCS. (Both Graph Search and Tree search)
(Visit siblings in the increasing
order of path costs)