Added multigraph support for hamiltonian loop/path

This commit is contained in:
Unick Soft 2020-04-01 21:21:20 +02:00
parent fe4f14970b
commit aa1be3fae5
5 changed files with 88 additions and 12 deletions

View File

@ -250,7 +250,23 @@ BaseAlgorithmEx.prototype.GetNodesPath = function(array, start, count)
var res = [];
for (var index = start; index < start + count; index++)
{
res.push(array[index].value);
if (array[index].type == 4)
{
res.push(array[index].value);
}
}
return res;
}
BaseAlgorithmEx.prototype.GetNodesEdgesPath = function(array, start, count)
{
var res = [];
for (var index = start; index < start + count; index++)
{
if (array[index].type == 4 || array[index].type == 5)
{
res.push(array[index].value);
}
}
return res;
}

View File

@ -45,6 +45,7 @@ function Application(document, window)
this.backgroundCommonStyle = new CommonBackgroundStyle();
this.backgroundPrintStyle = new PrintBackgroundStyle();
this.isBackgroundCommonStyleCustom = false;
this.renderPathWithEdges = false;
};
// List of graph.
@ -104,7 +105,12 @@ Application.prototype.redrawGraphTimer = function()
for (i = 0; i < this.renderPath.length - 1; i++)
{
var edge = null;
if (this.renderMinPath)
if (this.renderPathWithEdges)
{
edge = this.graph.FindEdgeById(this.renderPath[i + 1]);
i++;
}
else if (this.renderMinPath)
{
edge = this.graph.FindEdgeMin(this.renderPath[i], this.renderPath[i + 1]);
}
@ -124,13 +130,21 @@ Application.prototype.redrawGraphTimer = function()
if (i >= this.renderPath.length - 1)
{
i = 0;
if (this.renderPathWithEdges)
i = 1;
this.renderPathCounter = 0;
currentLength = 0;
this.renderPathLoops += 1;
}
var edge = null;
if (this.renderMinPath)
var currentVertexId = this.renderPath[i];
if (this.renderPathWithEdges)
{
edge = this.graph.FindEdgeById(this.renderPath[i]);
currentVertexId = this.renderPath[i - 1];
}
else if (this.renderMinPath)
{
edge = this.graph.FindEdgeMin(this.renderPath[i], this.renderPath[i + 1]);
}
@ -141,7 +155,7 @@ Application.prototype.redrawGraphTimer = function()
var progress = (this.renderPathCounter - currentLength) / edge.GetPixelLength();
this.RedrawEdgeProgress(context, edge, edge.vertex1.id == this.renderPath[i] ? progress : 1.0 - progress);
this.RedrawEdgeProgress(context, edge, edge.vertex1.id == currentVertexId ? progress : 1.0 - progress);
this.renderPathCounter += movePixelStep;
@ -232,7 +246,16 @@ Application.prototype.updateRenderPathLength = function()
{
for (var i = 0; i < this.renderPath.length - 1; i++)
{
var edge = this.graph.FindEdge(this.renderPath[i], this.renderPath[i + 1]);
var edge = null;
if (this.renderPathWithEdges)
{
edge = this.graph.FindEdgeById(this.renderPath[i + 1]);
i++;
}
else
{
edge = this.graph.FindEdge(this.renderPath[i], this.renderPath[i + 1]);
}
this.renderPathLength += edge.GetPixelLength();
}
}
@ -260,6 +283,23 @@ Application.prototype.setRenderPath = function(renderPath, renderMinPath)
{
this.renderPath = renderPath;
this.renderMinPath = renderMinPath;
this.renderPathWithEdges = false;
if (this.renderPath.length > 0)
{
this.startRenderTimer();
}
else
{
this.stopRenderTimer();
}
}
Application.prototype.setRenderPathWithEdges = function(renderPath)
{
this.renderPath = renderPath;
this.renderMinPath = false;
this.renderPathWithEdges = true;
if (this.renderPath.length > 0)
{
@ -1231,6 +1271,11 @@ Application.prototype.resultCallback = function(paths)
{
this.setRenderPath(paths["paths"][0], "minPath" in paths);
}
else if ((paths instanceof Object) && "pathsWithEdges" in paths)
{
this.setRenderPathWithEdges(paths["pathsWithEdges"][0]);
}
this.updateMessage();
this.redrawGraph();
}

View File

@ -52,9 +52,11 @@ FindHamiltonianLoop.prototype.resultCallback = function(pathObjects, properties,
this.message = result > 0 ? g_hasHamiltonianLoop : g_hasNotHamiltonianLoop;
if (result > 0)
{
var nodesPath = this.GetNodesPath(results, 1, results.length - 1);
outputResult["paths"] = [];
outputResult["paths"].push(nodesPath);
var nodesEdgesPath = this.GetNodesEdgesPath(results, 1, results.length - 1);
var nodesPath = this.GetNodesPath(results, 1, results.length - 1);
outputResult["pathsWithEdges"] = [];
outputResult["pathsWithEdges"].push(nodesEdgesPath);
this.selectedObjects = [];
for (var i = 0; i < pathObjects.length; i++)
@ -82,6 +84,12 @@ FindHamiltonianLoop.prototype.getPriority = function()
return -5;
}
// Algorithm support multi graph
FindHamiltonianLoop.prototype.IsSupportMultiGraph = function()
{
return true;
}
// Factory for connected components.
function CreateFindHamiltonianLoop(graph, app)
{

View File

@ -52,9 +52,11 @@ FindHamiltonianPath.prototype.resultCallback = function(pathObjects, properties,
this.message = result > 0 ? g_hasHamiltonianPath : g_hasNotHamiltonianPath;
if (result > 0)
{
var nodesPath = this.GetNodesPath(results, 1, results.length - 1);
outputResult["paths"] = [];
outputResult["paths"].push(nodesPath);
var nodesEdgesPath = this.GetNodesEdgesPath(results, 1, results.length - 1);
var nodesPath = this.GetNodesPath(results, 1, results.length - 1);
outputResult["pathsWithEdges"] = [];
outputResult["pathsWithEdges"].push(nodesEdgesPath);
this.selectedObjects = [];
for (var i = 0; i < pathObjects.length; i++)
@ -82,6 +84,11 @@ FindHamiltonianPath.prototype.getPriority = function()
return -5;
}
FindHamiltonianPath.prototype.IsSupportMultiGraph = function()
{
return true;
}
// Factory for connected components.
function CreateFindHamiltonianPath(graph, app)
{

View File

@ -10,7 +10,7 @@
<script src="<?= Root('i/js/dev/jquery-ui.js')?>"></script>
<script src="<?= Root('i/js/dev/jquery.feedback_me.js')?>"></script>
<script src="<?= Root("script/example.js?v=24")?>" ></script>
<script src="<?= Root("script/example.js?v=25")?>" ></script>
</head>
<!--
<div class="pull-right">