COMP 2119B Introduction to Data Structures and Algorithms
Assignment Three
Due Date: 22 April 2016 (Friday) 5:00pm
Assignment box: A4
[Note that for the questions involving algorithm design, before you present your
algorithm, try to describe your idea fir

[MIT: Q11.1-4, p.223]
We want to store some +ve numbers in a huge array
A[1.n] (n can be very big: n = 1,000,000,000) to
support the following operations:
Init(A): Initialize A to return -1 for all Read(i) query
Read(i): Return A[i] or 1 if no valid conte

COMP 2119B Introduction to Data Structures and Algorithms
Assignment One
Due Date: 15 February 2016 5:00pm
Assignment box: A4
[Note that for the questions involving algorithm design, before you present your
algorithm, try to describe your idea first! More

COMP 2119B
Introduction to Data Structures and
Algorithms
Lecturer: SM Yiu
Teaching Assistants: Xiaoqi Yu (Kiki)
Ki Wong (Ki)
Course web page
http:/moodle.hku.hk
COMP2119 [Section 2B, 2015]
1
Course Structure
Lectures
Tutorials
(Review, On-demand)
Assignm

Balanced
AVL Tree
Binary Search
Tree
A binary search tree can perform the following
operations, Insert, Delete, Search, Minimum, Maximum,
Predecessor, Successor, in O(h) time where h is the
Review height of the tree
Aim: we want to achieve O(log n)
Worst

Outcome (2): Sorting algorithms & heap data structure
Sorting (by comparisons)
Problem
Given a sequence of n numbers, output a permutation
(reordering) of this input sequence such that the
numbers are in increasing (or decreasing) order.
e.g. Input sequen

Outcome (1) and (3): How to analyze recursive algorithms?
Will teach an algortihm design technique.
Recurrence, recursive function, divide-and-conquer approach
Review:
A function is recursive if it is defined in terms of itself.
Let n be positive integers

Outcome (2): Introduction to data structures
An Introduction to Data Structure
Terms: Data Types, Data Structures, Abstract Data Type (ADT)
Data Types
- provided by a programming language
- a set of values + a set of operations on these values
- e.g. int

1) Consider the following graph.
(a) Which of the
v4
v0
followings are possible
v3
list of vertices for BFS?
v1
v2
v5
(i) v0, v3, v2, v1, v4, v5
(ii) v5, v4, v0, v3, v1, v2
(iii) v1, v0, v2, v4, v3, v5
(b) List the vertices in order of BFS starting from v

1) What is wrong with the following proof?
To show that n2 O(n):
Let c = 0.5, n0 = 1, then n n0,
n2 > 0.5n.
So, n2 O(n)
1
2) What is wrong with this proof?
To show that n2 = o(n3):
Let c = 2, n0 = 1, then n n0,
n2 < 2n3.
So, n2 = o(n3)
3
3) Let T(n) be th

Chapter 10
Sorting II
1
Merge sort
The idea of merge sort is to (1) divide the list into 2 roughly
equal parts, (2) sort the 2 parts individually and (3) merge
them afterwards.
Example:
24 13 1 26 28 15 27 2
1 13 24 26 | 2 15 27 28
13 24 26 | 2 15 27 28

Chapter 5
Trees
1
A 2-dimensional structure
A linked list is a one dimensional data
structure each element is followed by
exactly one other element (except for the
last).
2
A 2-dimensional structure
We can extend the concept of linked list so
that an el

Chapter 3
Data Structures
1
Data types
A programming language usually provides certain
basic data types. A data type is a set of values and a
collection of operations on those values.
e.g., integers with +, -, *, /, mod.
Some provide additional data typ

Chapter 2: Algorithms
Named after an Iranian
mathematician,
Al Khawarizmi
Dictionary of Computing
1
Programs
A program specifies in the exact syntax of some
programming language the computation one
expects a computer to perform. It specifies a finite
seq

Chapter 6
Searching
1
The problem
Given a set of n elements, search for one
with a specific key.
We have studied 2 ways of searching: linear
search and hashing.
2
The problem
For linear search:
average/worst case complexity is O(n)
For hashing:
averag

Chapter 8
Balanced Trees
1
Balancing a search tree
With binary search trees, average search time (with
random inserts and no deletes) is O(lg(n).
However, the worst case search time is still O(n).
Observation:
If the search tree is not balanced, the tre

Chapter 9
Sorting I
1
Problem definition
Problem: Given a list (A) of n numbers,
arrange the elements in A in increasing value.
Why sorting?
Make searching efficient
Check duplicates
Q: How would you sort 1 million oranges by
size?
2
The Model
We can

Chapter 7
Binary Search Trees
1
Binary search on a list
Can we do a binary search on a linked list of
elements?
Recall that the principle of binary search is to
examine the middle element and use the result to
eliminate half of the elements from conside

Chapter 4
Hashing
The key is the address
1
The Dictionary ADT
A dictionary is a set data type that supports 3
operations: INSERT, DELETE, and SEARCH.
Which operation do you think is used most often?
A linked list can be used to implement a dictionary.
H

Analysis of algorithms (Outcome (1)
Q: how good a given
Time
algorithm is?
- Not measure actual time (why?)
Recall
Two major concerns
Other issues (not our concern):
e.g. is it difficult to implement?
- Can be used to predict the
actual running time and f