0% found this document useful (0 votes)
4 views19 pages

Fall2022 Week1.1

The document outlines the structure and requirements for the Design and Analysis of Algorithms course taught by Charles Hughes in Fall 2022. It includes information on grading policies, important dates, course activities, prerequisites, and the materials used for the course. Additionally, it provides an overview of the topics covered, including algorithm design techniques and data structures, along with examples of strategies for problem-solving.

Uploaded by

MJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Fall2022 Week1.1

The document outlines the structure and requirements for the Design and Analysis of Algorithms course taught by Charles Hughes in Fall 2022. It includes information on grading policies, important dates, course activities, prerequisites, and the materials used for the course. Additionally, it provides an overview of the topics covered, including algorithm design techniques and data structures, along with examples of strategies for problem-solving.

Uploaded by

MJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Design and Analysis of

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.

Note: Finish Assignment 0 to meet


requirements for financial assistance.
6
Important Dates
Exam Dates (tentative for midterm; fixed for final):
Midterm: Friday, October 7; Final: Friday, December 9, 1:00-3:50
Exams will be online whether you are in the R or V section
The Final will be cumulative, although material from the midterm on
will be emphasized
Note: If topics appear weak on midterm, they will reappear on final
Withdrawal Deadline: Friday, October 28
Holidays: Veterans Day: Friday, November 11
Thanksgiving: Friday, November 25
Other things can affect the schedule: Hurricanes and Football Games;
In fact, there is a game on September 9.
Check UCF Academic Calendar (https://calendar.ucf.edu/2022/fall)
for other important dates
7
Overview
Review of mathematical background, sorting and
searching, algorithm design techniques such as
divide and conquer, greedy approach, dynamic
programming, worst case and average case analysis
techniques, data structures such as binary search
trees and various heaps, graph/string algorithms,
and a gentle introduction to complexity topics (P,
NP, NP-Complete, etc.)

8
Objectives and Outcomes
Understanding of various types of techniques for
algorithms design and (run time) analysis in
detail.

Also, a gentle introduction to several advanced


data structures and the connections between
some Complexity Theory topics and Algorithm
Analysis.

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.

What is best strategy to maximize what I get at stage n?

Strategy 1: I try to keep everything even until nth stage.


How much do I get?

Strategy 2: I leave the empty cups empty


How much do I get?

10
Strategy 1
Strategy 1: I try to keep everything even until nth stage.
How much do I get?

I get something short of 1 + (n-1)/n liters

Example with n=4


1. I fill evenly; you drink 1/4 liter
2. I fill each to 2/4 – 1/16; you drink 7/16
3. I fill each to 3/4 – 11/64; you drink 37/64
4. I fill one of them that you didn’t touch
I get 1 + 37/64 (less than 2 liters)
Can set up generous upper bound of (n-1)/n + 1 (in the limit 2)

11
Strategy 2
Strategy 2: I leave the empty cups empty and split over n-1, n-2, …
How much do I get?

Example with n=4


1. I fill evenly; you drink 1/4 liter
2. I fill 3 to 7/12; you drink 7/12
3. I fill 2 to 13/12; you drink 13/12
4. I fill one of them that you didn’t touch
I get 1 + 13/12 (more than 2 liters)
In general, I get 1/n + 1/n-1 + … + 1/2 + 1/1
This is the Harmonic Series Hn = ∑$!"# 𝑖/𝑥
$%#
Hn is approximately ∫# 𝑑𝑥/𝑥 = ln (n+1) – ln 1 = ln (n+1)
ln = loge where e =2.718…
Harmonic Series will arise again in this course, being used to show
some series is logarithmic in nature. The key is that the Harmonic
Series diverges at the same rate as does the natural log 12
Warm-up: Bitsets/Sparse Vectors

Bitsets can be associated with long bit vectors


They are typically made up of 64-bit components
A common use is to store a sparse vectors v (most data values are 0)
Rather than store a bunch of zeros, we only store the non-zero data
in a compressed vector, keeping the relative order in which these
appeared in a bitset representation of the original vector
The bitset has a 1 in position j if v[j] is non-zero
The bitset has a 0 in position j if v[j] is zero
We look up a value first in the bitset; if the corresponding bit is
zero, we return 0
if the corresponding bit is 1, we count the number of 1’s preceding
the j-th bit and that gives us an offset into the compressed vector
of values

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
}

Why does this work? I will provide evidence later.


13
Is it better than popcountA from the previous page?
Counting in an interval

/*
* 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?

Use pairs (i = 1 to n/2)


compare a2i-1 to a2i. Compare larger to max, saving larger;
Now compare smaller to min, saving smaller (3 compares).
If they are same just use either in compares to max and min
Can prove correctness
To prove H[i]. i = 1 ... n/2:
H[i] is max = max a[1,2i]; min = min a[1,2i]
at i =1: We save max a[1,2] in max and max a[1,2] in min, so H[1]
Assume H[k-1], k>1, max = max a[1, 2k-2]; min = min a[1, 2k-2]
H[k]: max = max(max, max a[2k-1 2k]);
min = min(min, min a[2k-1 2k]);
But then max = max a[1,2k]; min = min a[1.2k] by Ind Hyp and 18
so H[n/2] and we have succeeded in 1.5n (3 compares per pair)
Warm-up: heavy vs light

We have a collection of objects, some heavy, some light.


All light are the same weight; all heavy are same weight
There is at least on light and one heavy
How do we divide them into two piles; one light, one heavy
We can compare weights to find out which is heavier

Strategy 1. Pick one as a reference. Compare reference to all other


coins. Those of same weight are in same pile; other go to pile 2.
If any weigh more, reference is light and so are its mates. Others
are heavy. Take T(n) = n-1 compares. We want to 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

You might also like