Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Adjacency Lists - Advanced Data Structures and Algorithms - Solved Exam, Exams of Data Structures and Algorithms

Main points of this past exam are: Algorithms, Programs, Incomplete Instance, Wgraph Data Structure, Adjacency Lists, Missing Fields, Firstedge, Incomplete Representation, Intermediate State, Data Structures

Typology: Exams

2012/2013

Uploaded on 03/23/2013

sardai
sardai 🇮🇳

4

(10)

117 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
- 1 -
Be neat and concise, but complete.
1. (5 points) An incomplete instance of the wgraph data structure is shown below. Fill in the
missing fields for the adjacency lists. Insert the edges into the adjacency lists so that the
edge numbers appear in increasing order in each list.
CS 541 – Algorithms and Programs
Exam 1 Solutions
J
onathan Turne
r
9/25
/
03
1
f
irstedge a
2
b
4
c
2
d
1
e
7
f
edges
a
l3
lnext 6
rnext
e
r
7
wt
1
d8 3 b1
2
b4 5 a6
3
c5 7 b3
4
a8 6 c3
5
c10 9e1
6
b910 f8
7
d0 0 a1
8
e0 0 b6
f0 0 c2
9
10
1
f
irstedge a
2
b
4
c
2
d
1
e
7
f
edges
a
l3
lnext 6
rnext
e
r
7
wt
1
d8 3 b1
2
b4 5 a6
3
c5 7 b3
4
a8 6 c3
5
c10 9e1
6
b910 f8
7
d0 0 a1
8
e0 0 b6
f0 0 c2
9
10
pf3
pf4
pf5
pf8

Partial preview of the text

Download Adjacency Lists - Advanced Data Structures and Algorithms - Solved Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Be neat and concise, but complete.

  1. (5 points) An incomplete instance of the wgraph data structure is shown below. Fill in the missing fields for the adjacency lists. Insert the edges into the adjacency lists so that the edge numbers appear in increasing order in each list.

CS 541 – Algorithms and Programs

Exam 1 Solutions Jonathan Turner 9/25/

firstedge a 1

b 2

c 4

d 2

e 1

f 7

edges

a

l 3

lnext 6

rnext e

r 7

wt 1

(^2) d 8 1 3 b

(^3) b 4 6 5 a

(^4) c 5 3 7 b

(^5) a 8 3 6 c

(^6) c 10 1 9 e

(^7) b 9 8 10 f

(^8) d 0 1 0 a

e 0 6 0 b

f 0 2 0 c

9

10

firstedge a 1

b 2

c 4

d 2

e 1

f 7

edges

a

l 3

lnext 6

rnext e

r 7

wt 1

(^2) d 8 1 3 b

(^3) b 4 6 5 a

(^4) c 5 3 7 b

(^5) a 8 3 6 c

(^6) c 10 1 9 e

(^7) b 9 8 10 f

(^8) d 0 1 0 a

e 0 6 0 b

f 0 2 0 c

9

10

  1. (5 points) Let A be a list n numbers. Describe an algorithm that creates a list of the k smallest values in A and which runs in O ( n ) time so long as k < n /lg n. Explain why your algorithm runs in O ( n ) time.

Use makeheap to build a 2-heap on the set of items. This takes O(n) time. Then perform k deletemin operations. Since the time per deletemin is O(log n), the running time for k<n/ lg n deletemin operations is O(n).

  1. (10 points) In the worst-case analysis of Prim’s algorithm, we saw that the number of calls to the changekey procedure was O ( m ). In this problem, you are to show that it is also Ω( n^2 ). First, show that you can assign edge weights to the complete graph on 5 vertices so that Prim’s algorithm executes the changekey operation for every edge in the graph. Generalize this example to show that for each value of n , there is a weighted graph with Ω( n^2 ) edges and that changekey is invoked when all of these edges are examined.

The figure below shows an assignment of edge weights to a complete graph on 5 vertices that causes Prim’s algorithm to execute changekey once for every edge, assuming that it starts from vertex u 1. The generalization of this graph has n vertices u 1 ,...,un and the weight of the edge from u (^) i to u (^) j is

equal to |i − j| for all i and j. If Prim’s algorithm starts with vertex u 1 , then for all i, vertex u i will be

removed from the heap in step i and changekey will be invoked for all edges (u (^) i ,uj ) with j>i. That is,

it will be invoked for every edge. Since there are Ω (n^2 ) edges, changekey is invoked Ω (n^2 ) times.

u 1 u 2 u 3 u 4 u 5 1 1 1

2 2 2

4

(^33)

1

u 1 u 2 u 3 u 4 u 5 1 1 1

2 2 2

4

(^33)

1

  1. (10 points) A min-max heap is an extension of the heap data structure that efficiently supports access to an item of maximum key, as well as access to an item of minimum key. One way to implement a min-max heap is by extending the data structure for d -heaps. One divides the heap into levels, with the root being in level 0, its children in level 1 and so forth. For every node x on an even-numbered level, key ( x ) ≤ key ( y ) for all descendants y of x. For every node x on an odd-numbered level, key ( x ) ≥ key ( y ) for all descendants y of x.

The correctness of any data structure operation depends on its maintaining certain essential properties of the data structure. The data portion of the class declaration for a C++ implementation of a min-max heap data structure is shown below. To make things simpler, you may assume a binary heap (each node has at most two children). What properties of the data must be maintained by programs that operate on it? List as many as you can think of. Hint : a position i is on an even level if  lg i  is even. Otherwise, i is on an odd level. class minMaxHeap { int N; // max number of items in heap int n; // number of items in heap item *h; // {h[1],...,h[n]} is set of items int *pos; // pos[i] gives position of i in h keytyp *kvec; // kvec[i] is key of item i

... };

1 ≤ n ≤ N

for 2 ≤ i ≤ n, with  lg i  odd, kvec [ h [  i/2  ]] ≤ kvec [ h [ i ]]

for 4 ≤ i ≤ n, with  lg i  even, kvec [ h [  i/4  ]] ≤ kvec [ h [ i ]]

for 2 ≤ i ≤ n, with  lg i  even, kvec [ h [  i/2  ]] ≥ kvec [ h [ i ]]

for 4 ≤ i ≤ n, with  lg i  odd, kvec [ h [  i/4  ]] ≥ kvec [ h [ i ]]

for 1 ≤ i ≤ n, 1 ≤ h [ i ] ≤ n, 1 ≤ pos [ i ] ≤ n and pos [ h [ i ]] = i

for 1 ≤ i<j ≤ n, h [ i ]≠ h [ j ] and pos [ i ]≠ pos [ j ]

  1. (15 points) In the analysis of the round-robin algorithm, the execution is divided into a series of passes. The first pass ends when all trees on the queue initially have been selected and combined with other trees. For a graph with n vertices and m edges, what is the largest possible number of nodes in any of the leftist heaps at the conclusion of the first pass?

Explain how for any n and any m ≥ n − 1 , one can construct a graph on n vertices and m edges

for which the round-robin algorithm has a heap of this size at the end of the first pass. Give a tight bound on the time required by the round-robin algorithm on this graph, excluding the time required for initialization.

It’s possible to end the first phase with a single heap that has 2m+n − 1 nodes.

Construct a star on n vertices with edges of weight 1. A star is a tree in which there is one central

vertex with an edge to every other vertex. Now, add m − (n − 1) edges of weight larger than 1 between

any arbitrary pairs of vertices. Note that the original star is a unique minimum spanning tree for this graph. If we execute the round robin algorithm on this graph, it will combine all vertices into a

single component in the first pass, resulting in a single heap that contains all the edges, plus n − 1

dummy nodes from lazy melds.

The running time for the round robin algorithm on this graph (excluding the initialization) is

O(n α (n,n)), since none of the findmins removes any nodes. Consequently, each iteration takes

constant time, except for the find operations and these take at most α (n,n) time each.

  1. (20 points) Consider an alternative analysis of the partition data structure in which the accounting variable debit ( u ) is incremented if the path between u and the root has length at

least 2 and g ( u)= g(p(u)), where the function g ( x ) is defined by g ( x)= lg lg( 2 +rank(x)).

In all other cases, fcredit is incremented.

Show that fcredit is incremented O (log log log n ) times during each top level find.

The values of g(x) cannot decrease as you go up the tree, so for every value of i, there is at most one node where g(x)=i and g(p(x))>i. So the number of nodes where fcredit gets incremented is at most 2

plus the number of distinct values of g(x). Since g(x) ≥ 0 and rank(x) is at most lg(n), this is O(log

log log n).

Show that if g ( x )= i at the end of a sequence of operations, then 21 ( ) 2

i debit x.

Every time debit(x) is incremented, the rank(p(x)) increases by at least 1. We only increment debit(x) while g(p(x))=i and g(p(x))=i only so long as ( ()) 2 2 2 1 < −

i+ rank px_. So, once debit(x) is incremented_ 2 1 2

i+ times, g(p(x))>i and debit(x) is never incremented again.

Show that the number of nodes for which the final value of g ( x )= i is at most (^2 )

n i.

If g(x)=i, then ( )≥ 22 − 2

i

rank x. Since the number of nodes with rank equal k is at most n^ /^2 k the

number of nodes with g(x)=i is at most

≥ −^2 2 − −

= ^ + +

∑i i i

n n n k

k L

Show that for each value of i , the sum of the debit variables for all nodes whose final values of g ( x )= i is O ( n ).

The sum is at most n n i i i i 2 2 2 1 2 1

(^112) 2 2 2 2

− + −

=. For i=0 and i=1, this is 2n. For all larger values of i it is

<n, so in all cases, it is O(n).