0% found this document useful (1 vote)
771 views19 pages

Week 7 Graded Assignment PDF

The document contains details about graphs generated from student and author data, along with questions about the graphs. It discusses edges between nodes based on attributes and collaboration. Matrices are used to represent the graphs. Questions involve identifying properties of the graphs, representations in matrices, and determining values based on graph traversals and properties.

Uploaded by

khh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
771 views19 pages

Week 7 Graded Assignment PDF

The document contains details about graphs generated from student and author data, along with questions about the graphs. It discusses edges between nodes based on attributes and collaboration. Matrices are used to represent the graphs. Questions involve identifying properties of the graphs, representations in matrices, and determining values based on graph traversals and properties.

Uploaded by

khh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

BSCCS1001: Week-7, Graded Assignment

1. (4 = 2 + 2 points) Consider a graph generated from the “Scores” table that is repre-
sented by a matrix B. Each node in the graph corresponds to a student from the table.
SeqNo is used to label the nodes in the graph.

A={}
while (Table 1 has more rows) {
Read the first row X in Table 1
A[X.SeqNo] = [X.CityTown, X.Gender ]
Move X to Table 2
}
n = length(keys(A))
B = createMatrix(n, n)
foreach i in keys(A) {
foreach j in keys(A) {
if (i 6= j and isRelated(A[i], A[j])) {
B[i][j] = 1
}
}
}

Procedure isRelated(x, y)
if (first(x) == first(y) and last(x) == last(y)) {
return (True)
}
else {
return (False)
}
End isRelated
Study the pseudocode given above and answer the following questions:
(a) There is an edge between students i and j, with i 6= j, if and only if:
they are from the same city/town
they have the same gender
they are from the same city/town and have the same gender
they are from the same city/town or have the same gender
(b) Which of the following statements are true about this graph? It is a Multiple Select
Question (MSQ).
 There are two cliques in this graph, one for each gender.
 In every clique, there is at least a pair of students having different genders.
 All students in a given clique have the same gender.
 All students in a given clique are from the same city/town.
 There are no cliques in this graph.

Page 2
2. (2 points) The following table contains information regarding books in a library. Each
row in the table corresponds to a book and is authored by exactly two authors, with
equal contributions from both. There is a pool of n authors, each author being assigned
a unique index between 0 and n − 1. There are M books in total.

S.No Author-1 Author-2


0 4 0
.. .. ..
. . .
M −1 5 33

The table is represented by a dictionary named books, with the keys as serial numbers
and values as the corresponding list of authors. Assume that books has already been
computed. For example, we have:
books[0] = [4, 0].
A graph G is generated from this table. Each node corresponds to an author. There is
an edge between authors i and j if and only if they have collaborated on a book. Given
a pair of authors (i, j), with i 6= j, what does the value colab[i][j] represent at the end
of execution of the following pseudocode? Assume that the number of authors, n, is
given to you.

colab = createMatrix(n, n)
foreach i in keys(books) {
auth1 = first(books[i])
auth2 = last(books[i])
colab[auth1][auth2] = colab[auth1][auth2] + 1
colab[auth2][auth1] = colab[auth2][auth1] + 1
}

It represents the number of books published by authors i or j.


It represents the number of books in which authors i and j have collaborated.
It represents the number of books published by author i + number of books
published by author j.
It represents the number of books published by author i in which he has not
collaborated with author j.

Page 3
3. (8 = (1 + 1) × 4 points) Using the matrix colab computed from the previous problem,
answer the following questions. For each question, what do the variables aVar and bVar
represent at the end of execution of the pseudocode? Pick the correct option from the
table given below.

Answer Option
Maximum number of collaborators among all authors 1
An author with the maximum number of publications 2
A pair of authors who have collaborated most often 3
An author who has the maximum number of collaborators 4
Maximum number of books published by any pair of authors 5
The list of authors who have just one collaborator 6
Number of authors who have exactly one collaborator 7
Maximum number of books published by any author 8

Page 4
(a)

aVar = 0
bVar = [ ]
foreach r in rows(colab) {
foreach c in columns(colab) {
if (colab[r][c] > aVar) {
aVar = colab[r][c]
bVar = [r, c]
}
}
}

aVar:
bVar:

Page 5
(b)

aVar = 0
bVar = 0
foreach r in rows(colab) {
count = 0
foreach c in columns(colab) {
if (colab[r][c] > 0) {
count = count + 1
}
}
if (count > aVar) {
aVar = count
bVar = r
}
}

aVar:
bVar:

Page 6
(c)

aVar = 0
bVar = 0
foreach r in rows(colab) {
count = 0
foreach c in columns(colab) {
count = count + colab[r][c]
}
if (count > aVar) {
aVar = count
bVar = r
}
}

aVar:
bVar:

Page 7
(d)

aVar = 0
bVar = [ ]
foreach r in rows(colab) {
count = 0
foreach c in columns(colab) {
if (colab[r][c] > 0) {
count = count + 1
}
}
if (count == 1) {
aVar = aVar + 1
bVar = bVar ++ [r]
}
}

aVar:
bVar:

Page 8
4. (4 points) Continuing with the previous question, authors is a list of authors. isClique
is a procedure that determines if every pair of authors in this list have collaborated at
least once. It returns False if at least one pair of authors haven’t collaborated, and True
if every pair of authors have collaborated at least once. Select the correct code fragment
to complete the pseudocode. It is a Multiple Select Question (MSQ).

Procedure isClique(authors, colab)


foreach i in authors {
foreach j in authors {
*********************
* Fill the code *
*********************
}
}
return (True)
End isClique

 if (i 6= j and colab[i][j] > 0) {


return (False)
}
 if (i 6= j and colab[i][j] == 0) {
return (False)
}
 if (i 6= j) {
if (colab[i][j] == 1) {
return (False)
}
}
 if (i 6= j) {
if (colab[i][j] == 0) {
return (False)
}
}
 if (i 6= j) {
if (colab[i][j] == 0) {
return (True)
}
}

Page 9
5. (3 points) A computer scientist is planning to conduct an event in the city. She has
come up with a novel scheme to invite people:

• The host (computer scientist) can send out any number of invitations and doesn’t
accept any invitation.
• Each invitation is represented by a unique text message.
• Each invitee can choose to either accept or reject the invitation.
• If an invitee accepts the invitation, then he can invite exactly one other person
by forwarding the invitation that he received to this person, i.e., the invitee can
forward the message he received only once.
• If an invitee doesn’t accept the invitation, he cannot invite anyone.
• If a person receives multiple invitations, he can accept at most one of them. All
other invitations must be rejected.

This situation is modeled as a graph. There is a node corresponding to every person


who attends the event. n people attend the event and the attendees are indexed from 0
to n − 1. Given a pair of attendees (i, j), there is an edge from i to j if and only if the
following conditions are satisfied:

• j was invited by i
• j accepted i’s invitation

The graph is represented by a matrix A, such that A[i][j] = 1 if and only if there is an
edge from i to j.

Page 10
For the case of n = 5, which of the following is a possible representation of the graph?

Page 11
6. (10 = 2 + 3 + 1 × 5 points) Continuing with the previous question, consider the fol-
lowing graph of six attendees to the party:

(a) Who is the host? Enter a number between 0 and 5, both endpoints included.
Host:
(b) Which of the following statements are true? It is a Multiple Select Question (MSQ).
 0 and 5 have identical messages
 1, 2 and 5 have identical messages
 1, 2 and 3 have identical messages
 1 and 3 have different messages
 2 and 5 have different messages

Page 12
(c) Fill the missing values in the matrix representation, A, of the above graph.

a=
b=
c=
d=
e=

Page 13
7. (4 points) Continuing with the previous question, for a pair of attendees (i, j) other
than the host, we say that i is the ancestor of j, if their messages are identical and i
has accepted the invitation before j. Note that each invitation sent out by the host is a
unique text message. Assume that the matrix A that represents the graph has already
been computed.
isAncestor is a procedure that accepts the matrix A and two attendees i and j other
than the host as input, with i 6= j, and returns True if i is the ancestor of j, and False
otherwise. Select the correct code fragment to complete the pseudocode.

Procedure isAncestor(A, i, j)
k=i
flag = True
while (flag) {
flag = False
foreach c in columns(A) {
*********************
* Fill the code *
*********************
}
}
return (False)
End isAncestor

if (A[k][c] == 1) {
if (c == j) {
return (True)
}
k=c
exitloop
}

Page 14
if (A[k][c] == 1) {
if (c == j) {
return (True)
}
flag = True
exitloop
}

if (A[k][c] == 1) {
if (c == j) {
flag = True
return (True)
}
k=c
exitloop
}

if (A[k][c] == 1) {
if (c == j) {
return (True)
}
k=c
flag = True
exitloop
}

Page 15
8. (5 = 3 + 2 points) Assume that there are n stations in the “Trains” dataset, indexed
from 0 to n − 1. Consider a graph with stations as nodes. There is an edge from station
i to station j if and only if there is a direct train from i to j. The graph is represented
by a matrix direct, such that direct[i][j] = 1 if there is an edge from i to j, and zero
otherwise. Assume that direct has already been computed.
Station j is reachable from i if we can find a non-empty sequence of trains that take
a passenger from i to j. In other words, j is reachable from i if there is a non-empty
sequence of edges that connect i to j in the graph. The following pseudocode computes
a matrix B of identical dimensions as direct, such that B[i][j] = 1 if and only if j is
reachable from i. All other entries of B are zero.

Page 16
n = length(keys(direct))
B = createMatrix(n, n)
foreach r in rows(direct) {
foreach c in columns(direct) {
B[r][c] = direct[r][c]
}
}

hops = 0
while (hops < n - 1) {
B = oneMoreHop(B)
hops = hops + 1
}

Procedure oneMoreHop(B)
foreach r in rows(B) {
foreach c in columns(B) {
*********************
* Fill the code *
*********************
}
}
return (B)
End oneMoreHop

Page 17
(a) The pseudocode is incomplete. Select the correct code fragment to complete it. It
is a Multiple Select Question (MSQ).
 foreach k in columns(B) {
if (B[r][c] == 1) {
exitloop
}
if (B[r][k] == 1 and B[k][c] == 1) {
B[r][c] = 1
exitloop
}
}
 foreach k in columns(B) {
if (B[r][k] == 1 and B[k][c] == 1) {
B[r][c] = 1
}
}
 foreach k in rows(B) {
if (B[r][k] == 1 and B[k][c] == 1) {
B[r][c] = 1
}
}
 foreach k in columns(B) {
if (B[k][r] == 1 and B[k][c] == 1) {
B[r][c] = 1
}
}

Page 18
(b) Consider the following graph for the case of n = 5:

Matrix B is computed using the above pseudocode. What is the value of sum after
executing the following pseudocode?

sum = 0
foreach r in rows(B) {
foreach c in columns(B) {
sum = sum + B[r][c]
}
}

sum =

Page 19

You might also like