From b39ad705b5ca21d22a06faa8f1fb944198b1c2e9 Mon Sep 17 00:00:00 2001 From: Jason Park Date: Thu, 19 May 2016 00:57:56 -0500 Subject: [PATCH] tree --- css/stylesheet.css | 4 ++-- js/module/graph.js | 47 ++++++++++++++++++++++++++++++++++++- js/module/weighted_graph.js | 1 + js/script.js | 5 ++-- js/tracer.js | 6 ++--- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/css/stylesheet.css b/css/stylesheet.css index 24cf570..3b8ed83 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 7cf33af..0f1a380 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 608be74..5687ebc 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 ee3313c..7cdcc73 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 88296cd..f5bc147 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; }; -- GitLab