using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Esiur.Analysis.Graph { public class DirectedGraph : IGraph where T:struct { public List> Nodes { get;} = new List>(); public List> Edges { get; }= new List>(); public void Link(Node source, Node destination , T weight, string label) { var edge = new Edge() { SourceNode = source, DestinationNode = destination, Weight = weight, Label = label }; Edges.Add(edge); source.Destinations.Add(edge); destination.Sources.Add(edge); } public Node AddNode(T value, string label, int x, int y) { var n = new Node() { Value = value, Label = label, X = x, Y = y }; Nodes.Add(n); return n; } public Matrix TransitionMatrix { get; private set; } public Matrix CurrentStep { get; private set; } public Edge[,] EdgesMatrix { get; private set; } public void Build() { // create matrix var m = new T[Nodes.Count, Nodes.Count]; var e = new Edge[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(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]; } } } }