forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_Algorithm.dart
152 lines (127 loc) · 3.13 KB
/
Dijkstra_Algorithm.dart
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
/*
Djikstra's algorithm (named after its discover, E.W. Dijkstra) solves the
problem of finding the shortest path from a point in a graph (the source)
to a destination.
It turns out that one can find the shortest paths from a given source to all
points in a graph in the same time, hence this problem is sometimes called
the single-source shortest paths problem.
*/
import 'dart:io';
var INT_MAX = 9223372036854775807;
int minDistance(dist, visited, n)
{
int min = INT_MAX, min_index;
for (var v = 0; v < n + 1; v++)
{
if (( visited[v] == false ) && ( dist[v] <= min ))
{
min = dist[v];
min_index = v;
}
}
return min_index;
}
void printsol(dist, n)
{
print('Vertex \t\t Distance from Source\n');
for (var i = 0; i < n + 1; i++)
{
print('${i} \t\t ${dist[i]}\n');
}
}
void dijkstra(graph, src, n)
{
var dist = new List(n + 1);
var visited = new List(n + 1);
for (var i = 0; i < n + 1; i++)
{
dist[i] = INT_MAX;
visited[i] = false;
}
dist[src] = 0;
for (var count = 0; count < n; count++)
{
var u = minDistance(dist, visited, n);
visited[u] = true;
for (var v = 0; v < n + 1; v++)
{
if ( !visited[v] && graph[u][v] > 0 && dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v] )
{
dist[v] = dist[u] + graph[u][v];
}
}
}
printsol(dist, n);
}
void main()
{
print('Enter number of nodes 0 to ?');
int n = int.parse(stdin.readLineSync());
var max_edges = (n + 1) * (n);
var adjmat = new List.generate(n + 1, (_) => new List(n + 1));
for(var i = 0; i <= n; i++)
{
for(var j = 0; j <= n; j++)
{
adjmat[i][j] = 0;
}
}
print('Enter in the following format\nsrc\ndest\nweight\n');
for(var i = 0; i < max_edges; i++)
{
var src = int.parse(stdin.readLineSync());
var dest = int.parse(stdin.readLineSync());
var weight = int.parse(stdin.readLineSync());
print('*' * 20);
if( (src == -1) && (dest == -1) )
{
break;
}
if( src > n || dest > n || src < 0 || dest < 0 )
{
print('Invalid edge!\n');
i--;
}
else
{
adjmat[src][dest] = weight;
}
}
dijkstra(adjmat, 0, n);
}
/*
Input:
Enter number of nodes 0 to ?
9
Enter in the following format
Source
Destination
Weight
*******************************************************
The adjacency matrix will look like this
admat=[[0, 14, 0, 7, 0, 0, 0, 8, 0, 10],
[14, 0, 8, 0, 0, 0, 0, 11, 0, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2, 0],
[7, 0, 7, 0, 9, 12, 0, 0, 0, 5],
[0, 0, 0, 9, 0, 0, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 0, 2, 0, 0, 11],
[0, 0, 0, 12, 0, 2, 0, 1, 6, 15],
[8, 11, 0, 0, 0, 0, 1, 0, 7, 0],
[0, 0, 2, 0, 0, 0, 6, 7, 0, 0],
[10, 0, 0, 5, 0, 11, 15, 0, 0, 0]];
*******************************************************
Output:
Distance from Source:
Vertex Distance
0 0
1 14
2 14
3 7
4 16
5 11
6 9
7 8
8 15
9 10
*/