コード例 #1
0
        /// <summary>
        /// Performs a depth-first search on the successor relation, updating the pre-order index
        /// and pre-order last index relations of the graph adapter.
        /// </summary>
        /// <param name="a">graph adapter</param>
        /// <param name="nodes">nodes to be considered</param>
        /// <param name="start">start node</param>
        /// <returns>all visited nodes in pre-order</returns>
        public static T[] GetPreOrder <T>(this IGraphAdapter <T> a,
                                          IList <T> nodes, T start)
        {
            a.PreOrderIndex.Reset(nodes, -1);
            T[] result = new T[nodes.Count];
            int index  = 0;

            a.PreOrderDFS(result, start, ref index);
            // if some nodes are unreachable, the last elements of the result
            // array are null. These should be eliminated:
            result = result.Where(x => x != null).ToArray();
            return(result);
        }
コード例 #2
0
        private static void PreOrderDFS <T>(this IGraphAdapter <T> a,
                                            T[] result, T node, ref int index)
        {
            Contract.Requires <ArgumentNullException>(a != null);
            Contract.Requires <ArgumentNullException>(result != null);
            Contract.Requires <ArgumentException>(index >= 0);
            Contract.Requires <ArgumentException>(index < result.Length);

            result[index]         = node;
            a.PreOrderIndex[node] = index++;
            foreach (T child in a.Succs[node])
            {
                if (a.PreOrderIndex[child] < 0)
                {
                    a.PreOrderDFS(result, child, ref index);
                }
            }
            a.PreOrderLast[node] = result[index - 1];
        }