Answered step by step
Verified Expert Solution
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 nonrecursive 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 COMP Hwk
DFS and BFS Search in Graph
Author: Roman Brown
import java.io;
import java.util.;
public class brownH
class variables
use prt for System.out to save typing
PrintStream prt System.out;
Print Graph Matrix
private void prtGraphint g int n
int i j k;
Print Graph matrix
prtprintfttGraph Matrix is as followst ;
for k ; k n; k
prtprintfd k;
end for
prtprintft ;
for k ; k n; k
prtprintf;
end for
prtprintft;
for i ; i n; i
prtprintfd i;
for j ; j n; j
prtprintfd gij;
end inner for
prtprintft;
end outer for
end prtm
Non recursive DFS for node j from node i
private void dfsint g int n int i int jFinish st method
prtprintftDFS 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 bfsint g int n int i int jFinish nd method
prtprintftBFS for node d from node d: j i;
complete this method by using Queue
end BFS
private void processString args
prtprintftThis 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
System.out.printftOOOPS Invalid No of aguments!"
tTO Execute: java xxxxxH inputfilename";
return;
end if
get input file name
fname args;
try open input file
Scanner inf new Scannernew Filefname;
n inf.nextInt;read no of nodes
edges inf.nextInt;read no of edges
Allocate space for graph
g new intnn;
initialize graph matrix
for i ; i n; i
for j ; j n; j
gij;
read graph data
for k ; k edges; k
i inf.nextInt;
j inf.nextInt;
wt inf.nextInt;
gij gji wt;undirected graph
end for
Print graph
prtGraphg n;
read no of searches
srch inf.nextInt;
Get System Time
start System.currentTimeMillis;
for k ; k srch; k
i inf.nextInt;
j inf.nextInt;
prtprintftSearch from d for d i j;
dfsg n i j;
bfsg n i j;
end for
Get System Time
end System.currentTimeMillis;
prtprintftExec Time: d ms end start;
close input file
inf.close;
catchIOException eprtprintfIO Error s e ;
end process method
public static void mainString args throws Exception
System.out.printftDFS and BFS Search in Graph:"
ttTo compile: javac xxxxxHjava"
ttTo execute: java xxxxxH inputfilename";
create an instance of xxxxxH class
brownH g new brownH;
call process method
gprocessargs;
Write your name in next line
System.out.printftAuthor: Roman Brown Date: s java.time.LocalDate.now;
end main
end xxxxxH
graph info: Chapter figure with searches from node i to j
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started