-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
136 lines (123 loc) · 3.6 KB
/
script.js
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
// Define the metro stations and their connections (graph)
const metroStations = {
'Namma Metro Station': {
'MG Road Station': 10,
'Jayanagar Station': 5,
},
'MG Road Station': {
'Namma Metro Station': 10,
'Jayanagar Station': 10,
'Lakshmi Nagar Station': 20,
},
'Jayanagar Station': {
'Namma Metro Station': 5,
'MG Road Station': 10,
'Lakshmi Nagar Station': 10,
'Peenya Industry Station': 20,
},
'Lakshmi Nagar Station': {
'MG Road Station': 20,
'Jayanagar Station': 10,
'Peenya Industry Station': 10,
'Silk Board Station': 30,
},
'Peenya Industry Station': {
'Jayanagar Station': 20,
'Lakshmi Nagar Station': 10,
'Silk Board Station': 20,
},
'Silk Board Station': {
'Lakshmi Nagar Station': 30,
'Peenya Industry Station': 20,
'KR Puram Station': 20,
},
'KR Puram Station': {
'Silk Board Station': 20,
'Hosahalli Station': 20,
},
'Hosahalli Station': {
'KR Puram Station': 20,
'Sarjapur Road Station': 20,
},
'Sarjapur Road Station': {
'Hosahalli Station': 20,
'Kengeri Station': 20,
},
'Kengeri Station': {
'Sarjapur Road Station': 20,
'Yelagirinagar Station': 20,
},
'Yelagirinagar Station': {
'Kengeri Station': 20,
'Mahadevapura Station': 20,
},
'Mahadevapura Station': {
'Yelagirinagar Station': 20,
'Hosakote Station': 20,
},
'Hosakote Station': {
'Mahadevapura Station': 20,
'Attibele Station': 20,
},
'Attibele Station': {
'Hosakote Station': 20,
},
};
// Function to calculate the shortest route and fare
function calculate() {
// Get the source and destination stations from the dropdowns
const sourceStation = document.getElementById('source').value;
const destinationStation = document.getElementById('destination').value;
// Check if source and destination are selected
if (sourceStation === '' || destinationStation === '') {
alert('Please select source and destination stations.');
return;
}
// Dijkstra's algorithm implementation to find the shortest route and fare
const stations = Object.keys(metroStations);
const INF = Number.MAX_SAFE_INTEGER;
// Create a distance matrix and initialize with Infinity
const distances = {};
stations.forEach((station) => (distances[station] = INF));
distances[sourceStation] = 0;
const visited = {};
const path = {};
while (true) {
let currentStation = null;
// Find the nearest station
stations.forEach((station) => {
if (
!visited[station] &&
(currentStation === null ||
distances[station] < distances[currentStation])
) {
currentStation = station;
}
});
if (currentStation === null || distances[currentStation] === INF) {
break;
}
visited[currentStation] = true;
// Update distances to adjacent stations
for (const neighbor in metroStations[currentStation]) {
const distance =
distances[currentStation] + metroStations[currentStation][neighbor];
if (distance < distances[neighbor]) {
distances[neighbor] = distance;
path[neighbor] = currentStation;
}
}
}
// Build the route and calculate the fare
const route = [];
let current = destinationStation;
while (current !== sourceStation) {
route.unshift(current);
current = path[current];
}
route.unshift(sourceStation);
const fare = distances[destinationStation];
// Display the results
document.getElementById('route').textContent = route.join(' -> ');
document.getElementById('fare').textContent = fare + ' units'; // You can replace 'units' with your currency
}