diff options
-rw-r--r-- | IO/FileWriter.cs | 4 | ||||
-rw-r--r-- | IO/GraphFile.cs | 22 | ||||
-rw-r--r-- | Interfaces/IFileWriter.cs | 2 |
3 files changed, 17 insertions, 11 deletions
diff --git a/IO/FileWriter.cs b/IO/FileWriter.cs index b3a574d..7e10f62 100644 --- a/IO/FileWriter.cs +++ b/IO/FileWriter.cs @@ -13,9 +13,9 @@ namespace Graph.IO FilePath = filePath; } - public void WriteAllText(string content) + public void WriteAllLines(string[] lines) { - File.WriteAllText(FilePath, content); + File.WriteAllLines(FilePath, lines); } } }
\ No newline at end of file diff --git a/IO/GraphFile.cs b/IO/GraphFile.cs index a532681..8562170 100644 --- a/IO/GraphFile.cs +++ b/IO/GraphFile.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using Graph.Interfaces; @@ -43,21 +44,26 @@ namespace Graph.IO public static void Print(Graph<double> graph, IFileWriter writer) { - string outstring = ""; + string[] lines = new string[graph.Vertices.Count]; graph.Vertices.ForEach(vertex => { - outstring += $"\n{vertex.Id}"; + if (vertex.Parent == null) + return; - vertex.Edges.ForEach(edge => { - var vertex = graph.Vertices.Find(v => v.Id == edge.V); - if (vertex != null && vertex.Parent == vertex.Id) - outstring += $" {edge.V} {edge.Weight.ToString("F1")}"; - }); + int parent = vertex.Parent.GetValueOrDefault(); + + lines[parent] ??= $"{parent}"; + lines[vertex.Id] ??= $"{vertex.Id}"; + + lines[parent] += $" {vertex.Id} {vertex.Key.ToString("F1")}"; + lines[vertex.Id] += $" {parent} {vertex.Key.ToString("F1")}"; }); - writer.WriteAllText(outstring.Trim()); + + writer.WriteAllLines(lines); } } } +// vertex.Edges.ForEach(e => line += $" {e.V} {e.Weight.ToString("F1")}"); // var neighbors = graph.Vertices // .Where(u => u.Parent == vertex.Id && vertex.Edges.Exists(e => e.V == u.Id)); diff --git a/Interfaces/IFileWriter.cs b/Interfaces/IFileWriter.cs index 92dd692..f264c2a 100644 --- a/Interfaces/IFileWriter.cs +++ b/Interfaces/IFileWriter.cs @@ -2,6 +2,6 @@ namespace Graph.Interfaces { public interface IFileWriter { - void WriteAllText(string content); + void WriteAllLines(string[] lines); } }
\ No newline at end of file |