Iterative DFS vs Recursive DFS and different elements order

后端 未结 4 1639
时光说笑
时光说笑 2020-11-28 02:38

I have written a recursive DFS algorithm to traverse a graph:

void Graph::DFS(Node n)
{
    std::cout << ReadNode(n) << \" \";

    M         


        
4条回答
  •  抹茶落季
    2020-11-28 02:52

    Below is the sample code (as per @amit answer above) in C# for Adjacency Matrix.

    using System;
    using System.Collections.Generic;
    
    namespace GraphAdjMatrixDemo
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                // 0  1  2  3  4  5  6
                int[,] matrix = {     {0, 1, 1, 0, 0, 0, 0},
                                      {1, 0, 0, 1, 1, 1, 0},
                                      {1, 0, 0, 0, 0, 0, 1},
                                      {0, 1, 0, 0, 0, 0, 1},
                                      {0, 1, 0, 0, 0, 0, 1},
                                      {0, 1, 0, 0, 0, 0 ,0},
                                      {0, 0, 1, 1, 1, 0, 0}  };
    
                bool[] visitMatrix = new bool[matrix.GetLength(0)];
                Program ghDemo = new Program();
    
                for (int lpRCnt = 0; lpRCnt < matrix.GetLength(0); lpRCnt++)
                {
                    for (int lpCCnt = 0; lpCCnt < matrix.GetLength(1); lpCCnt++)
                    {
                        Console.Write(string.Format(" {0}  ", matrix[lpRCnt, lpCCnt]));
                    }
                    Console.WriteLine();
                }
    
                Console.Write("\nDFS Recursive : ");
                ghDemo.DftRecursive(matrix, visitMatrix, 0);
                Console.Write("\nDFS Iterative : ");
                ghDemo.DftIterative(matrix, 0);
    
                Console.Read();
            }
    
            //====================================================================================================================================
    
            public void DftRecursive(int[,] srcMatrix, bool[] visitMatrix, int vertex)
            {
                visitMatrix[vertex] = true;
                Console.Write(vertex + "  ");
    
                for (int neighbour = 0; neighbour < srcMatrix.GetLength(0); neighbour++)
                {
                    if (visitMatrix[neighbour] == false && srcMatrix[vertex, neighbour] == 1)
                    {
                        DftRecursive(srcMatrix, visitMatrix, neighbour);
                    }
                }
            }
    
            public void DftIterative(int[,] srcMatrix, int srcVertex)
            {
                bool[] visited = new bool[srcMatrix.GetLength(0)];
    
                Stack vertexStack = new Stack();
                vertexStack.Push(srcVertex);
    
                while (vertexStack.Count > 0)
                {
                    int vertex = vertexStack.Pop();
    
                    if (visited[vertex])
                        continue;
    
                    Console.Write(vertex + "  ");
                    visited[vertex] = true;
    
                    for (int neighbour = srcMatrix.GetLength(0) - 1; neighbour >= 0; neighbour--)
                    //for (int neighbour = 0; neighbour < srcMatrix.GetLength(0); neighbour++)
                    {
                        if (srcMatrix[vertex, neighbour] == 1 && visited[neighbour] == false)
                        {
                            vertexStack.Push(neighbour);
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题