Created
April 23, 2019 17:17
-
-
Save ktilcu/44c9039c1fb7851634c4153e884bbd9d to your computer and use it in GitHub Desktop.
BFS // source https://jsbin.com/bemexih
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="BFS"> </head> | |
<body> | |
<script> | |
</script> | |
<script id="jsbin-javascript"> | |
// noprotect | |
'use strict'; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var rels = "2 3,34 35,50 51,98 99,114 115,130 131,146 147,50 54,2 7,50 55,114 120,98 105,34 42,98 106,66 76,66 77,82 93,82 94,18 44,18 51,3 4,19 20,35 36,83 84,99 100,115 116,131 132,3 7,51 55,3 8,51 56,115 120,147 152,115 121,99 106,19 27,99 107,67 78,83 95,4 5,20 21,52 53,68 69,84 85,100 101,116 117,132 133,4 8,4 9,116 121,116 122,20 27,52 59,100 107,20 28,100 108,68 79,84 95,84 96,5 6,21 22,69 70,85 86,101 102,117 118,149 150,5 9,5 10,117 122,37 43,53 59,117 123,21 28,101 108,21 29,101 109,69 79,69 80,85 96,22 23,54 55,86 87,102 103,118 119,6 10,134 138,6 11,118 123,54 60,118 124,22 29,54 61,102 109,102 110,70 80,70 81,39 40,7 8,55 56,71 72,87 88,135 136,135 139,39 44,119 124,135 140,7 13,55 61,119 125,7 14,55 62,103 110,103 111,71 83,24 25,8 9,40 41,72 73,120 121,152 153,40 44,136 140,40 45,8 14,24 30,56 62,8 15,56 63,120 128,104 113,120 129,72 83,72 84,9 10,25 26,41 42,73 74,105 106,121 122,41 45,137 141,25 30,137 142,9 15,25 31,9 16,89 97,121 129,105 114,121 130,57 67,73 84,73 85,10 11,74 75,90 91,106 107,122 123,138 139,26 31,10 16,26 32,138 144,10 17,90 98,106 114,122 130,90 99,106 115,122 131,58 68,74 85,74 86,11 12,27 28,75 76,91 92,107 108,123 124,139 140,43 46,11 17,139 145,91 99,107 115,123 131,91 100,107 116,123 132,27 37,59 69,59 70,75 86,75 87,44 45,28 29,60 61,76 77,92 93,108 109,124 125,140 145,140 146,92 100,108 116,124 132,92 101,108 117,124 133,28 38,60 71,76 87,76 88,13 14,61 62,93 94,109 110,125 126,141 142,13 19,13 20,141 148,93 101,109 117,125 133,29 38,93 102,109 118,61 71,61 72,77 88,14 15,30 31,46 47,62 63,110 111,126 127,46 48,46 49,14 20,142 148,14 21,94 102,110 118,30 39,94 103,110 119,62 72,62 73,78 89,15 16,31 32,63 64,79 80,95 96,111 112,47 49,15 21,15 22,127 134,143 150,31 39,111 119,31 40,63 73,63 74,79 90,79 91,16 17,0 1,32 33,48 49,64 65,80 81,128 129,16 22,16 23,144 151,32 40,32 41,128 137,64 74,64 75,80 91,80 92,1 2,33 34,65 66,81 82,129 130,145 146,1 3,17 23,97 104,33 41,129 137,33 42,65 75,65 76,81 92,81 93"; | |
var pairs = rels.split(',').map(function (x) { | |
return x.split(' ').map(function (y) { | |
return parseInt(y); | |
}); | |
}); | |
var zoneCount = 154; | |
var zones = []; | |
var store = new Array(zoneCount).fill('').map(function (x) { | |
return []; | |
}); | |
var continents = {}; | |
for (var i = 0; i < zoneCount; i++) { | |
zones[i] = { id: i, neighbors: [] }; | |
} | |
for (var i = 0, len = pairs.length; i < len; i++) { | |
var v1 = pairs[i][0]; | |
var v2 = pairs[i][1]; | |
zones[v1].neighbors.push(v2); | |
zones[v2].neighbors.push(v1); | |
} | |
for (var i = 0, leni = zones.length; i < leni; i++) { | |
for (var j = 0, len = zones.length; j < len; j++) { | |
if (i === j) continue; | |
var path = bfs(i, j); | |
} | |
} | |
console.log('done'); | |
function bfs(start, end) { | |
var _continents$start; | |
var q = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | |
if (store[start][end] !== undefined) return store[start][end]; | |
var visited = {}; | |
var p = null; | |
q.push([start]); | |
while (q.length > 0) { | |
var path = q.shift(); | |
var last = path[path.length - 1]; | |
if (last === end && !p) { | |
p = path; | |
store[start][end] = path; | |
break; | |
} | |
store[start][last] = path; | |
var neighbors = zones[last].neighbors; | |
var j = 0, | |
len = neighbors.length; | |
while (j < len) { | |
var next = neighbors[j]; | |
if (!visited[next]) { | |
visited[next] = true; | |
q.push([].concat(_toConsumableArray(path), [next])); | |
} | |
j++; | |
} | |
} | |
if (!p) { | |
store[start][end] = null; | |
store[end][start] = null; | |
} | |
var v = Object.keys(visited).map(function (x) { | |
return parseInt(x); | |
}); | |
continents[start] = continents[start] ? (_continents$start = continents[start]).add.apply(_continents$start, _toConsumableArray(v)) : new Set(v); | |
return p; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// noprotect | |
var rels = "2 3,34 35,50 51,98 99,114 115,130 131,146 147,50 54,2 7,50 55,114 120,98 105,34 42,98 106,66 76,66 77,82 93,82 94,18 44,18 51,3 4,19 20,35 36,83 84,99 100,115 116,131 132,3 7,51 55,3 8,51 56,115 120,147 152,115 121,99 106,19 27,99 107,67 78,83 95,4 5,20 21,52 53,68 69,84 85,100 101,116 117,132 133,4 8,4 9,116 121,116 122,20 27,52 59,100 107,20 28,100 108,68 79,84 95,84 96,5 6,21 22,69 70,85 86,101 102,117 118,149 150,5 9,5 10,117 122,37 43,53 59,117 123,21 28,101 108,21 29,101 109,69 79,69 80,85 96,22 23,54 55,86 87,102 103,118 119,6 10,134 138,6 11,118 123,54 60,118 124,22 29,54 61,102 109,102 110,70 80,70 81,39 40,7 8,55 56,71 72,87 88,135 136,135 139,39 44,119 124,135 140,7 13,55 61,119 125,7 14,55 62,103 110,103 111,71 83,24 25,8 9,40 41,72 73,120 121,152 153,40 44,136 140,40 45,8 14,24 30,56 62,8 15,56 63,120 128,104 113,120 129,72 83,72 84,9 10,25 26,41 42,73 74,105 106,121 122,41 45,137 141,25 30,137 142,9 15,25 31,9 16,89 97,121 129,105 114,121 130,57 67,73 84,73 85,10 11,74 75,90 91,106 107,122 123,138 139,26 31,10 16,26 32,138 144,10 17,90 98,106 114,122 130,90 99,106 115,122 131,58 68,74 85,74 86,11 12,27 28,75 76,91 92,107 108,123 124,139 140,43 46,11 17,139 145,91 99,107 115,123 131,91 100,107 116,123 132,27 37,59 69,59 70,75 86,75 87,44 45,28 29,60 61,76 77,92 93,108 109,124 125,140 145,140 146,92 100,108 116,124 132,92 101,108 117,124 133,28 38,60 71,76 87,76 88,13 14,61 62,93 94,109 110,125 126,141 142,13 19,13 20,141 148,93 101,109 117,125 133,29 38,93 102,109 118,61 71,61 72,77 88,14 15,30 31,46 47,62 63,110 111,126 127,46 48,46 49,14 20,142 148,14 21,94 102,110 118,30 39,94 103,110 119,62 72,62 73,78 89,15 16,31 32,63 64,79 80,95 96,111 112,47 49,15 21,15 22,127 134,143 150,31 39,111 119,31 40,63 73,63 74,79 90,79 91,16 17,0 1,32 33,48 49,64 65,80 81,128 129,16 22,16 23,144 151,32 40,32 41,128 137,64 74,64 75,80 91,80 92,1 2,33 34,65 66,81 82,129 130,145 146,1 3,17 23,97 104,33 41,129 137,33 42,65 75,65 76,81 92,81 93"; | |
var pairs = rels.split(',').map(x=>x.split(' ').map(y=>parseInt(y))); | |
const zoneCount = 154 | |
const zones = []; | |
const store = new Array(zoneCount).fill('').map(x=>[]); | |
var continents = {} | |
for (let i =0; i<zoneCount; i++) { | |
zones[i] = {id:i, neighbors:[]}; | |
} | |
for (let i = 0, len = pairs.length; i<len; i++) { | |
var v1 = pairs[i][0]; | |
var v2 = pairs[i][1]; | |
zones[v1].neighbors.push(v2) | |
zones[v2].neighbors.push(v1) | |
} | |
for (let i=0, leni=zones.length; i< leni; i++) { | |
for (let j=0, len=zones.length; j < len; j++) { | |
if (i===j) continue; | |
var path = bfs(i,j); | |
} | |
} | |
console.log('done'); | |
function bfs (start, end, q=[]) { | |
if (store[start][end] !== undefined) return store[start][end]; | |
var visited = {} | |
var p = null; | |
q.push([start]); | |
while (q.length>0) { | |
var path = q.shift(); | |
var last = path[path.length-1]; | |
if (last === end && !p) { | |
p = path; | |
store[start][end] = path; | |
break; | |
} | |
store[start][last] = path; | |
var neighbors = zones[last].neighbors; | |
var j =0, len = neighbors.length; | |
while (j<len) { | |
var next = neighbors[j] | |
if (!visited[next]) { | |
visited[next]=true; | |
q.push([...path, next]); | |
} | |
j++; | |
} | |
} | |
if (!p) { | |
store[start][end] = null; | |
store[end][start] = null; | |
} | |
var v = Object.keys(visited).map(x=>parseInt(x)); | |
continents[start] = continents[start] ? continents[start].add(...v) : new Set(v); | |
return p; | |
}</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// noprotect | |
'use strict'; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var rels = "2 3,34 35,50 51,98 99,114 115,130 131,146 147,50 54,2 7,50 55,114 120,98 105,34 42,98 106,66 76,66 77,82 93,82 94,18 44,18 51,3 4,19 20,35 36,83 84,99 100,115 116,131 132,3 7,51 55,3 8,51 56,115 120,147 152,115 121,99 106,19 27,99 107,67 78,83 95,4 5,20 21,52 53,68 69,84 85,100 101,116 117,132 133,4 8,4 9,116 121,116 122,20 27,52 59,100 107,20 28,100 108,68 79,84 95,84 96,5 6,21 22,69 70,85 86,101 102,117 118,149 150,5 9,5 10,117 122,37 43,53 59,117 123,21 28,101 108,21 29,101 109,69 79,69 80,85 96,22 23,54 55,86 87,102 103,118 119,6 10,134 138,6 11,118 123,54 60,118 124,22 29,54 61,102 109,102 110,70 80,70 81,39 40,7 8,55 56,71 72,87 88,135 136,135 139,39 44,119 124,135 140,7 13,55 61,119 125,7 14,55 62,103 110,103 111,71 83,24 25,8 9,40 41,72 73,120 121,152 153,40 44,136 140,40 45,8 14,24 30,56 62,8 15,56 63,120 128,104 113,120 129,72 83,72 84,9 10,25 26,41 42,73 74,105 106,121 122,41 45,137 141,25 30,137 142,9 15,25 31,9 16,89 97,121 129,105 114,121 130,57 67,73 84,73 85,10 11,74 75,90 91,106 107,122 123,138 139,26 31,10 16,26 32,138 144,10 17,90 98,106 114,122 130,90 99,106 115,122 131,58 68,74 85,74 86,11 12,27 28,75 76,91 92,107 108,123 124,139 140,43 46,11 17,139 145,91 99,107 115,123 131,91 100,107 116,123 132,27 37,59 69,59 70,75 86,75 87,44 45,28 29,60 61,76 77,92 93,108 109,124 125,140 145,140 146,92 100,108 116,124 132,92 101,108 117,124 133,28 38,60 71,76 87,76 88,13 14,61 62,93 94,109 110,125 126,141 142,13 19,13 20,141 148,93 101,109 117,125 133,29 38,93 102,109 118,61 71,61 72,77 88,14 15,30 31,46 47,62 63,110 111,126 127,46 48,46 49,14 20,142 148,14 21,94 102,110 118,30 39,94 103,110 119,62 72,62 73,78 89,15 16,31 32,63 64,79 80,95 96,111 112,47 49,15 21,15 22,127 134,143 150,31 39,111 119,31 40,63 73,63 74,79 90,79 91,16 17,0 1,32 33,48 49,64 65,80 81,128 129,16 22,16 23,144 151,32 40,32 41,128 137,64 74,64 75,80 91,80 92,1 2,33 34,65 66,81 82,129 130,145 146,1 3,17 23,97 104,33 41,129 137,33 42,65 75,65 76,81 92,81 93"; | |
var pairs = rels.split(',').map(function (x) { | |
return x.split(' ').map(function (y) { | |
return parseInt(y); | |
}); | |
}); | |
var zoneCount = 154; | |
var zones = []; | |
var store = new Array(zoneCount).fill('').map(function (x) { | |
return []; | |
}); | |
var continents = {}; | |
for (var i = 0; i < zoneCount; i++) { | |
zones[i] = { id: i, neighbors: [] }; | |
} | |
for (var i = 0, len = pairs.length; i < len; i++) { | |
var v1 = pairs[i][0]; | |
var v2 = pairs[i][1]; | |
zones[v1].neighbors.push(v2); | |
zones[v2].neighbors.push(v1); | |
} | |
for (var i = 0, leni = zones.length; i < leni; i++) { | |
for (var j = 0, len = zones.length; j < len; j++) { | |
if (i === j) continue; | |
var path = bfs(i, j); | |
} | |
} | |
console.log('done'); | |
function bfs(start, end) { | |
var _continents$start; | |
var q = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | |
if (store[start][end] !== undefined) return store[start][end]; | |
var visited = {}; | |
var p = null; | |
q.push([start]); | |
while (q.length > 0) { | |
var path = q.shift(); | |
var last = path[path.length - 1]; | |
if (last === end && !p) { | |
p = path; | |
store[start][end] = path; | |
break; | |
} | |
store[start][last] = path; | |
var neighbors = zones[last].neighbors; | |
var j = 0, | |
len = neighbors.length; | |
while (j < len) { | |
var next = neighbors[j]; | |
if (!visited[next]) { | |
visited[next] = true; | |
q.push([].concat(_toConsumableArray(path), [next])); | |
} | |
j++; | |
} | |
} | |
if (!p) { | |
store[start][end] = null; | |
store[end][start] = null; | |
} | |
var v = Object.keys(visited).map(function (x) { | |
return parseInt(x); | |
}); | |
continents[start] = continents[start] ? (_continents$start = continents[start]).add.apply(_continents$start, _toConsumableArray(v)) : new Set(v); | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment