Fall2022 Week1.1
Fall2022 Week1.1
Algorithms
Instructor: Charles Hughes
Week 1.1: Preliminaries
1
About this course
Instructor: Charles Hughes
Term: Fall 2022
Office Location: HEC-247C
Class Meeting Days: Friday
Office Hours: Wednesday & Friday 10-11:30;
These are virtual Zoom at https://tinyurl.com/4rwe35ud
Class Meeting Time: 1:30-4:20
Class Location: BA-116 and virtual
https://tinyurl.com/hjcxyvmv
Email: [email protected] Webcourses@UCF messaging
Course Modality: RV
GTA: Qibing Jiang
Email: [email protected] Webcourses@UCF messaging
Office Hours:Tuesday/Thursday 3:00 PM – 5:00 PM
https://ucf.zoom.us/j/7662554201
2
Grading Policy
• Midterm – 150 points; Final Exam – 200 points
• Extra – 50 points increases weight of better exam, to your benefit
• Assignments, including Two Presentation Videos – 100 points
• Total Available: 500
• Grading will be A >= 90%, B+ >= 85%, B >= 80%, C+ >= 75%, C >= 70%,
D >= 50%, F < 50%
Minus grades might also be used if deemed appropriate.
Assignments must show effort and be turned in on time, otherwise they
will earn a zero.
Assignments can be discussed, but you must write your own answer and
include the name of the person with whom you have collaborated.
Late submissions will not be accepted unless arrangements have been
made in advance based on exceptions such as religious observances,
travel to academic conferences, and military obligations.
3
Text Materials
We will use material from
Introduction to Algorithms (3rd edition) by
Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest, and Clifford
See http://www.cs.ucf.edu/~sharma/
Algorithms_notes.pdf for lecture notes by the
publishers.
Click https://tinyurl.com/y9tbpa9n
for video lectures on some initial topics and
http://www.cs.ucf.edu/~sharma/COP3503
for slides from an undergraduate version of this
course to brush up on the preliminaries
4
Prerequisites
Prerequisites: The prerequisites for this
course are COP 3503 and COT 3100.
Although we don't enforce it strictly, we
assume some undergraduate-level exposure
to discrete mathematics, basic algorithms
and their analysis, and the ability to read,
recognize and write a valid proof.
5
Course Activities
The course activities include assignments,
exams, papers, and peer evaluations. You will
need regular access to the Internet for
exams, streaming class meetings, and videos.
You will need a webcam for monitoring
during exams.
8
Objectives and Outcomes
Understanding of various types of techniques for
algorithms design and (run time) analysis in
detail.
9
Warm-up: Game involving a fluid
We have n cups and n pitchers, each with a liter of the fluid
(commonly viewed as a beer game)
Stage 1: I pour first liter into cups. You drink from one of those.
…
Stage n-1: I pour (n-1)th liter into cups. You drink from one of those.
Stage n: I pour nth liter into cups. I drink from one of those.
10
Strategy 1
Strategy 1: I try to keep everything even until nth stage.
How much do I get?
11
Strategy 2
Strategy 2: I leave the empty cups empty and split over n-1, n-2, …
How much do I get?
13
Population count
int popcount1(uint64_t x) {
int c, i;
// Loops 64 times
for (i = 0, c = 0; i< 64; i++, x>>=1) c += (x & 1);
return c;
}
Versus
int popcount2(uint64_t x) {
int c;
// Sometimes loops 64 times but can be one step
for (c = 0; x; x>>=1) c += (x & 1);
return c;
}
14
Population count better
Code Segment that is Fast if most bits are 0 but can take 64
iterations if all 1’s
int popcountA(uint64_t x) {
int c;
// Each step skips to next 1, if it exists
for (c = 0; x; c++, x &= (x-1)) // 4 ops/1-bit + 1
return c;
}
Consider a binary number x = ?...?10…0
Unsigned x-1 = ?...?01…1; x&(x-1) = ?...?00…0 (clears leftmost 1)
Test of x leads to stopping as soon as no ones remain
15
Count in constant time
int popcount(uint64_t x) {
//put count of each 2 bits into those 2 bits
// m1_64=0x5555555555555555; binary: 01010101...
x -= (x >> 1) & m1_64;
//put count of each 4 bits into those 4 bits
// m2_64=0x3333333333333333; binary: 00110011..,
x = (x & m2_64) + ((x >> 2) & m2_64);
//put count of each 8 bits into those 8 bits
// m4_64=0x0f0f0f0f0f0f0f0f; binary: 0000111100001111…
x = (x + (x >> 4)) & m4_64;
// left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
// h01_64=0x0101010101010101; 00000001000000010000001…
x = (x * h01_64) >> 56;
//return x, which is population count of original x
return x; // Took just 12 arithmetic operations
}
/*
* Population count (number of one bits) in an interval
* within a 64-bit word
*/
int intervalPopcount(uint64_t x, int lo, int hi) {
if (lo<0) lo = 0;
if (hi>63) hi = 63;
if (lo>hi) return 0;
// allones=0xffffffffffffffff
x &= (allones_64>>(63-hi));
x &= (allones_64<<lo);
return popcountA(x);
}
14
Max and min in one pass
We have a sequence of n numbers a1, a2, …, an
To find max and min, we can start with max = - ∞ and min = + ∞.
We make a pass over the n items, comparing ai to max and min and
saving the larger and smaller. This takes T(n) = 2n – 2 compares.
Can we do better?
Study video (note its use of quads). We can discuss but key is to
make only 3 compares per quad leading to T(n) = 3n/4
19