Question: Please give me the correct code answer as per the directions in JAVA. import java.util.LinkedList; import java.util.Random; / * * * Lab 1 1 starter
Please give me the correct code answer as per the directions in JAVA.
import java.util.LinkedList;
import java.util.Random;
Lab starter code; description in Labpdf
public class RandomGraph
public static void mainString args
int n ; number of vertices
double p ; probability that an edge is present
long seed ; pseudorandom number generator seed
Random rng new Randomseed;
Graphm graph new Graphmn; Use adjacency matrix implementation
Create a random graph; each edge present with probability p
for int i ; i n; i
graph.setMarki;
for int j i ; j n; j
double u rngnextDouble;
if u p
graph.setEdgei j;
graph.setEdgej i;
System.out.printlnSet edge: i j;
Print adjacency matrix
System.out.printlnAdjacency matrix:";
for int i ; i n; i
for int j ; j n; j
if graphisEdgei j
System.out.print;
else
System.out.print;
System.out.println;
Traverse the graph
graphTraversegraph;
Depth first search
static void DFSGraph G int v
PreVisitG v; Take appropriate action
GsetMarkv;
for int w Gfirstv; w Gn; w Gnextv w
if GgetMarkw
DFSG w;
PostVisitG v; Take appropriate action
Breadth first queuebased search
static void BFSGraph G int start
LinkedList Q new LinkedList;
QaddLaststart;
GsetMarkstart;
while QisEmpty Process each vertex on Q
int v QremoveFirst;
PreVisitG v; Take appropriate action
for int w Gfirstv; w Gn; w Gnextv w
if GgetMarkw Put neighbors on Q
GsetMarkw; Mark as visited
QaddLastw;
PostVisitG v; Take appropriate action
static void PreVisitGraph G int v
System.out.printlnPreVisit: v;
static void PostVisitGraph G int v
System.out.printlnPostVisit: v;
static void graphTraverseGraph G
for int v ; v Gn; v
GsetMarkv; Initialize; means "unvisited"
for int v ; v Gn; v
if GgetMarkv
BFSG v;
DFSG v; You can toggle between BFS and DFS
Graph implementation using adjacency matrix
class Graphm implements Graph
private int matrix;
private int mark;
public Graphmint n
matrix new intnn;
mark new intn;
public int n
return matrix.length;
public void setEdgeint i int j int weight
matrixij weight;
public boolean isEdgeint i int j
return matrixij;
public void setMarkint v int value
markv value;
public int getMarkint v
return markv;
public int firstint v
for int i ; i matrixvlength; i
if matrixvi return i;
return matrix.length; No edge
public int nextint v int w
for int i w ; i matrixvlength; i
if matrixvi return i;
return matrix.length; No more edges
Graph interface
interface Graph
int n;
void setEdgeint i int j int weight;
boolean isEdgeint i int j;
void setMarkint v int value;
int getMarkint v;
int firstint v;
int nextint v int w;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
