public static void CreateFromFile(string expr, List <Exception> exceptionsList, SortedList <string, Graph> graphsList, RichTextBox output) { try { int startIndex = expr.IndexOf("\"") + 1; int endIndex = expr.IndexOf("\")"); string path = expr.Substring(startIndex, endIndex - startIndex); string name = expr.Contains(":=") ? expr.Substring(0, expr.IndexOf(":=")) : ""; var graphMatrix = new IntegerSquareMatrix(IntegerMatrix.GetFromFile(path)); if (graphMatrix.Columns == 0) { exceptionsList.Add(new FormatException("Incorrect matrix size in file")); } var graph = new Graph(graphMatrix, name); graphsList.Add(name, graph); output.Text = graph.ToString(); } catch (Exception e) { exceptionsList.Add(e); output.Text += e.Message; } }
public void SaveGraph(string path) { Directory.CreateDirectory(path); IntegerSquareMatrix a = ToMatrix(); string line = ""; for (int i = 0; i < a.Rows; i++) { line = ""; for (int j = 0; j < a.Columns; j++) { line += Convert.ToString(a[i, j]); if (j != a.Columns - 1) { line += " "; } } if (i != a.Rows - 1) { line += "\n"; } System.IO.File.AppendAllText(path + "\\AdjMatrix", line); } }
/// <summary> /// Graph constructor from adjacency matrix. Calls DFS to complete construction. /// </summary> /// <param name="matrix"></param> public Graph(IntegerSquareMatrix matrix, string name = "") { adjNodesList = new List <GraphNode>(matrix.Columns); for (int i = 0; i < matrix.Columns; i++) { ///create List of empty Nodes GraphNode a = new GraphNode(); adjNodesList.Add(a); a.Number = i; } for (int i = 0; i < matrix.Columns; i++) { for (int j = 0; j < matrix.Columns; j++) { if (matrix[i, j] != 0) { this[i].adjList.Add(this[j]); } } } DFS(); Name = name; }