public override DecompositionTree Construct(Graph graph, WidthParameter widthparameter) { DecompositionTree tree = new DecompositionTree(graph, widthparameter); // Create the leaves. DecompositionNode[] nodes = new DecompositionNode[graph.Vertices.Count]; for (int i = 0; i < nodes.Length; i++) { tree.Nodes[i] = nodes[i] = new DecompositionNode(new BitSet(nodes.Length, graph.Vertices[i].Index), i, tree); } int size = nodes.Length; while (size > 1) { // Find the pair of nodes whose combination is of minimal width. double min = double.PositiveInfinity; int first = -1, second = -1; for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { double width = widthparameter.GetWidth(graph, nodes[i].Set | nodes[j].Set); if (width < min) { min = width; first = i; second = j; } } } // Create the parent and connect it to its children. DecompositionNode node = new DecompositionNode(nodes[first].Set | nodes[second].Set, nodes.Length * 2 - size, tree); tree.Attach(node, nodes[first], Branch.Left); tree.Attach(node, nodes[second], Branch.Right); tree.Nodes[node.Index] = node; // Update the active set of nodes. nodes[first] = node; nodes[second] = nodes[size - 1]; size--; } tree.Attach(null, nodes[0], Branch.Left); tree.ComputeWidth(); return(tree); }
public override DecompositionTree Construct(Graph graph, WidthParameter widthparameter) { DecompositionTree tree = new DecompositionTree(graph, widthparameter); // Create the leaves. DecompositionNode[] nodes = new DecompositionNode[graph.Vertices.Count]; for (int i = 0; i < nodes.Length; i++) { tree.Nodes[i] = nodes[i] = new DecompositionNode(new BitSet(nodes.Length, graph.Vertices[i].Index), i, tree); } int size = nodes.Length; while (size > 1) { int first = this.rng.Next(size); int second = this.rng.Next(size - 1); if (second <= first) { second++; } // Create the parent and connect it to its children. DecompositionNode node = new DecompositionNode(nodes[first].Set | nodes[second].Set, nodes.Length * 2 - size, tree); tree.Attach(node, nodes[first], Branch.Left); tree.Attach(node, nodes[second], Branch.Right); tree.Nodes[node.Index] = node; // Update the active set of nodes. nodes[first] = node; nodes[second] = nodes[size - 1]; size--; } tree.Attach(null, nodes[0], Branch.Left); tree.ComputeWidth(); return(tree); }
protected DecompositionNode backtrack(DecompositionTree tree, DecompositionNode[] leaves, double[,] width, BitSet[,] sets, int index, int size, ref int treeindex) { if (size == 1) { DecompositionNode node = new DecompositionNode(leaves[index], tree); node.Index = index; tree.Nodes[index] = node; return(node); } DecompositionNode parent = new DecompositionNode(sets[index, size], treeindex--, tree); tree.Nodes[parent.Index] = parent; int split = -1; double splitwidth = double.PositiveInfinity; for (int i = index + 1; i < index + size; i++) { double max = Math.Max(width[index, i - index], width[i, size - (i - index)]); if (max <= splitwidth) { splitwidth = max; split = i; } } DecompositionNode left = this.backtrack(tree, leaves, width, sets, index, split - index, ref treeindex); DecompositionNode right = this.backtrack(tree, leaves, width, sets, split, size - (split - index), ref treeindex); tree.Attach(parent, left, Branch.Left); tree.Attach(parent, right, Branch.Right); tree.Attach(null, parent, Branch.Left); parent.UpdateWidthProperties(false); return(parent); }
/// <summary> /// Adds the vertex as leaf to the partial tree. /// </summary> /// <param name="tree">The partial tree.</param> /// <param name="vertex">The vertex that will be added.</param> /// <param name="index">The index of the vertex in the tree.</param> protected void add(DecompositionTree tree, Vertex vertex, int index) { // Create the child node. DecompositionNode child = new DecompositionNode(vertex, index, tree); tree.Nodes[index] = child; if (tree.Root == null) { tree.Attach(null, child, Branch.Left); } else { BitSet set = new BitSet(tree.Root.Set); set[vertex.Index] = true; DecompositionNode parent = new DecompositionNode(set, index + 1, tree); tree.Nodes[index + 1] = parent; tree.Attach(parent, tree.Root, Branch.Right); tree.Attach(parent, child, Branch.Left); tree.Attach(null, parent, Branch.Left); parent.UpdateWidthProperties(false); } }
public override DecompositionTree Construct(Graph graph, WidthParameter widthparameter) { int index = 0; DecompositionTree tree = new DecompositionTree(graph, widthparameter); DecompositionNode root = new DecompositionNode(~(new BitSet(tree.VertexCount)), index, tree); tree.Nodes[index++] = root; tree.Attach(null, root, Branch.Left); List <DecompositionNode> candidates = new List <DecompositionNode>(); candidates.Add(root); while (candidates.Count > 0) { // Select a random childless internal node. DecompositionNode parent = candidates[this.rng.Next(candidates.Count)]; candidates.Remove(parent); // Shuffle its set of vertices. int[] indices = parent.Set.ToArray(); indices.Shuffle(this.rng); // Split the set randomly into two non-empty sets. int split = 1 + this.rng.Next(indices.Length - 2); // Create its children. DecompositionNode left = null; if (split == 1) { left = new DecompositionNode(graph.Vertices[indices[0]], index, tree); } else { BitSet leftset = new BitSet(tree.VertexCount); for (int i = 0; i < split; i++) { leftset[indices[i]] = true; } left = new DecompositionNode(leftset, index, tree); candidates.Add(left); } tree.Nodes[index++] = left; tree.Attach(parent, left, Branch.Left); DecompositionNode right = null; if (split == indices.Length - 1) { right = new DecompositionNode(graph.Vertices[indices[indices.Length - 1]], index, tree); } else { BitSet rightset = new BitSet(tree.VertexCount); for (int i = split; i < indices.Length; i++) { rightset[indices[i]] = true; } right = new DecompositionNode(rightset, index, tree); candidates.Add(right); } tree.Nodes[index++] = right; tree.Attach(parent, right, Branch.Right); } tree.ComputeWidth(); return(tree); }