Advertisement
Guest User

Untitled

a guest
May 30th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. function round(n) {
  2. return Math.round(n*100) / 100;
  3. }
  4.  
  5. // Represents an edge from source to sink with capacity
  6. var Edge = function(source, sink, capacity) {
  7. this.source = source;
  8. this.sink = sink;
  9. this.capacity = capacity;
  10. };
  11.  
  12. // Main class to manage the network
  13. var Graph = function() {
  14. this.edges = {};
  15. this.nodes = [];
  16. this.nodeMap = {};
  17.  
  18. // Add a node to the graph
  19. this.addNode = function(node) {
  20. this.nodes.push(node);
  21. this.nodeMap[node] = this.nodes.length-1;
  22. this.edges[node] = [];
  23. };
  24.  
  25. // Add an edge from source to sink with capacity
  26. this.addEdge = function(source, sink, capacity) {
  27. // Create the two edges = one being the reverse of the other
  28. var edge = new Edge(source, sink, capacity);
  29. this.edges[source].push(edge);
  30. };
  31.  
  32. // Does edge from source to sink exist?
  33. this.edgeExists = function(source, sink) {
  34. if(this.edges[source] !== undefined)
  35. for(var i=0;i<this.edges[source].length;i++)
  36. if(this.edges[source][i].sink == sink)
  37. return this.edges[source][i];
  38. return null;
  39. };
  40.  
  41. // Turn the set of nodes and edges to a matrix with the value being
  42. // the capacity between the nodes
  43. this.getAssociatedMatrix = function() {
  44. var matrix = [];
  45. for(var i=0;i<this.nodes.length;i++) {
  46. var row = [];
  47. for(var j=0;j<this.nodes.length;j++) {
  48. var edge = this.edgeExists(this.nodes[j], this.nodes[i]);
  49. if(i == j) edge = {capacity:1};
  50. row.push(edge != null ? edge.capacity : 0);
  51. }
  52. matrix.push(row);
  53. }
  54. return matrix;
  55. };
  56.  
  57. // Normalizes a given matrix
  58. this.normalize = function(matrix) {
  59. // Find the sum of each column
  60. var sums = [];
  61. for(var col=0;col<matrix.length;col++) {
  62. var sum = 0;
  63. for(var row=0;row<matrix.length;row++)
  64. sum += matrix[row][col];
  65. sums[col] = sum;
  66. }
  67.  
  68. // For every value in the matrix divide by the sum
  69. for(var col=0;col<matrix.length;col++)
  70. for(var row=0;row<matrix.length;row++)
  71. matrix[row][col] = round(matrix[row][col] / sums[col]);
  72. };
  73.  
  74. // Prints the matrix
  75. this.print = function(matrix) {
  76. for(var i=0;i<matrix.length;i++) {
  77. for(var j=0;j<matrix[i].length;j++) {
  78. document.write((j==0?'':',')+matrix[i][j]);
  79. }
  80. document.write('<br>');
  81. }
  82. };
  83.  
  84. // Take the (power)th power of the matrix effectively multiplying it with
  85. // itself pow times
  86. this.matrixExpand = function(matrix, pow) {
  87. var resultMatrix = [];
  88. for(var row=0;row<matrix.length;row++) {
  89. resultMatrix[row] = [];
  90. for(var col=0;col<matrix.length;col++) {
  91. var result = 0;
  92. for(var c=0;c<matrix.length;c++)
  93. result += matrix[row][c] * matrix[c][col];
  94. resultMatrix[row][col] = result;
  95. }
  96. }
  97. return resultMatrix;
  98. };
  99.  
  100. // Applies a power of X to each item in the matrix
  101. this.matrixInflate = function(matrix, pow) {
  102. for(var row=0;row<matrix.length;row++)
  103. for(var col=0;col<matrix.length;col++)
  104. matrix[row][col] = Math.pow(matrix[row][col], pow);
  105. };
  106.  
  107. // Are the two matrices equal?
  108. this.equals = function(a,b) {
  109. for(var i=0;i<a.length;i++)
  110. for(var j=0;j<a[i].length;j++)
  111. if(b[i] === undefined || b[i][j] === undefined || a[i][j] - b[i][j] > 0.1) return false;
  112. return true;
  113. };
  114.  
  115. // Girvan–Newman algorithm
  116. this.getMarkovCluster = function(power, inflation) {
  117. var lastMatrix = [];
  118.  
  119. var currentMatrix = this.getAssociatedMatrix();
  120. this.print(currentMatrix);
  121. this.normalize(currentMatrix);
  122.  
  123. currentMatrix = this.matrixExpand(currentMatrix, power);
  124. this.matrixInflate(currentMatrix, inflation);
  125. this.normalize(currentMatrix);
  126.  
  127. var c = 0;
  128. while(!this.equals(currentMatrix,lastMatrix)) {
  129. lastMatrix = currentMatrix.slice(0);
  130.  
  131. currentMatrix = this.matrixExpand(currentMatrix, power);
  132. this.matrixInflate(currentMatrix, inflation);
  133. this.normalize(currentMatrix);
  134.  
  135. if(++c > 500) break; //JIC, fiddle fail
  136. }
  137. return currentMatrix;
  138. };
  139. };
  140.  
  141. var g = new Graph();
  142.  
  143. g.addNode('a');
  144. g.addNode('b');
  145. g.addNode('c');
  146. g.addNode('d');
  147.  
  148. g.addEdge('a','b',1);
  149. g.addEdge('b','a',1);
  150. g.addEdge('a','c',1);
  151. g.addEdge('c','a',1);
  152. g.addEdge('a','d',1);
  153. g.addEdge('d','a',1);
  154. g.addEdge('a','b',1);
  155. g.addEdge('b','d',1);
  156. g.addEdge('d','b',1);
  157.  
  158. var result = g.getMarkovCluster(2,2);
  159. g.print(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement