Beispiel #1
0
        /// <summary>
        /// Topologically sorts nodes in a given graph.  If no <paramref name="nodes"/> are specified all nodes in the graph
        /// are used (<see cref="IReadonlyDirectedGraph.Nodes"/>); if <paramref name="nodes"/> are specified, they must all
        /// belong to the given <paramref name="graph"/>.
        ///
        /// Returns a multi-value dictionary with keys ranging from 0 to max node height.
        /// </summary>
        public static MultiValueDictionary <int, NodeId> TopSort(this IReadonlyDirectedGraph graph, IEnumerable <NodeId> nodes = null)
        {
            nodes = nodes ?? graph.Nodes;
            MultiValueDictionary <int, NodeId> nodesByHeight = new MultiValueDictionary <int, NodeId>();

            foreach (var node in nodes)
            {
                var height = graph.GetNodeHeight(node);
                nodesByHeight.Add(height, node);
            }

            return(nodesByHeight);
        }
Beispiel #2
0
        private void VerifyNodeHeights(IReadonlyDirectedGraph graph, NodeId[] nodes)
        {
            XAssert.AreEqual(0, graph.GetNodeHeight(nodes[7]));
            XAssert.AreEqual(0, graph.GetNodeHeight(nodes[10]));
            XAssert.AreEqual(0, graph.GetNodeHeight(nodes[11]));

            XAssert.AreEqual(1, graph.GetNodeHeight(nodes[8]));
            XAssert.AreEqual(1, graph.GetNodeHeight(nodes[9]));

            XAssert.AreEqual(2, graph.GetNodeHeight(nodes[3]));
            XAssert.AreEqual(2, graph.GetNodeHeight(nodes[4]));
            XAssert.AreEqual(2, graph.GetNodeHeight(nodes[5]));
            XAssert.AreEqual(2, graph.GetNodeHeight(nodes[6]));

            XAssert.AreEqual(3, graph.GetNodeHeight(nodes[0]));
            XAssert.AreEqual(3, graph.GetNodeHeight(nodes[1]));
            XAssert.AreEqual(3, graph.GetNodeHeight(nodes[2]));
        }