Question: Please modify thisRandom Surfer program in Java. Modifications- Identify if there is a potential spider trap in your data and print a warning at the
Please modify this"Random Surfer" program in Java.
Modifications- Identify if there is a potential spider trap in your data and print a warning at the end of your program. You must identify all the conditions needed for a trapping to occur and their reflection in input and output data. Comment your code explaining what each new fragment is doing.
Program starts-->
import edu.princeton.cs.introcs.StdIn;
import edu.princeton.cs.introcs.StdOut;
public class RandomSurfer {
public static void main(String[] args) {
//Simulate random surfer.
int trials = Integer.parseInt(args[0]); // number of moves
int n = StdIn.readInt(); // number of pages
StdIn.readInt();
StdOut.print(" No.of pages="+n); // Printing the value of N i.e. no. of pages
System.out.println();
// Reading the transition matrix.
double[][] p = new double[n][n]; // p[i][j] = prob. that surfer moves from page i to page j
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
p[i][j] = StdIn.readDouble();
int[] freq = new int[n]; // freq[i] = no. of times surfer hits page i
// Start at page 0.
int page = 0;
for (int t = 0; t < trials; t++) {
// Making one random move to the next page.
double r = Math.random();
double sum = 0.0;
for (int j = 0; j < n; j++) {
// computing the interval that contains r.
sum += p[page][j];
if (r < sum) {
page = j;
break;
}
}
freq[page]++;
}
System.out.println("Page ranks :");
// Printing the page ranks.
for (int i = 0; i < n; i++) {
StdOut.printf("%8.5f", (double) freq[i] / trials); // Computing probability by dividing count by the no. of moves
}
StdOut.println();
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
