-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.js
190 lines (184 loc) · 5.25 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var G = ( function( G, d3){
/**
* Function to extend functionality of parent in child
* @param {Object} child child object
* @param {Object} parent parent object
* @return {null}
*/
G.extend = function ( child, parent ) {
var copyOfParent = Object.create( parent.prototype );
copyOfParent.constructor = child;
child.prototype = copyOfParent;
};
/**
* constructor for graph object
* @param {[type]}
* @param {[type]}
*/
G.Graph = function( nodes, edges, type ) {
this.nodes = nodes;
this.edges = edges;
this.nodes.forEach( function( node ) {
node.adj = [];
node.visited = 0;
} );
this.animation = new G.Animation();
// 1 for directed or else undirected
this.type = typeof type !== 'undefined' ? type : 0;
this.force = null;
this.svg = null;
};
G.Graph.prototype = {
constructor: G.Graph,
/**
* Generates adjacency list from a set of edges
* @return {null}
*/
generateAdjacency: function() {
for ( var edge in this.edges ) {
var link = this.edges[edge];
//console.log(link);
//console.log(link.source);
link.source.adj.push( [link.target,link.w] );
if ( this.type === 0 ) {
link.target.adj.push( [link.source,link.w] );
}
}
},
/**
* Adds a new node
* @return {null}
*/
addNode: function( v ) {
this.nodes.push( v );
},
/**
* Adds a new edge
* @return {null}
*/
addEdge: function( src, dest ) {
var edge = {"source": src, "target": dest};
src.adj.push( dest );
if ( this.type === 0 ) {
dest.adj.push( src );
}
this.edges.push( edge );
},
/**
* Initialize the graph with d3 rendering
* @param {int} height height of canvas
* @param {int} width width of canvas
* @return {null}
*/
initialize: function ( height, width ) {
// Scaling not done yet, values hardcoded
this.svg = d3.select( 'body' ).append( 'svg' )
.attr( 'width', width )
.attr( 'height', height );
this.force = d3.layout.force()
.size([width, height])
.linkDistance(210)
.charge(-350)
.nodes(this.nodes).links(this.edges)
.on("tick",function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
d3.selectAll("circle").attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
d3.selectAll("text").attr("x", function (d) {
return d.x;
})
.attr("y", function (d) {
return d.y;
});
});
var node = this.svg.selectAll('.node')
.data(this.nodes)
.enter().append('svg')
.attr('class','node');
var link = this.svg.selectAll('.link')
.data(this.edges)
.enter().append('line')
.attr('class', 'link');
node.append("circle")
.attr("r", width/30);//add scaling here
node.append("text")
.attr("dx", -3)
.attr("dy", ".35em")
.text(function(d) { return d.data !== 'undefined'?d.data:"1"; })
.style("stroke", "orange");
this.force.on('end', function() {
d3.selectAll("circle").attr("r",width/30).attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
link.attr('x1', function(d) { return d.source.x; })
.attr('y1', function(d) { return d.source.y; })
.attr('x2', function(d) { return d.target.x; })
.attr('y2', function(d) { return d.target.y; });
});
},
/**
* Starts animation
* @return {null}
*/
start: function () {
this.force.start();
}
};
G.BFS = function( nodes, edges, type ) {
G.Graph.call( nodes, edges, type );
G.extend( G.BFS, G.Graph );
this.color = ["blue","red","black"];//customization
};
G.BFS.prototype = {
constructor: G.BFS,
update: function () {
var node = this.svg.selectAll('.node')
.data(g.nodes);
var nodes = d3.selectAll("circle")
.style("fill", function(d) { return d.color; });
},
algorithm: function( node ) {
node.visited = 1;
//animation is an array of objects
//such an object consist of a node to change, and a set of properties changing
this.animation.anim.push([{"node":node,val:"color","src":this.color[0],"dest":this.color[1]}]);
var Q = [];
Q.push(node);
while( Q.length > 0 ) {
var u = Q.pop();
for( var i in u.adj ) {
var v = u.adj[i];
if( v.visited === 0 ) {
this.animation.anim.push([{"node":v,val:"color","src":this.color[0],"dest":this.color[1]}]);
Q.push(v);
v.visited = 1;
}
}
this.animation.anim.push([{"node":u,val:"color","src":this.color[1],"dest":this.color[2]}]);
}
}
};
return G;
}( G || {}, d3 ) );