Guest User

Untitled

a guest
May 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. //Represent a network as a matrix
  2. const network = [
  3. [ 0, 0, 0, 1, 1 ],
  4. [ 0, 0, 1, 1, 0 ],
  5. [ 0, 1, 0, 1, 0 ],
  6. [ 0, 0, 0, 0, 1 ],
  7. [ 0, 1, 1, 0, 0 ]
  8. ];
  9. //AD,AE,BC,BD,CD,DE,EB,EC - number of edges
  10. //Initially the minimum vertices monitored is 5/network hub span
  11. let min = network.length;
  12. //gather existing number of edges in the graph
  13. const edgesToCover = network.reduce( ( edges, vertex ) => {
  14. const index = network.indexOf( vertex );
  15. for ( let i = 0; i < vertex.length; i++ ) {
  16. if ( i !== index && network[ index ][ i ] === 1 && !edges.has( `${i}${index}` ) ) {
  17. edges.add( `${index}${i}` );
  18. }
  19. }
  20. return edges;
  21. }, new Set() );
  22. /*
  23. *@vertexCover checks if the given combination is viable and covers all edges
  24. */
  25. const vertexCover = function ( combinations, combination ) {
  26. //helper for minimum vertex cover
  27. let sum = 0;
  28. //covered edges for input combintation
  29. let covered = new Set();
  30. //loop through the combination, and check for edge cover
  31. for ( let i = 0; i < combination.length; i++ ) {
  32. //track the number of hubs on which monitoring devices are needed
  33. sum += combination[ i ];
  34. //continue only if the combintation specifies a monitor on this hub
  35. if ( combination[ i ] ) {
  36. //loop through the vertex's span and count edges covered
  37. for ( let j = 0; j < combination.length; j++ ) {
  38. //Do not check for loopbacks, accept if there is either a leading edge / a converging edge
  39. //and if the covered set does not already have the edge
  40. if ( i !== j && ( network[ i ][ j ] === 1 || network[ j ][ i ] === 1 ) && !covered.has( `${j}${i}` ) ) {
  41. covered.add( `${i}${j}` );
  42. }
  43. }
  44. }
  45. }
  46. //Are all edges covered with the input combo?
  47. if ( covered.size === edgesToCover.size && sum <= min ) {
  48. //If current comination has lesser monitors, reset the minimum required monitors
  49. min = sum;
  50. combinations.add( combination );
  51. }
  52. return combinations;
  53. };
  54. //Define env
  55. const numberOfHubs = 5;
  56. const permutations = Math.pow( 2, numberOfHubs );
  57. //compute / generate combinations for number of permutations
  58. const possibilities = ( function () {
  59. const combinations = [];
  60. for ( let i = permutations; i > 0; i-- ) {
  61. const combination = [];
  62. for ( let shift = 0; shift < network.length; shift++ ) {
  63. //right shift, the 32 bit unsigned integer in JS, represented by i, shift number of times
  64. let rightShifted = i >> shift;
  65. //follow the truthy values - when Anded, the result is a one wherever there is a one in the right shifted value
  66. let state = rightShifted & 1;
  67. //store the current state as an element of the combination
  68. combination.unshift( state );
  69. }
  70. //push the current generated combination into the list of combinations
  71. combinations.unshift( combination );
  72. }
  73. return combinations;
  74. } )();
  75. //reduce the combinations generated for available permutations, to
  76. // a set of viable and minimum span combinations
  77. const combinations = possibilities.reduce( vertexCover, new Set() );
  78. console.log( possibilities, combinations );
Add Comment
Please, Sign In to add comment