public static Decomposition FromLinearDecomposition(LinearDecomposition ld) { Tree tree = new Tree(); tree.Insert(ld.Graph.Vertices); BitSet parent = ld.Graph.Vertices; foreach (int item in ld.Sequence.Take(ld.Sequence.Count - 1)) { tree.InsertWithParent(parent * item, parent); tree.InsertWithParent(parent - item, parent); parent = parent - item; } return new Decomposition(ld.Graph, tree) { // TODO: speed up using this //maxNeighborhoodSize = ld.MaxNeighborhoodSize }; }
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; }
private static void SaveDecomposition(LinearDecomposition decomposition, string folder, string fileName) { Directory.CreateDirectory(folder); using ( TextWriter writer = new StreamWriter( File.Open( folder + "/" + fileName + ".lbdc", FileMode.Create))) { decomposition.Write(writer); writer.Flush(); } }
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 += v; right -= 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, new int[] { l }); BitSet nleaf = new BitSet(0, n, new int[] { 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 += l; right -= l; for (int i = 1; i < sequence.Count; i++) { int v = sequence[i]; left += v; right -= 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; }
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 += l; right -= l; // Base cases BitSet leaf = new BitSet(0, n, new int[] { l }); BitSet nleaf = new BitSet(0, n, new int[] { 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 += v; right -= 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)]; }