2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00
This commit is contained in:
2023-03-28 00:59:26 +03:00
parent 5a9abe4084
commit 1c440adf43
8 changed files with 255 additions and 14 deletions

View File

@ -1,15 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Esiur.Analysis.Graph
{
public class DirectedGraph <T>: IGraph
public class DirectedGraph <T>: IGraph where T:struct
{
public Node<T>[] Nodes { get; set; }
public Edge<T>[] Edges { get; set; }
public List<Node<T>> Nodes { get;} = new List<Node<T>>();
public List<Edge<T>> Edges { get; }= new List<Edge<T>>();
public void Link(Node<T> source, Node<T> destination , T weight, string label)
{
var edge = new Edge<T>() { SourceNode = source, DestinationNode = destination, Weight = weight, Label = label };
Edges.Add(edge);
source.Destinations.Add(edge);
destination.Sources.Add(edge);
}
public Node<T> AddNode(T value, string label, int x, int y)
{
var n = new Node<T>() { Value = value, Label = label, X = x, Y = y };
Nodes.Add(n);
return n;
}
public Matrix<T> TransitionMatrix { get; private set; }
public Matrix<T> CurrentStep { get; private set; }
public Edge<T>[,] EdgesMatrix { get; private set; }
public void Build()
{
// create matrix
var m = new T[Nodes.Count, Nodes.Count];
var e = new Edge<T>[Nodes.Count, Nodes.Count];
for(var i = 0; i < Nodes.Count; i++)
{
for(var j = 0; j < Nodes.Count; j++)
{
var link = Edges.FirstOrDefault(x => x.SourceNode == Nodes[i] && x.DestinationNode == Nodes[j]);
if (link == null)
m[i, j] = default(T);
else
{
m[i, j] = link.Weight;
e[i, j] = link;
}
}
}
TransitionMatrix = new Matrix<T>(m);
CurrentStep = TransitionMatrix;
EdgesMatrix = e;
}
public void Step()
{
CurrentStep *= CurrentStep;
// update weights
for(var i = 0; i < CurrentStep.Rows; i++)
for(var j = 0; j < CurrentStep.Columns; j++)
{
if (EdgesMatrix[i, j] != null)
EdgesMatrix[i, j].Weight = CurrentStep[i, j];
}
}
}
}

View File

@ -6,7 +6,11 @@ namespace Esiur.Analysis.Graph
{
public class Edge<T>
{
public T SourceNode { get; set; }
public T DestinationNode { get; set; }
public Node<T> SourceNode { get; set; }
public Node<T> DestinationNode { get; set; }
public T Weight { get; set; }
public string Label { get; set; }
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography.X509Certificates;
using System.Data.Common;
using System.Text;
namespace Esiur.Analysis.Graph
@ -20,7 +19,34 @@ namespace Esiur.Analysis.Graph
this.value = value;
}
public static Matrix<T> Multiply<T>(Matrix<T> a, Matrix<T> b) where T:struct
public static Matrix<T> operator *(Matrix<T> a, Matrix<T> b)
=> Multiply(a, b);
public Matrix<T> Pow(int power)
{
var rt = this;
for (var i = 1; i < power; i++)
rt = rt * rt;
return rt;
}
public override string ToString()
{
var rt = "";
for (var i = 0; i < Rows; i++)
{
rt += "|";
for (var j = 0; j < Columns - 1; j++)
rt += value[i, j] + ",";
rt += value[i, Columns - 1] + "|";
}
return rt;
}
public static Matrix<T> Multiply<T>(Matrix<T> a, Matrix<T> b) where T : struct
{
@ -36,7 +62,9 @@ namespace Esiur.Analysis.Graph
{ // bColumn
for (int k = 0; k < a.Columns; k++)
{ // aColumn
rt[i, j] += a[i, k] * b[k, j];
dynamic aValue = a[i, k];
dynamic bValue = b[k, j];
rt[i, j] += aValue * bValue;// a[i, k] * b[k, j];
}
}
}

View File

@ -10,5 +10,10 @@ namespace Esiur.Analysis.Graph
public int X { get; set; }
public int Y { get; set; }
public string Label { get; set; }
public List<Edge<T>> Destinations { get; } = new List<Edge<T>>();
public List<Edge<T>> Sources { get; } = new List<Edge<T>>();
}
}