Skip to main content

Depth First Search Implementation in Graph

Depth First Search Implementation in Graph


//This is a class to define the vertex in a graph

public class Vertex {

  char label;
  boolean visited;
   public Vertex(char label){

package Graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

// this class is abstract because we won't allow you make instance of it , it is of no use until not extended
abstract public class GraphBase {
 //declaring instance of vertex
 Vertex vertex;
 //declaring an array of vertex to hold all the vertex
 static Vertex[] vertexlist=new Vertex[10];
 //declaring edgelist to hold all edges key is vertex value is neighbouring vertex
 static HashMap<Character,ArrayList<Character>> edgelist=new HashMap<>();
 //count of vertex and edge
 static int 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
 int addVertex(char label)
  vertex=new Vertex(label);
  edgelist.put(vertex.label, null);
  return vertexcount;

  * in the same edgelist we add the vertices that are now in connection by simply updating value for a key
 int addEdge(char v1,char[] v2)
  ArrayList<Character> vertextoadd=new ArrayList<>();
  for(int i=0;i<v2.length;i++)
  edgelist.put(v1, vertextoadd);
  return edgecount;
 //A function that returns neighbour vertices for a given vertex
 ArrayList getNeighbours(char vertex)
  return edgelist.get(vertex);
 void listOfVertex()
  System.out.println("Total Vertex:"+ vertexcount);
  //This is how we traverse over a hashmap
  for(Object key:edgelist.keySet())
   System.out.println(key+" " +edgelist.get(key));

package Graph;

import java.util.ArrayList;
import java.util.Stack;

public class GraphSearch extends GraphBase {
 /*Depth First Search 
  * Basically it works on the concept of stack we take vertex from it serach its neighbour and do the same till no vertex left
 *1. Push first element in stack
 *2. Search for its neigbour and add them in stack and pop the vertex
 *3. do same for rest of the vertices until there is no vertex left in a stack
 void searchDFS()
 Stack<Character> stack=new Stack<>();
  ArrayList adjvertex=getNeighbours(stack.peek());
  for(int i=adjvertex.size()-1;i>=0;i--)
   stack.push((Character) adjvertex.get(i));


package Graph;

import java.util.Map.Entry;

public class GraphImpl extends GraphBase {

 public static void main(String[] args)
  GraphImpl graph=new GraphImpl();
  //Adding vertices
  //Adding edges and this is how we initialize inline array
  graph.addEdge('A', new char[]{'B','C'});
  graph.addEdge('B', new char[]{'D','E'});
  graph.addEdge('C',new char[]{'F','G'} );
  graph.addEdge('D',new char[]{'H'} );
  GraphSearch search=new GraphSearch();

For BFS implementation please use below link:-




Popular posts from this blog

5 books that your college doesn’t tell to read seriously for PLACEMENTS

1.Let us C - It can be the best book to pick in your first semester itself. The book is written in a very simple style so you can easily grasp most of the concepts of programming by just working hard on it. It will help you in building basics of programming around c language.

2.HEAD FIRST JAVA-This book you should buy in third semester and invest your whole second year into it by working on all the codes. This book will help you teaching the concepts in a very attractive way. You will never bore while working  through this book.

3.Data Structure Made Easy In Java - As soon you finished studying core java in the 4th semester just pick this book to understand the logic’s of data structure and algorithms. It will help you to go deep inside this subject from which questions are frequently asked in all the interviews. This book carries a great set of problems that will help in developing intellectual knowledge around algorithms.

4.Cracking The Coding Interview – This book you should start pra…

Bubble Sort in JAVA

What is bubble sort?

It is a sorting technique that is based on the comparison.Here we compare adjacent element, if the first element is larger than the second we swap each other. We do the same procedure again and again until array do not sort completely.

5 1 4 2 8

51428here pass is nothing but iterating the loops equal to number of elements in the array but if it already sorted before then we can break the loop anddo existPASS1 case0 1 //check 0 and first element15428 case1 2 //check 1 and 2 element14528 case2 314258 case3 414258 swap istruePASS2 //first pass completed now do second pass case0 114258 case1 212458 case2 312458 case3 412458 swap istrue PASS3 // third pass case0 112458 case1 212458 case2 312458 case3 412458 swap isfalse since swap is false we break from the loop and do not go for fourth and fifth pass

package sorting;publicclassBubbleSort{//function to print arraypublicstaticvoidprint(int[] arr){for(int i=0;i<=arr.length-1;i++) Syst…