/// <summary>
        /// Give a sequence of number, find the longest increasing subsequence
        /// </summary>
        /// <param name="numbers"></param>
        /// <returns></returns>
        public static string GetLongest(int[] numbers)
        {
            //var g = new DirectedGraph(numbers.Length);
            var g = new DirectedGraph(10);
            for (var i = 0; i < numbers.Length; i++)
            {
                for (var j = i + 1; j < numbers.Length; j++)
                {
                    if (numbers[i] < numbers[j])
                    {
                        g.AddEdge(numbers[i], numbers[j], 1);
                    }
                }
            }

            g.ReverseGraph();

            Debug.WriteLine(Dfs.StrongConnectedComponentAlgorithm(g));

            var v = Dfs.GetSource(g);

            g.ReverseGraph();//reverse back
            var stats = Dijkstra.LongestPath(g, v);
            //Debug.WriteLine(stats);
            return string.Empty;
        }
Beispiel #2
0
        public static DfsStats StrongConnectedComponentAlgorithm(DirectedGraph g)
        {
            g.ReverseGraph();//reverse G
            var linearization = GetLinearization(g);

            var ds = new DfsStats(g.V) { Clock = 1, ComponentCount = 0 };

            g.ReverseGraph();//reverse G back
            foreach (var v in linearization)
            {
                if (!ds.Visited[v])
                {
                    ds.ComponentCount++;
                    Explore(g, v, ds);
                }
            }

            return ds;
        }