3 - Algorithm Analysis
ucla | CS 131 | 2023-10-05 08:03
Table of Contents
Supplemental
- no sudocode or code for algos
- make steps in bullets, NO paragraphs
- don’t make assumptions abt the problem unless stated
Run Time
- the stable matching has complexity
in terms of iterations - But run time
iff. each iteration has a constant run time - We find this by picking apart the steps:
Gale-Shapley Run Time Analysis
Per iteration:
- identify a free student
- if we use a linked list for students, picking first is const time, and if we displace a student, placing them back is const time at front or back
- For each student
find the highest-ranked med school that the student has not approached before
- For each student
- if we use an array (pre-sorted by ranking) for each student, then checking the first element is const. time
- For med
check if it’s paired, and find the student paired with
- For med
- we can store this as a single integer for each med school representing the assigned student, all initialized at 0 at the beginning and once assigned, only switch to another assignment if at all, thus const. time
- Find
’s relative ranking of and $s’
- Find
- if we also give m a priority list of students using a hashmap or a list where the index is the student and the value is the ranking, we can get the ranks and compare in const. time
Election Problem
Problem Statement
candidates, votes (stored as an array of size with possible values being candidates 1,…, )- majority - the number of votes is
either 0 or 1 majorities and might NOT be const (they could be functions of each other)- Find the majority candidate if there exists one
- only use constant additional storage
- algo should run in linear time
Consider problem minimization
- given a list of 11 votes where one of them is 3, and another is 2 (arbitrarily)
- then the majority must have 6 or more votes for that number (e.g., 6 3s)
- now delete those two (3,2) from the list of 11 to make a list of 9 -> This list should still have a majority (if we deleted a majority) e.g. 5 or more 3s
Algo
- create storage one int for majority candidate number, another int for count
- iterate through the votes, and store the
[1,1]
as the ints[maj_cand,count]
- if the next vote is the same as the current
maj_cand
, increase the count, else decrease the count, if count reaches 0 setmaj_cand
- if the count goes from 0 to 1, change the
maj_cand
to whoever changed the count from 0 to 1 - at the end of the first scan, the count means nothing but the
maj_cand
left remaining is the only possiblemaj_cand
possible if count is > 1, else ifmaj_cand
is 0 i.e., count is 0 -> no majority - now do another scan and verify the possible majority candidate
maj_cand
by counting the number of appearances and check - thus, $f(n)=2n=>O(n)