-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
127 lines (107 loc) · 3.3 KB
/
dijkstra.cpp
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
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
// Number of vertices
int numV = 0;
// Calculate minimal distance
int minDistance(int dist[], bool visited[])
{
int min_distance = INT_MAX;
int min_vertex;
for (int i = 0; i < numV; i++)
if (visited[i] == false && dist[i] <= min_distance)
{
min_vertex = i;
min_distance = dist[i];
}
return min_vertex;
}
// Print best path
void printPath(int destination, int prev_vertex[])
{
if(prev_vertex[destination] != -1)
{
printPath(prev_vertex[destination], prev_vertex);
printf("->");
}
printf(" %d ", destination);
}
// Print distance of each vertex from source
void printDistance(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < numV; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Dijkstra Algorithm
void dijkstra(int graph[numV][numV], int source, int destination)
{
int dist[numV];
int prev_vertex[numV];
for(int i = 0; i < numV; i++)
prev_vertex[i] = -1;
bool visited[numV];
// Initialize all distances as maximum value
// Initialize all nodes as not visited
for (int i = 0; i < numV; i++)
{
dist[i] = INT_MAX;
visited[i] = false;
}
// Initialize source distance to source as 0
dist[source] = 0;
for (int i = 0; i < numV-1; i++)
{
// Find the minimal distance to vertices not yet visited
// and select vertex with minimal distance
int selected_vertex = minDistance(dist, visited);
// Mark the selected vertex as processed
visited[selected_vertex] = true;
// Get the distance to the adjacent vertices of selected vertex
for (int j = 0; j < numV; j++)
// Test if there is a edge from vertex(j) to selected_vertex
// and test if vertex(j) has not been visited yet
if(graph[selected_vertex][j] && !visited[j] && dist[selected_vertex] != INT_MAX)
{
int totalDistance = dist[selected_vertex] + graph[selected_vertex][j];
// if totalDistance smaller than older distance update value
if(totalDistance < dist[j])
{
dist[j] = totalDistance;
prev_vertex[j] = selected_vertex;
}
}
}
// print the constructed distance array
printDistance(dist, numV);
printf("Best path:\n");
printPath(destination,prev_vertex );
printf("\n");
}
int main()
{
numV = 9;
int graph[9][9] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0,8);
printf("\n\n");
numV = 7;
int graphNew[7][7] = {{0,1,0,0,0,0,0},
{1,0,3,6,0,0,0},
{0,3,0,0,0,1,0},
{0,6,0,0,1,0,0},
{0,0,0,1,0,0,2},
{0,0,1,0,0,0,1},
{0,0,0,0,2,1,0}
};
dijkstra(graphNew, 0,6);
return 0;
}