Question: Consider the java code for BFS and DFS provided: DFS: // Java program to print DFS // mtraversal from a given given // graph import

Consider the java code for BFS and DFS provided:

DFS:

// Java program to print DFS

// mtraversal from a given given

// graph

import java.io.*;

import java.util.*;

// This class represents a

// directed graph using adjacency

// list representation

class Graph {

private int V; // No. of vertices

// Array of lists for

// Adjacency List Representation

private LinkedListadj[];

// Constructor

@SuppressWarnings("unchecked") Graph(intv)

{

V = v;

adj = newLinkedList[v];

for (int i = 0;i

adj[i]= new LinkedList();

}

// Function to add an edge into thegraph

void addEdge(int v, int w)

{

adj[v].add(w);// Add w to v's list.

}

// A function used by DFS

void DFSUtil(int v, booleanvisited[])

{

// Mark thecurrent node as visited and print it

visited[v] =true;

System.out.print(v+ " ");

// Recur for allthe vertices adjacent to this

// vertex

Iteratori = adj[v].listIterator();

while(i.hasNext()) {

intn = i.next();

if(!visited[n])

DFSUtil(n,visited);

}

}

// The function to do DFS traversal.

// It uses recursive

// DFSUtil()

void DFS(int v)

{

// Mark all thevertices as

// notvisited(set as

// false bydefault in java)

booleanvisited[] = new boolean[V];

// Call therecursive helper

// function toprint DFS

// traversal

DFSUtil(v,visited);

}

// Driver Code

public static void main(Stringargs[])

{

Graph g = newGraph(4);

g.addEdge(0,1);

g.addEdge(0,2);

g.addEdge(1,2);

g.addEdge(2,0);

g.addEdge(2,3);

g.addEdge(3,3);

System.out.println(

"Followingis Depth First Traversal "

+"(starting from vertex 2)");

g.DFS(2);

}

}

BFS:

// Java program to print BFS traversal from a given sourcevertex.

// BFS(int s) traverses vertices reachable from s.

import java.io.*;

import java.util.*;

// This class represents a directed graph using adjacencylist

// representation

class Graph

{

private int V; // No. ofvertices

private LinkedList adj[];//Adjacency Lists

// Constructor

Graph(int v)

{

V = v;

adj = newLinkedList[v];

for (int i=0;i

adj[i]= new LinkedList();

}

// Function to add an edge into thegraph

void addEdge(int v,int w)

{

adj[v].add(w);

}

// prints BFS traversal from a givensource s

void BFS(int s)

{

// Mark all thevertices as not visited(By default

// set asfalse)

booleanvisited[] = new boolean[V];

// Create aqueue for BFS

LinkedListqueue = new LinkedList();

// Mark thecurrent node as visited and enqueue it

visited[s]=true;

queue.add(s);

while(queue.size() != 0)

{

//Dequeue a vertex from queue and print it

s= queue.poll();

System.out.print(s+"");

//Get all adjacent vertices of the dequeued vertex s

//If a adjacent has not been visited, then mark it

//visited and enqueue it

Iteratori = adj[s].listIterator();

while(i.hasNext())

{

intn = i.next();

if(!visited[n])

{

visited[n]= true;

queue.add(n);

}

}

}

}

// Driver method to

public static void main(Stringargs[])

{

Graph g = newGraph(4);

g.addEdge(0,1);

g.addEdge(0,2);

g.addEdge(1,2);

g.addEdge(2,0);

g.addEdge(2,3);

g.addEdge(3,3);

System.out.println("Followingis Breadth First Traversal "+

"(startingfrom vertex 2)");

g.BFS(2);

}

}

i. Modify the code to traverse the following graph, with starting node

i. Modify the code to traverse the following graph, with starting node 0: V. vi. 8 10 11 ii. Modify the program to search for the number provided by the user, or report "not found" Print the number of steps it takes to find a goal (or not found). 111. iv. Print the depth at which each visited node is found. Implement DFS with a depth limit search (ask the user to enter the limit). Implement IDS; print nodes visited at each depth, and the total number of steps to solution.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To address each of the requests for modifications of the given code to handle the graph in the image lets discuss each modification one by one i To modify the code to traverse the graph in the image s... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!