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
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
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): 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
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
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
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
[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 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
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
COMP2121A: Discrete Mathematics
Homework 1
Due Date: 7pm, 30 Sep, 2016
Rules: Discussion of the problems is permitted, but writing the assignment together is not
(i.e. you are not allowed to see the actual pages of another student).
Course Outcomes
[O1.
COMP2121A: Discrete Mathematics
Tutorial 1
6 Sep, 2016
1. Show that p (q r) and q (p r) are logically equivalent.
2. Let P (x) be x can speak English. and Q(x) be x knows C+., where the universe
of discourse is the set of all students in our class. Use qu
Chapter 7. Python programming language
Lab 7.5. Lets have some fun!
CSIS1123/COMP2123 Programming Technologies and Tools
Lab 7.5. Lets have some fun!
Please do this lab using the machines in the HW labs.
Or you can use a computer that is connected to the
THE UNIVERSITY OF HONG KONG
COMP1117C Computer Programming
Assignment 2
Deadline: 5:00pm, Mar 4, 2016
Part 1.
Consumer loans can be a complicated process. An example is the discount instalment loan,
which works as f
THE UNIVERSITY OF HONG KONG
COMP1117C Computer Programming I
Assignment 5
Deadline: 11:59pm, April 29, 2016
1. [100%] Write a project named Addr_book to manage an contact book. The file that contains the
main function should be named main.cpp.
This assig
The University of Hong Kong
COMP1117C Computer Programming
Assignment 4
Deadline: 5:00pm, April 8th 2016
The problem
Suppose that the company you are working for stores its workers' records in a text file called
record.txt as follows:
John
Ronald
Francis
THE UNIVERSITY OF HONG KONG
COMP1117C Computer Programming
Assignment 3
Due: 17:00PM, March 18, 2016
Problem 1 [100%]. Write a program Assignment3.cpp that implements the following four
functions:
1. [25%] void print_star (int n): Print a diamond of stars
1
Structures
A structure is a collection of one or more variables grouped
together under a single name. The data elements in a
structure are known as its member variables or simply
members, which can be of different types.
We can define a structure as i
WORKSHOP 1 ~
Code:Blocks
1
COMP 1117C 02/02/2016
CS ACCOUNT
You can get the information about getting
your CS account by the following steps:
Go
to our department homepage
http:/www.cs.hku.hk
Click Intranet
Click New Student
2
OUTLINE
Installation and
The do-while statement
Syntax
do body-statement while (expression)
body-statement
Yes
Example:
do
cfw_ cout < "Input Y/N:";
cin > ans;
while (ans = 'Y');
expression
No
Flowchart
Switch statement
switch(label)cfw_
Which alternative is executed
case consta
1
Read data from a file on disk
To do files I/O, you mist include the following line near the
beginning of your C+ program:
#include <fstream>
To read data from a disk file, say "data.txt", you must
declare a very special variable, of "type" ifstream:
Pointers
COMP1117C
1
Memory address
The main memory is a collection of
memory locations (or memory cells).
Each memory location has a unique
address.
Actually, we use the address of a
memory cell to read/write data in that
cell.
Thus, every variable you d
Arrays
1
Arrays
We can declare a list of variables with the same name,
and each of these variables can be accessed by its index.
To create these indexed variables, we need to declare
them as array as follows:
int score[100];
Example:
score[0], score[1], s
CSIS1117/COMP1117
Computer Programming
I
Course information
Reference book
Problem Solving with C+, Walter Savitch, Pearson (7th /8th /9th editions)
Online reference book
Sams Teach Yourself C+ in One Hour a Day
http:/www.pearsoned.co.uk/bookshop/detail
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 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