Skip to content

Commit a93eba3

Browse files
committed
Add uniform cost and bfs
1 parent 7d06ab9 commit a93eba3

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

md/Breadth-First-Search.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# BREADTH-FIRST-SEARCH
22

3+
## AIMA4e
4+
5+
__function__ BREADTH-FIRST-SEARCH(_problem_) __returns__ a solution, or failure
6+
 __if__ problem's initial state is a goal __then return__ empty path to initial state
7+
 _frontier_ ← a FIFO queue initially containing one path, for the _problem_'s initial state
8+
 _reached_ ← a set of states; initially empty
9+
 _solution_ ← failure
10+
 __while__ _frontier_ is not empty __do__
11+
   _parent_ ← the first node in _frontier_
12+
   __for__ _child_ __in__ successors(_parent_) __do__
13+
     _s_ ← _child_.state
14+
     __if__ _s_ is a goal __then__
15+
       __return__ _child_
16+
     __if__ _s_ is not in _reached_ __then__
17+
       add _s_ to _reached_
18+
       add _child_ to the end of _frontier_
19+
 __return__ _solution_
20+
21+
---
22+
__Figure 3.9__ Breadth-first search algorithm.
23+
24+
325
## AIMA3e
426
__function__ BREADTH-FIRST-SEARCH(_problem_) __returns__ a solution, or failure
527
 _node_ ← a node with STATE = _problem_.INITIAL\-STATE, PATH\-COST = 0

md/Successors.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Successors
2+
3+
## AIMA4e
4+
5+
6+
__function__ SUCCESSORS(_problem_, _parent_) __returns__ an action
7+
 _s_ ← _parent_.state
8+
 _nodes_ ← an empty list
9+
 __for__ _action_ in _problem_.actions(_s_) __do__
10+
   _s'_ ← _problem_.result(s,_action_)
11+
   _cost_ ← _parent_.pathCost + _problem_.stepCost(_s, action, s′_ )
12+
   _node_ ← Node(state = _s'_, parent = _parent_, action = _action_, pathCost = _cost_)
13+
   add _node_ to _nodes_
14+
 __return__ _nodes_

md/Uniform-Cost-Search.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# UNIFORM-COST-SEARCH
22

3+
## AIMA4e
4+
5+
__function__ UNIFORM-COST-SEARCH(_problem_) __returns__ a solution, or failure
6+
 __if__ problem's initial state is a goal __then return__ empty path to initial state
7+
 _frontier_ ← a priority queue ordered by pathCost, with a node for the initial state
8+
 _reached_ ← a table of {_state_: the best path that reached _state_}; initially empty
9+
 _solution_ ← failure
10+
 __while__ _frontier_ is not empty __and__ top(_frontier_) is cheaper than _solution_ __do__
11+
   _parent_ ← pop(_frontier_)
12+
   __for__ _child_ __in__ successors(_parent_) __do__
13+
     _s_ ← _child_.state
14+
     __if__ _s_ is not in _reached_ __or__ _child_ is a cheaper path than _reached_[_s_] __then__
15+
       _reached_[_s_] ← _child_
16+
       add _child_ to the _frontier_
17+
       __if__ _child_ is a goal and is cheaper than _solution_ __then__
18+
         _solution_ = _child_
19+
 __return__ _solution_
20+
21+
---
22+
__Figure 3.11__ Uniform-cost search on a graph. Finds optimal paths for problems with vary-
23+
ing step costs.
24+
25+
326
## AIMA3e
427
__function__ UNIFORM-COST-SEARCH(_problem_) __returns__ a solution, or failure
528
 _node_ ← a node with STATE = _problem_.INITIAL\-STATE, PATH\-COST = 0

0 commit comments

Comments
 (0)