Beispiel #1
0
        public static void MinimumDominatingSet(Graph g, int seed)
        {
            Output.WriteLine("[Minimum Dominating Set Output]");

            //MDS takes an undirected graph
            CombinationGenerator cg      = new CombinationGenerator(g.GetVertices().Count, seed);
            bool            setFound     = false;
            List <Vertex>   verts        = new List <Vertex>(g.GetVertices().Count);
            List <Vertex>   neighborhood = new List <Vertex>();
            List <Vertex[]> domSet       = new List <Vertex[]>();
            bool            restricted   = false;
            int             setLen       = 0;

            while (cg.HasNext())
            {
                int[] array = cg.GetNext();
                int[] tally = new int[array.Length];
                array.CopyTo(tally, 0);

                Vertex[]      possibleSet        = new Vertex[cg.CurrentLevel()];
                int           possibleSetCounter = 0;
                List <Vertex> neighbors          = new List <Vertex>();

                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] != 1)
                    {
                        continue;
                    }

                    possibleSet[possibleSetCounter] = g.GetVertices()[i];
                    possibleSetCounter++;

                    neighbors.AddRange(g.GetVertices()[i].GetAllNeighbors());
                }

                foreach (Vertex v in neighbors)
                {
                    int index = g.GetVertices().IndexOf(v);
                    if (index == -1)
                    {
                        continue;
                    }

                    tally[index] = 1;
                }

                bool validSet = true;
                for (int i = 0; i < tally.Length; i++)
                {
                    if (tally[i] != 1)
                    {
                        validSet = false;
                        break;  //we break, because we only want to find one dominating set
                    }
                }

                if (validSet)
                {
                    setFound = true;
                    if (!restricted)
                    {
                        cg.RestrictToCurrentLevel();
                        restricted = true;
                        setLen     = possibleSet.Length;
                    }

                    if (setLen == possibleSet.Length)
                    {
                        domSet.Add(possibleSet);
                        break;
                    }
                }
            }

            //Tally method
            //Initiate tally to be the generated array, (create a copy!)
            //go through the neighborhood of each vertex with value 1
            //for each neighbor, change their value in the tally to 1
            //If tally is all 1s, then you have everything

            if (setFound)
            {
                //print out the found set and find the others
                Output.WriteLine("Found minimum dominating sets:");
                foreach (Vertex[] v in domSet)
                {
                    string separator = "";
                    for (int i = 0; i < v.Length; i++)
                    {
                        Output.Write(separator + v[i].ToString());
                        separator = ",";
                    }
                    Output.WriteLine();
                }
            }

            Output.WriteLine("[End Minimum Dominating Set Output]");
        }