Showing posts with label data structure. Show all posts
Showing posts with label data structure. Show all posts

Thursday, 11 April 2024

links for Data Structure

 


1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭: https://lnkd.in/gXQux4zj

2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬: https://lnkd.in/gKja_D5H

3) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐑𝐞𝐜𝐮𝐫𝐬𝐢𝐨𝐧: https://lnkd.in/gQiasy8H

4) 𝐀 𝐆𝐞𝐧𝐞𝐫𝐚𝐥 𝐚𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐭𝐨 𝐁𝐚𝐜𝐤𝐭𝐫𝐚𝐜𝐤𝐢𝐧𝐠 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬: https://lnkd.in/gVkQX5vA

5) 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐒𝐭𝐫𝐢𝐧𝐠 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 𝐏𝐚𝐭𝐭𝐞𝐫𝐧: https://lnkd.in/gkNvEi8j

6) 10-𝐥𝐢𝐧𝐞 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞 𝐭𝐡𝐚𝐭 𝐜𝐚𝐧 𝐬𝐨𝐥𝐯𝐞 𝐦𝐨𝐬𝐭 '𝐬𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠' 𝐩𝐫𝐨𝐛𝐥𝐞𝐦𝐬: https://lnkd.in/giASrwds

7) 𝐒𝐥𝐢𝐝𝐢𝐧𝐠 𝐖𝐢𝐧𝐝𝐨𝐰 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞: https://lnkd.in/gjatQ5pK

8) 𝐓𝐰𝐨 𝐏𝐨𝐢𝐧𝐭𝐞𝐫𝐬 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gBfWgHYe

9) 𝐏𝐨𝐰𝐞𝐫𝐟𝐮𝐥 𝐔𝐥𝐭𝐢𝐦𝐚𝐭𝐞 𝐁𝐢𝐧𝐚𝐫𝐲 𝐒𝐞𝐚𝐫𝐜𝐡 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞: https://lnkd.in/gKEm_qUK

10) 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞 𝐟𝐨𝐫 𝐌𝐨𝐧𝐨𝐭𝐨𝐧𝐢𝐜 𝐒𝐭𝐚𝐜𝐤 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬: https://lnkd.in/gdYahWVN

11) 𝐆𝐫𝐞𝐞𝐝𝐲 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gw8CgMkC

12) 𝐀𝐥𝐥 𝐓𝐲𝐩𝐞𝐬 𝐨𝐟 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬 𝐟𝐨𝐫 𝐁𝐢𝐭𝐬 𝐌𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧𝐬: https://lnkd.in/gXzegWuU

13) 𝐆𝐫𝐚𝐩𝐡 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gKE6w7Jb

14) 𝐃𝐲𝐧𝐚𝐦𝐢𝐜 𝐏𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐢𝐧𝐠 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gbpRU46g

15) 14 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬 𝐭𝐨 𝐀𝐜𝐞 𝐂𝐨𝐝𝐢𝐧𝐠 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬: https://lnkd.in/gMZJVkFf

Repost to help others in your network ♻️

Join 6100+ readers of my free newsletter to master coding and system design using simple explanations and visuals: https://lnkd.in/dXtb8SwU

Friday, 30 April 2021

Count of Smaller Numbers After Self

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


/*https://www.youtube.com/watch?v=buDoT9ESw1c

*You are given an integer array nums and you have to return a new counts array. The counts array has the property

where counts[i] is the number of smaller elements to the right of nums[i].

Example:

Input: [5,2,6,1] Output: [2,1,1,0]

* */

public class CountOfSmallerNumbersAfterSelf {

    public static void main(String[] args) {

        int[] arr  ={7,5,2,6,1};

        Node root = new Node(arr[arr.length-1]);

        List<Integer> resultList = new ArrayList<>();

        resultList.add(0);

        for(int i=arr.length-2;i>=0;i--){

            resultList.add(insertNode(root,arr[i]));

        }

        Collections.reverse(resultList);

        System.out.println(resultList);



    }


    static int insertNode(Node root, int value){

        int result = 0;

        boolean isConnected = false;

        while (!isConnected){

            if(value<=root.value){

                //apend in left

                root.count++;

                if(root.left==null){

                    Node newNode = new Node(value);

                    root.left = newNode;

                    isConnected = true;

                }else{

                    root = root.left;

                }

            }else{

              //  append in right

                result = result+root.count;

                if(root.right==null){

                    Node newNode = new Node(value);

                    root.right = newNode;

                    isConnected = true;

                }else{

                    root = root.right;

                }

            }

        }


        return result;

    }




}


class Node{

    Node left;

    Node right;

    int value;

    int count=1;

    public Node(int value){

        this.value = value;

    }

}


Monday, 13 July 2020

Convert Min Heap into Max Heap

public class ConvertMinHeapToMaxheap {
    public static void main(String[] args) {
        ConvertMinHeapToMaxheap convertMinHeapToMaxheap = new ConvertMinHeapToMaxheap();
        int arr[]  ={3 ,5 ,9 ,6, 8, 20, 10, 12, 18, 9};
        for(int i = arr.length/2-1;i>=0;i--){
            convertMinHeapToMaxheap.heapify(arr,arr.length,i);
        }
        System.out.println(Arrays.toString(arr));
    }
    void heapify(int[] arr, int n, int i){
        int l = 2*i+1;
        int r = 2*i+2;
        int largest = i;
        if(l<n){
            if(arr[l]>arr[largest]){
                largest = l;
            }
        }

        if(r<n){
            if(arr[r]>arr[largest]){
                largest = r;
            }
        }

        if(largest!=i){
            int temp = arr[largest];
            arr[largest] = arr[i];
            arr[i] = temp;
            heapify(arr,n,largest);
        }
    }
}

output: [20, 18, 10, 12, 9, 9, 3, 5, 6, 8]

Sunday, 12 July 2020

Find kth min element in array in java using MinHeap

public class findKthsmallestElement {
    public static void main(String[] args) {
        int[] arr = {4,1,7,2,3,8,10};
        findKthsmallestElement findKthsmallestElement = new findKthsmallestElement();
        findKthsmallestElement.findKth(arr,3);
    }

    void findKth(int[] arr,int kth){
        if(kth<=0 || kth>arr.length){
            System.out.println("no element found");
            return;
        }
        for(int i = (arr.length/2)-1;i>=0;i--){
            heapify(arr,arr.length,i);
        }

        System.out.println(Arrays.toString(arr));
        
        int length = arr.length;


//execute loop k times from end index, move root element to ith element and heapify again to maintain min heap
        for(int i= length-1;i>=length-kth ;i--){
            swap(arr,i,0);
            heapify(arr,i,0);

        }
        System.out.println(+kth+" kth:"+arr[arr.length-kth]);
        System.out.println(Arrays.toString(arr));
        

    }

    void heapify(int[] arr, int n,int i){
        int l = 2*i+1;
        int r = 2*i+2;
        int smallest = i;
        if(l<n){
            if(arr[l]<arr[smallest]){
                smallest = l;
            }
        }
        if(r<n){
            if(arr[r]<arr[smallest]){
                smallest = r;
            }
        }
        if(smallest!=i){

            swap(arr,i,smallest);
            heapify(arr,n,smallest);
        }

    }

    void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

output: 
[1, 2, 7, 4, 3, 8, 10]
3 kth:3
[4, 8, 7, 10, 3, 2, 1]

Friday, 22 May 2020

check Array elements are consecutive


public class CheckArrayIsConsecutive {

  static boolean checkConsecutive(int[] arr){

        int min = Integer.MAX_VALUE;
        for(int i=0;i<arr.length;i++){
            if(min>arr[i]){
                min = arr[i];
            }
        }

        for(int i=0;i<arr.length;i++){
            if(Math.abs(arr[i])-min>=arr.length){ //means array size small than required consecutive element
                return false;
            }
            if(arr[Math.abs(arr[i])-min]<0){ //if we want to store negative at index there already a negative value
                return false;
            }

            arr[Math.abs(arr[i])-min] = -   arr[Math.abs(arr[i])-min];

        }


        return true;
    }
    public static void main(String[] args) {
        int[] arr = {77,78,76,75,72,73,74};
        boolean bool = checkConsecutive(arr);
        System.out.println(bool);
    }
}


output: true


if input below then output false:
 int[] arr = {77,78,76,75,72,73,79};

ref: https://github.com/mission-peace/interview/blob/master/src/com/interview/array/CheckIfArrayElementsAreConsecutive.java

Friday, 1 May 2020

Kruskal Minimum spinning tree

Kruskal MST is find a tree with minimum weight and connect all the vertex of graph.

A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given graph.

Steps:
1.Get all edges of graph
2.sort all edges by weight in ascending order
3.pick all edges in loop one by one
  a.get vertex of edge
  b.find root of both vertex using disjoint set algo
  c.if both vertex root is same then continue otherwise
  d.add into result list and do union of both vertex using disjoint set.


public class KruskalMST {

   public static class EdgeComparator implements Comparator<Graph<Integer>.Edge<Integer>> {

       @Override
       public int compare(Graph<Integer>.Edge<Integer> t1, Graph<Integer>.Edge<Integer> t2) {
           return t2.weight >= t1.weight ? -1 : 0;
       }
   }
     public List<Graph<Integer>.Edge<Integer>> mst(List<Graph<Integer>.Edge<Integer>> list, Collection<Graph<Integer>.Vertex<Integer>> vertexList){
           Collections.sort(list,new EdgeComparator());
           DisjointSet disjointSet = new DisjointSet();
           for(Graph<Integer>.Vertex<Integer> vertex: vertexList){
               disjointSet.make((int)vertex.id);
           }
           List<Graph<Integer>.Edge<Integer>> result = new ArrayList<>();
           for(Graph<Integer>.Edge<Integer> edge: list){
             int root1 = disjointSet.findSet((int)edge.v1.id);
             int root2 = disjointSet.findSet((int)edge.v2.id);
             if(root1==root2){
                 continue;
             }
             else{
                 result.add(edge);
                 disjointSet.union((int)edge.v1.id,(int)edge.v2.id);
             }
           }
           return result;
       }

    public static void main(String[] args) {
        Graph<Integer> graph = new Graph<Integer>(false);
        graph.addEdge(1, 2, 4);
        graph.addEdge(1, 3, 1);
        graph.addEdge(2, 5, 1);
        graph.addEdge(2, 6, 3);
        graph.addEdge(2, 4, 2);
        graph.addEdge(6, 5, 2);
        graph.addEdge(6, 4, 3);
        graph.addEdge(4, 7, 2);
        graph.addEdge(3, 4, 5);
        graph.addEdge(3, 7, 8);
        System.out.println(graph);

        List<Graph<Integer>.Edge<Integer>> list = graph.getAllEdges();

        KruskalMST kruskalMST = new KruskalMST();
        list = kruskalMST.mst(list,graph.getAllVertex());


         for(Graph<Integer>.Edge<Integer> e: list){
            System.out.print("vertex:"+e.getV1().id);
            System.out.println(" vertex: "+e.getV2().id);
            System.out.println("weight:"+e.getWeight());
            System.out.println("*******");
        }
    }
}


output:
vertex:2 vertex: 5
weight:1
*******
vertex:1 vertex: 3
weight:1
*******
vertex:4 vertex: 7
weight:2
*******
vertex:6 vertex: 5
weight:2
*******
vertex:2 vertex: 4
weight:2
*******
vertex:1 vertex: 2
weight:4
*******


Graph Class:

public class Graph<T> {
    private List<Edge<T>> allEdges;
    private Map<Long,Vertex<T>> allVertex;
    private boolean isDirected;

    Graph(boolean isDirected){
        this.isDirected = isDirected;
        allEdges = new ArrayList<>();
        allVertex = new HashMap<>();
    }

    void addEdge(long v1, long v2, int weight){
        Vertex<T> vertex1 = null;
        if(allVertex.containsKey(v1)){
            vertex1 = allVertex.get(v1);
        }else{
            vertex1 = new Vertex<>( v1);
            allVertex.put(v1,vertex1);
        }
        Vertex<T> vertex2 = null;
        if(allVertex.containsKey(v2)){
            vertex2 = allVertex.get(v2);
        }else{
            vertex2 = new Vertex<>( v2);
            allVertex.put(v2,vertex2);
        }

        Edge edge = new Edge(vertex1,vertex2,isDirected,weight);
        if(!allEdges.contains(edge)){
            allEdges.add(edge);
        }
        vertex1.addAdjecent(edge);
        if(!isDirected){
            vertex2.addAdjecent(edge);
        }

    }
    public List<Edge<T>> getAllEdges(){
        return allEdges;
    }

    public Collection<Vertex<T>> getAllVertex(){
        return allVertex.values();
    }

    class Edge<T>{
        Vertex<T> v1;
        Vertex<T> v2;
        boolean isDirected;
        int weight;
        Edge(Vertex v1, Vertex v2, boolean isDirected, int weight){
            this.v1 = v1;
            this.v2 = v2;
            this.isDirected = isDirected;
            this.weight = weight;
        }

        public Vertex<T> getV1() {
            return v1;
        }

        public void setV1(Vertex<T> v1) {
            this.v1 = v1;
        }

        public Vertex<T> getV2() {
            return v2;
        }

        public void setV2(Vertex<T> v2) {
            this.v2 = v2;
        }

        public boolean isDirected() {
            return isDirected;
        }

        public void setDirected(boolean directed) {
            isDirected = directed;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Edge<?> edge = (Edge<?>) o;
            return Objects.equals(v1, edge.v1) &&
                    Objects.equals(v2, edge.v2);
        }

        @Override
        public int hashCode() {
            return Objects.hash(v1, v2);
        }
    }
    class Vertex<T>{
        long id;
        T data;
        List<Edge<T>> allAdjecent;

        void addAdjecent(Edge<T> edge){
            allAdjecent.add(edge);
        }

        Vertex(long id){
            this.id = id;
            allAdjecent = new ArrayList<>();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Vertex<?> vertex = (Vertex<?>) o;
            return id == vertex.id;
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }
}

DisjointSet :

public class DisjointSet {

    Map<Integer,Node> map = new HashMap<>();
    class Node{
        int rank;
        int data;
        Node parent;
    }

    Node make(int val){
        Node node = new Node();
        node.data = val;
        node.rank = 0;
        node.parent = node;
        map.put(val,node);
        return node;
    }
    boolean union(int n1, int n2){
        Node node1 = map.get(n1);
        Node node2 = map.get(n2);

        Node parent1 = findSet(node1);
        Node parent2 = findSet(node2);
        if(parent1.data==parent2.data){
            return false;
        }
        if(parent1.rank>=parent2.rank){
            parent1.rank = (parent1.rank==parent2.rank)? parent1.rank+1:parent1.rank;
            parent2.parent = parent1;
        }else {
            parent1.parent = parent2;
        }
        return true;
    }
    Node findSet(Node node){
        Node parent = node.parent;
        if(node==parent){
            return parent;
        }else{
            node.parent = findSet(node.parent);
        }
        return node.parent;
    }

    int findSet(int val){
        return findSet(map.get(val)).data;
    }

    public static void main(String[] args) {
        DisjointSet ds= new DisjointSet();
        ds.make(1);
        ds.make(2);
        ds.make(3);
        ds.make(4);
        ds.make(5);
        ds.make(6);
        ds.make(7);

        ds.union(1, 2);
        ds.union(2, 3);
        ds.union(4, 5);
        ds.union(6, 7);
        ds.union(5, 6);
        ds.union(3, 7);

        System.out.println(ds.findSet(1));
        System.out.println(ds.findSet(2));
        System.out.println(ds.findSet(3));
        System.out.println(ds.findSet(4));
        System.out.println(ds.findSet(5));
        System.out.println(ds.findSet(6));
        System.out.println(ds.findSet(7));

    }
}

output:

Tuesday, 28 April 2020

Disjoint set

A disjoint-set data structure is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. A union-find algorithm is an algorithm that performs two useful operations on such a data structure:


import java.util.HashMap;
import java.util.Map;

public class DisjointSet {

    Map<Integer,Node> map = new HashMap<>();
    class Node{
        int rank;
        int data;
        Node parent;
    }

    Node make(int val){
        Node node = new Node();
        node.data = val;
        node.rank = 0;
        node.parent = node;
        map.put(val,node);
        return node;
    }
    boolean union(int n1, int n2){
        Node node1 = map.get(n1);
        Node node2 = map.get(n2);

        Node parent1 = findSet(node1);
        Node parent2 = findSet(node2);
        if(parent1.data==parent2.data){
            return false;
        }
        if(parent1.rank>=parent2.rank){
            parent1.rank = (parent1.rank==parent2.rank)? parent1.rank+1:parent1.rank;
            parent2.parent = parent1;
        }else {
            parent1.parent = parent2;
        }
        return true;
    }
    Node findSet(Node node){
        Node parent = node.parent;
        if(node==parent){
            return parent;
        }else{
            node.parent = findSet(node.parent);
        }
        return node.parent;
    }

    int findSet(int val){
        return findSet(map.get(val)).data;
    }

    public static void main(String[] args) {
        DisjointSet ds= new DisjointSet();
        ds.make(1);
        ds.make(2);
        ds.make(3);
        ds.make(4);
        ds.make(5);
        ds.make(6);
        ds.make(7);

        ds.union(1, 2);
        ds.union(2, 3);
        ds.union(4, 5);
        ds.union(6, 7);
        ds.union(5, 6);
        ds.union(3, 7);

        System.out.println(ds.findSet(1));
        System.out.println(ds.findSet(2));
        System.out.println(ds.findSet(3));
        System.out.println(ds.findSet(4));
        System.out.println(ds.findSet(5));
        System.out.println(ds.findSet(6));
        System.out.println(ds.findSet(7));

    }

}


Monday, 27 April 2020

TopologicalSort using for Directed Acyclic graph DFS

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.

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

public class TopologicalSortDFS {
    public TopologicalSortDFS() {

    }

    void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
         adj.get(v).add(u);
       // adj.get(u).add(v);
     }

    public static void main(String[] args) {

        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<8;i++){
            adj.add(new ArrayList<>());
        }
        TopologicalSortDFS g = new TopologicalSortDFS();
        g.addEdge(adj,7, 1);
        g.addEdge(adj,7, 0);
        g.addEdge(adj,5, 1);
        g.addEdge(adj,1, 2);
        g.addEdge(adj,1, 6);
        g.addEdge(adj,1, 4);
        g.addEdge(adj,3, 4);
        g.addEdge(adj,3, 0);
        g.addEdge(adj,0,6);
        boolean[] visited = new boolean[8];
        Stack<Integer> stack = new Stack<>();

        for(int i=0;i<8;i++){
            if(!visited[i]){
            g.DFS(i,adj,visited,stack);
        }}
        System.out.println(stack);

    }

    void DFS(int v,ArrayList<ArrayList<Integer>> adj,boolean[] visited, Stack<Integer> stack){
            visited[v]=true;
            System.out.println(v);
            ArrayList<Integer> list = adj.get(v);
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext()){
                Integer i = iterator.next();
                if(!visited[i]){
                   DFS(i,adj,visited,stack);
                }
            }
            stack.push(v);
    }
}

Sunday, 26 April 2020

Bipartite Graph DFS

package com.dsprep.graph;

import java.util.ArrayList;
import java.util.Iterator;

public class BipartiteGraphDFS {
    public BipartiteGraphDFS() {

    }

    void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
         adj.get(v).add(u);
        adj.get(u).add(v);
     }

    public static void main(String[] args) {

        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<10;i++){
            adj.add(new ArrayList<>());
        }
        BipartiteGraphDFS g = new BipartiteGraphDFS();
        g.addEdge(adj,1, 2);
        g.addEdge(adj,2, 3);
        g.addEdge(adj,2, 8);
        g.addEdge(adj,3, 4);
        g.addEdge(adj,4, 6);
        g.addEdge(adj,5, 7);
        g.addEdge(adj,5, 9);
        g.addEdge(adj,8, 9);
      //  g.addEdge(adj,7,9);
        boolean[] visited = new boolean[10];
        boolean[] color = new boolean[10];

        if(g.DFS(1,adj,visited,color)){
            System.out.println("Bipartite");
        }else{
            System.out.println("not bipartite");
        }

    }

    boolean DFS(int v,ArrayList<ArrayList<Integer>> adj,boolean[] visited, boolean[] color){
           visited[v]=true;
            System.out.println(v);
            ArrayList<Integer> list = adj.get(v);
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext()){
                Integer i = iterator.next();
                if(!visited[i]){
                    color[i] = !color[v];
                   if(!DFS(i,adj,visited,color)){
                       return false;
                   }
                }else if(color[i]==color[v]){
                    return false;
                }
        }
            return true;
    }
}

Bipartite Graphs BFS

Bipartite Graphs OR Bigraphs is a graph whose vertices can be divided into two independent groups or sets so that for every edge in the graph, each end of the edge belongs to a separate group. There should not be any edge where both ends belong to the same set.


public class BipartiteGraph {

    public BipartiteGraph() {

    }

    void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
         adj.get(v).add(u);
         adj.get(u).add(v);
     }

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<10;i++){
            adj.add(new ArrayList<>());
        }
        BipartiteGraph g = new BipartiteGraph();
        g.addEdge(adj,1, 2);
        g.addEdge(adj,2, 3);
        g.addEdge(adj,2, 8);
        g.addEdge(adj,3, 4);
        g.addEdge(adj,4, 6);
        g.addEdge(adj,5, 7);
        g.addEdge(adj,5, 9);
        g.addEdge(adj,8, 9);
     //   g.addEdge(adj,2, 4);

       if( g.BFS(1,adj)){
           System.out.println("Bipartite Graphs");
       }else{
           System.out.println("not Bipartite Graphs");
       }

    }

    boolean BFS(int v,ArrayList<ArrayList<Integer>> adj){
        LinkedList<Integer> queue = new LinkedList<>();
        queue.add(v);

        boolean[] visited = new boolean[adj.size()];
        int[] level = new int[adj.size()];
        level[v]=0;
        visited[v]=true;

        while (!queue.isEmpty()){
            v = queue.poll();
            //System.out.println(v);
            ArrayList<Integer> list = adj.get(v);
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext()){
                Integer i = iterator.next();
               // System.out.println("i is:"+i);
                if(!visited[i]){
                    level[i]=level[v]+1;
                    visited[i] = true;
                    queue.add(i);
                }
                else if (level[v]==level[i]){
                   return false;
                }
            }

        }
        return true;
    }

}

output:
Bipartite Graphs

if if add 2 to 4 in adjacent list then it will not Bipartite graph

Arrival and Departure time Vertices in DFS

Arrival Time / Pre -Visit Time /Starting Time:
Arrival time for a node is the time when it is first discovered in DFS . It is the time at which the node gets into the recursion stack .
Departure Time / Post-Visit Time/Finishing time :
Departure time for a node is the time when all of its adjacent node in the graph are explored and is ready to backtrack . It is the time at which the node pops out of the recursion stack .
Take an example — When parents bring food to home , they first ensures that every child of them is fed , then they start eating . Same simple concept is applied here .
Program: 

import java.util.ArrayList;
import java.util.Iterator;

public class ArrivalDepartureTime {

    public ArrivalDepartureTime() {

    }

    void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
         adj.get(v).add(u);
     }

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for(int i=0;i<8;i++){
            adj.add(new ArrayList<>());
        }
        ArrivalDepartureTime g = new ArrivalDepartureTime();
        g.addEdge(adj,0, 1);
        g.addEdge(adj,0, 2);
        g.addEdge(adj,2, 3);
        g.addEdge(adj,2, 4);
        g.addEdge(adj,3, 1);
        g.addEdge(adj,3, 5);
        g.addEdge(adj,4, 5);
        g.addEdge(adj,6, 7);
        g.arrivalDepartureVertex(adj,8);

    }

    void arrivalDepartureVertex(ArrayList<ArrayList<Integer>> adj,int V){
        boolean[] visited = new boolean[V];
        int[] arrival = new int[V];
        int[] departure = new int[V];
        int time = -1;
        for(int i=0;i<V;i++){
            if(!visited[i]){
               time = DFS(i,adj,visited,arrival,departure,time);
            }
        }

        for(int i=0;i<arrival.length;i++){
            System.out.println("vertex "+i +" arrival time:"+arrival[i]+" departure time: "+departure[i]);
        }

    }

    int DFS(int v,ArrayList<ArrayList<Integer>> adj,boolean[] visited, int[] arrival, int[] departure, int time){

        visited[v]=true;
        arrival[v] = ++time;

            ArrayList<Integer> list = adj.get(v);
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext()){
                Integer i = iterator.next();
                if(!visited[i]){
                  time =  DFS(i,adj,visited,arrival,departure,time);
                }
            }
            departure[v] = ++time;
            return time;
    }
}

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...