Fix search Eulerian path/loop if graph has separate nodes.

This commit is contained in:
Oleg Sh 2021-06-12 16:07:37 +02:00
parent d8ed01c438
commit 5076ba07c5
5 changed files with 41 additions and 12 deletions

View File

@ -154,13 +154,20 @@ function BaseAlgorithmEx(graph, app)
// inheritance. // inheritance.
BaseAlgorithmEx.prototype = Object.create(BaseAlgorithm.prototype); 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") if (location.hostname === "localhost" || location.hostname === "127.0.0.1")
console.log(queryString); console.log(queryString);
var graph = this.graph; 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 pathObjects = [];
var properties = {}; var properties = {};
var result = []; var result = [];
@ -204,15 +211,15 @@ BaseAlgorithmEx.prototype.CalculateAlgorithm = function(queryString, resultCallb
$data.each(function(){ $data.each(function(){
if ("hightlightNode" == $(this).attr('key') && $(this).text() == "1") if ("hightlightNode" == $(this).attr('key') && $(this).text() == "1")
{ {
pathObjects.push(graph.FindVertex(id)); pathObjects.push(graph.FindVertex(id));
} }
else else
{ {
if (!properties[id]) if (!properties[id])
{ {
properties[id] = {}; properties[id] = {};
} }
properties[id][$(this).attr('key')] = $(this).text(); properties[id][$(this).attr('key')] = $(this).text();
} }
}); });
}); });

View File

@ -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) Graph.prototype.FindVertex = function(id)
{ {
var res = null; var res = null;

View File

@ -4,10 +4,11 @@
*/ */
function GraphMLCreater(nodes, arcs) function GraphMLCreater(nodes, arcs, ignoreNodes = {})
{ {
this.nodes = nodes; this.nodes = nodes;
this.arcs = arcs; this.arcs = arcs;
this.ignoreNodes = ignoreNodes;
} }
@ -27,7 +28,8 @@ GraphMLCreater.prototype.GetXMLString = function()
for (var i = 0; i < this.nodes.length; i++) for (var i = 0; i < this.nodes.length; i++)
{ {
xmlBoby = xmlBoby + "<node id=\"" + this.nodes[i].id + "\"/>"; if (!this.ignoreNodes.hasOwnProperty(this.nodes[i].id))
xmlBoby = xmlBoby + "<node id=\"" + this.nodes[i].id + "\"/>";
} }
var hasDirected = false; var hasDirected = false;
for (var i = 0; i < this.arcs.length; i++) for (var i = 0; i < this.arcs.length; i++)

View File

@ -37,7 +37,7 @@ FindEulerianLoop.prototype.result = function(resultCallback)
this.CalculateAlgorithm("elloop=cgiInput&report=xml", function (pathObjects, properties, results) this.CalculateAlgorithm("elloop=cgiInput&report=xml", function (pathObjects, properties, results)
{ {
self.resultCallback(pathObjects, properties, results); self.resultCallback(pathObjects, properties, results);
}); }, true);
return true; return true;
} }

View File

@ -37,7 +37,7 @@ FindEulerianPath.prototype.result = function(resultCallback)
this.CalculateAlgorithm("elpath=cgiInput&report=xml", function (pathObjects, properties, results) this.CalculateAlgorithm("elpath=cgiInput&report=xml", function (pathObjects, properties, results)
{ {
self.resultCallback(pathObjects, properties, results); self.resultCallback(pathObjects, properties, results);
}); }, true);
return true; return true;
} }