-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA-Star Algorithm.cpp
More file actions
242 lines (202 loc) · 4.56 KB
/
A-Star Algorithm.cpp
File metadata and controls
242 lines (202 loc) · 4.56 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include<iostream>
#include<conio.h>
#include<malloc.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
using namespace std;
int state[MAX];
int N;
int adj[MAX][MAX];
int heuristic[MAX];
int length[MAX][MAX];
int temp_cost = 0;
struct node {
int fn;
int value;
int index;
int gn;
struct node* next;
}*start = NULL, * q, * temp, * new1;
//Function to insert in priority queue
void insert(struct node m)
{
int item = m.index, itprio = m.value, cost=m.gn;
new1 = (node*)malloc(sizeof(node));
//temp_cost += cost;
int a = cost + itprio;
new1->index = item;
new1->value = itprio;
new1->fn = a;
new1->next = NULL;
if (start == NULL)
{
start = new1;
}
else if (start != NULL && a <= start->fn)
{
new1->next = start;
start = new1;
}
else
{
q = start;
while (q->next != NULL && q->next->fn <= a)
{
q = q->next;
}
new1->next = q->next;
q->next = new1;
}
}
//Function for popping from the Priority Queue.
struct node del()
{
struct node n;
n.index = -1;
n.value = -1;
if (start == NULL)
{
printf("\nQUEUE UNDERFLOW\n");
}
else
{
n.index = start->index;
n.value = start->value;
new1 = start;
start = start->next;
}
return n;
}
//Function to determine if the Priority Queue is empty.
int empty() {
if (start == NULL)
return 1;
return 0;
}
int main() {
int origin, dest, value;
int cost;
//cin number of nodes
cout << "Enter the number of nodes" << endl;
cin >> N;
//max edges
int max_edges = N * (N - 1) / 2;
//input graph
for (int i = 1; i <= max_edges; i++) {
cout << "Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node" << endl;
cin >> origin >> dest >> cost >> value;
if (origin == -1 || dest == -1) {
break;
}
if (origin >= N || dest >= N || origin < 0 || dest < 0) {
cout << "invalid edge" << endl;
i--;
}
else {
adj[origin][dest] = 1;
heuristic[dest] = value;
length[origin][dest] = cost;
}
}
int start, goal;
//input start and goal
cout << "Enter the Source Node and Goal Node" << endl;
cin >> start >> goal;
for (int i = 0; i < N; i++) {
state[i] = initial;
}
//cout adjacency matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << adj[i][j] << " ";
}
cout << endl;
}
cout << endl;
//cout cost matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << length[i][j] << " ";
}
cout << endl;
}
cout << endl;
//cout heuristic values of nodes
for (int i = 0; i < N; i++) {
cout << heuristic[i] << " ";
}
cout << endl;
int temp_length;
struct node n, m;
m.index = start;
m.value = heuristic[1];
m.gn = 0;
insert(m);
state[m.index] = visited;
//best first search
cout << endl << endl << "The Optimal Path from Source to Goal is: " << endl;
while (!empty()) {
n = del();
state[n.index] = visited;
cout << n.index << " ";
if (n.index == goal) {
break;
}
else {
for (int i = 0; i < N; i++) {
if (adj[n.index][i] == 1 && state[i] == initial) {
m.gn = length[n.index][i];
m.index = i;
m.value = heuristic[i];
insert(m);
}
}
}
}
}
/*
Enter the number of nodes
7
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
0 1 3 11
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
0 2 4 12
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
1 3 7 6
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
1 4 10 4
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
3 4 2 4
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
2 4 12 4
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
2 5 5 11
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
4 6 5 0
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
5 6 16 0
Enter the origin and destination of edge and cost of the edge and heuristic value of the destination node
-1 -1 -1 -1
Enter the Source Node and Goal Node
0
6
0 1 1 0 0 0 0
0 0 0 1 1 0 0
0 0 0 0 1 1 0
0 0 0 0 1 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 1
0 0 0 0 0 0 0
0 3 4 0 0 0 0
0 0 0 7 10 0 0
0 0 0 0 12 5 0
0 0 0 0 2 0 0
0 0 0 0 0 0 5
0 0 0 0 0 0 16
0 0 0 0 0 0 0
0 11 12 6 4 11 0
The Optimal Path from Source to Goal is:
0 1 3 4 6
*/