Heuristic Search Algorithms
Heuristic Search Algorithms
import simpleai as sa
print(sa)
1
class HelloProblem(SearchProblem):
def actions(self, state):
if len(state) < len(GOAL):
return list(' ABCDEFGHIJKLMNOPQRSTUVWXYZ')
else:
return []
problem = HelloProblem(initial_state='')
result = astar(problem)
print(result.state)
print(result.path())
HELLO WORLD
[(None, ''), ('H', 'H'), ('E', 'HE'), ('L', 'HEL'), ('L', 'HELL'), ('O',
'HELLO'), (' ', 'HELLO '), ('W', 'HELLO W'), ('O', 'HELLO WO'), ('R', 'HELLO
WOR'), ('L', 'HELLO WORL'), ('D', 'HELLO WORLD')]
2
[4]: graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
3
'D' : [],
'E' : ['F'],
'F' : []
}
while queue:
s = queue.pop(0)
print (s, end = " ")
# Driver Code
bfs(visited, graph, 'A')
A B C D E F
1.2.1 Explanation
Lines 3-10: The illustrated graph is represented using an adjacency list. An easy way to do this
in Python is to use a dictionary data structure, where each vertex has a stored list of its adjacent
nodes.
Line 12: visited is a list that is used to keep track of visited nodes.
Line 13: queue is a list that is used to keep track of nodes currently in the queue.
Line 29: The arguments of the bfs function are the visited list, the graph in the form of a dictionary,
and the starting node A.
Lines 15-26: bfs follows the algorithm described above:
It checks and appends the starting node to the visited list and the queue. Then, while the queue
contains elements, it keeps taking out nodes from the queue, appends the neighbors of that node
to the queue if they are unvisited, and marks them as visited. This continues until the queue is
empty.
4
The Algorithm: Pick any node. If it is unvisited, mark it as visited and recur on all its adjacent
nodes. Repeat until all the nodes are visited, or the node to be searched is found.
Implementation Consider this graph, implemented in the code below:
5
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
# Driver Code
dfs(visited, graph, 'A')
A
B
D
E
F
C
1.3.1 Explanation
Lines 2-9: The illustrated graph is represented using an adjacency list - an easy way to do it in
Python is to use a dictionary data structure. Each vertex has a list of its adjacent nodes stored.
Line 11: visited is a set that is used to keep track of visited nodes. Line 21: The dfs function
is called and is passed the visited set, the graph in the form of a dictionary, and A, which is the
starting node. Lines 13-18: dfs follows the algorithm described above: It first checks if the current
node is unvisited - if yes, it is appended in the visited set. Then for each neighbor of the current
node, the dfs function is invoked again. The base case is invoked when all the nodes are visited.
The function then returns.
6
[6]: from simpleai.search import CspProblem, backtrack
if __name__ == '__main__':
# Specify the variables
names = ('Mark', 'Julia', 'Steve', 'Amanda', 'Brian', 'Joanne', 'Derek',␣
,→'Allan', 'Michelle', 'Kelly')
7
(('Julia', 'Derek'), constraint_func),
(('Julia', 'Brian'), constraint_func),
(('Steve', 'Amanda'), constraint_func),
(('Steve', 'Allan'), constraint_func),
(('Steve', 'Michelle'), constraint_func),
(('Amanda', 'Michelle'), constraint_func),
(('Amanda', 'Joanne'), constraint_func),
(('Amanda', 'Derek'), constraint_func),
(('Brian', 'Derek'), constraint_func),
(('Brian', 'Kelly'), constraint_func),
(('Joanne', 'Michelle'), constraint_func),
(('Joanne', 'Amanda'), constraint_func),
(('Joanne', 'Derek'), constraint_func),
(('Joanne', 'Kelly'), constraint_func),
(('Derek', 'Kelly'), constraint_func),
]
Color mapping: