public static GraphMatrixInc ConvertToMatrixInc(GraphList input) { GraphList from = new GraphList(1); from.Set(input); int sumc = 0; for (int i = 0; i < from.NodesNr; i++) { sumc += from.CountElem(i); } sumc = sumc / 2; GraphMatrixInc q = new GraphMatrixInc(from.NodesNr, sumc); int c = 0; for (int i = 0; i < from.NodesNr; i++)//pobiera po kolei elementy, dodaje do matrixinc i usuwa z listy { for (int j = 0; j < from.NodesNr; j++) { if (from.GetConnection(i, j)) { q.MakeConnection(i, j, c); c++; from.RemoveConnection(i, j); } q.setWeight(i, j, input.getWeight(i, j)); } } return(q); }
/// <summary> /// Returns new graph which is randomized /// </summary> /// <param name="x">Nr of tries to find proper connections to change</param> /// <returns></returns> public static GraphMatrix Randomize(GraphMatrix gr, int x = 1000) { Random r = new Random(); GraphMatrixInc temp = Converter.ConvertToMatrixInc(Converter.ConvertToList(gr)); int xxx = 0; do { int[] q = new int[2]; int[] w = new int[2]; for (int i = 0; i < x; i++) { q[0] = q[1] = w[0] = w[1] = -1; int a; int b; int xx = 0; do { a = r.Next(temp.ConnectNr); b = r.Next(temp.ConnectNr); ++xx; } while (a == b && xx < 10000);//find connections to swap for (int j = 0; j < temp.NodesNr; j++) { if (temp.GetConnectionArray(j, a)) { if (q[0] == -1) { q[0] = j; } else { q[1] = j; } } if (temp.GetConnectionArray(j, b)) { if (w[0] == -1) { w[0] = j; } else { w[1] = j; } } } if (temp.GetConnection(q[0], w[1]) || temp.GetConnection(q[1], w[0]) || q[0] == w[1] || q[1] == w[0]) { continue; } temp.ClearConnection(q[0], q[1], a); temp.ClearConnection(w[0], w[1], b); temp.MakeConnection(q[0], w[1], a); temp.MakeConnection(q[1], w[0], b); } } while (Converter.ConvertToMatrix(temp).Equals(gr) && ++xxx < 500); return(Converter.ConvertToMatrix(temp)); }
private static bool IsHamilton(GraphMatrixInc Graph, int currentNode, int endNode, int previousNode, HashSet <int> closedEdges, HashSet <int> visitedNodes) { if (visitedNodes.Count == Graph.NodesNr) { if (currentNode == endNode) { return(true); } else { return(false); //dotarlismy do node'a ktory nie jest ostatni. Nie zaliczylismy sciezki Hamiltona } } if (currentNode == endNode && previousNode != -1) { return(false); //dotarlismy do ostatniego node'a nie odwiedzajac wszystkich node'ow } var edges = Graph.GetEdgesList(); var neighbourEdges = edges .Where(e => e.Contains(currentNode)) .Where(e => closedEdges.Contains(e.EdgeNumber) == false) .Where(e => visitedNodes.Contains(e.Node1) == false || e.Node1 == endNode) .Where(e => visitedNodes.Contains(e.Node2) == false || e.Node2 == endNode) .ToList(); foreach (var edge in neighbourEdges) { closedEdges.Add(edge.EdgeNumber); visitedNodes.Add(currentNode); if (IsHamilton(Graph, edge.getNodeOnOtherSide(currentNode), endNode, currentNode, closedEdges, visitedNodes)) { return(true); } closedEdges.Remove(edge.EdgeNumber); visitedNodes.Remove(currentNode); } return(false); }
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 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> /// Laduje graf w postaci macierzy węzłów i połączęń z podanego pliku /// </summary> /// <param name="path">Sciezka do pliku z roszerzeniem *.matrixinc</param> /// <returns>Graf macierzowy(węzły,połączenia)</returns> public static GraphMatrixInc LoadMatrixInc(string path) { if (!File.Exists(path)) { throw new Exception("File does not exist"); } StreamReader sr = new StreamReader(path); string s = sr.ReadLine(); int y = s.Length; int x = 0; string[] dane = new string[1000]; dane[0] = s; while (dane[x] != null && (x + 1) < 1000) { dane[x + 1] = sr.ReadLine(); x++; } int[,] tab = new int[x, y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { if (dane[i][j] == '1') { tab[i, j] = 1; } else { tab[i, j] = 0; } } } GraphMatrixInc graph = new GraphMatrixInc(x, y, tab); sr.Close(); return(graph); }
//i 2 inne metody do ladowania 2 innych typow grafow public static void SaveMatrixInc(GraphMatrixInc graph, string path) { StreamWriter sw = new StreamWriter(path); int x = graph.NodesNr; int y = graph.ConnectNr; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { if (graph.GetConnectionArray(i, j)) { sw.Write('1'); } else { sw.Write('0'); } } sw.Write('\n'); } sw.Close(); }
public static bool IsHamilton(GraphMatrixInc Graph) { return(IsHamilton(Graph, 0, 0, -1, new HashSet <int>(), new HashSet <int>())); }
public static GraphList ConvertToList(GraphMatrixInc from) { return(Converter.ConvertToList(Converter.ConvertToMatrix(from))); }