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

Driver program to perform operations in graph

Driver program to perform operations in graph:-



package com.problems.graph;publicclassGraphDriver{publicstaticvoidmain(String[] args){ BFSGraph g=new BFSGraph(); g.addVertex('A'); g.addVertex('B'); g.addVertex('C'); g.addVertex('D'); g.addVertex('E'); g.addVertex('F'); g.addVertex('G'); g.addVertex('H'); g.addEdge(0,1); g.addEdge(1,2); g.addEdge(1,7); g.addEdge(2,3); g.addEdge(2,4); g.addEdge(7,4); g.addEdge(4,5); g.addEdge(4,6); g.bfs();}}

Shortest Path from source to Vertex :- Dijkstra Algorithm

Shortest Path from source to Vertex :- Dijkstra Algorithm:-

Dijkstra Algorithms is an algorithm use to find the shortest path from source vertex to a given vertex.



package Graph;importjava.util.HashMap;abstractpublicclassDirectedGraph{ Vertex[] vertexlist=new Vertex[10]; HashMap<Character,HashMap<Character,Integer>> edgelist=new HashMap<>(); Vertex vertex;//count of vertex and edgestaticint vertexcount=0;int edgecount=0;/* * This function takes a label and insert in the vertex list as well as edge list since it is new vertex it will add * null to its adjoining vertices */intaddVertex(char label){ vertex=new Vertex(label); vertexlist[vertexcount]=vertex; System.out.println(vertexlist[vertexcount].label); edgelist.put(vertex.label,null); vertexcount++;return vertexcount;}intaddEdge(char label,char[] labels,int[] distances){ HashMap<Character,Integer> vertexlist=new HashMap<>();for(int i=0;i<labels.length;i++){ vertexlist.put(labels[i],…