Neeldhara
  • About
  • Research
    • Overview
    • People
    • Publications
    • Surveys
  • Teaching
    • Courses
    • Materials
  • Lists
    • Puzzles
    • Bookmarks
  • Exposition
    • Talks
    • Videos
  • Events
  • Blog

ES242. Data Structures and Algorithms I. Quiz 02 Solutions

ES242. Data Structures and Algorithms I.

Quiz 02 Solutions

Released: 12 Jan, 2023

Back to course page

Problem 1. Doubly Linked Lists

If p is the address of a node in a doubly linked list L, then:

  • next(p) is the address of the next node in the linked list
  • prev(p) is the address of the previous node in the linked list
  • data(p) is the information contained in the the node at address p

Note that:

  • if p is the address of the first node in L then prev(p) is NULL.
  • if p is the address of the last node in L then next(p) is NULL.

Also, data(p), next(p) and prev(p) returns a sensible value only if p is not NULL, otherwise they are UNDEFINED.

If L is a linked list with five elements and p is the address of the third element, then what does next(prev(next(next(p)))) represent?

  • Address of the 1st element
  • Address of the 2nd element
  • Address of the 3rd element
  • Address of the 4th element
  • Address of the 5th element
  • UNDEFINED

If L is a linked list with five elements and p is the address of the third element, then what does data(prev(prev(next(p)))) represent?

  • Data of the 1st element
  • Data of the 2nd element
  • Data of the 3rd element
  • Data of the 4th element
  • Data of the 5th element
  • UNDEFINED
Solution

For the first part:

next(prev(next(next(p)))) = next(prev(next(next(3)))) = next(prev(next(4))) = next(prev(5)) = next(4) = 5

For the second part:

data(prev(prev(next(p)))) = data(prev(prev(next(3)))) = data(prev(prev(4))) = data(prev(3)) = data(2)

Problem 2. Adjacency Lists

Suppose AAA is the adjacency matrix of a simple undirected graph G=(V,E)G = (V,E)G=(V,E) with nnn vertices given by {1,2,…,n}\{1,2,\ldots,n\}{1,2,…,n}, that is,

A[i,j]={1if (i,j)∈E,0if (i,j)∉E. A[i,j] = \begin{cases} 1 & \text{if } (i,j) \in E,\\ 0 & \text{if } (i,j) \notin E. \end{cases} A[i,j]={10​if (i,j)∈E,if (i,j)∈/E.​

Note that A[i,i]=0A[i,i] = 0A[i,i]=0 for all i∈{1,2,…,n}i \in \{1,2,\ldots,n\}i∈{1,2,…,n} since GGG is simple.

Suppose (i,j)∈E(i,j) \in E(i,j)∈E for some i,j∈{1,2,…,n}i,j \in \{1,2,\ldots,n\}i,j∈{1,2,…,n}, i≠ji \neq ji=j. Let kkk denote the number of vertices that are adjacent to both iii and jjj.

What is the value of A2[i,j]A^2[i,j]A2[i,j]?

  • 000
  • 111
  • kkk
  • k+1k+1k+1

Suppose (i,j)∉E(i,j) \notin E(i,j)∈/E for some i,j∈{1,2,…,n}i,j \in \{1,2,\ldots,n\}i,j∈{1,2,…,n}, i≠ji \neq ji=j. Let kkk denote the number of vertices that are adjacent to both iii and jjj.

What is the value of A2[i,j]A^2[i,j]A2[i,j]?

  • 000
  • 111
  • kkk
  • k+1k+1k+1
Solution

A2[i,j]=kA^2[i,j] = kA2[i,j]=k irrespective of whether (i,j)∈E(i,j) \in E(i,j)∈E or not. Notice that the entry in the iii-th row and jjj-th column of A2A^2A2 is the product of the iii-th row of AAA and the jjj-th column of AAA, and the only terms that are not zeroed-out in this product are those that correspond to vertices adjacent to both iii and jjj. Note that both iii and jjj are not adjacent to themselves, which is why their adjacency (or lack of it) does not change the final answer.

Problem 3. Edge List

Suppose every vertex of a graph GGG on nnn vertices has ddd neighbors.

What is the size of the edge list?

  • d⋅nd \cdot nd⋅n
  • d⋅n/2d \cdot n/2d⋅n/2
  • 2d⋅n2d \cdot n2d⋅n
  • (d+n)(d + n)(d+n)

Is it possible that both ddd and nnn are odd?

  • Yes
  • No
Solution

If every vertex has ddd neighbors then there are ddd edges incident to all the nnn vertices in the graph. Thus we have dndndn edges but with each edge counted exactly twice: in particular the edge (u,v)(u,v)(u,v) gets counted as being one of the edges incident on uuu and one of the edges incident on vvv. Therefore the total number of edges, and therefore the size of the edge list, is d⋅n/2d \cdot n/2d⋅n/2.

Since the total number of edges in any graph is a whole number, and is given by d⋅n/2d \cdot n/2d⋅n/2, it is not possible that both ddd and nnn are odd.


© 2022 • Neeldhara Misra • Credits •

 

Corrections? Please leave a comment here or a PR in this repository, thanks!

I’d rather be a failure at something I love than a success at something I hate.

George Burns

You live and you learn — at any rate, you live.

Douglas Adams

A problem worthy of attack proves its worth by fighting back.

Paul Erdos

×