Question
1. Fill in all missing code, as prompted in the comments in the code (surrounded by ***). 2. Make sure there is a comment above
1. Fill in all missing code, as prompted in the comments in the code (surrounded by ***). 2. Make sure there is a comment above every line of code explaining what it is doing. Some lines already have comments, so you can leave those alone. 3. Run your code with various values of N (perhaps values such as 5, 20, 50, 100), and answer the following questions in a comment after your code. (a) In general, does the ranking order change more or less with increasing hyperlink matrix size? (b) For every value of N, the ranks start at 1 for all pages when d = 0. Why is this? (c) Run your code several times with N = 4. Keep repeating until you see a very high number of rank switches (e.g. 266). If you look at the graph, you will not see nearly so many switches. Explain roughly what is happening. 4. Answer the following questions, which are unrelated to the code. Please include these answers in the comment block after your code. 0 0.5 0 0.5 0 0 0 0 0 0 0 0.5 1 0.5 0 0 (a) Given the hyperlink matrix above, how many pages link to page 2? (b) Given the hyperlink matrix above, how many pages does page 4 link to? (c) Given the hyperlink matrix above and a damping ratio of 0.8, what is the rank of page 1?
% *** Get user input for value of N ***
N =
% REPLACE THIS COMMENT WITH AN EXPLANATION OF THIS LINE
H = randi([0 1],N,N);
% REPLACE THIS COMMENT WITH AN EXPLANATION OF THIS LINE
H(1:N+1:end) = 0;
% Divide each column by the sum of its entries, or if the column is all
% zeros then keep it all zeros by dividing by 1
H = H./max(1,sum(H,1));
% *** Create an identity matrix of the proper size ***
I =
% *** Create a vector of ones of the proper size ***
one =
% *** Create a vector of damping ratio values, starting at 0.001 and
% going to 0.999 in increments of 0.001 ***
d_vec =
% *** Create a matrix to store the rank vector for every value of the
% damping ratio ***
r_mat =
% This is called a "for loop", and it repeats its contents a
% predetermined number of times. In this case, it repeats as many
% times as there are elements in "d_vec". During each iteration, it
% takes a single value from "d_vec" and calculates the ranks of all the
% pages for that specific damping ratio. Then, it stores the results
% in the matrix "r_mat".
for ii = 1:length(d_vec)
% This assigns a single value from "d_vec" to the variable "d"
d = d_vec(ii);
% *** Solve the PageRank problem and store the results in "r" ***
r =
% This stores the rank vector that was just computed in the
% appropriate column of the matrix "r_mat", corresponding to the
% damping ratio that we used.
r_mat(:,ii) = r;
end
% This opens Figure 1, so that we don't open a new figure every time we
% run the code
figure(1);
% *** Plot the rankings matrix "r_mat" against the vector of damping
% ratios "d_vec" ***
% *** Label the x-axis "d" ***
% *** Label the y-axis "Rank" ***
% This counts how many times there's a switch in rank. The code goes
% through the ranks matrix "r_mat", and finds every time there is a
% change in the order of the ranks. Then, it sums up how many times such
a
% switch occurred.
[~,s] = sort(r_mat);
% This finds the number of switches.
switch_num = sum(any(diff(s,1,2)));
% *** Use an "fprintf" statement to print how many rank switches there
% were ***
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