Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I tried to find an answer for these two respective functions for DFS and BFS , but to no avail. DFS requires a Stack (

I tried to find an answer for these two respective functions for DFS and BFS, but to no avail. DFS requires a Stack (non-recursive) while BFS uses Queue. Do you have an answer for these two functions? Here's the code I have so far:
Complete the required methods and write your name in main method
// CSUN Spring 24 COMP469 Hwk-2
// DFS and BFS Search in Graph
// Author: Roman Brown
import java.io.*;
import java.util.*;
public class brownH2{
//class variables
//use prt for System.out to save typing
PrintStream prt = System.out;
//Print Graph Matrix
private void prtGraph(int g[][], int n){
int i, j, k;
// Print Graph matrix
prt.printf("\t\tGraph Matrix is as follows\t ");
for (k =1; k = n; k++)
prt.printf("%4d|", k);
//end for
prt.printf("\t ");
for (k =1; k = n; k++)
prt.printf("-----");
//end for
prt.printf("\t");
for (i =1; i = n; i++){
prt.printf("%2d|", i);
for (j =1; j = n; j++)
prt.printf("%4d|", g[i][j]);
//end inner for
prt.printf("\t");
}// end outer for
}// end prtm
//Non recursive DFS for node j from node i
private void dfs(int g[][], int n, int i, int j){//Finish 1st method
prt.printf("\tDFS for node %d from node %d:", j, i);
//complete this method by using stack (non recursive)
}// end DFS from start to goal
// BFS for node j from node i
private void bfs(int g[][], int n, int i, int j){//Finish 2nd method
prt.printf("\tBFS for node %d from node %d:", j, i);
//complete this method by using Queue
}// end BFS
private void process(String args[]){
prt.printf("\tThis method reads weighted graph info from inputfile"+
"\tand uses adjacency matrix for graph representation."+
"\tNext reads no. of bfs and dfs searches from"+
" i to j.");
// Method local variables
int i, j, k, wt, srch;
int g[][], n, edges;//no. of nodes and edges;
long start, end;
int cnt = args.length; // get no. of arguments
String fname;
if (cnt 1){
System.out.printf("\tOOOPS Invalid No. of aguments!"+
"\tTO Execute: java xxxxxH2 inputfilename");
return;
}// end if
// get input file name
fname = args[0];
try{// open input file
Scanner inf = new Scanner(new File(fname));
n = inf.nextInt();//read no. of nodes
edges = inf.nextInt();//read no. of edges
//Allocate space for graph
g = new int[n+1][n+1];
// initialize graph matrix
for (i =1; i = n; i++)
for (j =1; j = n; j++)
g[i][j]=0;
// read graph data
for (k =1; k = edges; k++){
i = inf.nextInt();
j = inf.nextInt();
wt = inf.nextInt();
g[i][j]= g[j][i]= wt;//undirected graph
}// end for
// Print graph
prtGraph(g, n);
//read no. of searches
srch = inf.nextInt();
//Get System Time
start = System.currentTimeMillis();
for (k =1; k = srch; k++){
i = inf.nextInt();
j = inf.nextInt();
prt.printf("\tSearch from %d for %d", i, j);
dfs(g, n, i, j);
bfs(g, n, i, j);
}// end for
//Get System Time
end = System.currentTimeMillis();
prt.printf("\tExec Time: %d ms", end - start);
// close input file
inf.close();
}catch(IOException e){prt.printf("I/O Error %s", e );
}
}// end process method
public static void main(String[] args) throws Exception{
System.out.printf("\tDFS and BFS Search in Graph:"+
"\t\tTo compile: javac xxxxxH2.java" +
"\t\tTo execute: java xxxxxH2 inputfilename");
// create an instance of xxxxxH2 class
brownH2 g = new brownH2();
// call process method
g.process(args);
//Write your name in next line1
System.out.printf("\tAuthor: Roman Brown Date: %s", java.time.LocalDate.now());
}// end main
}// end xxxxxH2
/*graph info: Chapter 3, figure 3.9 with 3 searches from node i to j
510
12100
13125
14100
1575
2350
2475
25125
34100
35125
4550
3
15
25
51
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

what are the provisions in the absence of Partnership Deed?

Answered: 1 week ago

Question

1. What is called precipitation?

Answered: 1 week ago

Question

1.what is dew ?

Answered: 1 week ago

Question

8. Demonstrate aspects of assessing group performance

Answered: 1 week ago