-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.py
72 lines (53 loc) · 1.92 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from Search_Algorithms import BFS, DFS, Greedy, AStar_search
#initial state
n = int(input("Enter n\n"))
print("Enter your" ,n,"*",n, "puzzle")
root = []
for i in range(0,n*n):
p = int(input())
root.append(p)
print("The given state is:", root)
#count the number of inversions
def inv_num(puzzle):
inv = 0
for i in range(len(puzzle)-1):
for j in range(i+1 , len(puzzle)):
if (( puzzle[i] > puzzle[j]) and puzzle[i] and puzzle[j]):
inv += 1
return inv
def solvable(puzzle): #check if initial state puzzle is solvable: number of inversions should be even.
inv_counter = inv_num(puzzle)
if (inv_counter %2 ==0):
return True
return False
#1,8,2,0,4,3,7,6,5 is solvable
#2,1,3,4,5,6,7,8,0 is not solvable
from time import time
if solvable(root):
print("Solvable, please wait. \n")
time1 = time()
BFS_solution = BFS(root, n)
BFS_time = time() - time1
print('BFS Solution is ', BFS_solution[0])
print('Number of explored nodes is ', BFS_solution[1])
print('BFS Time:', BFS_time , "\n")
time2 = time()
DFS_solution = DFS(root, n)
DFS_time = time() - time2
print('DFS Solution is ', DFS_solution[0])
print('Number of explored nodes is ', DFS_solution[1])
print('DFS Time:', DFS_time, "\n")
time3 = time()
Greedy_solution = Greedy(root, n)
Greedy_time = time() - time3
print('Greedy Solution is ', Greedy_solution[0])
print('Number of explored nodes is ', Greedy_solution[1])
print('Greedy Time:', Greedy_time , "\n")
time4 = time()
AStar_solution = AStar_search(root, n)
AStar_time = time() - time4
print('A* Solution is ', AStar_solution[0])
print('Number of explored nodes is ', AStar_solution[1])
print('A* Time:', AStar_time)
else:
print("Not solvable")