Esempio n. 1
0
        /// <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));
        }
Esempio n. 2
0
        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);
        }