Question

Show the breadth-first trace and then the depth-first trace of these graphs in java.

0 B. 2 3 D E

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Graph-1:
BFS Traversal: 0 1 4 2 3
DFS Traversal: 0 4 3 2 1

Graph-2:
BFS Traversal: A B E C D
DFS Traversal: A B E D C

Java code for above problem

import java.util.*;
class Main
{
   // testing main method
   public static void main(String args[])
   {
       graph_1();
       graph_2();
   }
  
   // method for graph_1
   public static void graph_1()
   {
       List<List<Integer>> graph=new ArrayList<List<Integer>>();
       for(int i=0;i<5;i++)
       {
           graph.add(new ArrayList<Integer>());
       }
       graph.get(0).add(1);
       graph.get(0).add(4);
       graph.get(1).add(0);
       graph.get(1).add(2);
       graph.get(1).add(3);
       graph.get(1).add(4);
       graph.get(2).add(1);
       graph.get(2).add(3);
       graph.get(3).add(1);
       graph.get(3).add(2);
       graph.get(3).add(4);
       graph.get(4).add(0);
       graph.get(4).add(1);
       graph.get(4).add(3);  
       System.out.println("Graph-1: ");  
       bfs(graph);
       dfs(graph);
       System.out.println();
   }
  
   // method for graph_2
   public static void graph_2()
   {
       List<List<Integer>> graph=new ArrayList<List<Integer>>();
       for(int i=0;i<5;i++)
       {
           graph.add(new ArrayList<Integer>());
       }
       graph.get(0).add(1);
       graph.get(1).add(4);
       graph.get(3).add(0);
       graph.get(4).add(0);
       graph.get(4).add(2);
       graph.get(4).add(3);
       System.out.println("Graph-2: ");      
       bfs(graph);
       dfs(graph);
       System.out.println();
   }

   // method that prints the bfs traversal of given graph
   public static void bfs(List<List<Integer>> graph)
   {
       System.out.print("BFS Traversal: ");
       int V=graph.size();
       boolean [] visited=new boolean[V];
       Queue<Integer> queue=new LinkedList<Integer>();
      
       queue.add(0);
       visited[0]=true;
      
       while(!queue.isEmpty())
       {
           Integer node=queue.poll();
           System.out.print(node+" ");
           List<Integer> list=graph.get(node);
           for(int value: list)
           {
               if(!visited[value])
               {
                   visited[value]=true;
                   queue.add(value);
               }
           }
       }
       System.out.println();
   }
  
   // method that prints the dfs traversal of given graph
   public static void dfs(List<List<Integer>> graph)
   {
       System.out.print("DFS Traversal: ");
       int V=graph.size();
       boolean [] visited=new boolean[V];
       Stack<Integer> stack=new Stack<Integer>();
      
       stack.add(0);
       visited[0]=true;
      
       while(!stack.isEmpty())
       {
           Integer node=stack.pop();
           System.out.print(node+" ");
           List<Integer> list=graph.get(node);
           for(int value: list)
           {
               if(!visited[value])
               {
                   visited[value]=true;
                   stack.add(value);
               }
           }
       }
       System.out.println();
   }
}

Sample output

Note: Assume 0 as A, 1 as B, 2 as C, 3 as D and 4 as E for graph-2

Graph-1: BFS Traversal: 0 1 4 2 3 DFS Traversal: 0 4 3 2 1 Graph-2: BFS Traversal: 0 1 4 2 3 DFS Traversal: 0 1 4 3 2

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
Show the breadth-first trace and then the depth-first trace of these graphs in java. 0 B....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT