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;
/**
* Lab11 starter code; description in Lab11.pdf.
*/
public class RandomGraph {
public static void main(String[] args){
int n =5; // number of vertices
double p =0.3; // probability that an edge is present
long seed =123; // pseudo-random number generator seed
Random rng = new Random(seed);
Graphm graph = new Graphm(n); // Use adjacency matrix implementation
// Create a random graph; each edge present with probability p
for (int i =0; i n; ++i){
graph.setMark(i,0);
for (int j = i +1; j n; ++j){
double u = rng.nextDouble();
if (u p){
graph.setEdge(i, j,1);
graph.setEdge(j, i,1);
System.out.println("Set edge: "+ i +","+ j);
}
}
}
// Print adjacency matrix
System.out.println("Adjacency matrix:");
for (int i =0; i n; ++i){
for (int j =0; j n; ++j){
if (graph.isEdge(i, j))
System.out.print("1");
else
System.out.print("0");
}
System.out.println();
}
// Traverse the graph
graphTraverse(graph);
}
/** Depth first search */
static void DFS(Graph G, int v){
PreVisit(G, v); // Take appropriate action
G.setMark(v,1);
for (int w = G.first(v); w G.n(); w = G.next(v, w)){
if (G.getMark(w)==0){
DFS(G, w);
}
}
PostVisit(G, v); // Take appropriate action
}
/** Breadth first (queue-based) search */
static void BFS(Graph G, int start){
LinkedList Q = new LinkedList>();
Q.addLast(start);
G.setMark(start,1);
while (!Q.isEmpty()){// Process each vertex on Q
int v = Q.removeFirst();
PreVisit(G, v); // Take appropriate action
for (int w = G.first(v); w G.n(); w = G.next(v, w)){
if (G.getMark(w)==0){// Put neighbors on Q
G.setMark(w,1); // Mark as visited
Q.addLast(w);
}
}
PostVisit(G, v); // Take appropriate action
}
}
static void PreVisit(Graph G, int v){
// System.out.println("PreVisit: "+ v);
}
static void PostVisit(Graph G, int v){
// System.out.println("PostVisit: "+ v);
}
static void graphTraverse(Graph G){
for (int v =0; v G.n(); v++){
G.setMark(v,0); // Initialize; 0 means "unvisited"
}
for (int v =0; v G.n(); v++){
if (G.getMark(v)==0){
BFS(G, v);
// DFS(G, v); // You can toggle between BFS and DFS
}
}
}
}
// Graph implementation using adjacency matrix
class Graphm implements Graph {
private int[][] matrix;
private int[] mark;
public Graphm(int n){
matrix = new int[n][n];
mark = new int[n];
}
public int n(){
return matrix.length;
}
public void setEdge(int i, int j, int weight){
matrix[i][j]= weight;
}
public boolean isEdge(int i, int j){
return matrix[i][j]!=0;
}
public void setMark(int v, int value){
mark[v]= value;
}
public int getMark(int v){
return mark[v];
}
public int first(int v){
for (int i =0; i matrix[v].length; i++){
if (matrix[v][i]!=0) return i;
}
return matrix.length; // No edge
}
public int next(int v, int w){
for (int i = w +1; i matrix[v].length; i++){
if (matrix[v][i]!=0) return i;
}
return matrix.length; // No more edges
}
}
// Graph interface
interface Graph {
int n();
void setEdge(int i, int j, int weight);
boolean isEdge(int i, int j);
void setMark(int v, int value);
int getMark(int v);
int first(int v);
int next(int v, int w);
}
Please give me the correct code answer as per the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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!