Skip to content

Commit b2d165d

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
Getting New Changes
2 parents 1bed547 + f07c8d5 commit b2d165d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2556
-371
lines changed

Diff for: .travis.yml

-14
This file was deleted.

Diff for: Graphs/A*.py

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
grid = [[0, 1, 0, 0, 0, 0],
3+
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
4+
[0, 1, 0, 0, 0, 0],
5+
[0, 1, 0, 0, 1, 0],
6+
[0, 0, 0, 0, 1, 0]]
7+
8+
'''
9+
heuristic = [[9, 8, 7, 6, 5, 4],
10+
[8, 7, 6, 5, 4, 3],
11+
[7, 6, 5, 4, 3, 2],
12+
[6, 5, 4, 3, 2, 1],
13+
[5, 4, 3, 2, 1, 0]]'''
14+
15+
init = [0, 0]
16+
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
17+
cost = 1
18+
19+
#the cost map which pushes the path closer to the goal
20+
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
21+
for i in range(len(grid)):
22+
for j in range(len(grid[0])):
23+
heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
24+
if grid[i][j] == 1:
25+
heuristic[i][j] = 99 #added extra penalty in the heuristic map
26+
27+
28+
#the actions we can take
29+
delta = [[-1, 0 ], # go up
30+
[ 0, -1], # go left
31+
[ 1, 0 ], # go down
32+
[ 0, 1 ]] # go right
33+
34+
35+
#function to search the path
36+
def search(grid,init,goal,cost,heuristic):
37+
38+
closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
39+
closed[init[0]][init[1]] = 1
40+
action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid
41+
42+
x = init[0]
43+
y = init[1]
44+
g = 0
45+
f = g + heuristic[init[0]][init[0]]
46+
cell = [[f, g, x, y]]
47+
48+
found = False # flag that is set when search is complete
49+
resign = False # flag set if we can't find expand
50+
51+
while not found and not resign:
52+
if len(cell) == 0:
53+
resign = True
54+
return "FAIL"
55+
else:
56+
cell.sort()#to choose the least costliest action so as to move closer to the goal
57+
cell.reverse()
58+
next = cell.pop()
59+
x = next[2]
60+
y = next[3]
61+
g = next[1]
62+
f = next[0]
63+
64+
65+
if x == goal[0] and y == goal[1]:
66+
found = True
67+
else:
68+
for i in range(len(delta)):#to try out different valid actions
69+
x2 = x + delta[i][0]
70+
y2 = y + delta[i][1]
71+
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
72+
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
73+
g2 = g + cost
74+
f2 = g2 + heuristic[x2][y2]
75+
cell.append([f2, g2, x2, y2])
76+
closed[x2][y2] = 1
77+
action[x2][y2] = i
78+
invpath = []
79+
x = goal[0]
80+
y = goal[1]
81+
invpath.append([x, y])#we get the reverse path from here
82+
while x != init[0] or y != init[1]:
83+
x2 = x - delta[action[x][y]][0]
84+
y2 = y - delta[action[x][y]][1]
85+
x = x2
86+
y = y2
87+
invpath.append([x, y])
88+
89+
path = []
90+
for i in range(len(invpath)):
91+
path.append(invpath[len(invpath) - 1 - i])
92+
print "ACTION MAP"
93+
for i in range(len(action)):
94+
print action[i]
95+
96+
return path
97+
98+
a = search(grid,init,goal,cost,heuristic)
99+
for i in range(len(a)):
100+
print a[i]
101+

Diff for: Graphs/Breadth_First_Search.py

-70
This file was deleted.

Diff for: Graphs/Deep_First_Search.py

-32
This file was deleted.

0 commit comments

Comments
 (0)