Added hamiltonoan loop/path

This commit is contained in:
Unick Soft 2018-10-06 00:27:39 +02:00
parent 62e7489df6
commit 7b1920485d
6 changed files with 215 additions and 1 deletions

View File

@ -129,4 +129,10 @@
$g_lang["export_graph"] = "Export to file";
$g_lang["import_graph"] = "Import from file";
$g_lang["has_hamiltonian_path"] = "Graph has Hamiltonian path";
$g_lang["has_not_hamiltonian_path"] = "Graph has not Hamiltonian path";
$g_lang["has_hamiltonian_loop"] = "Graph has Hamiltonian cycle";
$g_lang["has_not_hamiltonian_loop"] = "Graph has not Hamiltonian cycle";
?>

View File

@ -132,4 +132,10 @@
$g_lang["export_graph"] = "Экспортировать граф";
$g_lang["import_graph"] = "Импортировать граф";
$g_lang["has_hamiltonian_path"] = "Граф содержит Гамильтонову цепь";
$g_lang["has_not_hamiltonian_path"] = "Граф не содержит Гамильтонову цепь";
$g_lang["has_hamiltonian_loop"] = "Граф содержит Гамильтонов цикл";
$g_lang["has_not_hamiltonian_loop"] = "Граф не содержит Гамильтонов цикл";
?>

View File

@ -0,0 +1,92 @@
/**
* Find Eulerian Loop.
*
*/
function FindHamiltonianLoop(graph, app)
{
BaseAlgorithmEx.apply(this, arguments);
this.message = g_processing;
this.selectedObjects = [];
}
// inheritance.
FindHamiltonianLoop.prototype = Object.create(BaseAlgorithmEx.prototype);
FindHamiltonianLoop.prototype.getName = function(local)
{
return local == "ru" ? "Найти Гамильтонов цикл" : "Find Hamiltonian cycle";
}
FindHamiltonianLoop.prototype.getId = function()
{
return "OlegSh.FindHamiltonianLoop";
}
// @return message for user.
FindHamiltonianLoop.prototype.getMessage = function(local)
{
return this.message;
}
FindHamiltonianLoop.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("hamloop=cgiInput&report=xml", function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});
return true;
}
FindHamiltonianLoop.prototype.resultCallback = function(pathObjects, properties, results)
{
result = results.length > 0 && results[0].value > 0 && results[0].type == 1;
var outputResult = {};
outputResult["version"] = 1;
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);
this.selectedObjects = [];
for (var i = 0; i < pathObjects.length; i++)
{
this.selectedObjects[pathObjects[i].id] = 1;
}
this.message = this.message + ": ";
for (var i = 0; i < nodesPath.length; i++)
{
this.message = this.message + this.graph.FindVertex(nodesPath[i]).mainText + ((i < nodesPath.length - 1) ? "&rArr;" : "");
}
}
this.outResultCallback(outputResult);
}
FindHamiltonianLoop.prototype.getObjectSelectedGroup = function(object)
{
return (object.id in this.selectedObjects) ? this.selectedObjects[object.id] : 0;
}
FindHamiltonianLoop.prototype.getPriority = function()
{
return -7.1;
}
// Factory for connected components.
function CreateFindHamiltonianLoop(graph, app)
{
return new FindHamiltonianLoop(graph, app)
}
// Gerister connected component.
RegisterAlgorithm (CreateFindHamiltonianLoop);

View File

@ -0,0 +1,92 @@
/**
* Find Eulerian Loop.
*
*/
function FindHamiltonianPath(graph, app)
{
BaseAlgorithmEx.apply(this, arguments);
this.message = g_processing;
this.selectedObjects = [];
}
// inheritance.
FindHamiltonianPath.prototype = Object.create(BaseAlgorithmEx.prototype);
FindHamiltonianPath.prototype.getName = function(local)
{
return local == "ru" ? "Найти Гамильтонову цепь" : "Find Hamiltonian path";
}
FindHamiltonianPath.prototype.getId = function()
{
return "OlegSh.FindHamiltonianPath";
}
// @return message for user.
FindHamiltonianPath.prototype.getMessage = function(local)
{
return this.message;
}
FindHamiltonianPath.prototype.result = function(resultCallback)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("hampath=cgiInput&report=xml", function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});
return true;
}
FindHamiltonianPath.prototype.resultCallback = function(pathObjects, properties, results)
{
result = results.length > 0 && results[0].value > 0 && results[0].type == 1;
var outputResult = {};
outputResult["version"] = 1;
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);
this.selectedObjects = [];
for (var i = 0; i < pathObjects.length; i++)
{
this.selectedObjects[pathObjects[i].id] = 1;
}
this.message = this.message + ": ";
for (var i = 0; i < nodesPath.length; i++)
{
this.message = this.message + this.graph.FindVertex(nodesPath[i]).mainText + ((i < nodesPath.length - 1) ? "&rArr;" : "");
}
}
this.outResultCallback(outputResult);
}
FindHamiltonianPath.prototype.getObjectSelectedGroup = function(object)
{
return (object.id in this.selectedObjects) ? this.selectedObjects[object.id] : 0;
}
FindHamiltonianPath.prototype.getPriority = function()
{
return -7.1;
}
// Factory for connected components.
function CreateFindHamiltonianPath(graph, app)
{
return new FindHamiltonianPath(graph, app)
}
// Gerister connected component.
RegisterAlgorithm (CreateFindHamiltonianPath);

View File

@ -86,6 +86,12 @@ var g_flowNotExists = "Flow from %1 to %2 does not exists";
var g_sourceVertex = "Source";
var g_sinkVertex = "Sink";
var g_hasHamiltonianLoop = "Graph has Hamiltonian Loop";
var g_hasNotHamiltonianLoop = "Graph has not Hamiltonian Loop";
var g_hasHamiltonianPath = "Graph has Hamiltonian Path";
var g_hasNotHamiltonianPath = "Graph has not Hamiltonian Path";
function loadTexts()
{
g_textsSelectAndMove = document.getElementById("SelectAndMoveObject").innerHTML;
@ -171,4 +177,10 @@ function loadTexts()
g_sourceVertex = document.getElementById("sourceVertex").innerHTML;
g_sinkVertex = document.getElementById("sinkVertex").innerHTML;
g_hasHamiltonianLoop = document.getElementById("hasHamiltonianLoop").innerHTML;
g_hasNotHamiltonianLoop = document.getElementById("hasNotHamiltonianLoop").innerHTML;
g_hasHamiltonianPath = document.getElementById("hasHamiltonianPath").innerHTML;
g_hasNotHamiltonianPath = document.getElementById("hasNotHamiltonianPath").innerHTML;
}

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=7")?>" ></script>
<script src="<?= Root("script/example.js?v=8")?>" ></script>
</head>
<!--
<div class="pull-right">
@ -419,6 +419,12 @@
<p id="sourceVertex" class="translation"><?= L('sourceVertex')?></p>
<p id="sinkVertex" class="translation"><?= L('sinkVertex')?></p>
<p id="hasNotHamiltonianLoop" class="translation"><?= L('has_not_hamiltonian_loop')?></p>
<p id="hasHamiltonianLoop" class="translation"><?= L('has_hamiltonian_loop')?></p>
<p id="hasNotHamiltonianPath" class="translation"><?= L('has_not_hamiltonian_path')?></p>
<p id="hasHamiltonianPath" class="translation"><?= L('has_hamiltonian_path')?></p>
</section>
<!--
<script>