Created
March 8, 2022 21:59
-
-
Save stivio00/a712bd889c26f8cf3672b42f2d439a1f to your computer and use it in GitHub Desktop.
C# GraphUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjListStorage : Dictionary<uint, List<uint>> { | |
public static AdjListStorage Random(int nodes, float density = .5f){ | |
AdjListStorage storage = new AdjListStorage(); | |
List<uint> nodeIds = Enumerable.Range(0, nodes - 1).Select(Convert.ToUInt32).ToList(); | |
Random random = new Random(); | |
foreach(var nodeId in nodeIds) { | |
var edges = nodeIds.OrderBy(x => random.Next()).Take((int)(nodes*density)).ToList(); | |
storage.Add(nodeId, edges); | |
} | |
return storage; | |
} | |
public string ToMermaid(bool wrapMardown=true){ | |
StringBuilder sb = new StringBuilder(); | |
if (wrapMardown) sb.AppendLine("```Mermaid"); | |
sb.AppendLine("graph"); | |
foreach(var kvp in this){ | |
foreach(var edge in kvp.Value){ | |
sb.AppendLine($"\t{kvp.Key} --> {edge}"); | |
} | |
} | |
if (wrapMardown) sb.AppendLine("```"); | |
return sb.ToString(); | |
} | |
public int[,] ToAdjMatrix(){ | |
int[,] matrix = new int[Count, Count]; | |
int i = 0; | |
foreach(uint nodeIdSource in Keys){ | |
int j = 0; | |
foreach(uint nodeIdTarget in Keys){ | |
matrix[i, j] = this[nodeIdSource].Contains(nodeIdTarget) ? 1 : 0; | |
j++; | |
} | |
i++; | |
} | |
return matrix; | |
} | |
} | |
class NodeIO { | |
private const uint MAGICKNUMBER = 0x70617267; //GRAP | |
public static Dictionary<uint, List<uint>> ReadFile(string path){ | |
using var stream = File.Open(path, FileMode.Open); | |
byte[] block = new byte[4]; | |
stream.Read(block); | |
uint header = BitConverter.ToUInt32(block); | |
if (header != MAGICKNUMBER ) throw new FormatException(); | |
stream.Read(block); | |
uint nodeCount = BitConverter.ToUInt32(block); | |
Dictionary<uint, List<uint>> adjStorage = new(); | |
for(int node = 0; node < nodeCount; node++) { | |
stream.Read(block); | |
uint nodeId = BitConverter.ToUInt32(block); | |
List<uint> adjList = new(); | |
stream.Read(block); | |
uint edgesCount = BitConverter.ToUInt32(block); | |
for(int edge = 0; edge < edgesCount; edge++) { | |
stream.Read(block); | |
uint adjNodeId = BitConverter.ToUInt32(block); | |
adjList.Add(adjNodeId); | |
} | |
adjStorage.Add(nodeId, adjList); | |
} | |
return adjStorage; | |
} | |
public static void WriteFile(Dictionary<uint,List<uint>> adjStorage, string path){ | |
using var stream = File.Open(path, FileMode.Create); | |
stream.Write(BitConverter.GetBytes(MAGICKNUMBER)); | |
stream.Write(BitConverter.GetBytes(adjStorage.Count())); | |
foreach (var kvp in adjStorage) { | |
stream.Write(BitConverter.GetBytes(kvp.Key)); | |
stream.Write(BitConverter.GetBytes(kvp.Value.Count())); | |
foreach ( var value in kvp.Value){ | |
stream.Write(BitConverter.GetBytes(value)); | |
} | |
} | |
} | |
} | |
static class LatexUtil { | |
public static string ToLatex(int[,] matrix){ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine(@"$$\begin{bmatrix} "); | |
for (int x = 0; x < matrix.GetLength(0); x += 1) { | |
for (int y = 0; y < matrix.GetLength(1); y += 1) { | |
sb.Append(matrix[x,y]); | |
if (y != matrix.GetLength(1)) sb.Append(@"&"); | |
} | |
if (x != matrix.GetLength(0)) sb.Append(@"\\"); | |
} | |
sb.AppendLine(@"\end{bmatrix}$$"); | |
return sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment