Esempio n. 1
0
        public static void TestNShortPath()
        {
            int          n = 2;
            List <int[]> result;

            int[] aPath;
            //--------------------------------------------------------------edie by SharpKey
            string dictPath = Path.Combine(Environment.CurrentDirectory, "Data") + Path.DirectorySeparatorChar;

            Console.WriteLine("正在初始化字典库,请稍候...");
            //WordSegmentSample sample = new WordSegmentSample(DictPath, 2);
            WordSegment wordSegment = new WordSegment();

            wordSegment.InitWordSegment(dictPath);
            Segment m_Seg = new Segment(wordSegment.m_dictBigram, wordSegment.m_dictCore);//Seg class
            //wordSegment.Segment("", 2);
            ColumnFirstDynamicArray <ChainContent> apCost = m_Seg.TestSegment("始##始这个人的确实在末##末", 0.1, 2);

            Console.WriteLine(apCost.ToString());
            //----------------------------------
            NShortPath.Calculate(apCost, n);
            NShortPath.printResultByIndex();

            //----------------------------------------------------
            // 所有路径
            //----------------------------------------------------
            Console.WriteLine("\r\n\r\n所有路径:");
            for (int i = 0; i < n; i++)
            {
                result = NShortPath.GetPaths(i);
                for (int j = 0; j < result.Count; j++)
                {
                    aPath = result[j];
                    for (int k = 0; k < aPath.Length; k++)
                    {
                        Console.Write("{0}, ", aPath[k]);
                    }

                    Console.WriteLine();
                }
                Console.WriteLine("========================");
            }

            //----------------------------------------------------
            // 最佳路径
            //----------------------------------------------------
            Console.WriteLine("\r\n最佳路径:");
            aPath = NShortPath.GetBestPath();
            for (int k = 0; k < aPath.Length; k++)
            {
                Console.Write("{0}, ", aPath[k]);
            }

            Console.WriteLine();

            //----------------------------------------------------
            // 最多 n 个路径
            //----------------------------------------------------
            Console.WriteLine("\r\n最多 {0} 条路径:", 5);
            result = NShortPath.GetNPaths(5);
            for (int j = 0; j < result.Count; j++)
            {
                aPath = result[j];
                for (int k = 0; k < aPath.Length; k++)
                {
                    Console.Write("{0}, ", aPath[k]);
                }

                Console.WriteLine();
            }
        }
Esempio n. 2
0
        public static void TestNShortPath()
        {
            int          n = 2;
            List <int[]> result;

            int[] aPath;

            ColumnFirstDynamicArray <ChainContent> apCost = new ColumnFirstDynamicArray <ChainContent>();

            apCost.SetElement(0, 1, new ChainContent(1));
            apCost.SetElement(1, 2, new ChainContent(1));
            apCost.SetElement(1, 3, new ChainContent(2));
            apCost.SetElement(2, 3, new ChainContent(1));
            apCost.SetElement(2, 4, new ChainContent(1));
            apCost.SetElement(3, 4, new ChainContent(1));
            apCost.SetElement(4, 5, new ChainContent(1));
            apCost.SetElement(3, 6, new ChainContent(2));
            apCost.SetElement(4, 6, new ChainContent(3));
            apCost.SetElement(5, 6, new ChainContent(1));
            Console.WriteLine(apCost.ToString());

            NShortPath.Calculate(apCost, n);
            NShortPath.printResultByIndex();

            //----------------------------------------------------
            // 所有路径
            //----------------------------------------------------
            Console.WriteLine("\r\n\r\n所有路径:");
            for (int i = 0; i < n; i++)
            {
                result = NShortPath.GetPaths(i);
                for (int j = 0; j < result.Count; j++)
                {
                    aPath = result[j];
                    for (int k = 0; k < aPath.Length; k++)
                    {
                        Console.Write("{0}, ", aPath[k]);
                    }

                    Console.WriteLine();
                }
                Console.WriteLine("========================");
            }

            //----------------------------------------------------
            // 最佳路径
            //----------------------------------------------------
            Console.WriteLine("\r\n最佳路径:");
            aPath = NShortPath.GetBestPath();
            for (int k = 0; k < aPath.Length; k++)
            {
                Console.Write("{0}, ", aPath[k]);
            }

            Console.WriteLine();

            //----------------------------------------------------
            // 最多 n 个路径
            //----------------------------------------------------
            Console.WriteLine("\r\n最多 {0} 条路径:", 5);
            result = NShortPath.GetNPaths(5);
            for (int j = 0; j < result.Count; j++)
            {
                aPath = result[j];
                for (int k = 0; k < aPath.Length; k++)
                {
                    Console.Write("{0}, ", aPath[k]);
                }

                Console.WriteLine();
            }
        }