Skip to main content

DEPTH FIRST SEARCH IN JAVA

DEPTH FIRST SEARCH IN JAVA



public void dfs()
 {
  
  vertex[0].visited=true;
  displyVertex(0);
  stackobj.push(0);
  
  while(!stackobj.isEmpty())
  {
   int v=adjVertex((Integer) stackobj.peek());
   if(v==-1)
   {
    stackobj.pop();
   }
    else{
     vertex[v].visited=true;
     displyVertex(v);
     stackobj.push(v);
     
    }
   }
  }
  

Comments

.

Popular posts from this blog

Nth Node from End in LinkedList

Nth Node from End in LinkedList 1)  Find length of  linked list. 2) Traverse (length-position+1) element from begin. //nth Node from end public Node nodeFromEnd ( Node head , int position ){ Node temp = head ; int length = 1 ; while ( temp . next != null ) { length ++; temp = temp . next ; } System . out . println ( "length" + length ); temp = head ; int c = 1 ; while ( c !=( length - position + 1 )) { temp = temp . next ; c ++; } return temp ; }