Example #1
0
        public static int Compute(LinearDecomposition decomposition)
        {
            Graph graph = decomposition.Graph;
            List<int> sequence = decomposition.Sequence;

            // Left is the set of vertices that have been processed so far.
            BitSet left = new BitSet(0, graph.Size);

            // Right is the set of vertices that we have yet to process, initially set to all vertices in the graph.
            BitSet right = graph.Vertices;

            // The leftneighborset contains ISN elements, that save the size of each independent set in Left, and the corresponding neighborhood in Right that belongs to this IS.
            LookupTable table = new LookupTable();

            // Insert the initial neighborhood and IS of size 0 and an empty neighborhood.
            table.Update(new Isn(new BitSet(0, graph.Size), 0));

            // maxIS saves the largest independent set that we have encountered so far.
            int maxIs = 0;

            // Loop over all vertices in the sequence; we have to process all of them.
            for (int i = 0; i < sequence.Count; i++)
            {
                // Take the next vertex in the sequence.
                int v = sequence[i];

                //Save all updated element in a new neighborset, so that we do not disturb any looping over elements.
                LookupTable newTable = new LookupTable();

                foreach (Isn iset in table)
                {
                    // If a neighborhood Right contains the currently selected element, then we have to remove it from the neighborhood.
                    // The corresponding IS in Left is still valid, but it simply belongs to a smaller neighborhood in Right (hence the removal of v).
                    if (iset.Elements.Contains(v))
                    {
                        iset.Elements -= v;
                        newTable.Update(iset);
                    }
                    // If the neighborhood does not contain v, then there are two cases that have to be handled.
                    else
                    {
                        // Case a: If v is not an element of the largest IS, then the previous IS is still valid.
                        // We have no risk of v being contained in the neighborhood of Right, because if that would be the case we would not be in the else-part of the if-statement.
                        // Thus, we simply add an unmodified copy of j to the new list of neighborhoodsets.
                        newTable.Update(iset);

                        // Case b: If v is an element of the largest IS, then we should include a new entry for this newly created IS.
                        // The size of this IS will increase by one (adding v will cause this).
                        // The neighborhood of this IS is the old neighborhood, plus any vertices in Right that are in the neighborhood of v. Vertex v causes the addition of these vertices.
                        // The largest saved IS might change because of this addition of a new erlement.
                        Isn newIset = new Isn(iset.Elements, iset.Size);
                        newIset.Size++;
                        newIset.Elements = newIset.Elements + (graph.OpenNeighborhood(v) * right);
                        maxIs = Math.Max(maxIs, newIset.Size);
                        newTable.Update(newIset);
                    }
                }

                // Safely update all sets that we are working with
                left += v;
                right -= v;
                table = newTable;

            }
            // The largest IS that we have encountered is the one we will return
            return maxIs;
        }