Esempio n. 1
0
// ------------------
            public virtual void dfs()            // depth-first search
            {                                    // begin at vertex 0
                vertexList[0].wasVisited = true; // mark it
                displayVertex(0);                // display it
                theStack.push(0);                // push it
                while (!theStack.Empty)          // until stack empty,
                {
// get an unvisited vertex adjacent to stack top
                    int v = getAdjUnvisitedVertex(theStack.peek());
                    if (v == -1) // if no such vertex,
                    {
                        theStack.pop();
                    }
                    else // if it exists,
                    {
                        vertexList[v].wasVisited = true; // mark it
                        displayVertex(v); // display it
                        theStack.push(v); // push it
                    }
                } // end while
// stack is empty, so we're done
                for (int j = 0; j < nVerts; j++) // reset flags
                {
                    vertexList[j].wasVisited = false;
                }
            } // end dfs