Add creating graph using Edge List.

This commit is contained in:
Oleg Sh
2022-09-18 13:19:53 +02:00
parent b3838b70ca
commit 4b82685aaa
40 changed files with 644 additions and 1 deletions

View File

@@ -846,6 +846,28 @@ Application.prototype.onPostLoadEvent = function()
wasLoad = true;
}
var pairs = document.getElementById("inputPair").innerHTML;
if (pairs.length > 0)
{
pairs = pairs.replaceAll('>', '>');
pairs = pairs.replaceAll('&lt;', '<');
if (!this.SetPairSmart(pairs))
{
this.userAction("Pair.Failed");
this.ShowPairErrorDialog(pairs);
}
else
{
this.userAction("Pair.Success");
}
this.updateMessage();
this.redrawGraph();
wasLoad = true;
}
if (!wasLoad)
{
var graphName = this.getParameterByName("graph");
@@ -933,6 +955,22 @@ Application.prototype.SetAdjacencyMatrix = function (matrix, separator)
return res;
}
Application.prototype.SetPair = function (pair)
{
var res = true;
var r = {};
var c = {};
if (!this.TestPair(pair))
{
$.get( "/" + SiteDir + "cgi-bin/addFailedMatrix.php?text=pair&matrix=" + encodeURIComponent(pair), function( data ) {;});
res = false;
}
this.graph.SetPair(pair, new Point(this.GetRealWidth(), this.GetRealHeight()), this.currentEnumVerticesType);
this.AutoAdjustViewport();
this.redrawGraph();
return res;
}
Application.prototype.GetIncidenceMatrix = function ()
{
@@ -944,6 +982,11 @@ Application.prototype.TestIncidenceMatrix = function (matrix, rowsObj, colsObj)
return this.graph.TestIncidenceMatrix(matrix, rowsObj, colsObj);
}
Application.prototype.TestPair = function (pair)
{
return this.graph.TestPair(pair);
}
Application.prototype.SetIncidenceMatrix = function (matrix)
{
var res = true;
@@ -1011,6 +1054,22 @@ Application.prototype.SetIncidenceMatrixSmart = function (matrix)
return res;
}
Application.prototype.SetPairSmart = function (pair)
{
var res = false;
if (this.TestPair(pair))
{
res = this.SetPair(pair);
}
else
{
res = false;
}
return res;
}
Application.prototype.SaveGraphOnDisk = function ()
{
@@ -1288,6 +1347,34 @@ Application.prototype.ShowIncidenceMatrixErrorDialog = function(matrix)
buttons: dialogButtons,
});
}
Application.prototype.ShowPairErrorDialog = function(pair)
{
var dialogButtons = {};
pair = pair.replaceAll(/\n/g,'%0A');
pair = pair.replaceAll('>', '&gt;');
pair = pair.replaceAll('<', '&lt;');
dialogButtons[g_readMatrixHelp] = function() {
window.location.assign(g_language == "ru" ? "./wiki/Справка/СписокРебер" : "./wiki/Help/EdgeList");
};
dialogButtons[g_fix] = function() {
window.location.assign("./create_graph_by_edge_list?pair=" + pair);
};
dialogButtons[g_close] = function() {
$( this ).dialog( "close" );
};
$( "#pairErrorInc" ).dialog({
resizable: false,
title: g_pairWrongFormat,
width: 400,
modal: true,
dialogClass: 'EdgeDialog',
buttons: dialogButtons,
});
}
Application.prototype.SetFindPathReport = function (value)
{

View File

@@ -38,6 +38,21 @@ Graph.prototype.AddNewVertex = function(vertex)
return this.vertices.length - 1;
}
Graph.prototype.ClearGraph = function() {
// List of vertex.
this.vertices = [];
// List of arcs.
this.edges = [];
// Unique Id of new graph.
this.uidGraph = 0;
// Unique Id of new edge.
this.uidEdge = 10000;
// Has direction edge.
this.hasDirect = false;
// Is graph multi
this.isMultiGraph = false;
}
Graph.prototype.AddNewEdgeSafe = function(graph1, graph2, isDirect, weight, replaceIfExists = true)
{
return this.AddNewEdge(new BaseEdge(graph1, graph2, isDirect, weight), replaceIfExists);
@@ -138,6 +153,21 @@ Graph.prototype.FindVertex = function(id)
return res;
}
Graph.prototype.FindVertexByTitle = function(title)
{
var res = null;
for (var i = 0; i < this.vertices.length; i++)
{
if (this.vertices[i].mainText == title)
{
res = this.vertices[i];
break;
}
}
return res;
}
// deprecated
Graph.prototype.FindEdge = function(id1, id2)
{
@@ -349,6 +379,43 @@ Graph.prototype.TestAdjacencyMatrix = function (matrix, rowsObj, colsObj, separa
return bGoodFormat;
}
Graph.prototype.TestPair = function (pair)
{
let lines = pair.split ("\n");
// Check:
// a - b
// a > b
// a < b
// a-(1)-b
// a-(1)>b
// a<(1)-b
let regExp = [
/^.+-.+$/g,
/^.+\>.+$/g,
/^.+<.+$/g,
/^.+-\(\d+\.?\d+\)-.+$/g,
/^.+-\(\d+\.?\d+\)\>.+$/g,
/^.+<\(\d+\.?\d+\)\-.+$/g
];
let res = true;
for (let i = 0; i < lines.length; ++i)
{
let resLine = false;
let line = lines[i];
if (line == "") {
continue;
}
for (let j = 0; j < regExp.length; ++j) {
if (line.match(regExp[j])) {
resLine = true;
}
}
res = resLine && res;
}
return res;
}
Graph.prototype.IsVerticesHaveSamePosition = function (position, vertexCount)
{
@@ -366,6 +433,10 @@ Graph.prototype.IsVerticesHaveSamePosition = function (position, vertexCount)
return res;
}
Graph.prototype.GetRandomPosition = function (viewportSize) {
return new Point(Math.random() * viewportSize.x, Math.random() * viewportSize.y);
}
Graph.prototype.GetRandomPositionOfVertex = function (matrix, vertexIndex, viewportSize)
{
var point = new Point(0, 0);
@@ -628,6 +699,88 @@ Graph.prototype.SetAdjacencyMatrix = function (matrix, viewportSize, currentEnum
}
}
Graph.prototype.SetPair = function (pairs, viewportSize, currentEnumVerticesType)
{
if (this.TestPair(pairs))
{
this.ClearGraph();
let lines = pairs.split ("\n");
// Extract:
// a - b
// a > b
// a < b
// a-(1)-b
// a-(1)>b
// a<(1)-b
let regExp = [
/^(.+)-(.+)$/g,
/^(.+)\>(.+)$/g,
/^(.+)<(.+)$/g,
/^(.+)-\((\d+|\d+\.?\d+)\)-(.+)$/g,
/^(.+)-\((\d+|\d+\.?\d+)\)\>(.+)$/g,
/^(.+)<\((\d+|\d+\.?\d+)\)\-(.+)$/g,
];
let bWeightGraph = false;
var newVertices = [];
for (var i = 0; i < lines.length; i++)
{
let line = lines[i];
if (line == "") {
continue;
}
for (let j = regExp.length - 1; j >= 0; --j) {
if (!line.match(regExp[j])) {
continue;
}
let groupes = Array.from(line.matchAll(regExp[j]));
let groupe = groupes[0];
let vetext1Title = groupe[1];
let vertex1 = this.FindVertexByTitle(vetext1Title);
if (vertex1 == null) {
let newPosition = this.GetRandomPosition(viewportSize);
vertex1 = this.vertices[this.AddNewVertex(new BaseVertex(newPosition.x, newPosition.y, currentEnumVerticesType))];
vertex1.mainText = vetext1Title;
newVertices.push(vertex1);
}
let vetext2Title = groupe[j <= 2 ? 2 : 3];
let vertex2 = this.FindVertexByTitle(vetext2Title);
if (vertex2 == null) {
let newPosition = this.GetRandomPosition(viewportSize);
vertex2 = this.vertices[this.AddNewVertex(new BaseVertex(newPosition.x, newPosition.y, currentEnumVerticesType))];
vertex2.mainText = vetext2Title;
newVertices.push(vertex2);
}
let isDirect = j == 1 || j == 2 || j == 4 || j == 5;
let weight = 1;
if (j > 2) {
weight = groupe[2];
bWeightGraph = true;
}
let isRevertEdge = j == 2 || j == 5;
let nEdgeIndex = this.AddNewEdgeSafe(isRevertEdge ? vertex2 : vertex1, isRevertEdge ? vertex1 : vertex2, isDirect, weight, false);
this.FixEdgeCurve(nEdgeIndex);
break;
}
}
// Set use weight false, because we have unweighted graph.
if (!bWeightGraph)
{
this.edges.forEach(function(part, index, theArray) {
theArray[index].useWeight = false;
});
}
this.VerticesReposition(viewportSize, newVertices);
}
}
Graph.prototype.TestIncidenceMatrix = function (matrix, rowsObj, colsObj, separator)
{

View File

@@ -43,6 +43,8 @@ var g_write_to_us = "Write to us";
var g_fixMatrix = "Fix matrix";
var g_readMatrixHelp = "Matrix format help";
var g_matrixWrongFormat = "Matrix is wrong";
var g_pairWrongFormat = "Edge List is wrong";
var g_fix = "Fix"
var g_save_image_dialog = "Save graph image";
@@ -395,4 +397,7 @@ function loadTexts()
g_reverseAllEdges = document.getElementById("reverseAllEdges").innerHTML;
g_makeAllUndirected = document.getElementById("makeAllUndirected").innerHTML;
g_makeAllDirected = document.getElementById("makeAllDirected").innerHTML;
g_pairWrongFormat = document.getElementById("pairWrongFormat").innerHTML;
g_fix = document.getElementById("fixButton").innerHTML;;
}