Question

In: Computer Science

Write the program in JAVA. Make an undirected, but disconnected graph of at least 15 vertices...

Write the program in JAVA.

Make an undirected, but disconnected graph of at least 15 vertices and 25 edges.

you may add function calls in your main to add edges between vertices - one function call per edge. You may choose any graph representation of your choice from the 3 ways we discussed in the class.

Write a program to do DFS traversal of the graph.

Also write a program to do BFS traversal of the same graph.

Draw your graph on a paper and upload it with you code.

run the each of the two programs and capture screen shots and submit them. Check that the program does indeed do the correct traversal (based on your hand drawn graph)

Solutions

Expert Solution

Let the graph with 15 vertices and 25 edges is as follows:

Java code for BFS travsersal and DFS traversal is as follows:

import java.util.*;
class Main
{
   // testing main method
   public static void main(String args[])
   {
       List<List<Integer>> graph=createGraph();
       bfs_traversal(graph);
       dfs_traversal(graph);
   }
  
   // method that creates the graph
   public static List<List<Integer>> createGraph()
   {
       List<List<Integer>> graph=new ArrayList<List<Integer>>();
       for(int i=0;i<15;i++)
       {
           graph.add(new ArrayList<Integer>());
       }
      
       graph.get(0).add(1);
       graph.get(0).add(2);
       graph.get(0).add(3);
      
       graph.get(1).add(0);
       graph.get(1).add(2);
       graph.get(1).add(3);
       graph.get(1).add(4);
      
       graph.get(2).add(0);
       graph.get(2).add(1);
       graph.get(2).add(3);
      
       graph.get(3).add(0);
       graph.get(3).add(1);
       graph.get(3).add(2);
       graph.get(3).add(4);
      
       graph.get(4).add(2);
       graph.get(4).add(3);
      
       graph.get(5).add(6);
       graph.get(5).add(8);
       graph.get(5).add(9);
      
       graph.get(6).add(5);
       graph.get(6).add(7);
       graph.get(6).add(8);
       graph.get(6).add(9);
       graph.get(6).add(10);
      
       graph.get(7).add(6);
       graph.get(7).add(9);
       graph.get(7).add(10);
      
       graph.get(8).add(5);
       graph.get(8).add(6);
       graph.get(8).add(9);
       graph.get(8).add(11);
      
       graph.get(9).add(5);
       graph.get(9).add(6);
       graph.get(9).add(7);
       graph.get(9).add(8);
       graph.get(9).add(10);
       graph.get(9).add(11);
      
       graph.get(10).add(6);
       graph.get(10).add(7);
       graph.get(10).add(9);
       graph.get(10).add(11);
      
       graph.get(11).add(8);
       graph.get(11).add(9);
       graph.get(11).add(10);
      
       graph.get(12).add(13);
       graph.get(12).add(14);
      
       graph.get(13).add(12);
       graph.get(13).add(14);
      
       graph.get(14).add(12);
       graph.get(14).add(13);
      
       return graph;
   }

   // method that prints the bfs traversal of given graph
   public static void bfs_traversal(List<List<Integer>> graph)
   {
       System.out.print("BFS Traversal: ");
       boolean [] visited=new boolean[graph.size()];
       for(int i=0;i<graph.size();i++)
       {
           if(!visited[i])
           {
               bfs(graph,i,visited);
           }
       }
       System.out.println();
   }
  
   public static void bfs(List<List<Integer>> graph,int index,boolean visited[])
   {
       Queue<Integer> queue=new LinkedList<Integer>();
       queue.add(index);
       visited[index]=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);
               }
           }
       }
   }
  
   // method that prints the dfs traversal of given graph
   public static void dfs_traversal(List<List<Integer>> graph)
   {
       System.out.print("DFS Traversal: ");
       boolean [] visited=new boolean[graph.size()];
       for(int i=0;i<graph.size();i++)
       {
           if(!visited[i])
           {
               dfs(graph,i,visited);
           }
       }
       System.out.println();
   }
  
   public static void dfs(List<List<Integer>> graph,int index,boolean visited[])
   {
       Stack<Integer> stack=new Stack<Integer>();
       stack.add(index);
       visited[index]=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);
               }
           }
       }
   }
}

Sample output

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


Related Solutions

Write a complete Java program which takes vertices and edges of an undirected graph from the...
Write a complete Java program which takes vertices and edges of an undirected graph from the input file input.txt (the graph as adjacency matrix) ,(adjacent vertics of every vertex ),(DFS traversal of the graph),(BFS traversal of the graph),(Graph is connected or not connected)
Write a program in java that detects if there is a cycle in an undirected graph...
Write a program in java that detects if there is a cycle in an undirected graph using DFS Two lines of input: 1. Two integers V for the number of vertices and E for the number of edges respectively. 2. List of integers that shows how the graph is connected. Ex input: 4 4 01020323 Output: Graph contains cycle Ex input: 5 4 01020314 Output: Graph doesn't contains cycle
Let G be a simple undirected graph with n vertices where n is an even number....
Let G be a simple undirected graph with n vertices where n is an even number. Prove that G contains a triangle if it has at least (n^2 / 4) + 1 edges using mathematical induction.
Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each...
Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each edge labeled from the set {0,1}. Describe and analyze the worst-case time complexity of an efficient algorithm to find any cycle consisting of edges whose labels alternate 0,1.
Let G(V, E,w) be a weighted undirected graph, where V is the set of vertices, E...
Let G(V, E,w) be a weighted undirected graph, where V is the set of vertices, E is the set of edges, and w : E → R + is the weight of the edges (R + is the set of real positive numbers). Suppose T(G) is the set of all minimum spanning trees of G and is non-empty. If we know that the weight function w is a injection, i.e., no two edges in G have the same weight, then:...
Show that a graph with at least 2k vertices is k-connected if and only if for...
Show that a graph with at least 2k vertices is k-connected if and only if for any two unrelated subsets X, Y of V, such that | X | = k = | Y |, there are k foreign paths between X and Y.
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
Consider an undirected graph G that has n distinct vertices. Assume n≥3. How many distinct edges...
Consider an undirected graph G that has n distinct vertices. Assume n≥3. How many distinct edges will there be in any circuit for G that contains all the vertices in G? What is the maximum degree that any vertex in G can have? What is the maximum number of distinct edges G can have? What is the maximum number of distinct edges that G can have if G is disconnected?
Is it always possible to make an undirected graph with two connected components connected by adding...
Is it always possible to make an undirected graph with two connected components connected by adding a single edge? • Why or why not? (Proof or counter example) [if true, give a proof, if false, give counter example] • Does the same hold true for directed graphs (and strongly connected components rather than connected components)? (Proof or counter example) [if true, give a proof, if false, give counter example]
Write a C++ or Java program name that conducts the BFS traversal of a graph and...
Write a C++ or Java program name that conducts the BFS traversal of a graph and displays city names in the range of hop(s) from a starting city Input format: This is a sample input from a user. 4 Monterey LA SF SD 6 Monterey LA LA SD SD Monterey SF Monterey SF LA SF SD Monterey 2 The first line (= 4 in the example) indicates that there are four vertices in the graph. The following four lines describe...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT