This preview shows pages 1–3. Sign up to view the full content.
Cs445 — Homework #3
Augmented Data Structures, Graphs, Prim algorithms and
Random variables
Due: 3/22/2005 during class meeting.
All questions are worth 13 points, excluding the bonus question that is worth 20 points.
1. Suggest a data structure that supports the following operations on the grades that a
(single) student at the university receives. Assume that the total number
n
of exams
that the student might take is large. The data structure should support the following
operations:
(a)
insert
(
exam
date,exam
grade
). This operation informs the data structure that
the student received at date
exam
date
the grade
exam
grade
.
(b)
average
(
date
1
, date
2
). As a response to this operation the data structure should
answer what is the average of all grades that are received between the dates
date
1
and
date
2
.
The time per operation should be
O
(log
n
), where
n
is the total number of exam
taken. Hint — use search trees or SkipList, and maintain extra information in each
cell of the structure.
Answer:
We can use an augmented AVL tree, augmented RedBlack tree, or an
augmented SkipList . Each node contains a ﬁeld “total” which holds the sum of the
grades in the subtree for which it is the root and another ﬁeld “number” that holds
the number of nodes in the subtree.
Modiﬁcation to insert : Add the ”grade” to be inserted, to the
total
ﬁeld of each node
that we traverse during insertion.
Modiﬁcation to delete : Subtract the ”grade” to be deleted from the total
field
of
each node traversed during deletion.
Average : Search
date
1
in the tree. Maintain a variable
total
1
initialized to 0. During
traversal, to ﬁnd
date
1
, if next node is left child, then
total
1
remains unchanged. If
next node is right child, add “total” value of the left node to
total
1
, and the grade of
the exam store at current node. If
date
1
happens to be stored at a node
v
of the tree,
also add
total
1
the value of “total” stored at the left child of
v
. Repeat the same for
date
2
and store total grade in
total
2
. Similarly deﬁne (and compute)
number
1
and
number
2
.
The
average
(
date
1
, date
2
)
=
(
total
1

total
2
)
/
(
number
1

number
2
)
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2. Assume that the graph
G
(
V,E
) is given, where the weights of each edge is an integer
between 1 and 10. Show that in this case,prim’s algorithm can be implemented for
this graph in time
O
(

E

+

V

). Hint — suggest a data structure that replaces the
priority queue, and analyze the running time.
Answer:
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '06
 Williams
 Algorithms, Data Structures

Click to edit the document details