CSE 241 Class Notes
CSE 241 Class Notes
Jeremy Buhler
http://classes.engineering.wustl.edu/cse241/.
• Who are you? Ask for show of hands: CSE students? Engineering? Arts and
Sciences? Sophomores? CS minors? Second majors?
1. First, everything I’m going to say and more is on the web site.
1
4. We will use Piazza for discussions and help requests. Please create an account if
you have not done so and enroll in 241 to see the discussion boards.
5. TAs and I may not answer help requests sent via email, or we may ask you to repost
via Piazza. With almost 200 people, it will be much easier to keep track if we keep
all discussions centralized.
6. Evaluation: 5 homeworks worth 20%, 4 labs worth 20%, three equally weighted
exams worth 60%. No cumulative final!
7. Turn-ins: no late homeworks accepted! (I’ll try to hand out solutions immediately).
Late labs up to three days, 10% off per day. If no late labs, can redo one and turn
in at end of semester for regrade (cost of 10%).
8. We’ve implemented electronic turn-in for labs using personalized SVN repositories,
similar to 131, and for homeworks using Blackboard. See the web site for instruc-
tions. Lab 0 and Homework 0 are out today, due 9/2, to make sure everyone can
successfully use the infrastructure. (Yes, they are for credit.)
9. Collaboration: I try to balance fun with fairness. No written sharing, “Iron Chef
rule”. Everyone must read policy on class web and sign statement of compliance
with each homework. Also, we plan to do electronic comparisons among turned-in
labs to check for code copying.
• Abstractions: ways of thinking about how to organize data. You already know
some – lists, queues, stacks. We’ll introduce more abstractions – sparse tables, trees,
graphs – that can be realized efficiently.
Let’s start on the first of these three points with an example problem.
• At any time, you must quickly be able to identify the closest pair of planes (points)
on the screen! (E.g., to sound a warning if they’re too close).
2
• Closest pair problem: other uses as well
CheckAll(P)
minDist ← ∞
j←0
while j ≤ n − 2 do
k ←j+1
while k ≤ n − 1 do
d ← distance(P [j], P [k])
if d < minDist
minDist ← d
p1 ← P [j]
p2 ← P [k]
k++
j++
return p1 , p2 , minDist
Does anyone have a better idea or improvement?
[Solicit, use as example of alternative algorithms.]
3
• (Yes, it’s correct – checks every pair, stores pair of min distance.)
• Is it fast? In particular, what if I gave you a different algorithm for closest pair – is
CheckAll faster or slower?
• Code, debug, time execution? On what machine? With what inputs? Will the
planes line up the same way as our test points? (If not, will we be sued for a
crash?!?!?!?)
• We desire machine independence: say something useful about speed that doesn’t
depend on PC versus supercomputer versus smartphone
• We need a worst-case time estimate for each input size: what if Dr. Evil designed
the flight plan? What if we’re unlucky?
Make clear that choosing to consider the worst case is a fundamental
assumption. Compare average case – can be useful if distribution of inputs known,
but only if worst case is not catastrophic!
4
5 Let’s Count Statements for CheckAll
Assume an input array P of size n.
minDist ← ∞
j←0
while j ≤ n − 2 do
k ←j+1
while k ≤ n − 1 do
if d < minDist
minDist ← d
k++
j++
return minDist
n−2
X n−2
X
S(n) = 3 + 3n − 2 + (n − j) + 4 (n − j − 1)
j=0 j=0
= 3n + 1 + (n + n − 1 + . . . + 2) + 4 (n − 1 + n − 2 + . . . + 1)
n(n + 1) (n − 1)n
= 3n + 1 + −1 +4
2 2
5 2 3
= n + n
2 2
Some hints for doing this yourself:
• For each loop in a nested set, write down the number of times its body statements
execute as a function of its explicit bounds.
• (That would be high bound minus low bound plus one, if the interval is closed.)
• (For instance, the inner loop shown runs from k = j + 1 to k = n − 1, so its body
executes n − j − 1 times. Wait until you’ve labeled every statment in this way before
trying to sum over the different values of loop counters like j.)
• If a loop iterates m times, its test is executed m+1 times (including the last failure).
5
• Remember your sequence sums, e.g.
n
X n(n + 1)
i=
2
i=1
• We say that this term determines the worst-case asymptotic time complexity
of the algorithm.
• Hence, we say that running time is “roughly a constant times n2 ,” or, for short,
Θ(n2 )!!!! More on the meaning of Θ next week!
• “Eventually?” For large enough input size n, or, as computer scientists like to say,
in asymptopia (a magical land where only asymptotic complexity matters).
• If you weren’t sure how many planes might show up at the airport, which algorithm
would you rather use?
• In your “ideal language” of choice, be careful that statements take only constant
time, regardless of input size.
6
• Example: Multiplying two matrices of size n × n is written as one line in Matlab but
takes time Θ(n3 )!