AdvancedAlgorithms.GraphTraversal.DepthFirst C# (CSharp) Method

DepthFirst() private static method

private static DepthFirst ( int graph, int n ) : void
graph int
n int
return void
        private static void DepthFirst(int[][] graph, int n)
        {
            Stack<int> nodes = new Stack<int>();
            nodes.Push(0);
            bool[] visitedNodes = new bool[n];

            bool firstNode = true;
            while (nodes.Count != 0)
            {
                int currentNode = nodes.Pop();

                if (!firstNode)
                    Console.Write(", ");
                Console.Write(currentNode);

                visitedNodes[currentNode] = true;

                for (int i = 0; i < n; i++)
                {
                    int edge = graph[currentNode][i];
                    int reverseEdge = graph[i][currentNode];
                    //check there is an edge between them
                    if (edge != HAS_EDGE && reverseEdge != HAS_EDGE)
                        continue;

                    //check if its visited
                    if (visitedNodes[i])
                        continue;

                    //check if its in queue
                    if (!nodes.Contains(i))
                    {
                        nodes.Push(i);
                    }
                }

                firstNode = false;
            }
        }