Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.onload = function() {
- var topology = {
- 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}],
- links: [
- {source: 0, target: 1},
- {source: 1, target: 2},
- {source: 2, target: 3},
- {source: 3, target: 4},
- {source: 4, target: 5},
- {source: 5, target: 6},
- {source: 6, target: 1},
- {source: 6, target: 7},
- {source: 7, target: 8},
- {source: 8, target: 9},
- {source: 9, target: 10},
- {source: 10, target: 11},
- {source: 11, target: 12},
- {source: 12, target: 9}
- ]
- };
- var nodes = [];
- for (var i in topology.nodes) {
- nodes[i] = new Vertex(i);
- }
- for (var o in topology.links) {
- var s = topology.links[o].source,
- t = topology.links[o].target;
- nodes[s].connections.push(nodes[t]);
- }
- var graph = new Graph(nodes);
- var tarjan = new Tarjan(graph);
- var scc = tarjan.run();
- var tps = [];
- var trees = [], kk=0;
- for (var o in scc) {
- if(scc[o].length>2) {
- var tp = {nodes: [], links: []};
- for(var j in scc[o]) {
- tp.nodes.push({index: +scc[o][j].index});
- tp.links.push({source: +scc[o][j].index, target: +scc[o][j].connections[0].index});
- }
- tps.push(tp);
- } else {
- trees[kk]={index: scc[o][0].index, children: []};
- for (var i = 0; i < scc[o][0].connections.length; i++) {
- trees[kk].children.push({index: scc[o][0].connections[i].index});
- };
- kk++;
- }
- }
- for (var i in tps){
- for (var j in tps[i].nodes){
- tps[i].nodes[j].id = tps[i].nodes[j].index;
- tps[i].nodes[j].index = +j;
- }
- for (var j in tps[i].links){
- tps[i].links[j].s = tps[i].links[j].source;
- tps[i].links[j].t = tps[i].links[j].target;
- tps[i].links[j].source = tps[i].links.length-1-j;
- if (j==='0') {tps[i].links[j].target = +j}
- else tps[i].links[j].target = tps[i].links[j-1].source;
- }
- }
- var dataRing = [{
- id: 'ring'+0,
- type: "ring",
- data: tps[0]
- },{
- id: 'ring'+1,
- type: "ring",
- data: tps[1]
- }];
- for (var i = 0; i < 1; i++) {
- dataRing.push({
- id: 'tree'+i,
- type: "tree",
- data: trees[i]
- });
- };
- PRISM.Topology({
- width: 500,
- height: 500,
- rootId: 'ring',
- }).show(dataRing);
- // console.log('tps=',tps);
- // window.tps = tps;
- // console.log('trees=',trees);
- // var pos = [0,0], b =false;
- // for (var i in scc) {
- // for (var j in scc[i].links) {
- // console.log(scc[i].links[j]);
- // if(tps[i].links[j].s == trees[0].index) {
- // pos = [tps[i].links[j].target.x, tps[i].links[j].target.y];
- // //b=true;
- // //break;
- // } else if(tps[i].links[j].t == trees[0].index) {
- // console.log(1);
- // pos = [tps[i].links[j].source.x, tps[i].links[j].source.y];
- // //b=true;
- // //break;
- // }
- // }
- // //if(b) break;
- // };
- // console.log(pos);
- };
- function Graph(vertices) {
- this.vertices = vertices || [];
- }
- function Vertex(name) {
- this.name = name || null;
- this.connections = [];
- // used in tarjan algorithm
- // went ahead and explicity initalized them
- this.index = -1;
- this.lowlink = -1;
- }
- Vertex.prototype = {
- equals: function(vertex) {
- // equality check based on vertex name
- return (vertex.name && this.name == vertex.name);
- }
- };
- function VertexStack(vertices) {
- this.vertices = vertices || [];
- }
- VertexStack.prototype = {
- contains: function(vertex) {
- for (var i in this.vertices) {
- if (this.vertices[i].equals(vertex)) {
- return true;
- }
- }
- return false;
- }
- };
- function Tarjan(graph) {
- this.index = 0;
- this.stack = new VertexStack();
- this.graph = graph;
- this.scc = [];
- }
- Tarjan.prototype = {
- run: function() {
- for (var i in this.graph.vertices) {
- if (this.graph.vertices[i].index < 0) {
- this.strongconnect(this.graph.vertices[i]);
- }
- }
- return this.scc;
- },
- strongconnect: function(vertex) {
- // Set the depth index for v to the smallest unused index
- vertex.index = this.index;
- vertex.lowlink = this.index;
- this.index = this.index + 1;
- this.stack.vertices.push(vertex);
- // Consider successors of v
- // aka... consider each vertex in vertex.connections
- for (var i in vertex.connections) {
- var v = vertex;
- var w = vertex.connections[i];
- if (w.index < 0) {
- // Successor w has not yet been visited; recurse on it
- this.strongconnect(w);
- v.lowlink = Math.min(v.lowlink, w.lowlink);
- } else if (this.stack.contains(w)) {
- // Successor w is in stack S and hence in the current SCC
- v.lowlink = Math.min(v.lowlink, w.index);
- }
- }
- // If v is a root node, pop the stack and generate an SCC
- if (vertex.lowlink == vertex.index) {
- // start a new strongly connected component
- var vertices = [];
- var w = null;
- if (this.stack.vertices.length > 0) {
- do {
- w = this.stack.vertices.pop();
- // add w to current strongly connected component
- vertices.push(w);
- } while (!vertex.equals(w));
- }
- // output the current strongly connected component
- // ... i'm going to push the results to a member scc array variable
- if (vertices.length > 0) {
- this.scc.push(vertices);
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement