Comp Sci E214 Tutorial 2, Assignments of Computer Science

THe second tutorial for second year electrical engineering from 2025

Typology: Assignments

2025/2026

Uploaded on 03/18/2026

jimminy-cricket
jimminy-cricket 🇿🇦

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science E214
Tutorial 2
1 JBook 1.5.19
Write a program randomgraph.py that takes as command-line arguments an integer Nand
a floating-point value p[0,1]. The program should plot Nequally spaced points on the
circumference of a unit circle. Each pair of points should furthermore be connected by a line
with probability p, i.e. the presence of each line is determined independently with probability
p.
Example output for various parameters is given below.
2 Discerning Surfing (JBook 1.6.2, 1.6.11, and 1.6.12)
Encode the eight-page example shown below for use as input to transition.py in a file called
eightpage.txt. Then implement and use transition.py to obtain a transition matrix for
the example. Use this matrix to calculate the page ranks by implementing and using both
randomsurfer.py and markov.py.
Now modify transition.py to ignore the effect of multiple links. That is, if there are
multiple links from one page to another, count them as one link only. Call your modified
program singletransition.py. Repeat the analysis above on the example, but using the
modified transition matrix obtained with singletransition.py.
Repeat the analysis described above on the three-page example provided in threepage.txt
in the tutorial resources.
Compare your results in each case: which do you think is a better approach to ranking web
pages?
1
pf3
pf4

Partial preview of the text

Download Comp Sci E214 Tutorial 2 and more Assignments Computer Science in PDF only on Docsity!

Computer Science E

Tutorial 2

1 JBook 1.5.

Write a program randomgraph.py that takes as command-line arguments an integer N and a floating-point value p ∈ [0, 1]. The program should plot N equally spaced points on the circumference of a unit circle. Each pair of points should furthermore be connected by a line with probability p, i.e. the presence of each line is determined independently with probability p. Example output for various parameters is given below.

2 Discerning Surfing (JBook 1.6.2, 1.6.11, and 1.6.12)

Encode the eight-page example shown below for use as input to transition.py in a file called eightpage.txt. Then implement and use transition.py to obtain a transition matrix for the example. Use this matrix to calculate the page ranks by implementing and using both randomsurfer.py and markov.py.

Now modify transition.py to ignore the effect of multiple links. That is, if there are multiple links from one page to another, count them as one link only. Call your modified program singletransition.py. Repeat the analysis above on the example, but using the modified transition matrix obtained with singletransition.py. Repeat the analysis described above on the three-page example provided in threepage.txt in the tutorial resources. Compare your results in each case: which do you think is a better approach to ranking web pages?

3 The binomial distribution (JBook 2.1.34)

Write a function

def binomial(n, k, p)

in a program binomial.py to compute the probability of obtaining exactly k heads in N flips of a biased coin (heads with probability p) using the formula

f (n, k, p) = pk(1 − p)n−kN !/(k!(n − k)!)

Hint: To stave off overflow, compute x = ln f (n, k, p) and then return ex. You may want to use an additional function for this. Write a main function for your program that obtains N and p from the command line and prints out ∑n

k=

f (n, k, p) ,

which should be equal to one (up to numerical accuracy). Set up your program to call the main function when it is executed, but not when it is imported: in the former case, the value of the special variable name is ’ main ’; in the latter, it is the name of the module (i.e. ’binomial’ in this case). Also use the class binomialclient.py provided in the tutorial resources to test your binomial function. Take a look at the contents of binomialclient.py and ensure you un- derstand how it tests the code.

4 The Erlang loss formula

Consider a model of a telephone network switch where λ is the arrival rate of telephone calls (measured in calls per second) to a switch that can carry maximally N calls, and 1/μ is the average duration (measured in seconds) of a call. In telephony jargon, λ is called the offered traffic per unit time and ρ = λ/μ the offered load. The load ρ is measured in units of “Erlangs”. It is known that (under some reasonable assumptions we don’t go into here) the probability that an incoming call is lost because all N circuits are busy is a function of the load, ρ, and N. This blocking probability is given by the Erlang loss formula (also known as the Erlang B-formula):

B(N, ρ) = ρN^ /N! ∑N n=0 ρ n/n! (1)

Eqn. (1) looks easy to evaluate, but think of the situation where ρ = 100 and N = 200. Then there is a term of the form (100)^200 /200! that needs to be evaluated, and this is difficult to do exactly, even for a computer. To get around this we use a recursive formulation for B(N, ρ) which calculates B(N, ρ) in terms of B(N − 1 , ρ)

B(N, ρ) = ρ B(N − 1 , ρ) N + ρB(N − 1 , ρ)

where B(0, ρ) = 1. Create a program erlang.py, and use it to perform the following:

  1. Write a recursive function recErlang which takes two parameters n and ρ and computes and returns the blocking probability B(n, ρ) using Eqn. (2).

(a) (b) (c) (d)

Figure 2: Order

(a) (b) (c) (d)

Figure 3: Right

(a) (b) (c) (d)

Figure 4: Shaded