public static Graph CreateEulerGraph(int nodes, out string finalEulerPath)
        {
            while (true)
            {
                Random rand = new Random();
                int    CounterOfConnections = 0;
                int    n = nodes;
                double b = 0.5;
                //tworze losowy graf o 10 wierzchołkach i stopniu 0.5
                int[,] result = new int[n, n];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        result[i, j] = 0;
                    }
                }
                for (int i = 1; i < n; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if (rand.NextDouble() < b)
                        {
                            result[i, j] = 1;
                            result[j, i] = 1;
                            CounterOfConnections++;
                        }
                    }
                }
                //zmieniam graf aby był eulerowski

                for (int i = 0; i < n - 1; i++)
                {
                    int deg = 0;
                    for (int j = 0; j < n; j++)
                    {
                        if (result[i, j] > 0)
                        {
                            deg++;
                        }
                    }
                    //check if degree is even
                    if (deg % 2 != 0)
                    {
                        int x = rand.Next(n - i - 1) + i + 1;
                        if (result[i, x] > 0)
                        {
                            result[i, x] = 0;
                            result[x, i] = 0;
                            CounterOfConnections--;
                        }
                        else
                        {
                            result[i, x] = 1;
                            result[x, i] = 1;
                            CounterOfConnections++;
                        }
                    }
                }
                if (CounterOfConnections != 0)
                {
                    Graph finalGraph = GraphCreator.CreateFromMatrix(result);
                    finalEulerPath = CreateEulerPath(result, n, CounterOfConnections, finalGraph);
                    return(finalGraph);
                }
            }
        }
        public static string CreateEulerPath(int[,] result, int n, int count, Graph eulersGraph)
        {
            int CounterOfConnections = count;
            //wypisuje mój eulerowski graf
            List <int> countOfNodes = new List <int>();

            for (int i = 0; i < n; i++)
            {
                countOfNodes.Add(0);
                for (int j = 0; j < n; j++)
                {
                    if (result[i, j] == 1)
                    {
                        countOfNodes[i]++;
                    }
                }
                countOfNodes[i] /= 2;
            }
            //stos moich podcyklów
            List <List <int> > StackOfLists = new List <List <int> >();

            //indexy w tablicy sąsiedztwa
            int x_index = 0, y_index = 0;

            //CounterOfList mówi nam ile list mamy na stosie
            int CounterOfLists = 0;

            while (true)
            {
                if (CounterOfConnections == 0)
                {
                    break;
                }
                //znczy to że przeleciałem cały graf już
                StackOfLists.Add(new List <int>());
                //szukam wierzchołka które jeszcze nie jest w żadnym podgrafie
                //jeżeli nie ma jeszcze żadnego podgrafu to szukam pierwszego lepszego
                if (CounterOfLists == 0)
                {
                    for (int i = 0; i < n; i++)
                    {
                        if (countOfNodes[i] != 0)
                        {
                            x_index = i;
                        }
                    }
                }
                else
                {
                    foreach (int element in StackOfLists[CounterOfLists - 1])
                    {
                        if (countOfNodes[element] != 0)
                        {
                            x_index = element;
                        }
                    }
                }
                //szukam ygrekowej składowej
                for (int i = 0; i < n; i++)
                {
                    if (result[x_index, i] == 1)
                    {
                        y_index = i;
                    }
                }
                //kończe szukanie tego wierzchołka
                StackOfLists[CounterOfLists].Add(x_index);
                while (true)
                {
                    bool noChange = true;
                    StackOfLists[CounterOfLists].Add(y_index);
                    countOfNodes[x_index]--;
                    result[x_index, y_index] = 0;
                    result[y_index, x_index] = 0;
                    CounterOfConnections--;
                    //dodało nam wierzchołek do cyklu i teraz sprawdza czy czasem
                    //czasem nie trzeba rzucić nowej listy na stos
                    for (int i = 0; i < n; i++)
                    {
                        if (result[y_index, i] == 1)
                        {
                            x_index  = y_index;
                            y_index  = i;
                            noChange = false;
                            break;
                        }
                    }
                    if (noChange)
                    {
                        break;
                    }
                }
                CounterOfLists++;
            }
            //złoże teraz mój stos w finalną liste
            List <int> finalList = new List <int>();

            finalList = ReturnFinalList(StackOfLists, finalList, 0);
            //tworze z mojej scierzki stringa
            string EulerPath = "";

            for (int i = 0; i < finalList.Count; i++)
            {
                EulerPath = EulerPath + " " + finalList[i];
            }

            return(EulerPath);
        }
Beispiel #3
0
        //Randomizacja grafu
        public static bool RandomizeGraph(Graph oldGraph, int countChanges)
        {
            // Po randomizacji zmienia się najwieksza spójna składowa, dlatego resetuje
            oldGraph.ResetStronglyConnections();

            int connectionsCount = oldGraph.Connections.Count;

            if (connectionsCount < 2)
            {
                return(false);
            }
            //for (int i = 0; i < oldGraph.Nodes.Count; ++i)
            //    oldGraph.Nodes[i].GraphicalStringConnections = 0;
            //for (int i = 0; i < oldGraph.Connections.Count; ++i)
            //{
            //    oldGraph.Nodes[oldGraph.Connections[i].Node1.ID].GraphicalStringConnections++;
            //    oldGraph.Nodes[oldGraph.Connections[i].Node2.ID].GraphicalStringConnections++;
            //}
            //oldGraph.Nodes.Sort((x, y) => x.GraphicalStringConnections.CompareTo(y.GraphicalStringConnections));
            //if (oldGraph.Nodes.FindLastIndex(x => x.GraphicalStringConnections == 1) >= oldGraph.Nodes.Count - 2)
            //{
            //    oldGraph.Nodes.Sort((x, y) => x.ID.CompareTo(y.ID));
            //    return false;
            //}
            //oldGraph.Nodes.Sort((x, y) => x.ID.CompareTo(y.ID));
            for (int i = 0; i < connectionsCount; ++i)
            {
                if (oldGraph.Connections[i].Node1.ID > oldGraph.Connections[i].Node2.ID)
                {
                    Node temp = oldGraph.Connections[i].Node1;
                    oldGraph.Connections[i].Node1 = oldGraph.Connections[i].Node2;
                    oldGraph.Connections[i].Node2 = temp;
                }
            }

            Random     rnd             = new Random();
            Connection emptyConnection = oldGraph.Connections.Find(x => x.Node1.ID == 123123123);
            int        secureCounter   = 1000;

            while (countChanges > 0)
            {
                secureCounter--;
                if (secureCounter == 0)
                {
                    return(false);
                }
                int id1 = rnd.Next(0, connectionsCount);
                int id2 = rnd.Next(0, connectionsCount);
                if (id1 == id2)
                {
                    continue;
                }
                Connection c1 = oldGraph.Connections[id1];
                Connection c2 = oldGraph.Connections[id2];
                if (c1.Node1.ID == c2.Node1.ID || c1.Node1.ID == c2.Node2.ID || c1.Node2.ID == c2.Node1.ID ||
                    c1.Node2.ID == c2.Node2.ID)
                {
                    continue;
                }
                Node a = c1.Node1;
                Node b = c1.Node2;
                Node c = c2.Node1;
                Node d = c2.Node2;

                if (emptyConnection == oldGraph.Connections.Find(x => x.Node1.ID == a.ID && x.Node2.ID == c.ID))
                {
                    if (b.ID > d.ID)
                    {
                        if (emptyConnection == oldGraph.Connections.Find(x => x.Node1.ID == d.ID && x.Node2.ID == b.ID))
                        {
                            c1.Node2 = c;
                            c2.Node1 = d;
                            c2.Node2 = b;
                            countChanges--;
                        }
                    }
                    else
                    {
                        if (emptyConnection == oldGraph.Connections.Find(x => x.Node1.ID == b.ID && x.Node2.ID == d.ID))
                        {
                            c1.Node2 = c;
                            c2.Node1 = b;
                            countChanges--;
                        }
                    }
                }
                else if (emptyConnection == oldGraph.Connections.Find(x => x.Node1.ID == a.ID && x.Node2.ID == d.ID))
                {
                    if (b.ID > c.ID)
                    {
                        if (emptyConnection == oldGraph.Connections.Find(x => x.Node1.ID == c.ID && x.Node2.ID == b.ID))
                        {
                            c1.Node2 = d;
                            c2.Node1 = c;
                            c2.Node2 = b;
                            countChanges--;
                        }
                    }
                    else
                    {
                        if (emptyConnection == oldGraph.Connections.Find(x => x.Node1.ID == b.ID && x.Node2.ID == c.ID))
                        {
                            c1.Node2 = d;
                            c2.Node1 = b;
                            c2.Node2 = c;
                            countChanges--;
                        }
                    }
                }
            }
            return(true);
        }