From 5076ba07c5a215f05cb77b9b7b5e55ff4b0b4dbf Mon Sep 17 00:00:00 2001 From: Oleg Sh Date: Sat, 12 Jun 2021 16:07:37 +0200 Subject: [PATCH] Fix search Eulerian path/loop if graph has separate nodes. --- script/Algorithms.js | 23 +++++++++++++++-------- script/Graph.js | 20 ++++++++++++++++++++ script/GraphMLCreater.js | 6 ++++-- script/plugins/EulerianLoop.js | 2 +- script/plugins/EulerianPath.js | 2 +- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/script/Algorithms.js b/script/Algorithms.js index 2124962..38bda03 100644 --- a/script/Algorithms.js +++ b/script/Algorithms.js @@ -154,13 +154,20 @@ function BaseAlgorithmEx(graph, app) // inheritance. BaseAlgorithmEx.prototype = Object.create(BaseAlgorithm.prototype); -BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallback) +BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallback, ignoreSeparateNodes = false) { if (location.hostname === "localhost" || location.hostname === "127.0.0.1") console.log(queryString); var graph = this.graph; - var creator = new GraphMLCreater(graph.vertices, graph.edges); + var ignoreNodes = {}; + + if (ignoreSeparateNodes) + for (var i = 0; i < graph.vertices.length; i++) + if (!graph.HasConnectedNodes(graph.vertices[i])) + ignoreNodes[graph.vertices[i].id] = 1; + + var creator = new GraphMLCreater(graph.vertices, graph.edges, ignoreNodes); var pathObjects = []; var properties = {}; var result = []; @@ -204,15 +211,15 @@ BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallb $data.each(function(){ if ("hightlightNode" == $(this).attr('key') && $(this).text() == "1") { - pathObjects.push(graph.FindVertex(id)); + pathObjects.push(graph.FindVertex(id)); } else { - if (!properties[id]) - { - properties[id] = {}; - } - properties[id][$(this).attr('key')] = $(this).text(); + if (!properties[id]) + { + properties[id] = {}; + } + properties[id][$(this).attr('key')] = $(this).text(); } }); }); diff --git a/script/Graph.js b/script/Graph.js index 365ec93..32b0b06 100644 --- a/script/Graph.js +++ b/script/Graph.js @@ -99,6 +99,26 @@ Graph.prototype.DeleteVertex = function(vertexObject) } } +Graph.prototype.HasConnectedNodes = function(vertexObject) +{ + var res = false; + + var index = this.vertices.indexOf(vertexObject); + if (index > -1) + { + for (var i = 0; i < this.edges.length; i++) + { + if (this.edges[i].vertex1 == vertexObject || this.edges[i].vertex2 == vertexObject) + { + res = true; + break; + } + } + } + + return res; +} + Graph.prototype.FindVertex = function(id) { var res = null; diff --git a/script/GraphMLCreater.js b/script/GraphMLCreater.js index 21e7443..70ddf2c 100755 --- a/script/GraphMLCreater.js +++ b/script/GraphMLCreater.js @@ -4,10 +4,11 @@ */ -function GraphMLCreater(nodes, arcs) +function GraphMLCreater(nodes, arcs, ignoreNodes = {}) { this.nodes = nodes; this.arcs = arcs; + this.ignoreNodes = ignoreNodes; } @@ -27,7 +28,8 @@ GraphMLCreater.prototype.GetXMLString = function() for (var i = 0; i < this.nodes.length; i++) { - xmlBoby = xmlBoby + ""; + if (!this.ignoreNodes.hasOwnProperty(this.nodes[i].id)) + xmlBoby = xmlBoby + ""; } var hasDirected = false; for (var i = 0; i < this.arcs.length; i++) diff --git a/script/plugins/EulerianLoop.js b/script/plugins/EulerianLoop.js index c63ca3a..c277973 100755 --- a/script/plugins/EulerianLoop.js +++ b/script/plugins/EulerianLoop.js @@ -37,7 +37,7 @@ FindEulerianLoop.prototype.result = function(resultCallback) this.CalculateAlgorithm("elloop=cgiInput&report=xml", function (pathObjects, properties, results) { self.resultCallback(pathObjects, properties, results); - }); + }, true); return true; } diff --git a/script/plugins/EulerianPath.js b/script/plugins/EulerianPath.js index c867fad..3f46685 100755 --- a/script/plugins/EulerianPath.js +++ b/script/plugins/EulerianPath.js @@ -37,7 +37,7 @@ FindEulerianPath.prototype.result = function(resultCallback) this.CalculateAlgorithm("elpath=cgiInput&report=xml", function (pathObjects, properties, results) { self.resultCallback(pathObjects, properties, results); - }); + }, true); return true; }