/// <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)); }
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); }