public static int Compute(LinearDecomposition decomposition) { Graph graph = decomposition.Graph; int n = graph.Size; List<int> sequence = decomposition.Sequence; BitSet left = new BitSet(0, graph.Size); BitSet right = graph.Vertices; BitSet VG = graph.Vertices; LinearRepresentativeTable cuts = new LinearRepresentativeTable(graph, sequence); LookupTable table = new LookupTable(); // first initialize the very first leaf node int l = sequence[0]; left.Add(l); right.Remove(l); // Base cases BitSet leaf = new BitSet(0, n) { l }; BitSet nleaf = new BitSet(0, n) { graph.OpenNeighborhood(l).First() }; table[new BitSet(0, n), new BitSet(0, n)] = int.MaxValue; table[leaf, new BitSet(0, n)] = 1; table[leaf, nleaf] = 1; table[new BitSet(0, n), nleaf] = 0; for (int i = 1; i < sequence.Count; i++) { int v = sequence[i]; left.Add(v); right.Remove(v); LinearRepresentativeList LRw = cuts[left]; LinearRepresentativeList LRw_ = cuts[right]; LinearRepresentativeList LRa = cuts[left - v]; LinearRepresentativeList LRa_ = cuts[right + v]; LookupTable newTable = new LookupTable(); foreach (BitSet outside in LRw_) { foreach (BitSet inside in LRa) { BitSet nrw_ = graph.Neighborhood(outside) * (left - v); BitSet ra_ = LRa_.GetRepresentative(nrw_); BitSet nra = graph.Neighborhood(inside) * right; BitSet rw = LRw.GetRepresentative(nra); int savedValue = newTable[rw, outside]; int newValue = table[inside, ra_]; BitSet raw_ = inside + outside; BitSet nraw_ = graph.Neighborhood(raw_); if (!nraw_.Contains(v)) newValue = int.MaxValue; int min = Math.Min(savedValue, newValue); newTable[rw, outside] = min; //-------- nrw_ = graph.Neighborhood(outside + v) * (left - v); ra_ = LRa_.GetRepresentative(nrw_); nra = graph.Neighborhood(inside + v) * right; rw = LRw.GetRepresentative(nra); savedValue = newTable[rw, outside]; newValue = table[inside, ra_]; newValue = newValue == int.MaxValue ? newValue : newValue + 1; min = Math.Min(savedValue, newValue); newTable[rw, outside] = min; } } table = newTable; } return table[new BitSet(0, graph.Size), new BitSet(0, graph.Size)]; }
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.Remove(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.Copy(), 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.Add(v); right.Remove(v); table = newTable; } // The largest IS that we have encountered is the one we will return return maxIS; }
public static void Compute(LinearDecomposition decomposition) { Graph graph = decomposition.Graph; List<int> sequence = decomposition.Sequence; int n = graph.Size; BitSet right = graph.Vertices; BitSet left = new BitSet(0, n); LookupTable table = new LookupTable(n); LinearRepresentativeTable cuts = new LinearRepresentativeTable(graph, sequence); table[new BitSet(0, n), 0] = 1; table[new BitSet(0, n), 1] = 0; for (int i = 0; i < sequence.Count; i++) { int v = sequence[i]; left.Add(v); right.Remove(v); LinearRepresentativeList LRw = cuts[left]; LinearRepresentativeList LRa = cuts[left - v]; LookupTable newTable = new LookupTable(n); foreach (BitSet ra in LRa) { BitSet nra = graph.Neighborhood(ra) * right; BitSet rw = LRw.GetRepresentative(nra); int maxValue = int.MinValue; int limit = (left - v).Count; for (int k = 0; k <= limit; k++) if (table[ra, k] > 0) maxValue = Math.Max(maxValue, k); for (int j = 0; j <= maxValue; j++) { newTable[rw, j] = newTable[rw, j] + table[ra, j]; } //------------ // ra + {v} is not a valid independent set if (LRa.GetNeighborhood(ra).Contains(v)) continue; //------------ // add {v} to the independent set BitSet nrav = graph.Neighborhood(ra + v) * right; BitSet rwv = LRw.GetRepresentative(nrav); for (int j = 0; j <= maxValue; j++) { newTable[rwv, j + 1] = newTable[rwv, j + 1] + table[ra, j]; } } table = newTable; } return; }
public static void Compute(LinearDecomposition decomposition) { Graph graph = decomposition.Graph; List<int> sequence = decomposition.Sequence; int n = graph.Size; BitSet right = graph.Vertices; BitSet left = new BitSet(0, n); LookupTable table = new LookupTable(n); LinearRepresentativeTable cuts = new LinearRepresentativeTable(graph, sequence); int l = sequence[0]; BitSet leaf = new BitSet(0, n) { l }; BitSet nleaf = new BitSet(0, n) { graph.OpenNeighborhood(l).First() }; table[leaf, new BitSet(0, n), 1] = 1; table[leaf, nleaf, 1] = 1; table[new BitSet(0, n), nleaf, 0] = 1; left.Add(l); right.Remove(l); for (int i = 1; i < sequence.Count; i++) { int v = sequence[i]; left.Add(v); right.Remove(v); LinearRepresentativeList LRw = cuts[left]; LinearRepresentativeList LRw_ = cuts[right]; LinearRepresentativeList LRa = cuts[left - v]; LinearRepresentativeList LRa_ = cuts[right + v]; LookupTable newTable = new LookupTable(n); foreach (BitSet outside in LRw_) { foreach (BitSet inside in LRa) { BitSet nrw_ = graph.Neighborhood(outside) * (left - v); BitSet ra_ = LRa_.GetRepresentative(nrw_); BitSet nra = graph.Neighborhood(inside) * right; BitSet rw = LRw.GetRepresentative(nra); BitSet ra = inside; BitSet rw_ = outside; BitSet raw_ = inside + outside; BitSet nraw_ = graph.Neighborhood(raw_); if (nraw_.Contains(v)) // this means rb_ exists ==> multiplier is equal to 1 { for (int ka = 0; ka < n; ka++) { newTable[rw, rw_, ka] = newTable[rw, rw_, ka] + table[ra, ra_, ka]; } } //-------- nrw_ = graph.Neighborhood(outside + v) * (left - v); ra_ = LRa_.GetRepresentative(nrw_); nra = graph.Neighborhood(inside + v) * right; rw = LRw.GetRepresentative(nra); for (int ka = 0; ka < n; ka++) { newTable[rw, rw_, ka + 1] = newTable[rw, rw_, ka + 1] + table[ra, ra_, ka]; } } } table = newTable; } return; }