diff --git a/lang/en/home.php b/lang/en/home.php index 57f9246..ddfccbb 100755 --- a/lang/en/home.php +++ b/lang/en/home.php @@ -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"; ?> diff --git a/lang/ru/home.php b/lang/ru/home.php index 14e4f5d..b062920 100755 --- a/lang/ru/home.php +++ b/lang/ru/home.php @@ -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"] = "Граф не содержит Гамильтонов цикл"; ?> diff --git a/script/plugins/HamiltonianLoop.js b/script/plugins/HamiltonianLoop.js new file mode 100644 index 0000000..469c769 --- /dev/null +++ b/script/plugins/HamiltonianLoop.js @@ -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) ? "⇒" : ""); + } + } + + 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); diff --git a/script/plugins/HamiltonianPath.js b/script/plugins/HamiltonianPath.js new file mode 100644 index 0000000..aaddec3 --- /dev/null +++ b/script/plugins/HamiltonianPath.js @@ -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) ? "⇒" : ""); + } + } + + 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); diff --git a/script/texts.js b/script/texts.js index a383d9c..a408355 100644 --- a/script/texts.js +++ b/script/texts.js @@ -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; } diff --git a/tpl/home.php b/tpl/home.php index f88eafe..1fd8766 100755 --- a/tpl/home.php +++ b/tpl/home.php @@ -10,7 +10,7 @@ - +