Added Floid algorithm.

This commit is contained in:
Unick Soft 2017-12-10 22:01:46 +03:00
parent d6d27397a4
commit 6a6919b2f1
8 changed files with 290 additions and 3 deletions

View File

@ -40,6 +40,11 @@
display: none; display: none;
} }
#floidMatrix
{
display: none;
}
#saveDialog #saveDialog
{ {
display: none; display: none;
@ -66,6 +71,14 @@
overflow-wrap: normal; overflow-wrap: normal;
} }
#FloidMatrixField
{
overflow: scroll;
width : 100%;
height: 300px;
overflow-wrap: normal;
}
#NeedAlgorithmMessage #NeedAlgorithmMessage
{ {
width : 100%; width : 100%;
@ -282,6 +295,10 @@
{ {
height: 200px; height: 200px;
} }
#IncidenceMatrixField
{
height: 200px;
}
#addVertex #addVertex
{ {

View File

@ -111,4 +111,11 @@
$g_lang["has_eulerian_path"] = "Graph has Eulerian path"; $g_lang["has_eulerian_path"] = "Graph has Eulerian path";
$g_lang["has_not_eulerian_path"] = "Graph has not Eulerian path"; $g_lang["has_not_eulerian_path"] = "Graph has not Eulerian path";
$g_lang["min_dist_matrix_description"] = "Matrix of minimal distances";
$g_lang["graphOfMinDist"] = "Graph of minimal distances";
$g_lang["checkToSave"] = "Check to save";
$g_lang["showDistMatrix"] = "Show distance matrix";
$g_lang["distMatrixText"] = "Distance matrix";
?> ?>

View File

@ -115,4 +115,10 @@
$g_lang["has_eulerian_path"] = "Граф содержит Эйлерову цепь"; $g_lang["has_eulerian_path"] = "Граф содержит Эйлерову цепь";
$g_lang["has_not_eulerian_path"] = "Граф не содержит Эйлерову цепь"; $g_lang["has_not_eulerian_path"] = "Граф не содержит Эйлерову цепь";
$g_lang["min_dist_matrix_description"] = "Матрица минимальных расстояний графа";
$g_lang["graphOfMinDist"] = "Граф минимальных расстояний.";
$g_lang["checkToSave"] = "Нажмите для сохранения";
$g_lang["showDistMatrix"] = "Показать матрицу расстояний";
$g_lang["distMatrixText"] = "Матрица расстояний";
?> ?>

View File

@ -109,6 +109,17 @@ BaseAlgorithm.prototype.needRestoreUpText = function()
return true; return true;
} }
// @return true, if you change resotry graph after use.
BaseAlgorithm.prototype.wantRestore = function()
{
return false;
}
// calls this method if wantRestore return true.
BaseAlgorithm.prototype.restore = function()
{
}
// @return 0, if object is not selected, in other case return groupe of selection. // @return 0, if object is not selected, in other case return groupe of selection.
BaseAlgorithm.prototype.getObjectSelectedGroup = function(object) BaseAlgorithm.prototype.getObjectSelectedGroup = function(object)
{ {

View File

@ -751,6 +751,11 @@ AlgorithmGraphHandler.prototype.RestoreAll = function()
{ {
this.RestoreUpText(); this.RestoreUpText();
} }
if (this.algorithm.wantRestore())
{
this.algorithm.restore();
}
} }
AlgorithmGraphHandler.prototype.SaveUpText = function() AlgorithmGraphHandler.prototype.SaveUpText = function()
@ -795,6 +800,11 @@ AlgorithmGraphHandler.prototype.InitControls = function()
this.algorithm.messageWasChanged(); this.algorithm.messageWasChanged();
} }
AlgorithmGraphHandler.prototype.GetMessage = function()
{
return this.algorithm.getMessage(g_language);
}
/** /**
* Groupe rename vertices. * Groupe rename vertices.

215
script/plugins/Floid.js Normal file
View File

@ -0,0 +1,215 @@
/**
* Find short path.
*
*/
function FloidAlgorithm(graph, app)
{
BaseAlgorithmEx.apply(this, arguments);
this.selectedObjects = {};
this.matrix = [];
this.updateMessage(false);
this.edgesCopy = [];
}
// inheritance.
FloidAlgorithm.prototype = Object.create(BaseAlgorithmEx.prototype);
// First selected.
FloidAlgorithm.prototype.firstObject = null;
// Second selected.
FloidAlgorithm.prototype.secondObject = null;
// Path
FloidAlgorithm.prototype.pathObjects = null;
// infinity
FloidAlgorithm.prototype.infinity = 1E8;
FloidAlgorithm.prototype.getName = function(local)
{
return local == "ru" ? "Алгоритм Флойда — Уоршелла" : "FloydWarshall algorithm";
}
FloidAlgorithm.prototype.getId = function()
{
return "OlegSh.FloidAlgorithm";
}
// @return message for user.
FloidAlgorithm.prototype.getMessage = function(local)
{
return this.message;
}
FloidAlgorithm.prototype.result = function(resultCallback)
{
var result = {};
result["version"] = 1;
this.matrix = [];
for (var i = 0; i < this.graph.vertices.length; i ++)
{
this.matrix.push([]);
var v1 = this.graph.vertices[i];
var str = "";
for (var j = 0; j < this.graph.vertices.length; j ++)
{
var v2 = this.graph.vertices[j];
var edge = this.graph.FindEdge(v1.id, v2.id);
if (edge != null)
{
this.matrix[i][j] = edge.GetWeight();
}
else
{
this.matrix[i][j] = this.infinity;
}
}
}
for (var k = 0; k < this.graph.vertices.length; k ++)
for (var i = 0; i < this.graph.vertices.length; i ++)
for (var j = 0; j < this.graph.vertices.length; j ++)
{
if (this.matrix[i][j] > this.matrix[i][k] + this.matrix[k][j])
{
this.matrix[i][j] = this.matrix[i][k] + this.matrix[k][j];
}
}
//console.log(this.matrix);
// Remove all edges.
this.egdesCopy = this.graph.edges.slice();
this.removeAllEdges();
this.graph.hasDirect = false;
// Added new edges
for (var i = 0; i < this.graph.vertices.length; i ++)
{
for (var j = 0; j < this.graph.vertices.length; j ++)
{
if (i != j)
{
var directed = (this.matrix[i][j] != this.matrix[j][i]);
if (this.matrix[i][j] < this.infinity && (directed || i < j))
{
this.graph.AddNewEdgeSafe(this.graph.vertices[i], this.graph.vertices[j], directed, this.matrix[i][j]);
}
}
}
}
this.app.redrawGraph();
return result;
/*
if (this.firstObject && this.secondObject)
{
this.outResultCallback = function (result ) { resultCallback(result); };
self = this;
this.CalculateAlgorithm("dsp=cgiInput&start=" + this.firstObject.id + "&finish=" + this.secondObject.id + "&report=xml", function (pathObjects, properties, results)
{
self.resultCallback(pathObjects, properties, results);
});
}
return null;
*/
}
FloidAlgorithm.prototype.getObjectSelectedGroup = function(object)
{
return 0;
}
FloidAlgorithm.prototype.messageWasChanged = function()
{
var self = this;
var matrixButton = document.getElementById("showFloidMatrix");
if (matrixButton)
{
matrixButton.onclick = function () {
var dialogButtons = {};
dialogButtons[g_close] = function() {
$( this ).dialog( "close" );
};
$( "#FloidMatrixField" ).val(self.GetFloidMatrix());
$( "#floidMatrix" ).dialog({
resizable: false,
height: "auto",
width: "auto",
modal: true,
title: g_minDistMatrixText,
buttons: dialogButtons,
dialogClass: 'EdgeDialog'
});
};
}
$('#saveFloidGraph').change(function() {
self.updateMessage(this.checked);
});
}
FloidAlgorithm.prototype.GetFloidMatrix = function()
{
return this.graph.GetAdjacencyMatrix();
}
FloidAlgorithm.prototype.changedType = function()
{
var enumReport = document.getElementById("enumReport");
this.app.SetCurrentValue("findShortPathReportType", enumReport.options[enumReport.selectedIndex].value);
this.updateUpText();
}
FloidAlgorithm.prototype.getPriority = function()
{
return -9.5;
}
FloidAlgorithm.prototype.removeAllEdges = function()
{
while (this.graph.edges.length > 0)
{
this.graph.DeleteEdge(this.graph.edges[0]);
}
}
FloidAlgorithm.prototype.wantRestore = function()
{
console.log($("#saveFloidGraph").is(':checked'));
return !$("#saveFloidGraph").is(':checked');
}
FloidAlgorithm.prototype.restore = function()
{
this.removeAllEdges();
this.graph.hasDirect = false;
console.log(this.egdesCopy);
for (var i = 0; i < this.egdesCopy.length; i ++)
{
this.graph.AddNewEdgeSafe(this.egdesCopy[i].vertex1, this.egdesCopy[i].vertex2, this.egdesCopy[i].isDirect, this.egdesCopy[i].weight);
}
}
FloidAlgorithm.prototype.updateMessage = function(save)
{
this.message = g_graphOfMinDist + " <label style=\"margin-bottom: 0px\">" + g_checkToSave + " <input id=\"saveFloidGraph\" type=\"checkbox\"" + (save ? "checked" : "") + "></label>" +
"<button type=\"button\" class=\"btn btn-default btn-xs\" id=\"showFloidMatrix\" style=\"float:right\">" + g_showDistMatrix + "</button>"
}
// Factory for connected components.
function CreateFloidAlgorithm(graph, app)
{
return new FloidAlgorithm(graph, app)
}
// Gerister connected component.
RegisterAlgorithm (CreateFloidAlgorithm);

View File

@ -73,6 +73,11 @@ var g_vote = "Vote";
var g_recommendAlgorithm = "Recommend algorithm"; var g_recommendAlgorithm = "Recommend algorithm";
var g_graphOfMinDist = "Graph of minimal distances.";
var g_checkToSave = "Check to save";
var g_showDistMatrix = "Show Distance matrix";
var g_minDistMatrixText = "Minimal distances matrix";
function loadTexts() function loadTexts()
{ {
g_textsSelectAndMove = document.getElementById("SelectAndMoveObject").innerHTML; g_textsSelectAndMove = document.getElementById("SelectAndMoveObject").innerHTML;
@ -145,4 +150,9 @@ function loadTexts()
g_hasEulerianPath = document.getElementById("hasEulerianPath").innerHTML; g_hasEulerianPath = document.getElementById("hasEulerianPath").innerHTML;
g_hasNotEulerianPath = document.getElementById("hasNotEulerianPath").innerHTML; g_hasNotEulerianPath = document.getElementById("hasNotEulerianPath").innerHTML;
g_graphOfMinDist = document.getElementById("graphOfMinDist").innerHTML;
g_checkToSave = document.getElementById("checkToSave").innerHTML;
g_showDistMatrix = document.getElementById("showDistMatrix").innerHTML;
g_minDistMatrixText = document.getElementById("distMatrixText").innerHTML;
} }

View File

@ -221,6 +221,14 @@
</form> </form>
</div> </div>
<div id="floidMatrix">
<form>
<fieldset>
<p><?= L('min_dist_matrix_description') ?></p>
<textarea name="floidMatrixField" id="FloidMatrixField" wrap="off"></textarea>
</fieldset>
</form>
</div>
<div id="saveDialog"> <div id="saveDialog">
<form> <form>
@ -386,8 +394,11 @@
<p id="hasNotEulerianPath" class="translation"><?= L('has_not_eulerian_path')?></p> <p id="hasNotEulerianPath" class="translation"><?= L('has_not_eulerian_path')?></p>
<p id="hasEulerianPath" class="translation"><?= L('has_eulerian_path')?></p> <p id="hasEulerianPath" class="translation"><?= L('has_eulerian_path')?></p>
<p id="graphOfMinDist" class="translation"><?= L('graphOfMinDist')?></p>
</section> <p id="checkToSave" class="translation"><?= L('checkToSave')?></p>
<p id="showDistMatrix" class="translation"><?= L('showDistMatrix')?></p>
<p id="distMatrixText" class="translation"><?= L('distMatrixText')?></p>
</section>
<!-- <!--
<script> <script>
(adsbygoogle = window.adsbygoogle || []).push({}); (adsbygoogle = window.adsbygoogle || []).push({});