summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-03-28 00:59:03 +0000
committerToby Vincent <tobyv13@gmail.com>2021-03-28 01:00:13 +0000
commitd42df9247474e174da432f0d33b95be98cf9241b (patch)
tree7159837f04d1ebfeb0e1cbc0a9407ca5f561ae30
parent58f0850f0df9e02ef50a147f3ebe1a8711318a35 (diff)
fixed graph printing
-rw-r--r--IO/FileWriter.cs4
-rw-r--r--IO/GraphFile.cs22
-rw-r--r--Interfaces/IFileWriter.cs2
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