Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. window.onload = function() {
  2. var topology = {
  3. nodes: [{id: 0},{id: 1},{id: 2},{id: 3},{id: 4},{id: 5},{id: 6},{id: 7},{id: 8},{id: 9},{id: 10},{id: 11},{id: 12}],
  4. links: [
  5. {source: 0, target: 1},
  6.  
  7. {source: 1, target: 2},
  8. {source: 2, target: 3},
  9. {source: 3, target: 4},
  10. {source: 4, target: 5},
  11. {source: 5, target: 6},
  12. {source: 6, target: 1},
  13.  
  14. {source: 6, target: 7},
  15. {source: 7, target: 8},
  16. {source: 8, target: 9},
  17.  
  18. {source: 9, target: 10},
  19. {source: 10, target: 11},
  20. {source: 11, target: 12},
  21. {source: 12, target: 9}
  22. ]
  23. };
  24. var nodes = [];
  25. for (var i in topology.nodes) {
  26. nodes[i] = new Vertex(i);
  27. }
  28. for (var o in topology.links) {
  29. var s = topology.links[o].source,
  30. t = topology.links[o].target;
  31.  
  32. nodes[s].connections.push(nodes[t]);
  33. }
  34.  
  35. var graph = new Graph(nodes);
  36. var tarjan = new Tarjan(graph);
  37. var scc = tarjan.run();
  38. var tps = [];
  39. var trees = [], kk=0;
  40.  
  41. for (var o in scc) {
  42. if(scc[o].length>2) {
  43. var tp = {nodes: [], links: []};
  44. for(var j in scc[o]) {
  45. tp.nodes.push({index: +scc[o][j].index});
  46. tp.links.push({source: +scc[o][j].index, target: +scc[o][j].connections[0].index});
  47. }
  48. tps.push(tp);
  49. } else {
  50. trees[kk]={index: scc[o][0].index, children: []};
  51. for (var i = 0; i < scc[o][0].connections.length; i++) {
  52. trees[kk].children.push({index: scc[o][0].connections[i].index});
  53. };
  54. kk++;
  55. }
  56. }
  57. for (var i in tps){
  58. for (var j in tps[i].nodes){
  59. tps[i].nodes[j].id = tps[i].nodes[j].index;
  60. tps[i].nodes[j].index = +j;
  61. }
  62. for (var j in tps[i].links){
  63. tps[i].links[j].s = tps[i].links[j].source;
  64. tps[i].links[j].t = tps[i].links[j].target;
  65. tps[i].links[j].source = tps[i].links.length-1-j;
  66. if (j==='0') {tps[i].links[j].target = +j}
  67. else tps[i].links[j].target = tps[i].links[j-1].source;
  68. }
  69. }
  70.  
  71.  
  72.  
  73. var dataRing = [{
  74. id: 'ring'+0,
  75. type: "ring",
  76. data: tps[0]
  77. },{
  78. id: 'ring'+1,
  79. type: "ring",
  80. data: tps[1]
  81. }];
  82. for (var i = 0; i < 1; i++) {
  83. dataRing.push({
  84. id: 'tree'+i,
  85. type: "tree",
  86. data: trees[i]
  87. });
  88. };
  89.  
  90. PRISM.Topology({
  91. width: 500,
  92. height: 500,
  93. rootId: 'ring',
  94. }).show(dataRing);
  95.  
  96. // console.log('tps=',tps);
  97. // window.tps = tps;
  98.  
  99. // console.log('trees=',trees);
  100. // var pos = [0,0], b =false;
  101. // for (var i in scc) {
  102. // for (var j in scc[i].links) {
  103. // console.log(scc[i].links[j]);
  104. // if(tps[i].links[j].s == trees[0].index) {
  105. // pos = [tps[i].links[j].target.x, tps[i].links[j].target.y];
  106.  
  107. // //b=true;
  108. // //break;
  109. // } else if(tps[i].links[j].t == trees[0].index) {
  110. // console.log(1);
  111. // pos = [tps[i].links[j].source.x, tps[i].links[j].source.y];
  112.  
  113. // //b=true;
  114. // //break;
  115. // }
  116. // }
  117. // //if(b) break;
  118. // };
  119. // console.log(pos);
  120. };
  121.  
  122. function Graph(vertices) {
  123. this.vertices = vertices || [];
  124. }
  125.  
  126. function Vertex(name) {
  127. this.name = name || null;
  128. this.connections = [];
  129. // used in tarjan algorithm
  130. // went ahead and explicity initalized them
  131. this.index = -1;
  132. this.lowlink = -1;
  133. }
  134. Vertex.prototype = {
  135. equals: function(vertex) {
  136. // equality check based on vertex name
  137. return (vertex.name && this.name == vertex.name);
  138. }
  139. };
  140.  
  141. function VertexStack(vertices) {
  142. this.vertices = vertices || [];
  143. }
  144. VertexStack.prototype = {
  145. contains: function(vertex) {
  146. for (var i in this.vertices) {
  147. if (this.vertices[i].equals(vertex)) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. };
  154.  
  155. function Tarjan(graph) {
  156. this.index = 0;
  157. this.stack = new VertexStack();
  158. this.graph = graph;
  159. this.scc = [];
  160. }
  161. Tarjan.prototype = {
  162. run: function() {
  163. for (var i in this.graph.vertices) {
  164. if (this.graph.vertices[i].index < 0) {
  165. this.strongconnect(this.graph.vertices[i]);
  166. }
  167. }
  168. return this.scc;
  169. },
  170. strongconnect: function(vertex) {
  171. // Set the depth index for v to the smallest unused index
  172. vertex.index = this.index;
  173. vertex.lowlink = this.index;
  174. this.index = this.index + 1;
  175. this.stack.vertices.push(vertex);
  176. // Consider successors of v
  177. // aka... consider each vertex in vertex.connections
  178. for (var i in vertex.connections) {
  179. var v = vertex;
  180. var w = vertex.connections[i];
  181. if (w.index < 0) {
  182. // Successor w has not yet been visited; recurse on it
  183. this.strongconnect(w);
  184. v.lowlink = Math.min(v.lowlink, w.lowlink);
  185. } else if (this.stack.contains(w)) {
  186. // Successor w is in stack S and hence in the current SCC
  187. v.lowlink = Math.min(v.lowlink, w.index);
  188. }
  189. }
  190. // If v is a root node, pop the stack and generate an SCC
  191. if (vertex.lowlink == vertex.index) {
  192. // start a new strongly connected component
  193. var vertices = [];
  194. var w = null;
  195. if (this.stack.vertices.length > 0) {
  196. do {
  197. w = this.stack.vertices.pop();
  198. // add w to current strongly connected component
  199. vertices.push(w);
  200. } while (!vertex.equals(w));
  201. }
  202. // output the current strongly connected component
  203. // ... i'm going to push the results to a member scc array variable
  204. if (vertices.length > 0) {
  205. this.scc.push(vertices);
  206. }
  207. }
  208. }
  209. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement