The fastest implementation of Dijkstra's algorithm in javascript

A weighted graph is given. You need to solve 2 problems: find the length of paths from the starting point to all points and also get (restore) the shortest (cheapest, optimal, etc.) path from the starting point to any other.

Dijkstra's algorithm suggests itself logically.

BUT!

You need the implementation to work quickly.
For an estimate: for 160 vertices and 350 edges, executing in 3 milliseconds is slow.

I tried to find implementations of this a JavaScript algorithm on the internet. However, either they were too slow
Like this one
or this

Either worked incorrectly, i.e. looped, although the input data was correct
for example, this is

I also attach here an example with ~160 vertices and ~350 edges, so that you don't have to invent such an example yourself to measure the time

var graph = {

40: {41: 105.16330203209385, 49: 35.95854287280075, 91: 365.41610033805244},
41: {40: 105.16330203209385, 42: 52.89007014380016, 96: 284.90007530099},
42: {41: 52.89007014380016, 43: 49.86819899505683, 86: 357.4019834882249, 161: 291.8128348465009, 2551: 291.8207238893861, 8177: 125.51245953452907},
43: {42: 49.86819899505683, 44: 79.18079075547753, 69: 773.7300245357471, 137: 463.41237148188674, 168: 236.50394195087094, 184: 518.4807809544371, player: 424.2989071724448},
44: {43: 79.18079075547753, 45: 90.83360606053981, 90: 259.8407665207612, 95: 279.411144869276, 190: 246.32285632446946, 194: 224.5772017876507, 8177: 93.88798917266715},
45: {44: 90.83360606053981, 46: 40.2601818009928},
46: {45: 40.2601818009928, 47: 80.31837977090295, 160: 341.52947036128677, 167: 282.7221748698908},
47: {46: 80.31837977090295, 48: 45.378520844445866, 113: 84.10938294083888, 137: 513.6122449619479, 194: 309.30390726060676},
48: {47: 45.378520844445866, 49: 42.01982543140207},
49: {40: 35.95854287280075, 48: 42.01982543140207, 115: 80.73588919181459, 199: 382.4252940520127, 2551: 245.95308452576458, player: 356.16663709256125},
53: {54: 69.82905046610144},
54: {53: 69.82905046610144, 55: 36.89122723725341, 64: 672.2998601462713, db2: 445.06616939812926, db3: 1158.5442884850133},
55: {54: 36.89122723725341, 56: 25.28985792675144},
56: {55: 25.28985792675144},
60: {61: 35.416497801167246, 69: 33.91097748061123},
61: {60: 35.416497801167246, 79: 182.83357107127233},
64: {54: 672.2998601462713, db3: 486.7536481150631},
69: {43: 773.7300245357471, 60: 33.91097748061123, 134: 381.86594263303124, 138: 373.17392477929263, 168: 547.4030201239677, 184: 255.73717566383445, 8177: 729.9618210601787},
70: {79: 61.87457805450789},
73: {74: 27.53156245061905},
74: {73: 27.53156245061905, 75: 60.42930213144073, 214: 157.5379813237738},
75: {74: 60.42930213144073, 76: 23.04449141268447, 211: 130.4532561303535},
76: {75: 23.04449141268447, 77: 33.32706145836687, 145: 338.17803666210386},
77: {76: 33.32706145836687, 78: 44.436786980587094, 140: 369.8417462894574, 227: 370.64854634825195},
78: {77: 44.436786980587094, 79: 95.67987146765796},
79: {61: 182.83357107127233, 70: 61.87457805450789, 78: 95.67987146765796, 134: 458.3837359394246, 144: 164.08339248404633, 184: 285.80953218952124, 189: 378.9327568459244, 199: 522.8087714711911, 214: 247.458530942144, 219: 221.26599171060894, 222: 328.8566975982385},
80: {81: 31.99091133666505, 89: 47.37309491054031},
81: {80: 31.99091133666505, 82: 18.482125019131523, 92: 186.10076385620917},
82: {81: 18.482125019131523},
85: {86: 38.30910387902856, 160: 140.58712091180325, 8177: 277.7985746888594},
86: {42: 357.4019834882249, 85: 38.30910387902856, 87: 34.17590192185435, 98: 253.9010464199508},
87: {86: 34.17590192185435, 88: 31.717045454318647},
88: {87: 31.717045454318647, 89: 46.505807395662835},
89: {80: 47.37309491054031, 88: 46.505807395662835, 94: 11.77035161957156, 95: 38.52418784028625, 164: 238.16603610755064, 169: 246.42327116515582, 190: 548.4405778257062},
90: {44: 259.8407665207612, 91: 80.6040051899815, 99: 63.414851389705724},
91: {40: 365.41610033805244, 90: 80.6040051899815, 92: 129.31862590221718, 2551: 340.02623533449474, player: 350.04622145258276},
92: {81: 186.10076385620917, 91: 129.31862590221718, 93: 17.014794638367174},
93: {92: 17.014794638367174, 94: 107.81212095323181},
94: {89: 11.77035161957156, 93: 107.81212095323181, 95: 48.81440169056348, 169: 257.5421204372971, 190: 559.0816703975345},
95: {44: 279.411144869276, 89: 38.52418784028625, 94: 48.81440169056348, 96: 51.965228022387045, 8177: 188.1610655675721},
96: {41: 284.90007530099, 95: 51.965228022387045, 97: 42.493801157402245},
97: {96: 42.493801157402245, 98: 76.51874618264816},
98: {86: 253.9010464199508, 97: 76.51874618264816, 99: 50.256920854111094, 162: 254.03531208114921, 2551: 369.4527333309115, player: 430.38030020191263},
99: {90: 63.414851389705724, 98: 50.256920854111094, 168: 309.41194345952965, 184: 606.1752354860017, 194: 412.59609038621943, 8177: 128.39521986850684},
105: {106: 40.502652460422986, rb3: 596.3768944663615, rb4: 218.56615168137122},
106: {105: 40.502652460422986},
112: {113: 56.890826754690984},
113: {47: 84.10938294083888, 112: 56.890826754690984, 114: 44.971521845365736, 199: 377.72278954722674, 2551: 250.82487360351706},
114: {113: 44.971521845365736, 115: 30.39941919684934, player: 406.2603301787468},
115: {49: 80.73588919181459, 114: 30.39941919684934},
121: {122: 26.4764307012406, 180: 333.4919506962407, 197: 224.11574141326125},
122: {121: 26.4764307012406, 123: 85.09854045024798},
123: {122: 85.09854045024798, 124: 66.06615282686656, 188: 240.1895168095516, 222: 145.98976289724948},
124: {123: 66.06615282686656, 125: 68.9626658726196, 154: 285.63443323423286, 197: 81.28243262604742, 199: 120.03368992094568, 200: 521.1161335753012, 220: 132.9232424883914, 235: 502.7356682290173},
125: {124: 68.9626658726196, 126: 38.943242837290754},
126: {125: 38.943242837290754, 127: 33.163731118050336, 130: 488.36987469432336},
127: {126: 33.163731118050336, 128: 33.404198741679295, 189: 190.281661870841, 222: 179.82995490915707},
128: {127: 33.404198741679295},
130: {126: 488.36987469432336, 131: 39.32473302511219, 139: 61.03461545929612, 165: 81.30716116326501, 189: 331.8650824133968, 197: 301.74331187036734},
131: {130: 39.32473302511219, 132: 55.42370986351895},
132: {131: 55.42370986351895, 133: 79.69349323544078, 166: 67.3763049187362, 192: 318.54092414747043},
133: {132: 79.69349323544078, 134: 20.88052479596501},
134: {69: 381.86594263303124, 79: 458.3837359394246, 133: 20.88052479596501, 135: 35.59118973162082, 144: 610.3090410067813, 164: 180.61814056298095},
135: {134: 35.59118973162082, 136: 56.51867994261978, 185: 235.8916661215193},
136: {135: 56.51867994261978, 137: 63.301411556559216},
137: {43: 463.41237148188674, 47: 513.6122449619479, 136: 63.301411556559216, 138: 59.07343205194048, 180: 152.16660903789744, 193: 237.50078461204208},
138: {69: 373.17392477929263, 137: 59.07343205194048, 139: 36.593864364824086, 168: 174.26360890820277, 8177: 356.93660108485227},
139: {130: 61.03461545929612, 138: 36.593864364824086, 184: 163.52947726263545},
140: {77: 369.8417462894574, 141: 31.783831070674122, 149: 57.19080386036463, 157: 220.27163225083572},
141: {140: 31.783831070674122, 142: 43.52217897680467, 212: 355.7027373052974},
142: {141: 43.52217897680467, 143: 34.47915609273915, 153: 278.68421664307084, 219: 283.8179725289804},
143: {142: 34.47915609273915, 144: 63.30212258982895, 227: 121.85680795083613},
144: {79: 164.08339248404633, 134: 610.3090410067813, 143: 63.30212258982895, 145: 67.32562350561739, 185: 345.0079147553116, 223: 302.3593097992749},
145: {76: 338.17803666210386, 144: 67.32562350561739, 146: 40.002337310940405, 212: 308.5458007129418},
146: {145: 40.002337310940405, 147: 36.28002732885, 202: 114.02554819729143},
147: {146: 36.28002732885, 148: 38.80539895882165, 153: 272.4710445691966, 157: 236.17539343646496, 218: 360.2303518497902},
148: {147: 38.80539895882165, 149: 60.76422605278673},
149: {140: 57.19080386036463, 148: 60.76422605278673, 200: 219.7939434026094, 204: 201.55625431985254, 220: 185.04192904393753, 227: 77.76324371726739, 236: 258.1247404032064},
152: {153: 41.337936179115474, 227: 235.2074162235845},
153: {142: 278.68421664307084, 147: 272.4710445691966, 152: 41.337936179115474, 154: 77.56097298280768, 204: 368.14569546270917},
154: {124: 285.63443323423286, 153: 77.56097298280768, 155: 27.839694542272085, 209: 301.28170224107254, 221: 215.4335728798769, 235: 224.64469994242543},
155: {154: 27.839694542272085, 156: 18.982764068571615},
156: {155: 18.982764068571615, 157: 31.11961929802304},
157: {140: 220.27163225083572, 147: 236.17539343646496, 156: 31.11961929802304, 227: 228.18741588038438},
160: {46: 341.52947036128677, 85: 140.58712091180325, 161: 28.441599735803532, 169: 114.85066605391305, 8177: 140.33409704584713},
161: {42: 291.8128348465009, 160: 28.441599735803532, 162: 54.20012357975497},
162: {98: 254.03531208114921, 161: 54.20012357975497, 163: 41.3888146146421, 2551: 623.0399505311785},
163: {162: 41.3888146146421, 164: 40.25097587062904},
164: {89: 238.16603610755064, 134: 180.61814056298095, 163: 40.25097587062904, 165: 83.71679912082705},
165: {130: 81.30716116326501, 164: 83.71679912082705, 166: 47.13231992458931, 192: 301.51054155761403},
166: {132: 67.3763049187362, 165: 47.13231992458931, 167: 75.31708435243745},
167: {46: 282.7221748698908, 166: 75.31708435243745, 168: 22.06889237237542},
168: {43: 236.50394195087094, 69: 547.4030201239677, 99: 309.41194345952965, 138: 174.26360890820277, 167: 22.06889237237542, 169: 90.40807626927251, 8177: 182.71805709047803, player: 660.6320632458752},
169: {89: 246.42327116515582, 94: 257.5421204372971, 160: 114.85066605391305, 168: 90.40807626927251, 184: 387.60114332631235, 190: 304.43314188813645, 195: 246.22489560536593},
170: {},
171: {172: 39.00259005444423},
172: {171: 39.00259005444423, 173: 48.86649972594193},
173: {172: 48.86649972594193},
180: {121: 333.4919506962407, 137: 152.16660903789744, 181: 63.64776392942703, 189: 125.33020981473317, 197: 109.40475344201299},
181: {180: 63.64776392942703, 182: 24.718866358798138},
182: {181: 24.718866358798138, 183: 25.690918406222263},
183: {182: 25.690918406222263, 184: 38.751130184418095, 194: 197.83059642108458},
184: {43: 518.4807809544371, 69: 255.73717566383445, 79: 285.80953218952124, 99: 606.1752354860017, 139: 163.52947726263545, 169: 387.60114332631235, 183: 38.751130184418095, 185: 75.66100127301493, 8177: 478.2815951431},
185: {135: 235.8916661215193, 144: 345.0079147553116, 184: 75.66100127301493, 186: 49.6133887921204, 222: 218.04266558629692, 227: 305.5005550532781},
186: {185: 49.6133887921204, 187: 24.25053431206494},
187: {186: 24.25053431206494, 188: 42.8234357875276},
188: {123: 240.1895168095516, 187: 42.8234357875276, 189: 130.48152567212878},
189: {79: 378.9327568459244, 127: 190.281661870841, 130: 331.8650824133968, 180: 125.33020981473317, 188: 130.48152567212878, 195: 165.9262268374238, 199: 146.07459191521332, 219: 585.593015592556, 221: 105.54771303483847, 223: 128.16875394348722},
190: {44: 246.32285632446946, 89: 548.4405778257062, 94: 559.0816703975345, 169: 304.43314188813645, 191: 70.61802588549347, 199: 104.88079811045276, 8177: 337.2985237275377},
191: {190: 70.61802588549347, 192: 32.272029619118605},
192: {132: 318.54092414747043, 165: 301.51054155761403, 191: 32.272029619118605, 193: 41.35537604492597},
193: {137: 237.50078461204208, 192: 41.35537604492597, 194: 30.816119644011263},
194: {44: 224.5772017876507, 47: 309.30390726060676, 99: 412.59609038621943, 183: 197.83059642108458, 193: 30.816119644011263, 195: 31.40869971024647},
195: {169: 246.22489560536593, 189: 165.9262268374238, 194: 31.40869971024647, 196: 19.435562823007153, 8177: 318.945172309603},
196: {195: 19.435562823007153, 197: 107.1506698368598, 221: 251.92623057012156},
197: {121: 224.11574141326125, 124: 81.28243262604742, 130: 301.74331187036734, 180: 109.40475344201299, 196: 107.1506698368598, 198: 71.94959459518995},
198: {197: 71.94959459518995, 199: 53.21176924210634},
199: {49: 382.4252940520127, 79: 522.8087714711911, 113: 377.72278954722674, 124: 120.03368992094568, 189: 146.07459191521332, 190: 104.88079811045276, 198: 53.21176924210634, 222: 194.01671571159096},
200: {124: 521.1161335753012, 149: 219.7939434026094, 201: 51.59820366487804, 209: 57.03687052650553, 221: 446.669384670336},
201: {200: 51.59820366487804, 202: 82.8626939527005, 236: 90.37098960545049},
202: {146: 114.02554819729143, 201: 82.8626939527005, 203: 39.40638069110732, 218: 436.407907889379, 231: 137.42350047638314},
203: {202: 39.40638069110732, 204: 104.89480626042655},
204: {149: 201.55625431985254, 153: 368.14569546270917, 203: 104.89480626042655, 205: 63.30193294445094},
205: {204: 63.30193294445094},
209: {154: 301.28170224107254, 200: 57.03687052650553, 234: 116.05438254004737},
210: {211: 42.30165572597134, 219: 32.15172511880403},
211: {75: 130.4532561303535, 210: 42.30165572597134, 212: 60.0281194646555},
212: {141: 355.7027373052974, 145: 308.5458007129418, 211: 60.0281194646555, 213: 33.82825518940869, 227: 391.86061913738223},
213: {212: 33.82825518940869, 214: 42.54090537631494},
214: {74: 157.5379813237738, 79: 247.458530942144, 213: 42.54090537631494, 215: 49.888093699309934},
215: {214: 49.888093699309934, 216: 39.851515082343695},
216: {215: 39.851515082343695},
217: {218: 26.231546762251014},
218: {147: 360.2303518497902, 202: 436.407907889379, 217: 26.231546762251014, 219: 42.97667404536861},
219: {79: 221.26599171060894, 142: 283.8179725289804, 189: 585.593015592556, 210: 32.15172511880403, 218: 42.97667404536861, 222: 531.8522323663475, 227: 370.9990665742771},
220: {124: 132.9232424883914, 149: 185.04192904393753, 221: 57.38276622492863, 229: 24.18216024283879},
221: {154: 215.4335728798769, 189: 105.54771303483847, 196: 251.92623057012156, 200: 446.669384670336, 220: 57.38276622492863, 222: 81.23467948862621, 235: 428.52683452051633},
222: {79: 328.8566975982385, 123: 145.98976289724948, 127: 179.82995490915707, 185: 218.04266558629692, 199: 194.01671571159096, 219: 531.8522323663475, 221: 81.23467948862621, 223: 72.57090855008987},
223: {144: 302.3593097992749, 189: 128.16875394348722, 222: 72.57090855008987, 224: 61.0132077739784},
224: {223: 61.0132077739784, 225: 7.838317856740105},
225: {224: 7.838317856740105, 226: 30.448702787148285},
226: {225: 30.448702787148285, 227: 74.25253401337827},
227: {77: 370.64854634825195, 143: 121.85680795083613, 149: 77.76324371726739, 152: 235.2074162235845, 157: 228.18741588038438, 185: 305.5005550532781, 212: 391.86061913738223, 219: 370.9990665742771, 226: 74.25253401337827, 228: 59.677880235700854},
228: {227: 59.677880235700854, 229: 67.53231487179364},
229: {220: 24.18216024283879, 228: 67.53231487179364},
231: {202: 137.42350047638314, 232: 24.017731260242194},
232: {231: 24.017731260242194, 233: 14.363781980044742},
233: {232: 14.363781980044742, 234: 24.44948246300028},
234: {209: 116.05438254004737, 233: 24.44948246300028, 235: 39.324747502659484},
235: {124: 502.7356682290173, 154: 224.64469994242543, 221: 428.52683452051633, 234: 39.324747502659484, 236: 53.46540106587038},
236: {149: 258.1247404032064, 201: 90.37098960545049, 235: 53.46540106587038},
2551: {42: 291.8207238893861, 49: 245.95308452576458, 91: 340.02623533449474, 98: 369.4527333309115, 113: 250.82487360351706, 162: 623.0399505311785, 8177: 413.30376238306854},
8177: {42: 125.51245953452907, 44: 93.88798917266715, 69: 729.9618210601787, 85: 277.7985746888594, 95: 188.1610655675721, 99: 128.39521986850684, 138: 356.93660108485227, 160: 140.33409704584713, 168: 182.71805709047803, 184: 478.2815951431, 190: 337.2985237275377, 195: 318.945172309603, 2551: 413.30376238306854},
db2: {54: 445.06616939812926, lb1: 70.71067811865476},
db3: {54: 1158.5442884850133, 64: 486.7536481150631, rb3: 70.71067811865476},
lb1: {db2: 70.71067811865476, lb4: 814},
lb4: {lb1: 814, ub1: 70.71067811865476},
player: {43: 424.2989071724448, 49: 356.16663709256125, 91: 350.04622145258276, 98: 430.38030020191263, 114: 406.2603301787468, 168: 660.6320632458752},
rb3: {105: 596.3768944663615, db3: 70.71067811865476},
rb4: {105: 218.56615168137122, ub4: 70.71067811865476},
ub1: {lb4: 70.71067811865476, ub4: 1596},
ub4: {ub1: 1596, rb4: 70.71067811865476},
}

If you can somehow optimize the implementations that I have attached links to above, and from these optimizations they will work quickly, I will also be very grateful!

Author: Данил Черкашин, 2021-01-13

1 answers

The implementation of the classical Dijkstra algorithm specified by nörbörnën looks fine.

However, you have the case of a sparse graph, since the number of edges is comparable to the number of vertices, not the square of their number.

For this case, there is an efficient algorithm, described here in C++, working for O(E logV).

I have implemented a similar approach in Delphi for the grid graph and for the road graph. For 10^4 vertices, the acceleration was very high. significant.

 0
Author: MBo, 2021-01-13 19:32:59