title: Breadth First Search (BFS) And Depth First Search (DFS) Code - Need suggestion on how can I improve it further
body: <p>I have written BFS and DFS code purely with my understanding as below. Which is along with the test example. Looking for the input - how I can make it better in terms of logic &amp; data Structure. I know there are already cooked &amp; may be perfect code in the internet but I wish to attempt mine.</p>
  
  <p>PS: Code is not perfect however I have tested on given example. Apology if you find it messy. Your comments will be welcomed. </p>
  
  <pre><code>package com.company.graph;
  
  import java.util.*;
  
  public class BrethFirstSearch {
  
  
      public static void main(String...args){
  
  
          /*
           Input Undirected Graph -
  
              2      4       1
          A-------B-------C-------D
          | \             |   \   |
        7 |  \9         13|   3\  |6
          |   \           |     \ |
          E----F----------G-------H
            1      8         13
  
          Task 1 - Represent this graph as Data Structure that have optimal Space &amp; Time complexity for store &amp; traversal
          Task 2 - Perform "Breadth First Search"
  
  
          Simplified Representation of Graph where replaced vertex names with numbers..
               2      4       1
          0-------1-------2-------3
          | \             |   \   |
        7 |  \9         13|   3\  |6
          |   \           |     \ |
          4----5----------6-------7
            1      8         13
  
          */
  
  
          // We store number instated of letters since in real world every vertex may have full qualified name ex - "LasVegas" instead of just "A"
          Map&lt;Integer,String&gt; vertices = new HashMap&lt;&gt;();
          vertices.put(0,"A");
          vertices.put(1,"B");
          vertices.put(2,"C");
          vertices.put(3,"D");
          vertices.put(4,"E");
          vertices.put(5,"F");
          vertices.put(6,"G");
          vertices.put(7,"H");
  
          Map&lt;Edge, Integer&gt; edges = new HashMap&lt;&gt;();
  
          //Note - I have store both the side to make Graph search simpler. Comments will be welcomed!!
          edges.put(new Edge(0,1), 2);
          edges.put(new Edge(0,4), 7);
          edges.put(new Edge(0,5), 9);
          edges.put(new Edge(1,0), 2);
          edges.put(new Edge(1,2), 4);
          edges.put(new Edge(2,1), 4);
          edges.put(new Edge(2,3), 1);
          edges.put(new Edge(2,6), 13);
          edges.put(new Edge(2,7), 3);
          edges.put(new Edge(3,2), 1);
          edges.put(new Edge(3,7), 6);
          edges.put(new Edge(4,0), 7);
          edges.put(new Edge(4,5), 1);
          edges.put(new Edge(5,0), 9);
          edges.put(new Edge(5,4), 1);
          edges.put(new Edge(5,6), 8);
          edges.put(new Edge(6,2), 13);
          edges.put(new Edge(6,5), 8);
          edges.put(new Edge(6,7), 13);
          edges.put(new Edge(7,2), 3);
          edges.put(new Edge(7,3), 6);
          edges.put(new Edge(7,6), 13);
  
  
          breadthFirstSearch(vertices, edges);
          depthFirstSearch(vertices,edges);
  
      }
  
  
      static void depthFirstSearch(Map&lt;Integer,String&gt; vertices, Map&lt;Edge, Integer&gt; edges){
  
          System.out.format("%n%n%n%n************  Depth First Search - DFS ***********%n%n");
  
          LinkedList&lt;Integer&gt; listOfVertex = new LinkedList&lt;&gt;(vertices.keySet());
          List&lt;Edge&gt; listOfEdges = new ArrayList&lt;&gt;(edges.keySet());
  
          Stack&lt;Integer&gt; dfsStack = new Stack&lt;&gt;();
  
  
          while (!listOfVertex.isEmpty()){
  
              Integer v = listOfVertex.getFirst();
              dfsStack.push(listOfVertex.remove());
  
              System.out.format("*** Start DFS from Vertex %S ***%n", vertices.get(v));
  
              while(!dfsStack.empty()){
                  Integer vO = v;
                  for (Edge edge: listOfEdges) {
                      if (v.equals(edge.getV1()) &amp;&amp; listOfVertex.indexOf(edge.getV2()) != -1){  // found new vertex
                          Integer nextV = edge.getV2();
                          System.out.format("  Searching from Vertex %S -----&gt; %S%n", vertices.get(edge.getV1()), vertices.get(nextV));
                          dfsStack.push(nextV);
                          listOfVertex.remove(nextV);
                          v = nextV;
                          break;
                      }
                  }
                  if(vO.equals(v)){   //means not new vertex found from current vertex
                      v = dfsStack.pop();     //Search for previous vertex
                      System.out.format("Vertex %S has been conquered %n", vertices.get(v));
                  }
              }
  
          }
  
      }
  
  
  
      static void breadthFirstSearch(Map&lt;Integer,String&gt; vertices, Map&lt;Edge, Integer&gt; edges){
  
          System.out.format("************  Breadth First Search - BFS ***********%n%n");
  
          LinkedList&lt;Integer&gt; listOfVertex = new LinkedList&lt;&gt;(vertices.keySet());
          List&lt;Edge&gt; listOfEdges = new ArrayList&lt;&gt;(edges.keySet());
  
          BfsQueue bfsQueue = new BfsQueue();
          bfsQueue.add(listOfVertex.remove());  //start from 1st vertex = 0 alias A
  
          while (!bfsQueue.isEmpty()){
  
              //remove and start search from this vertex
              Integer v = bfsQueue.remove();
              System.out.format("Vertex %S has been conquered %n", vertices.get(v));
  
              //Search the Vertices from v
              listOfEdges.
                      forEach(edge -&gt; {
                          if(v.equals(edge.getV1()) &amp;&amp; listOfVertex.indexOf(edge.getV2()) != -1){
                              bfsQueue.add(edge.getV2());
                          }});
  
              //Mark the Searched Vertex
              Iterator&lt;Integer&gt; i = bfsQueue.getIterator();
              while (i.hasNext()){
                  Integer vertex = i.next();
                  if (listOfVertex.remove(vertex)){
                      System.out.format("     Searching from Vertex %S ------&gt; %S %n", vertices.get(v), vertices.get(vertex));
                  }
  
              }
          }
      }
  
  
  
  
      static class BfsQueue {
  
          private LinkedList&lt;Integer&gt; list = new LinkedList&lt;&gt;();
  
          Iterator&lt;Integer&gt; getIterator(){
              return list.iterator();
          }
  
  
          Integer remove(){
              Integer v = null;
              if(!list.isEmpty()){
                  v = list.getFirst();
                  list.removeFirst();
              }
              return v;
          }
  
          void add(Integer v){
              list.add(v);
          }
  
          boolean isEmpty(){
              return list.isEmpty();
          }
  
          boolean isPresent(Integer v){
              return list.indexOf(v) != -1;
          }
  
      }
  
  
      static class Edge {
  
          int v1;   //1st vertex
          int v2;   //2nd vertex
  
          public Edge(int v1, int v2) {
              this.v1 = v1;
              this.v2 = v2;
          }
  
  
          public int getV1() {
              return v1;
          }
  
          public int getV2() {
              return v2;
          }
      }
  }
  </code></pre>
  
  <p>The Output is :</p>
  
  <pre><code>"C:\Program Files\Java\jdk-11.0.4\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=64975:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath C:\CodeBase\Test\out\production\Test com.company.graph.BrethFirstSearch
  ************  Breadth First Search - BFS ***********
  
  Vertex A has been conquered 
       Searching from Vertex A ------&gt; E 
       Searching from Vertex A ------&gt; B 
       Searching from Vertex A ------&gt; F 
  Vertex E has been conquered 
  Vertex B has been conquered 
       Searching from Vertex B ------&gt; C 
  Vertex F has been conquered 
       Searching from Vertex F ------&gt; G 
  Vertex C has been conquered 
       Searching from Vertex C ------&gt; H 
       Searching from Vertex C ------&gt; D 
  Vertex G has been conquered 
  Vertex H has been conquered 
  Vertex D has been conquered 
  
  
  
  
  ************  Depth First Search - DFS ***********
  
  *** Start DFS from Vertex A ***
    Searching from Vertex A -----&gt; E
    Searching from Vertex E -----&gt; F
    Searching from Vertex F -----&gt; G
    Searching from Vertex G -----&gt; H
    Searching from Vertex H -----&gt; C
    Searching from Vertex C -----&gt; D
  Vertex D has been conquered 
  Vertex C has been conquered 
    Searching from Vertex C -----&gt; B
  Vertex B has been conquered 
  Vertex H has been conquered 
  Vertex G has been conquered 
  Vertex F has been conquered 
  Vertex E has been conquered 
  Vertex A has been conquered 
  
  Process finished with exit code 0
  
  </code></pre>