private static GraphMatrix GetMatGraph1(GraphMatrix graph = null) { var matGraph = new GraphMatrix(8); Vertex v0 = new Vertex(0, "A"); Vertex v1 = new Vertex(1, "B"); Vertex v2 = new Vertex(2, "C"); Vertex v3 = new Vertex(3, "D"); Vertex v4 = new Vertex(4, "E"); Vertex v5 = new Vertex(5, "F"); Vertex v6 = new Vertex(6, "G"); Vertex v7 = new Vertex(7, "H"); Vertex[] vertices = new Vertex[] { v0, v1, v2, v3, v4, v5, v6, v7 }; matGraph.AddVertices(vertices); matGraph.AddEdge(v0, v1, 20); matGraph.AddEdge(v0, v3, 80); matGraph.AddEdge(v0, v6, 90); matGraph.AddEdge(v1, v5, 10); matGraph.AddEdge(v2, v3, 10); matGraph.AddEdge(v2, v5, 50); matGraph.AddEdge(v2, v7, 20); matGraph.AddEdge(v3, v2, 10); matGraph.AddEdge(v3, v6, 20); matGraph.AddEdge(v4, v1, 50); matGraph.AddEdge(v4, v6, 30); matGraph.AddEdge(v5, v2, 10); matGraph.AddEdge(v5, v3, 40); matGraph.AddEdge(v6, v0, 20); return(matGraph); }
public static int[,] FindPaths(GraphMatrix graphMatrix, int pathLength, bool shortest = true) { var shimbelMatrix = new int[ graphMatrix.GetNumberOfVertices(), graphMatrix.GetNumberOfVertices()]; var multiplyMatrix = new int[ graphMatrix.GetNumberOfVertices(), graphMatrix.GetNumberOfVertices()]; Array.Copy( graphMatrix.GetWeightMatrix(), shimbelMatrix, graphMatrix.GetWeightMatrix().Length); Array.Copy( graphMatrix.GetWeightMatrix(), multiplyMatrix, graphMatrix.GetWeightMatrix().Length); for (int i = 0; i < pathLength - 1; ++i) { shimbelMatrix = ShimbelMatrixMultiply(shimbelMatrix, multiplyMatrix, shortest); } return(shimbelMatrix); }
public void OnDecodePruferCall(List <int> code) { var graphMatrix = MinimumSpanningTree.DecodePrufer(code); if (_graphMatrix == null) { _graphMatrix = new GraphMatrix(code.Count + 2, 1, -10, 10); _graphMatrix.SetAdjacencyMatrix(graphMatrix); _graphMatrix.SetWeightMatrix(graphMatrix); _graphMatrix.SetSstMatrix(graphMatrix); _graphGenerated = false; _sstGenerated = true; _flowNetworkGenerated = false; } else { _graphMatrix.SetSstMatrix(graphMatrix); } string pruferCode = String.Join(" ", code); _view.AppendToLog($"{_logEntryNumber++}: Prufer's Decode:" + Environment.NewLine + "Matrix: " + Environment.NewLine + MatrixPrinter.GetMatrix(graphMatrix) + $"Code: {pruferCode}" + Environment.NewLine + Environment.NewLine); _graphMatrix.OutputToFileSst(); UpdateViewGraphImage(); }
public static List <int> FindEulerianCycle(GraphMatrix g) { var cycle = new List <int>(); var graph = g.GetCopy(); var stack = new Stack <int>(); stack.Push(0); while (stack.Count != 0) { int v = stack.Peek(); for (int i = 0; i < graph.GetNumberOfVertices(); ++i) { if (graph.HasEdge(v, i)) { stack.Push(i); graph.DeleteEdge(v, i); break; } } if (v == stack.Peek()) { stack.Pop(); cycle.Add(v); } } return(cycle); }
public static GraphMatrix MakeHamiltonian(GraphMatrix g) { var graph = g.GetCopy(); graph.MakeUndirected(); if (graph.GetNumberOfVertices() > 2) { int vertices = graph.GetNumberOfVertices(); for (int i = 0; i < vertices; ++i) { int j = 0; while (graph.GetDegree(i) < vertices / 2 + vertices % 2) { if (i != j && !graph.HasEdge(i, j)) { graph.AddEdge(i, j); } j++; } } } return(graph); }
private static void SingleOddEliminate(GraphMatrix graph) { var oddVertices = graph.GetOddVertices(); int odd = -1; foreach (int v in oddVertices) { if (graph.GetDegree(v) > 1) { odd = v; break; } } if (odd == -1) { return; } for (int i = 0; i < graph.GetNumberOfVertices(); ++i) { if (graph.HasEdge(odd, i) && graph.GetDegree(i) != 1) { graph.DeleteEdge(odd, i); break; } } }
private static void ConnectedOddEliminate(GraphMatrix graph) { var oddVertices = graph.GetOddVertices(); var used = new bool[graph.GetNumberOfVertices()]; for (int i = 0; i < oddVertices.Count; ++i) { int first = oddVertices[i]; for (int j = 0; j < oddVertices.Count; ++j) { int second = oddVertices[j]; if (!used[first] && !used[second] && i != j) { for (int k = 0; k < graph.GetNumberOfVertices(); ++k) { if (k == first || k == second || graph.GetDegree(k) % 2 != 0) { continue; } if (!graph.HasEdge(first, k) && !graph.HasEdge(second, k)) { graph.AddEdge(first, k); graph.AddEdge(second, k); used[first] = true; used[second] = true; } } } } } }
private void CreateNew(object sender, RoutedEventArgs e) { GraphMatrix newGraph = new GraphMatrix(1); Graph.Set(newGraph); Graph.OnChange(); GraphRenderer.Displayer = new CircleDisplayer(); }
/// <summary> /// Tworzenie macierzy odleglosci pomiedzy wszystkimi parami wierzcholkow w grafie spojnym /// </summary> /// <param name="from"></param> Graf, w ktorym liczymy odleglosci miedzy wszystkimi parami wierzcholkow /// <returns></returns> Macierz odleglosci miedzy wszystkimi parami wierzcholkow public static int[,] distancesMatrix(GraphMatrix from) { GraphMatrix graph = CreateBiggestCoherent(from); int nodes = graph.NodesNr; int[,] distances = new int[nodes, nodes]; List <int> path = new List <int>(); int total_dist = 0; int dist = 0; for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { distances[i, j] = 0; } } for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { if (i == j) { distances[i, j] = 0; } else if (distances[i, j] != 0) { continue; } else { path = PathFinding.Dijkstra(graph, i, j); if (path.Count == 0) { continue; } if (path.Count == 1) { distances[i, j] = distances[j, i] = graph.getWeight(i, path[0]); } else { for (int k = 0; k < path.Count - 1; ++k) { dist += graph.getWeight(path[k], path[k + 1]); } total_dist = dist + graph.getWeight(i, path[0]); distances[i, j] = distances[j, i] = total_dist; total_dist = dist = 0; path.Clear(); } } } } return(distances); }
private static void PrintPath(GraphMatrix g, int[] Dist, int[] Parent, int src) { for (int i = 0; i < g.Vertices.Length; i++) { Console.Write($"shortest path from {src} to {i} is {Dist[i]} "); int p = Parent[i]; PrintHelper(i, p, Parent, g); Console.WriteLine(); } }
public GraphPresenter(IGraphView view, int maxVertices = 25) { if (maxVertices < 2) { throw new ArgumentOutOfRangeException(); } _view = view; _reservedGraph = null; MaxVertices = maxVertices; }
public void ErdosGenerator() { for (int i = 1; i < 100; ++i) { for (int j = 1; j < i; ++j) { GraphMatrix graph = GraphGenerator.generatorGER(i, j); Assert.IsTrue(graph.ConnectionCount == j, string.Format("Nodes : {0}, Connections : {1}, Have : {2}", i, j, graph.ConnectionCount)); } } }
public void SimpleGraphicalTest() { Random rand = new Random(); for (int i = 0; i < 1000; ++i) { GraphMatrix graph = GraphGenerator.generatorGnp(rand.Next(10, 100), 0.5); Assert.IsTrue(Misc.Exists(graph.GetDegreeSequence())); } }
private static void PrintHelper(int vertex, int p, int[] parent, GraphMatrix g) { if (p == -1) { return; } Console.Write($"{p} -> {vertex}({g.AdjMatrix[p, vertex]})"); PrintHelper(p, parent[p], parent, g); }
public static GraphMatrix generatorGER(int nodes, int branches) { if ((nodes < 1) || branches > ((nodes * nodes - nodes) / 2)) { Console.WriteLine(nodes + " " + branches); Console.Read(); throw new Exception("Incorrect parameters"); } GraphMatrix w = new GraphMatrix(nodes); int max = (nodes * nodes - nodes) / 2; int[] ar = new int[branches]; Random r = new Random(); for (int i = 0; i < branches; i++) //losowanie wezlow { while (true) //sprawdzanie czy sie nie powtarzaja { ar[i] = r.Next(max); bool q = false; for (int k = 0; k < i; k++) { if (ar[k] == ar[i]) { q = true; } } if (!q) { break; } } } int counter = 0; for (int i = 1; i < nodes; i++)//wpisywanie wylosowanych liczb { for (int j = 0; j < i; j++) { for (int k = 0; k < branches; k++) { if (ar[k] == counter) { w.MakeConnection(i, j); } } counter++; } } return(w); }
public static List <List <int> > FindHamiltonianCycles(GraphMatrix g) { var hamiltonianPaths = new List <List <int> >(); foreach (int vertex in g.GetAdjacentVertices(0)) { var paths = GeneratePermutations(g.GetNumberOfVertices(), 0, vertex); foreach (var path in paths) { if (!g.HasEdge(0, path[0]) || !g.HasEdge(path[path.Count - 1], vertex)) { continue; } bool hamiltonianPath = true; for (int i = 0; i < path.Count - 1; ++i) { if (!g.HasEdge(path[i], path[i + 1])) { hamiltonianPath = false; break; } } if (hamiltonianPath) { path.Insert(0, 0); path.Insert(path.Count, vertex); path.Insert(path.Count, 0); hamiltonianPaths.Add(path); } } } /* * var builder = new StringBuilder(); * for (int i = 0; i < hamiltonianPaths.Count; ++i) * { * builder.AppendLine( * String.Join( * " -- ", * hamiltonianPaths[i].Select(x => x += 1).ToArray())); * } * * Console.Out.WriteLine(builder.ToString()); */ return(hamiltonianPaths); }
static bool por(GraphMatrix a, GraphMatrix b) { for (int i = 1; i < b.NodesNr; i++)//wpisywanie wylosowanych liczb { for (int j = 1; j < i; j++) { if (a.GetConnection(i, j) != b.GetConnection(i, j)) { return(false); } } } return(5 < 3); }
private static int FindMin(GraphMatrix g, int[] Keys) { int min = Int32.MaxValue; int index = -1; for (int i = 0; i < Keys.Length; i++) { if (g.Vertices[i].IsVisited == false && Keys[i] < min) { min = Keys[i]; index = i; } } return(index); }
public void BuildDistanceMap() { DistanceMatrix = (int[, ])GraphMatrix.Clone(); for (int i = 0; i < Length; i++) { for (int j = 0; j < Length; j++) { for (int k = 0; k < Length; k++) { //if (DistanceMatrix[j, i] < INF && DistanceMatrix[i, k] < INF) DistanceMatrix[j, k] = Math.Min(DistanceMatrix[j, k], DistanceMatrix[j, i] + DistanceMatrix[i, k]); } } } }
private static int FindMin(GraphMatrix g, int[] Dist) { int index = -1; int min = Int32.MaxValue; for (int v = 0; v < g.Vertices.Length; v++) { if (g.Vertices[v].IsVisited == false && Dist[v] < min) { min = Dist[v]; index = v; } } return(index); }
private GraphMatrix Generate() { int nodesCount; double propability; nodesCount = int.Parse(NodeCount.Text); propability = double.Parse(Propability.Text) / 100.0; GraphMatrix graph = GraphGenerator.generatorGnp(nodesCount, propability); //graph.generatorGER(nodesCount, connectionCount); return(graph); }
public void TestIncIO() { createAppdataFolder(); var file = Path.Combine(AppDataDirectory, "tests\\test.inc"); Random rand = new Random(); for (int i = 0; i < 25; ++i) { GraphMatrix matrix = GraphGenerator.generatorGnp(10 + rand.Next(100), 0.5); GraphMatrixInc inc = Converter.ConvertToMatrixInc(matrix); GraphLoad.SaveMatrixInc(inc, file); GraphMatrixInc second = GraphLoad.LoadMatrixInc(file); Assert.IsTrue(inc.Equals(second)); } }
public void TestListIO() { createAppdataFolder(); var file = Path.Combine(AppDataDirectory, "tests\\test.list"); Random rand = new Random(); for (int i = 0; i < 25; ++i) { GraphMatrix matrix = GraphGenerator.generatorGnp(2 + rand.Next(i), 0.5); GraphList list = Converter.ConvertToList(matrix); GraphLoad.SaveList(list, file); GraphList second = GraphLoad.LoadList(file); Assert.IsTrue(list.Equals(second)); } }
private GraphMatrix Generate() { int nodesCount, connectionCount; nodesCount = Int32.Parse(NodeCount.Text); connectionCount = Int32.Parse(ConnectionCount.Text); GraphMatrix graph = GraphGenerator.generatorGER(nodesCount, connectionCount); //graph.generatorGER(nodesCount, connectionCount); return(graph); }
public TestRun() { /*Graphmatrix*/ graph = GraphMatrix <char, int, double> .GraphFactory(); /*GraphList*/ //graph=GraphList<char, int, double>.GraphFactory(); graph.Insert('a'); graph.Insert('b'); graph.Insert('c'); graph.Insert('d'); graph.Insert(1, 0, 1, 2.0); graph.Insert(1, 1, 2, 1.0); graph.Insert(1, 2, 3, 3.0); graph.Insert(1, 1, 3, 1.5); }
public void TestMatrixIO() { createAppdataFolder(); var file = Path.Combine(AppDataDirectory, "tests\\test.matrix"); Random rand = new Random(); for (int i = 0; i < 25; ++i) { GraphMatrix matrix = GraphGenerator.generatorGnp(1000 + rand.Next(1000), 0.5); GraphLoad.SaveMatrix(matrix, file); GraphMatrix second = GraphLoad.LoadMatrix(file); Assert.IsTrue(matrix.Equals(second)); } }
public static GraphMatrix ConvertToMatrix(GraphMatrixInc from) { GraphMatrix x = new GraphMatrix(from.NodesNr); for (int i = 0; i < from.NodesNr; i++) { for (int j = 0; j < from.NodesNr; j++) { if (from.GetConnection(i, j)) { x.MakeConnection(i, j); } x.setWeight(i, j, from.getWeight(i, j)); } } return(x); }
/// <summary> /// Generates graph with desired number of nodes. Each node can have connection with another with desired propability /// </summary> /// <param name="nodes">Number of nodes</param> /// <param name="prob">[0-1] Propability of creating edge</param> /// <returns></returns> public static GraphMatrix generatorGnp(int nodes, double prob) { GraphMatrix w = new GraphMatrix(nodes); Random r = new Random(); for (int i = 1; i < w.NodesNr; i++) { for (int j = 0; j < i; j++) { if (r.NextDouble() < prob) { w.MakeConnection(i, j); } } } return(w); }
private GraphMatrix createCopy(GraphMatrix original) { GraphMatrix matrix = new GraphMatrix(original.NodesNr); for (int y = 0; y < original.NodesNr; ++y) { for (int x = 0; x < original.NodesNr; ++x) { if (original.GetConnection(x, y)) { matrix.MakeConnection(x, y); } } } return(matrix); }
public static GraphList ConvertToList(GraphMatrix from) { GraphList q = new GraphList(from.NodesNr); for (int i = 0; i < from.NodesNr; i++) { for (int j = 0; j < i; j++) { if (from.GetConnection(i, j)) { q.MakeConnection(i, j); } q.setWeight(i, j, from.getWeight(i, j)); } } return(q); }