diff --git a/Data_science/Graph/SparseGraph.vb b/Data_science/Graph/SparseGraph.vb index 2edb8b38f..bb43fd360 100644 --- a/Data_science/Graph/SparseGraph.vb +++ b/Data_science/Graph/SparseGraph.vb @@ -1,5 +1,6 @@ Imports System.Runtime.CompilerServices Imports System.Xml.Serialization +Imports Microsoft.VisualBasic.Linq Imports Microsoft.VisualBasic.Math.LinearAlgebra.Matrix Public Class SparseGraph : Implements ISparseGraph @@ -15,6 +16,19 @@ Public Class SparseGraph : Implements ISparseGraph End Set End Property + Public ReadOnly Property Vertex As String() + Get + Return graph _ + .Select(Iterator Function(e) As IEnumerable(Of String) + Yield e.u + Yield e.v + End Function) _ + .IteratesALL _ + .Distinct _ + .ToArray + End Get + End Property + Public NotInheritable Class Edge : Implements IInteraction Public Property u As String Implements IInteraction.source @@ -28,6 +42,11 @@ Public Class SparseGraph : Implements ISparseGraph _v = v End Sub + Sub New(line As IInteraction) + _u = line.source + _v = line.target + End Sub + Public Overrides Function ToString() As String Return $"[{u}, {v}]" End Function @@ -58,6 +77,11 @@ Public Class SparseGraph : Implements ISparseGraph Dim index_u As Dictionary(Of String, IInteraction()) Dim graph As Edge() + Public Overrides Function ToString() As String + Dim vlist = Vertex + Return $"sparse graph of {vlist.Length}x{vlist.Length} vertex and {graph.Length} edges." + End Function + Public Function CreateMatrix(keys As String()) As NumericMatrix Dim rows As New List(Of Double()) @@ -110,4 +134,11 @@ Public Class SparseGraph : Implements ISparseGraph Yield edge Next End Function + + Public Shared Function Copy(g As ISparseGraph) As SparseGraph + Dim edges As Edge() = g.GetGraph.Select(Function(e) New Edge(e)).ToArray + Dim graph As New SparseGraph With {.Edges = edges} + + Return graph + End Function End Class