-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.c
102 lines (89 loc) · 2.51 KB
/
dijkstra.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
#include "dijkstra.h"
/******************************************************************************
* dijkstra()
*
* Arguments: fp - pointer to the output
* graph
* main_array
* src - source word
* end - end words
* max - maximum changes
*
* Returns: void
*
* Description: find the shortest dijkstra
*
*****************************************************************************/
void dijkstra(FILE* fp, Graph* graph, char** main_array, int src,int end, int max)
{
//V: extract number of vertices from graph
int V = graph->num_V;
//cost: int vector that saves the
//cost from the source to each vertex
int cost[V];
//prev: int vector that saves the previous vectors
int prev[V];
//initialize prev
for(int v = 0; v<V; v++)
prev[v] = NO_PREV;
//create heap
Heap* h = createHeap(V);
//create array of heap nodes
HeapNode* pointerarray[V];
//initialize heap nodes
for (int v = 0; v < V; v++)
{
cost[v] = INT_MAX;
h->array[v] = newHeapNode(v,cost[v]);
h->pos[v] = v;
pointerarray[v]= h->array[v];
}
free(h->array[src]);
//create source heap node
h->array[src] = newHeapNode(src, cost[src]);
pointerarray[src] = h->array[src];
h->pos[src] = src;
cost[src] = 0;
decreaseKey(h, src, cost[src]);
h->size = V;
//while h contains all nodes whose shortest
//distance hasn't been finalized
while (!isEmpty(h))
{
//extracts the vertex with mininum
//distance value
HeapNode* heapNode =extractMinimum(h);
//stores the extracted vertex index
int u = heapNode->v;
//goes through all the adjacent vertices of u
//and updates the cost
AdjNode* pCrawl =graph->adj_list[u];
while (pCrawl != NULL)
{
int v = pCrawl->vertex;
//if we find the target vertex
if(u == end)
break;
//if the distance to the vertex v is not initialized
//and the cost to v through u is less than the previous one
if (isInHeap(h, v) && cost[u] != INT_MAX && pCrawl->cost + cost[u] < cost[v] && pCrawl->cost <= max*max )
{
cost[v] = cost[u] + pCrawl->cost;
prev[v]=u;
// update distance
decreaseKey(h, v, cost[v]);
}
pCrawl = pCrawl->next;
}
}
//print solution
printSolution(fp, src, end, cost, prev,V, main_array);
//free heap
for (int v = 0; v < V; v++)
{
free(pointerarray[v]);
}
free(h->pos);
free(h->array);
free(h);
}