java code key to the dfs algorithm is being able to find the vertices that are unvisited and adjacent to specified vertex how do you do thisthe adjacency matrix is the key by going to the row for the specified vertex and stepping across the columnsyou can pick out the columns with the column number is the number of an adjacent vertex you can then check whether this vertex is unvisited if soyou've found what you want--the next vertex to visit if no vertices on the row are simultaneously (adjacentand also unvisitedthere are no unvisited vertices adjacent to the specified vertex we put the code for this process in the getadjunvisitedvertex(method/returns an unvisited vertex adjacent to public int getadjunvisitedvertex(int vfor(int = <nvertsj++if(adjmat[ ][ ]== &vertexlist[jwasvisited==falsereturn /return first such vertex return - /no such vertices /end getadjunvisitedvertex(now we're ready for the dfs(method of the graph classwhich actually carries out the depth-first search you can see how this code embodies the three rules listed earlier it loops until the stack is empty within the loopit does four things it examines the vertex at the top of the stackusing peek( it tries to find an unvisited neighbor of this vertex if it doesn' find oneit pops the stack if it finds such vertexit visits that vertex and pushes it onto the stack here' the code for the dfs(methodpublic void dfs(/depth-first search /begin at vertex vertexlist[ wasvisited true/mark it displayvertex( )/display it thestack push( )/push it while!thestack isempty(/until stack empty/get an unvisited vertex adjacent to stack top int getadjunvisitedvertexthestack peek()if( =- /if no such vertexthestack pop()/pop new one