Esempio n. 1
0
        private string[] Build(int[] stat, int end)
        {
            int           node  = end;
            MyStack <int> stack = new MyStack <int>();

            stack.Push(node);
            int x = stat[node];

            while (x > 0)
            {
                for (int j = 0; j < _matrix.GetUpperBound(0) + 1; j++)
                {
                    if (stat[j] == x - _matrix[node, j])
                    {
                        stack.Push(j);
                        x    = stat[j];
                        node = j;
                        break; // for
                    }
                }
            }
            string[] result = new string[stack.Count];
            int      index  = 0;

            while (stack.Count > 0)
            {
                result[index++] = _nodes[stack.Pop()];
            }
            return(result);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //int[,] graphMatrix; ;
            //GraphMatrixFromFile("graph_matrix.txt", out graphMatrix);
            //PrintGraphMatrix(ref graphMatrix);


            int GRAPH_SIZE = 6;

            int[,] GRAPH =
            {
                { 0, 1, 0, 0, 1, 0 },
                { 1, 0, 1, 0, 0, 1 },
                { 0, 1, 0, 1, 0, 1 },
                { 0, 0, 1, 0, 1, 0 },
                { 1, 0, 0, 1, 0, 1 },
                { 0, 1, 1, 0, 1, 0 }
            };



            #region Задание 2:
            //Написать рекурсивную функцию обхода графа в глубину.

            Console.WriteLine("Обход графа в глубину:\n");

            MyStack <int> stack             = new MyStack <int>();
            int[]         graphTopsActive_1 = new int[GRAPH_SIZE];
            int           step = 0;

            void GraphDepthTraverse(int topNumber)
            {
                graphTopsActive_1[topNumber] = 1;
                for (int relationTopNumber = 0; relationTopNumber < GRAPH_SIZE; relationTopNumber++)
                {
                    if (graphTopsActive_1[relationTopNumber] == 0 && GRAPH[topNumber, relationTopNumber] != 0)
                    {
                        stack.Push(relationTopNumber);
                    }
                }

                Console.WriteLine($"Шаг {++step}:");
                Console.Write("Вершины: ");
                PrintArray(ref graphTopsActive_1);

                Console.Write($"Стэк: ");
                stack.Print();
                Console.WriteLine("");

                if (stack.Length != 0)
                {
                    GraphDepthTraverse(stack.Pop());
                }
            }

            GraphDepthTraverse(0);

            #endregion



            #region Задание 3:
            //Написать функцию обхода графа в ширину.

            Console.WriteLine("Обход графа в ширину:\n");

            int[]       graphTopsActive_2 = new int[GRAPH_SIZE];
            MyDeq <int> deq = new MyDeq <int>();
            step = 0;

            void GraphWideTraverse(int top)
            {
                graphTopsActive_2[top] = 1;
                for (int relationTopNumber = 0; relationTopNumber < GRAPH_SIZE; relationTopNumber++)
                {
                    if (graphTopsActive_2[relationTopNumber] == 0 && GRAPH[top, relationTopNumber] != 0)
                    {
                        deq.PushFront(relationTopNumber);
                    }
                }

                Console.WriteLine($"Шаг {++step}:");
                Console.Write("Вершины: ");
                PrintArray(ref graphTopsActive_2);

                Console.Write($"Очередь: ");
                deq.Print();
                Console.WriteLine("");

                if (deq.Length != 0)
                {
                    GraphWideTraverse(deq.PopBack());
                }
            }

            GraphWideTraverse(0);

            #endregion



            Console.ReadKey();
        }