Professional Documents
Culture Documents
IIT Madras Week 10 Computational Thinking GA ?
IIT Madras Week 10 Computational Thinking GA ?
Question (1-4)
Statement
The following pseudocode is executing using the “Scores” table. At the end of the execution,
matrix represents the adjacency matrix of the graph generated from the “Scores” table.
1 D = {}
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 D[X.SeqNo] = {"P": X.Physics, "C": X.Chemistry, "M": X.Mathematics}
5 Move X to Table 2
6 }
7
8 matrixPh = getAdjMatrix(D, "P")
9 matrixCh = getAdjMatrix(D, "C")
10 matrixMa = getAdjMatrix(D, "M")
11
12 Procedure getAdjMatrix(D, Subject)
13 n = length(keys(D))
14 matrix = createMatrix(n, n)
15 foreach i in rows(matrix){
16 foreach j in columns(matrix){
17 if(i != j){
18 diff = D[i][Subject] - D[j][Subject]
19 if(10 <= diff and diff <= 20){
20 matrix[i][j] = 1
21 }
22 }
23 }
24 }
25 return(matrix)
26 End getAdjMatrix
Question-1 [5 Marks]
Statement
If matrixPh[i][j] = 1, then
Options
(a)
(b)
(c)
(d)
Solution
Consider the procedure getAdjMatrix. For each pair of keys i and j in D, matrix[i] [j] is initialized
with value 0 using the createMatrix procedure. For each pair of values i and j with i 6 = j, D[i]
[Subject] - D[j] [Subject] is stored in diff . If diff is at least 10 and at most 20 then an edge is
added from i to j in the graph by setting the corresponding cell with value 1. Therefore, matrix[i]
[j] is set to 1 if i scored at most 10 and at least 20 more marks than j in the subject which is
passed as the second parameter. matrixPh is constructed based on the Physics marks of the
students. Therefore, i scored at least 10 and at most 20 more marks in Physics than j.
Options
(a)
(b)
(c)
(d)
(e)
Solution
Recall the construction of the matrix matrixPh. For each i and j with i j,
(a) This is False because there can be a pair of students who scored with mark difference at least
21 (Say 90 and 60 marks in Physics) or at most 9 (say same marks in Physics). In this case, both
matrixPh[i][j] = 0 then matrixPh[j][i] = 0.
That is, D[j] [“P ”] < D[i] [“P ”]. Therefore, There will be no edge from j to i. Thus,
(c) Consider the case that both i and j scored the same marks in Physics. Then, both matrixPh[i]
[j] = 0 and matrixPh[j][i] = 0. This is a counterexample to show that the option is False.
(d) We show that if matrixPh[i][j] = 1 then matrixPh[j][i] = 0. Therefore, this is the negation of the
True statement.
(e) Consider the case that i is scored 10 less than j in Physics. Then, there will be an
edge from j to ibut not from i to j. This is a counterexample for this option.
1 n = length(keys(D))
2 matrixHelp = createMatrix(n, n)
3 foreach in in rows(matrixHelp){
4 foreach j in columns(matrixHelp){
5 matrixHelp[i][j] = matrixCh[i][j] + matrixPh[i][j] + matrixMa[i]
[j]
6 }
7 }
Let i and j be indices of two students. Choose the correct statement(s) from the given options. It is
a Multiple Select Question (MSQ)
Options
(a)
(b)
(c)
matrixHelp[i][j] matrixHelp[j][i]
(d)
(e)
Solution
Observe that the graph represented by the matrix matrix in the procedure getAdjMatrix shows
the help-relationship between the students on a specific subject. For all three subjects Physics,
Chemistry and Mathematics, we created three matrices matrixPh, matrixCh and matrixMa,
respectively. For each i and j, matrixHelp[i][j] is the sum of the corresponding cells in the three
matrices. Therefore, based on the definition of the edges, matrixHelp[i][j] denotes the number of
subjects that i can help j, where the mark difference is bounded between 10 and 20.
(a) Since there are exactly three subject, a student can help the other one in at most three
subjects. Therefore, the following statement is True:
(c) Consider the following case: D[i] = {“P”: 60, “C”: 70, “M”: 80} and D[i] = {“P”: 70, “C”: 60, “M”: 80}. i
can help j in one subject (Physics). Similarly, j can help i in one subject (Chemistry). None of then
can help each other in Mathematics. That is matrixHelp[i] [j] = matrixHelp[i] [j] = 1. Therefore,
the following statement is False:
(d) Consider the case that both i and j scored the same marks in all three subjects. Then,
matrixHelp[i] [j] = matrixHelp[i] [j] = 0. This is a counterexample for the following statement:
(e) Suppose i can help j in all three subjects, then i scored at least 10 and at most 20 more marks
than j in all three subjects. That is, j cannot help i in any subject. Therefore, the following
statement is True:
1 A = 0, B = 0
2 foreach i in rows(matrixHelp){
3 foreach j in columns(matrixHelp){
4 ***********************
5 *** Fill the code ***
6 ***********************
7 }
8 }
Options
(a)
(b)
(c)
Solution
When to update A: if both i and j can help each other in at least one subject, then A shoul be
incremented by one. The following code fragment can do it. Observe
that the variable A will be incremented twice for each such pair i and j. To avoid the over
counting, we have to consider either i <= j, or j <= i.
When to update B: if both i can help j in all three subject, then B shoul be incremented by one.
The following code fragment can do it. By combining the above code fragments,
1 if(matrixHelp[i][j] == 3){
2 B = B +1
3 }
1 if(i< j){
2 if(matrixHelp[i][j] > 0 and matrixHelp[j][i] > 0){
3 A = A + 1
4 }
5 }
6 if(matrixHelp[i][j] > 2){
7 B = B + 1
8 }
0 [0, 2, 3]
... ...
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] = [0, 2, 3]. Consider the followig question.
Question 5 [5 Marks]
Statement
The following pseudocode generates a graph G from books. Each node corresponds to an author.
There is an edge between two different authors i and j if they have co-authored a book, and the
edge is labeled with the number of books they have co-authored. Choose the correct code
fragment to complete the following pseudocode.
1 matrix = createMatrix(n, n)
2 foreach i in keys(books){
3 foreach j in books[i]{
4 foreach k in books[i]{
5 ***********************
6 *** Fill the code ***
7 ***********************
8 }
9 }
10 }
Options
(a)
1 matrix[j][k] = matrix[j][k] + 1
(b)
1
2 if(j != k){
3 matrix[j][k] = matrix[j][k] + 1
4 }
(c)
1 if(j != k){
2 matrix[j][k] = matrix[j][k] + 1
3 matrix[k][j] = matrix[k][j] + 1
4 }
(d)
1 matrix[j][k] = matrix[j][k] + 1
2 matrix[k][j] = matrix[k][j] + 1
Solution
Our aim is to construct a graph where authors are denoted by vertices and the co-authoring
relationship is denoted by edges. For each book i, books[i] is the list of authors who co-authored
i. For each j and k in books[i], we have add the co-author relationship. Therefore,
1 matrix[j][k] = matrix[j][k] + 1.
Observe that if i and j are the same during the iteration, then we should skip the matrix update.
Therefore, the following code fragment completes the pseudocode:
1 if(j != k){
2 matrix[j][k] = matrix[j][k] + 1
3 }
1 matrix2 = createMatrix(n, n)
2 foreach j in rows(matrix2){
3 foreach k in columns(matrix2){
4 matrix2[j][k] = []
5 }
6 }
7 foreach i in keys(books){
8 foreach j in book[i]{
9 foreach k in books[i]{
10 foreach h in books[i]{
11 if(i != j and j != h and k != h and not member(matrix2[j]
[k], h)){
12 matrix2[j][k] = matrix2[j][k] ++ [h]
13 matrix2[k][j] = matrix2[k][j] ++ [h]
14 }
15 }
16 }
17 }
18 }
Options
(a)
(b)
(c)
List of authors who have co-authored at least two book with both i and j
(d)
List of authors who have co-authored at least two book with either i or j
Solution
Note that the matrix matrix2 is indexed by pair of authors, and the cells are initialized with an
empty list. Therefore, each edge of the representing graph is labeled with a list. In the three levels
of iteration over books[i], j, k and h are denoting any three authors in the list. The following
condition,
1 j != k and k != h and h != i
captures that the three authors must be a distinct authors. The following condition
1 not(member(matrix2[j][k], h))
captures that the h should not exists in matrix2[j] [k]. Observe that all three authors are co-
authored the book i. Therefore, the matrix2[i] [j] captures the list of authors who have co-
authored a book with both j and k.
Options
(a)
(b)
(c)
(d)
Solution
Here we relate matrix and matrix2. The matrix captures the number of books co-authored by a
pair of authors whereas the matrix2 captures the number authors who have co-authored a book
with a pair of authors.
(a) If i and j have not co-authored any book then we will have
(b) If i and j have not co-authored any book then the common third co-authors list will be empty.
On the other hand, if the common third co-authors list is not empty then i and j have co-authored
at least one book. Therefore, the following combination is not possible:
(c) If i and j have co-authored with books where all such books are only co-authored by only i and
j.
(d) If i and j have co-authored with books where some books are co-authored at least
one more author other than i and j.
findAuthor(matrix) finds an author who has the maximum number of co-authors. Choose the
correct implementation of the procedure findAuthor. It is a Multiple Select Question.
Options
(a)
1 Procedure findAuthor(M)
2 Max = 0
3 A = 0
4 foreach i in rows(M){
5 Sum = 0
6 foreach j in columns(M){
7 if(M[i][j] > 0){
8 Sum = Sum + 1
9 }
10 }
11 if(Sum > Max){
12 Max = Sum
13 A = i
14 }
15 }
16 return(A)
17 End findAuthor
(b)
1 Procedure findAuthor(M)
2 Max = 0
3 A = 0
4 foreach i in rows(M){
5 Sum = 0
6 foreach j in columns(M){
7 if(M[i][j] > 0){
8 Sum = Sum + M[i][j]
9 }
10 }
11 if(Sum > Max){
12 Max = Sum
13 A = i
14 }
15 }
16 return(A)
17 End findAuthor
(c)
1 Procedure findAuthor(M)
2 Max = 0
3 A = 0
4 foreach i in columns(M){
5 Sum = 0
6 foreach j in rows(M){
7 if(M[j][i] > 0){
8 Sum = Sum + 1
9 }
10 }
11 if(Sum > Max){
12 Max = Sum
13 A = i
14 }
15 }
16 return(A)
17 End findAuthor
(d)
1 Procedure findAuthor(M)
2 Max = 0
3 A = 0
4 foreach i in columns(M){
5 Sum = 0
6 foreach j in rows(M){
7 if(M[j][i] > 0){
8 Sum = Sum + M[j][i]
9 }
10 }
11 if(Sum > Max){
12 Max = Sum
13 A = i
14 }
15 }
16 return(A)
17 End findAuthor
Solution
We want to find an author who has the maximum number of co-authors. For any two authors i
and j, matrix[i] [j] stores the number of books co-authored by both i and j. The number of
authors who are co-authored a book with an i can be computed as follows:
1 Sum = 0
2 foreach j in columns(M){
3 if(M[i][j] > 0){
4 Sum = Sum + 1
5 }
6 }
We cans witch the role of i and j since the matrix is symmetric. Based on the pseudocodes in the
options, Max captures the maximum number of co-authors for any author, and A captures such
an author. For each customer i, Sum captures the number of shopping partners of i. Once Sum is
computed for i, we have to check i has more co-authors than the current maximum. By
combining all the above logic, we can obtain the following pseudocode:
1 Procedure findAuthor(M)
2 Max = 0
3 foreach i in rows(M){
4 Sum = 0
5 foreach j in columns(M){
6 if(M[i][j] > 0){
7 Sum = Sum + 1
8 }
9 }
10 if(Sum > Max){
11 Max = Sum
12 A = i
13 }
14 }
15 return(A)
16 End findAuthor
1 matrix[i][j] = matrix[j][i].
Also note that the lists columns(matrix) and rows(matrix) are the same. Therefore, we can
obtain another equivalent pseudocode from the above.
1 Procedure findAuthor(M)
2 Max = 0
3 A = 0
4 foreach i in columns(M){
5 Sum = 0
6 foreach j in rows(M){
7 if(M[i][j] > 0){
8 Sum = Sum + 1
9 }
10 }
11 if(Sum > Max){
12 Max = Sum
13 A = i
14 }
15 }
16 return(A)
17 End findAuthor
1 n = length(keys(D))
2 matrix = createMatrix(n, n)
3 foreach i in rows(matrix){
4 foreach j in columns(matrix){
5 if(i != j){
6 matrix[i][j] = intersection(D[i], D[j])
7 }
8 else{
9 matrix[i][j] = []
10 }
11 }
12 }
Explanation
As the customers are indexed between 0 to n-1, i and j are the index number of two customers.
From Line 3 and line 4 it is cleared that each and every customer is being compared with each
other customer.
Line 5: i != j means if the customers are not same, then matrix[i][j] = intersection(D[i], D[j]). As D[i]
is the list of shops visited by customer i and D[j] is the list of shops visited by customer j.
intersection(D[i], D[j]) will return the list of the common elements between D[i] and D[j] which
means intersection(D[i], D[j]) returns the list of common shops visited by both the customers i
and j.
Therefore, whenever the different customers let us say i and j, matrix stores the list of common
shops visited by i and j.
Line 8-10: else block will be executed when if block does not get executed. This means when i == j,
matrix[i][j] stores the empty list for the same customer.
Question 9 [5 Marks]
Statement
Consider the adjacency matrix matrix constructed above. Assume that there exists a procedure
removeDuplicate which receives a list as input and removes all duplicate elements in the list.
When will findGoodSet(matrix) return True?
1 Procedure findGoodSet(M)
2 foreach i in rows(M){
3 foreach j in columns(M){
4 foreach h in rows(M){
5 list1 = []
6 if(length(M[i][j]) == 1 and member(D[h], first(M[i][j]))){
7 list1 = list1 ++ M[i][j]
8 }
9 if(length(M[i][h]) == 1 and member(D[j], first(M[i][h]))){
10 list1 = list1 ++ M[i][h]
11 }
12 if(length(M[j][h]) == 1 and member(D[i], first(M[j][h]))){
13 list1 = list1 ++ M[j][h]
14 }
15 list1 = removeDuplicate(list1)
16 if(length(list1) == 1){
17 return(True)
18 }
19 }
20 }
21 }
22 return(False)
23 End findGoodSet
Options
(a)
If there exists three customers who have visited all three shops.
(b)
If there exist three customers such that every pair of customers among them have visited only
one and the same shop in common.
(c)
If there exists three customers who have visited exactly one shop.
(d)
If there exists three customers where each pair among them have visited exactly one shop in
common.
Answer
(b)
Solution
Fix some values for i, j, and h from 0 to n-1.
Line 6: There are two condition added by "and". Therefore to get executed this if block, both the
conditions should be True. The first condition is
1 length(M[i][j]) == 1
As M[i][j] stores the list of shops visited by both the customers and if the length is 1, then
customers i and j have visited only one common shop.
1 member(D[h], first(M[i][j]))
As D[h] is the list of shops visited by the customer h, and first(M[i][j]) is the first element of list
store by M in i, j cell. M[i][j] is the list of shops visited by both i and j and from previous condition if
the length is 1 then M[i][j] contains only one shop if it is in D[h] then i, j have visited only one
common shop (pair-wise) and that shop is also visited by h.
Line 9 - 11: list1 will append the a shop, if this shop is the only one common shop visited by i and
h, and it is also visited by j.
Line 12 - 14: list1 will append the a shop, if this shop is the only one common shop visited by j and
h, and it is also visited by i.
If length(list1) == 1, then i, j, and h have visited only one common shop pairwise and only one
shop common overall too.
Options
(a)
1 Procedure findTopCustomer(M)
2 Max = 0, A = 0
3 foreach i in rows(M){
4 C = 0
5 foreach j in columns(M) {
6 if(length(M[i][j]) == 2){
7 C = C + 1
8 }
9 }
10 if(C > Max){
11 Max = C
12 A = i
13 }
14 }
15 return(A)
16 End findTopCustomer
(b)
1 Procedure findTopCustomer(M)
2 Max = 0, A = 0
3 foreach i in rows(M){
4 C = 0
5 foreach j in columns(M){
6 if(length(M[i][j]) > 1){
7 C = C + 1
8 }
9 }
10 if(C > Max){
11 Max = C
12 A = i
13 }
14 }
15 return(A)
16 End findTopCustomer
(c)
1 Procedure findTopCustomer(M)
2 Max = 0, A = 0
3 foreach i in rows(M){
4 C = 0
5 foreach j in columns(M) {
6 if(length(M[i][j]) == 2){
7 C = C + 1
8 }
9 }
10 if(C > Max){
11 Max = C
12 A = i
13 }
14 }
15 return(Max)
16 End findTopCustomer
(d)
1 Procedure findTopCustomer(M)
2 Max = 0, A = 0
3 foreach i in rows(M){
4 C = 0
5 foreach j in columns(M){
6 if(length(M[i][j]) > 1){
7 C = C + 1
8 }
9 }
10 if(C > Max){
11 Max = C
12 A = i
13 }
14 }
15 return(Max)
16 End findTopCustomer
Answer
(b)
Solution
We want to find the customer who have the maximum shopping partner. As per definition of the
shopping partner, i and j should have visited at least two common shops. This can be captured
using the following conditional statement:
1 Procedure findTopCustomer(M)
2 Max = 0, A = 0
3 foreach i in rows(M){
4 C = 0
5 foreach j in columns(M){
6 if(length(M[i][j] > 1)){
7 C = C + 1
8 }
9 }
10 if(C > Max){
11 Max = C
12 A = i
13 }
14 }
15 return(A)
16 End findTopCustomer