/// <summary>
        /// Compute a RecursiveTree built by using maximal independent sets
        /// </summary>
        /// <param name="inputGraph">The input graph</param>
        /// <param name="timer">A RUNNING! timer to keep track of the total time this computation takes</param>
        /// <param name="maxTimeAllowed">The total time the computation is allowed to take. If this time is exceeded, all remaining nodes are placed in a single line on top of the tree with independent sets computed thus far</param>
        /// <returns>A RecursiveTree built by using maximal independent sets</returns>
        public static RecursiveTree <Node> TreeFromIndependentSets(Node[] inputGraph, Stopwatch timer, double maxTimeAllowed)
        {
            List <Node> graph          = new List <Node>(inputGraph);
            List <Node> independentSet = CalculateMaximal(graph);

            Node[]        oldNodes = new Node[graph.Count];
            List <Node>[] neighboursPreviousLevel = new List <Node> [graph.Count];
            for (int i = 0; i < neighboursPreviousLevel.Length; i++)
            {
                neighboursPreviousLevel[i]    = new List <Node>();
                oldNodes[graph[i].Number - 1] = graph[i];
            }
            RecursiveTree <Node>[] treesFromNodes = new RecursiveTree <Node> [graph.Count];
            List <Node>            newNodes       = graph;
            List <Node>            oldNewNodes;

            do
            {
                oldNewNodes = new List <Node>(newNodes);

                // The independent set contains nodes from the last layer (1 layer down in the tree)
                // Nodes in this specific layer; each layer has its own nodes. These nodes are not in the independent set
                newNodes = new List <Node>(newNodes.Count - independentSet.Count);

                // Dictionary from nodenumber to node in the current layer
                Node[] fromNumber = new Node[graph.Count];

                // Compute the subtrees for the nodes in the independent set and compute the new layer of nodes
                ComputeSubtreesAndNextLayer(oldNewNodes, oldNodes, independentSet, newNodes, fromNumber, treesFromNodes, neighboursPreviousLevel);

                // Compute the new connections for the next layer
                int totalEdges = 0;
                ComputeNewConnections(newNodes, oldNodes, fromNumber, independentSet, neighboursPreviousLevel, ref totalEdges);

                // If the remaining nodes form a clique or we are taking too long, put the remaining nodes in a line and return
                if (newNodes.Count * (newNodes.Count - 1) / 2 == totalEdges || timer.Elapsed.TotalSeconds >= maxTimeAllowed)
                {
                    RecursiveTree <Node> cliqueTreeLeaf = CreateLine(newNodes);
                    foreach (RecursiveTree <Node> tree in treesFromNodes)
                    {
                        if (tree != null && tree.Parent == null)
                        {
                            cliqueTreeLeaf.AddChild(tree);
                            tree.Parent = cliqueTreeLeaf;
                        }
                    }
                    return(cliqueTreeLeaf.Root);
                }

                // Calculate new independent set for next iteration
                independentSet = CalculateMaximal(newNodes);
            } while (newNodes.Count > 0);

            return(treesFromNodes[oldNewNodes[0].Number - 1]);
        }
        /// <summary>
        /// Creates a subtree in the form of a single line from a list of nodes
        /// </summary>
        /// <param name="nodes">The nodes to be in the subtree</param>
        /// <returns>The leaf of the subtree with the nodes in a single line</returns>
        private static RecursiveTree <Node> CreateLine(List <Node> nodes)
        {
            RecursiveTree <Node> leaf  = new RecursiveTree <Node>(nodes[0]);
            RecursiveTree <Node> child = leaf;

            for (int i = 1; i < nodes.Count; i++)
            {
                RecursiveTree <Node> node = new RecursiveTree <Node>(nodes[i]);
                node.AddChild(child);
                child.Parent = node;
                child        = node;
            }
            return(leaf);
        }
Exemple #3
0
        /// <summary>
        /// Creates a subtree in the form of a single line from a list of nodes
        /// </summary>
        /// <param name="nodes">The nodes to be in the subtree</param>
        /// <returns>The root of a subtree with the nodes in a single line</returns>
        private RecursiveTree <Node> CreateLine(List <Node> nodes)
        {
            RecursiveTree <Node> root   = new RecursiveTree <Node>(nodes[0]);
            RecursiveTree <Node> parent = root;

            for (int i = 1; i < nodes.Count; i++)
            {
                RecursiveTree <Node> node = new RecursiveTree <Node>(nodes[i])
                {
                    Parent = parent
                };
                parent.AddChild(node);
                parent = node;
            }
            return(root);
        }
Exemple #4
0
        /// <summary>
        /// Recursive method used for computing the heuristic tree, computes the tree for a subset of nodes
        /// </summary>
        /// <param name="nodes">All nodes in the entire tree</param>
        /// <param name="ancestors">The list of ancestors for the current subtree</param>
        /// <param name="left">The INCLUSIVE index in nodes where the current subtree starts</param>
        /// <param name="right">The EXCLUSIVE index in nodes where the current subtree ends</param>
        /// <param name="timer">A RUNNING! timer to keep track of how long this computation is allowed to take</param>
        /// <param name="maxTimeAllowed">The maximum time this computation is allowed to take</param>
        /// <param name="fast">Whether heuristics should be updated during the computation. Fast means they are not updated</param>
        /// <returns>The resulting heuristic tree for this subset of nodes</returns>
        private RecursiveTree <Node> RecGetHeuristicTree(Node[] nodes, HashSet <Node> ancestors, int left, int right, Stopwatch timer, double maxTimeAllowed, bool fast) // Left inclusive, right exclusive
        {
            // Compute the array for this subset of nodes
            subArray = nodes.Skip(left).Take(right - left);

            // If the time is up, return the nodes in a single line with for this subtree
            if (timer.Elapsed.TotalSeconds >= maxTimeAllowed)
            {
                return(CreateLine(subArray.ToList()));
            }

            // If the new tree has only one node, return this node
            if (left - right == 1)
            {
                return(new RecursiveTree <Node>(nodes[left]));
            }

            // Select a node as the root of this subtree based on the heuristics of the nodes in this subgraph and compute the connected component when this node is removed
            // These components are the subtrees that are children of the selected node
            Node selectedNode            = GetHeuristicNode(subArray, fast);
            RecursiveTree <Node> newTree = new RecursiveTree <Node>(selectedNode);

            ComputeConnectedComponents(nodes, ancestors, selectedNode, left, right);
            Tuple <int, int>[] borders = ComputeNewSubgraphBorders(nodes, left);

            // For each of the new subtrees, do a recursive call to this method to compute it
            for (int i = 0; i < borders.Length; i++)
            {
                RecursiveTree <Node> ChildTree = RecGetHeuristicTree(nodes, ancestors, borders[i].Item1, borders[i].Item2, timer, maxTimeAllowed, fast);
                ChildTree.Parent = newTree;
                newTree.AddChild(ChildTree);
            }

            // Remove the selected node from the ancestors so the possible other subtrees of the parent of this subtree do not accidentaly use it.
            // We do this to avoid having to copy the entire ancestors list for each recursive call
            ancestors.Remove(selectedNode);
            return(newTree);
        }
Exemple #5
0
        /// <summary>
        /// Create a RecursiveTree with all nodes in a single line (each tree has exactly one child, except for the single leaf)
        /// </summary>
        /// <returns>The root of a RecursiveTree in a line</returns>
        public static RecursiveTree <Node> LineRecursiveTree(ref List <RecursiveTree <Node> > allRecTreeNodes, Node[] allNodes)
        {
            allRecTreeNodes = new List <RecursiveTree <Node> >();
            int numberOfNodes = allNodes.Length;

            // Create the root of the line
            RecursiveTree <Node> recTree = new RecursiveTree <Node>(allNodes[0]);

            allRecTreeNodes.Add(recTree);

            // Loop through all other nodes and create trees for them with the correct parent and cildren references
            RecursiveTree <Node> child = recTree;

            for (int i = 1; i < numberOfNodes; i++)
            {
                Node newNode = allNodes[i];
                RecursiveTree <Node> parent = new RecursiveTree <Node>(newNode);
                child.Parent = parent;
                parent.AddChild(child);
                child = parent;
                allRecTreeNodes.Add(parent);
            }
            return(recTree);
        }
Exemple #6
0
 /// <summary>
 /// Operation that adds a child to a node
 /// </summary>
 /// <param name="node">The node to which the child is added</param>
 /// <param name="child">The child to be added</param>
 public AddChildOperation(RecursiveTree <Node> node, RecursiveTree <Node> child)
 {
     this.node  = node;
     this.child = child;
     node.AddChild(child);
 }
Exemple #7
0
        /// <summary>
        /// Recursive method for calculating the best heuristic tree
        /// </summary>
        /// <param name="bestFoundSolution">The optimal solution thus far from this level of the tree, used for early stopping</param>
        /// <param name="nodes">A list of all nodes in this subtree</param>
        /// <param name="ancestors">The list of all ancestors of the current subtree</param>
        /// <param name="checkedSubsets">A dictionary with checked subsets, used for memoization</param>
        /// <returns>The optimal exact tree for the list of input nodes</returns>
        private RecursiveTree <Node> RecGetBestTree(int bestFoundSolution, List <Node> nodes, HashSet <Node> ancestors, Dictionary <string, RecursiveTree <Node> > checkedSubsets)
        {
            // If the currently best found solution is smaller than the list of ancestors here, we cannot possibly improve it. Thus, we return an empty tree
            if (bestFoundSolution < ancestors.Count + 1)
            {
                return(null);
            }

            // Check if we have already computed a subtree for this set of nodes, if so, return that tree
            string asBits = NodeSubsetRepresentation(nodes);

            if (checkedSubsets.ContainsKey(asBits) && checkedSubsets[asBits] != null)
            {
                RecursiveTree <Node> computedTree = new RecursiveTree <Node>(checkedSubsets[asBits]);
                return(computedTree);
            }

            // Sort the nodes on their remaining degree value (descending) and recursively compute the best trees
            HashSet <Node> nodesAsHash = new HashSet <Node>(nodes);

            nodes = nodes.OrderByDescending(n => n.RemainingDegree(nodesAsHash)).ToList();
            RecursiveTree <Node> bestTree = null;

            foreach (Node selectedNode in nodes)
            {
                RecursiveTree <Node> newTree  = new RecursiveTree <Node>(selectedNode);
                HashSet <Node>       beenList = new HashSet <Node>(ancestors)
                {
                    selectedNode
                };
                bool broken = false;

                foreach (Node n in nodes)
                {
                    // Find the connected component this node is in
                    if (beenList.Contains(n))
                    {
                        continue;
                    }
                    List <Node>    connectedNodes = DFS.All(n, (nn) => { return(true); }, beenList);
                    HashSet <Node> newHash        = new HashSet <Node>(ancestors)
                    {
                        selectedNode
                    };

                    // Compute the best possible subtree for this connected component
                    RecursiveTree <Node> ChildTree = RecGetBestTree(bestFoundSolution, connectedNodes, newHash, checkedSubsets);
                    if (ChildTree == null)
                    {
                        // If the resulting tree is null, it is not a viable solution
                        broken = true;
                        break;
                    }
                    ChildTree.Parent = newTree;
                    newTree.AddChild(ChildTree);
                }

                // If we found a viable solution, update the best found solution thus far
                if (!broken)
                {
                    int newDepth = newTree.Depth;
                    if (newDepth + ancestors.Count < bestFoundSolution)
                    {
                        bestFoundSolution = newDepth + ancestors.Count;
                        bestTree          = newTree;
                    }
                }
            }

            // Save the tree in the memoization dictionary and return it
            checkedSubsets[asBits] = bestTree;
            return(bestTree);
        }