Graph Searching (BFS&DFS) Implementation

Here is the adjacency matrix representation based implementation of Graph with both graph searching techniques . Following graph has been taken as example

Fullscreen capture 1172010 41854 PM

Adjacency matrix representation is below

int array[][] = {{0,1,1,0,0,0},
                     {1,0,0,1,1,0},
                     {1,0,0,0,0,1},
                     {0,1,0,0,0,0},
                     {0,1,0,0,0,0},
                     {0,0,1,0,0,0}};

Complete Code is as follows

package demo.graph;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;

/**
* Adjency Matrix reprentation Implementation
* @author ydevatra
*/
public class Graph {
    private int order;
    private int[][] graph;
    private int rootVertex=0;
    public Graph(Graph g){
        this.order = g.getOrder();
        graph = new int[order][order];
        for(int i =0;i<order;i++){
            for(int j =0; j<order;j++){
                if(g.isArc(i,j))
                    addArc(i, j);
            }
        }
    }

    public Graph(int[][] array){
        this.order = array.length;
        graph = new int[order][order];
        for(int i =0;i<order;i++){
            for(int j =0; j<order;j++){
                if(array[i][j]==1)
                    addArc(i, j);
            }
        }
    }

    public int getOrder(){
        return order;
    }
    public boolean isArc(int i,int j){
        if(graph[i][j]==1)
            return true;
        else
            return false;
    }

    public void addArc(int i, int j){
        graph[i][j]=1;
    }

    public boolean isEdge(int i, int j){
        if(isArc(i,j)&&isArc(j,i))
            return true;
        else
            return false;
    }

    public void addEdge(int i, int j){
        addArc(i, j);
        addArc(j,i);
    }

    /**
     *
     * @param v
     * @return
     */
    public int[] getNeighbours(int v){
        int[] neighbours = new int[order];
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(v<order){
            for(int i=0;i<order;i++){
                if(isArc(v,i))
                    list.add(i);
            }
        }
        //coverting list to int array
        neighbours = new int[list.size()];
        for(int i=0;i<list.size();i++){
            neighbours[i]=list.get(i);
        }
        return neighbours;
    }

    public int[] breadthFirstSearch(){
        int[] vertexOrder = new int[order];
        int count =0;
        Queue q = new java.util.LinkedList<Integer>();
        int vertex = rootVertex;
        q.add(new Integer(vertex));
        vertexOrder[rootVertex]=++count;
        while(!q.isEmpty()){
            vertex = ((Integer)q.poll()).intValue();

            int[] neighbours = getNeighbours(vertex);
            for (int i=0;i<neighbours.length;i++){
                if(vertexOrder[neighbours[i]]==0){
                    q.add(neighbours[i]);
                    vertexOrder[neighbours[i]]=++count;
                }
             }
        }
        return vertexOrder;

    }

    public void depthFirstSearch(int startVertex,int[] isVisited, int[] vertexOrder,CounterClass count){
        int vertex = startVertex;
        isVisited[vertex]=count.incCount1();
       int[] neighbours = getNeighbours(vertex);
        for( int i=0;i<neighbours.length;i++){
            vertex = neighbours[i];
            if(isVisited[vertex]==0){
                depthFirstSearch(neighbours[i],isVisited, vertexOrder,count);
            }
        }
        vertexOrder[startVertex]=count.incCount2();
    }

    public int[] depthFirstSearch(){
        int[] vertexOrder = new int[order];
        int[] isVisited = new int[order];
        CounterClass count = new CounterClass();

        depthFirstSearch(0, isVisited, vertexOrder,count);
        return vertexOrder;
    }

    public static void main(String[] args){
         int array[][] = {{0,1,1,0,0,0},
                        {1,0,0,1,1,0},
                        {1,0,0,0,0,1},
                        {0,1,0,0,0,0},
                        {0,1,0,0,0,0},
                        {0,0,1,0,0,0}};
        Graph g = new Graph(array);
        int bfs[]  = g.breadthFirstSearch();
        int dfs[] = g.depthFirstSearch();
        System.out.println("BFS - ");
        printArray(bfs);
        System.out.println("DFS - ");
        printArray(dfs);
    }

  private class CounterClass{
      int count1;
      int count2;
      public CounterClass(){
            count1=0;
            count2=0;
      }

      public int incCount1(){
          return ++count1;
      }

      public int incCount2(){
          return ++count2;
      }
  }

  private static void printArray(int[] array){
        TreeMap sortedMap = new TreeMap();
        for(int i =0;i<array.length;i++){
            sortedMap.put(array[i], i);
        }
        Iterator keySet = sortedMap.keySet().iterator();
        while(keySet.hasNext()){
             System.out.print(" "+sortedMap.get(keySet.next()));
         }
        for(int i =0 ; i<array.length; i++){
        }
        System.out.println("");
  }

}


Here is the output of the program

BFS -
0 1 2 3 4 5


DFS -
3 4 1 5 2 0

 

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example