Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var LineByLineReader = require('line-by-line'),
  2.     lr1 = new LineByLineReader('inputFW.txt'),
  3.     lr2 = new LineByLineReader('inputBFS.txt');
  4.  
  5. let inputFW = [];
  6. let inputBFS = [];
  7.  
  8. lr1.on('line', function (line) {
  9.   inputFW.push(line.split(' ').map(item => Number(item)));
  10. });
  11.  
  12. lr2.on('line', function (line) {
  13.   inputBFS.push(line.split(' ').map(item => Number(item)));
  14. });
  15.  
  16. var pathMatrix = [];
  17. var output = [];
  18.  
  19. function floydWarchallAlgorithm(input) {
  20.   let nodesNr = input[0][0];
  21.   let edgeNr = input[0][1];
  22.   output = new Array(nodesNr);
  23.   pathMatrix = new Array(nodesNr);
  24.  
  25.   for(let i = 0; i < nodesNr; i++) {
  26.     output[i] = new Array(nodesNr).fill(Infinity);
  27.     pathMatrix[i] = new Array(nodesNr).fill(null);
  28.  
  29.     for(let j = 0; j < nodesNr; j++) {
  30.       if(i === j) output[i][j] = 0;
  31.     }
  32.   }
  33.  
  34.   for(let i = 1; i <= edgeNr; i++) {
  35.     output[input[i][0] - 1][input[i][1] - 1] = input[i][2];
  36.     pathMatrix[input[i][0] - 1][input[i][1] - 1] = input[i][1] - 1;
  37.   }
  38.  
  39.   for(let k = 0; k < nodesNr; k++) {
  40.     for(let i = 0; i < nodesNr; i++) {
  41.       for(let j = 0; j < nodesNr; j++) {
  42.         let dist = output[i][j];
  43.         let dist2 = output[i][k] + output[k][j];
  44.         if(dist > dist2 || dist === Infinity) {
  45.           output[i][j] = dist2;
  46.           pathMatrix[i][j] = pathMatrix[i][k];
  47.         }
  48.       }
  49.     }
  50.   }
  51.   output.forEach(item => console.log(item));
  52.   console.log('- - - - - - - -');
  53.   pathMatrix.forEach(item => console.log(item));
  54.   console.log('- - - - - - - -');
  55.   printSolution(nodesNr);
  56. }
  57.  
  58. function printPath(source, target) {
  59.   if(pathMatrix[source][target] === null) return [];
  60.   let path = [source + 1];
  61.   while(source !== target) {
  62.     source = pathMatrix[source][target];
  63.     path.push(source + 1);
  64.   }
  65.   return path.join('-');
  66. }
  67.  
  68. function printSolution(nodesNr) {
  69.   for(let i = 0; i < nodesNr; i++) {
  70.     for(let j = 0; j < nodesNr; j++) {
  71.       if(output[i][j] !== Infinity && output[i][j] !== 0) {
  72.         console.log('d[' + Number(i+1) + ',' + Number(j+1) + '] = ' + output[i][j]);
  73.         console.log('PATH: ' + printPath(i, j));
  74.       }
  75.     }
  76.   }
  77. }
  78.  
  79. function bfs(input, source, target) {
  80.   let q = [ [source] ];
  81.   let path = [];
  82.   let node;
  83.  
  84.   while(q.length) {
  85.     path = q.shift();
  86.     node = path[path.length - 1];
  87.     if(node === target) return path;
  88.  
  89.     for(let i = 1; i < input.length; i++) {
  90.       if(input[i][0] === node) {
  91.         let path2 = path.slice();
  92.         path2.push(input[i][1]);
  93.         q.push(path2);
  94.       }
  95.     }
  96.   }
  97.   return path;
  98. }
  99.  
  100.  
  101. lr1.on('end', function () {
  102.   console.log('>>> FW');
  103.   floydWarchallAlgorithm(inputFW);
  104. });
  105.  
  106. lr2.on('end', function () {
  107.   console.log('\n>>> BFS');
  108.   console.log(bfs(inputBFS, 0, 11).reverse());
  109. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement