-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
114 lines (90 loc) · 3.68 KB
/
graph.c
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include "graph.h"
#include "constants.h"
#include "node.h"
#include "priorityqueue.h"
#include "queueentry.h"
struct Graph* initialiseGraph()
{
struct Graph* temp = malloc(sizeof(struct Graph));
temp->size = 0;
return temp;
}
void addNode(char name, char connections[NUMNODES - 1], int connectionWeights[NUMNODES - 1], struct Graph* graph, int x, int y){
// Index where node is stored is like a Hashtable, the index is from the ASCII character - 65
graph->nodes[name-65] = *(newNode(name, connections, connectionWeights, x , y));
graph->size++;
}
bool isRealNode(struct node val ){
return ((val.name <= 90) && (val.name >= 65) && (val.shortestPathVal <= LARGESTPATHVAL));
}
void printNodes(struct Graph* graph){
for (int i = 0; i < NUMLETTERS; i++)
{
if (isRealNode(graph->nodes[i]))
{
printf("%c ", graph->nodes[i].name);
for (int j = 0; graph->nodes[i].connections[j] != '\0'; j++) {
printf("%c(%d) ", graph->nodes[i].connections[j], graph->nodes[i].connectionWeights[j]);
}
printf("\n");
}
}
}
void aStarSearch(struct Graph* graph, char start, char end)
{
struct PriorityQueue* pq = initialisePriorityQueue();
int locationStart = start - 65;
graph->nodes[locationStart].shortestPathVal = 0;
graph->nodes[locationStart].directDistancePlusShortestDistance = graph->nodes[locationStart].directDistance;
graph->nodes[locationStart].shortestPathPrev = start;
int locationEnd = end - 65;
for (int i = 0; i < NUMLETTERS; i++)
{
if (isRealNode(graph->nodes[i]))
{
graph->nodes[i].directDistance = round(sqrt((graph->nodes[locationEnd].x - graph->nodes[i].x)*(graph->nodes[locationEnd].x - graph->nodes[i].x) + (graph->nodes[locationEnd].y - graph->nodes[i].y)*(graph->nodes[locationEnd].y - graph->nodes[i].y)));
if (i==start-65){graph->nodes[locationStart].directDistancePlusShortestDistance = graph->nodes[locationStart].directDistance;}
enqueue(&graph->nodes[i],pq);
}
}
struct node* currentNode;
struct node* nextNode;
while (pq->size != 0) {
locationStart = dequeue(pq)->node->name - 65;
currentNode = &graph->nodes[locationStart];
currentNode->explored = true;
if (currentNode->name == end){
break;
}
for (int i = 0; graph->nodes[locationStart].connections[i] != '\0'; i++) {
nextNode = &graph->nodes[graph->nodes[locationStart].connections[i] - 65];
if (nextNode->explored == false &&
currentNode->connectionWeights[i] + currentNode->shortestPathVal < nextNode->shortestPathVal) {
nextNode->shortestPathVal = currentNode->shortestPathVal + currentNode->connectionWeights[i];
nextNode->shortestPathPrev = currentNode->name;
nextNode->directDistancePlusShortestDistance = nextNode->shortestPathVal + nextNode->directDistance;
struct node *temp = dequeueNode(nextNode, pq);
enqueue(temp, pq);
}
}
}
for (int i = 0; i < NUMLETTERS; i++)
{
if (isRealNode(graph->nodes[i]) && graph->nodes[i].name == end)
{
printf("The shortest path from %c to %c is:", start, end);
currentNode = &graph->nodes[i];
while(currentNode->name != start)
{
printf(" %c", currentNode->name);
currentNode = &graph->nodes[currentNode->shortestPathPrev - 65];
}
printf(" %c", start);
break;
}
}
}