Change spanning tree calculations. Ignore orientation of edges.

This commit is contained in:
Unick Soft
2020-09-05 12:51:17 +02:00
parent ca1733d7ff
commit cf0970f5a1
11 changed files with 70 additions and 8 deletions

View File

@@ -172,6 +172,27 @@ Graph.prototype.FindEdgeMin = function(id1, id2)
return res;
}
Graph.prototype.FindEdgeMinIgnoreDirection = function(id1, id2)
{
var res = null;
var minWeight = this.infinity;
for (var i = 0; i < this.edges.length; i++)
{
var edge = this.edges[i];
if ((edge.vertex1.id == id1 && edge.vertex2.id == id2)
|| (edge.vertex1.id == id2 && edge.vertex2.id == id1))
{
if (edge.weight < minWeight)
{
res = edge;
minWeight = edge.weight;
}
}
}
return res;
}
Graph.prototype.FindAllEdges = function(id1, id2)
{
var res = [];

View File

@@ -27,11 +27,12 @@ MinimumSpanningTree.prototype.getMessage = function(local)
{
if (!this.isNotConneted )
{
return local == "ru" ? "Вес минимального оставного дерева равен " + this.MST : "Weight of minimum spanning tree is " + this.MST;
return g_SpanningTreeResult + this.MST + ". " +
(this.graph.hasDirectEdge() ? g_SpanningTreeIgnoreDir : "");
}
else
{
return local == "ru" ? "Граф не является связным" : "Graph is disconnected";
return g_SpanningTreeNotConnected;
}
}
@@ -41,9 +42,10 @@ MinimumSpanningTree.prototype.result = function(resultCallback)
this.edges = [];
this.isNotConneted = true;
var tempVertexes = this.graph.vertices.slice();
connectedVertex = getVertexToVertexArray(this.graph, false);
connectedVertex = getVertexToVertexArray(this.graph, true);
if (!this.graph.hasDirectEdge())
// We ignore orientation for this algorithm.
//if (!this.graph.hasDirectEdge())
{
res = this.resultStartedFrom(tempVertexes[0], connectedVertex);
this.isNotConneted = res.isNotConneted;
@@ -53,7 +55,7 @@ MinimumSpanningTree.prototype.result = function(resultCallback)
this.edges = res.edges;
}
}
else
/*else
{
for (var i = 0; i < tempVertexes.length; i++)
{
@@ -69,7 +71,7 @@ MinimumSpanningTree.prototype.result = function(resultCallback)
}
}
}
}
}*/
var result = {};
result["version"] = 1;
@@ -105,7 +107,8 @@ MinimumSpanningTree.prototype.resultStartedFrom = function(vertex, connectedVert
for (j = 0; j < connectedVertex[element.id].length; j++)
{
var connectedElement = connectedVertex[element.id][j];
var connectedEdge = this.graph.FindEdgeMin(element.id, connectedElement.id);
var connectedEdge = this.graph.FindEdgeMinIgnoreDirection(element.id, connectedElement.id);
if (inTree.indexOf(connectedElement) < 0)
{
if (minEdge == null || minEdge.weight > connectedEdge.weight)

View File

@@ -128,6 +128,9 @@ var g_modernGraphStyleName = "Visualisation based on weight";
var g_RadiusAndDiameter = "Search graph radius and diameter";
var g_findShortPathName = "Find shortest path using Dijkstra's algorithm";
var g_VerticesDegreeName = "Calculate vertexes degree";
var g_SpanningTreeResult = "Min Spanning Tree is";
var g_SpanningTreeIgnoreDir = "We ignored edges direction for calculation";
var g_SpanningTreeNotConnected = "Graph is not connected";
function loadTexts()
{
@@ -264,4 +267,8 @@ function loadTexts()
g_RadiusAndDiameter = document.getElementById("RadiusAndDiameter").innerHTML;
g_findShortPathName = document.getElementById("findShortPathName").innerHTML;
g_VerticesDegreeName = document.getElementById("VerticesDegreeName").innerHTML;
g_SpanningTreeResult = document.getElementById("MinSpanningTreeResult").innerHTML;
g_SpanningTreeIgnoreDir = document.getElementById("MinSpanningIgnoreDir").innerHTML;
g_SpanningTreeNotConnected = document.getElementById("MinSpanningNotConnected").innerHTML;
}