diff --git a/css/stylesheet.css b/css/stylesheet.css index 24cf570852e0faea7f3125519824a37ebd7f0f2b..3b8ed83b51227f048ccf856ca36b7658d6a63bd2 100644 --- a/css/stylesheet.css +++ b/css/stylesheet.css @@ -188,14 +188,14 @@ section { .data_container { top: 60px; - bottom: 70%; + bottom: 60%; left: 0; right: 0; } .code_container { left: 0; - top: 30%; + top: 40%; right: 0; bottom: 0; } diff --git a/js/module/graph.js b/js/module/graph.js index 7cf33af084d523f0fb1bcb808df0467328b8c22a..0f1a3807f754103abbcf773b9797fa479a79e17f 100644 --- a/js/module/graph.js +++ b/js/module/graph.js @@ -26,6 +26,7 @@ GraphTracer.prototype.clear = function () { GraphTracer.prototype.createRandomData = function (N, ratio) { Tracer.prototype.createRandomData.call(this, arguments); + if (!N) N = 5; if (!ratio) ratio = .3; var G = []; for (var i = 0; i < N; i++) { @@ -39,9 +40,51 @@ GraphTracer.prototype.createRandomData = function (N, ratio) { return G; }; +GraphTracer.prototype.setTreeData = function (G, root) { + root = root || 0; + var maxDepth = -1; + + var chk = []; + for (var i = 0; i < G.length; i++) chk.push(false); + var getDepth = function (node, depth) { + if (chk[node]) throw "the given graph is not a tree because it forms a circuit"; + chk[node] = true; + if (maxDepth < depth) maxDepth = depth; + for (var i = 0; i < G[node].length; i++) { + if (G[node][i]) getDepth(i, depth + 1); + } + }; + getDepth(root, 1); + + if (this.setData(G, root)) return true; + + var place = function (node, x, y) { + var temp = graph.nodes(n(node)); + temp.x = x; + temp.y = y; + }; + + var wgap = 1 / (maxDepth - 1); + var dfs = function (node, depth, top, bottom) { + place(node, depth * wgap, (top + bottom) / 2); + var children = 0; + for (var i = 0; i < G[node].length; i++) { + if (G[node][i]) children++; + } + var vgap = (bottom - top) / children; + var cnt = 0; + for (var i = 0; i < G[node].length; i++) { + if (G[node][i]) dfs(i, depth + 1, top + vgap * cnt, top + vgap * ++cnt); + } + }; + dfs(root, 0, 0, 1); + + this.refresh(); +}; + // Override GraphTracer.prototype.setData = function (G) { - if (Tracer.prototype.setData.call(this, arguments)) return; + if (Tracer.prototype.setData.call(this, arguments)) return true; graph.clear(); var nodes = []; @@ -82,6 +125,8 @@ GraphTracer.prototype.setData = function (G) { ratio: 1 }); this.refresh(); + + return false; }; GraphTracer.prototype._clear = function () { diff --git a/js/module/weighted_graph.js b/js/module/weighted_graph.js index 608be7439cc6b77b62fc5834c472a4e31f3ce9a5..5687ebc22e72a8aa87148375f9a6855762faab26 100644 --- a/js/module/weighted_graph.js +++ b/js/module/weighted_graph.js @@ -20,6 +20,7 @@ WeightedGraphTracer.prototype.clear = function () { WeightedGraphTracer.prototype.createRandomData = function (N, ratio, min, max) { Tracer.prototype.createRandomData.call(this, arguments); + if (!N) N = 5; if (!ratio) ratio = .3; if (!min) min = 1; if (!max) max = 5; diff --git a/js/script.js b/js/script.js index ee3313c42d7025efc08f575b6d7dea7ad86c6627..7cdcc73ae3aeeccd56a0a430ed5f01429ab66c00 100644 --- a/js/script.js +++ b/js/script.js @@ -20,8 +20,9 @@ dataEditor.on('change', function () { }); var loadFile = function (category, algorithm, file, explanation) { + lastData = null; $('#explanation').html(explanation); - dataEditor.setValue('var G = [];', -1); + dataEditor.setValue(''); codeEditor.setValue(''); var dir = './algorithm/' + category + '/' + algorithm + '/' + file + '/'; @@ -49,7 +50,7 @@ var loadAlgorithm = function (category, algorithm) { $('#desc_ref').empty(); $('.files_bar').empty(); $('#explanation').html(''); - dataEditor.setValue('var G = [];', -1); + dataEditor.setValue(''); codeEditor.setValue(''); var dir = './algorithm/' + category + '/' + algorithm + '/'; diff --git a/js/tracer.js b/js/tracer.js index 88296cd949a7874f812c1034ec8ff777a376b3ba..f5bc1475a63fad0ad7cb21639ab397d58c05af7a 100644 --- a/js/tracer.js +++ b/js/tracer.js @@ -1,5 +1,5 @@ var timer = null; -var lastModule = null, lastData = null; +var lastData = null; var stepLimit = 1e6; var Tracer = function (module) { @@ -9,7 +9,6 @@ var Tracer = function (module) { this.traceOptions = null; this.traceIndex = -1; this.stepCnt = 0; - lastData = null; }; Tracer.prototype.resize = function () { @@ -31,8 +30,7 @@ Tracer.prototype.createRandomData = function (arguments) { Tracer.prototype.setData = function (arguments) { var data = JSON.stringify(arguments); - if (lastModule == this.module && lastData == data) return true; - lastModule = this.module; + if (lastData == data) return true; lastData = data; return false; };