CS503: First Lecture, Fall 2008 Michael Barnathan
Welcome Who I am: – Michael Barnathan – First year teaching. – First year the course is being taught in Java. – I’ll do my best, but please do voice any concerns. What this course will be about: – Data Structures – Algorithms – Java Prereq:CS501B or equivalent Java experience. What I expect of you: – It’s like learning to play an instrument: – Come to class – don’t miss your “lessons”. – Keep programming on your own – “practice” Your goals: Let them guide your learning.
Syllabus It’s online.online We may need to vary things based on the small number of students in the class. Any changes will generally work in your favor: – More individual attention. – Less lecturing, more guiding. – Possibly no exams.
What are they? Data structures are models. – They are means of arranging data within your program. – Choosing the right arrangement can make your job easier! Algorithms are recipes. – For solving problems in general: Programming. Cooking. Driving. Solving math problems. Many other areas. – You don’t need a computer to execute them. – In fact, very often you are the one executing them: Following driving directions. Solving puzzles. Interacting with a program (such as the Guessing Game).
How do we measure them? Correctness: – Produces the correct answer given the correct input. “Halting”: – Stops after it does what it’s supposed to. – Doesn’t crash. Performance: – Time (e.g. CPU time) – Space (e.g. Memory) Development time – don’t overlook it. Complexity: – Is the solution simple, intuitive, easy to understand?
Tradeoffs As Computer Scientists, you will make many tradeoffs: – Space vs. time. – Space and time vs. complexity. – Everything vs. dev. time. “Optimization” of an algorithm takes time and effort. Making these tradeoffs wisely is what separates “good” from “great”.
Measuring Time and Space An abstract notion, not usually measured in seconds, but “units” that only have meaning relative to one another. – For example, “2” takes twice as long as “1”, but we have no idea how long “1” takes. We care only about the order of growth. – Not how long something takes. – Instead, how much longer it takes with more data. We’ll start with “Worst-case” analysis. – Murphy’s Law: assume that everything that can slow the algorithm down will. – The purpose of this is to find out the absolute longest amount of time / largest amount of space that an algorithm might use. There’s also a “best-case”, which assumes the opposite.
Big O notation. O(f(n)), where f(n) is some function; e.g.: – O(1)“Constant time” – O(log n)“Logarithmic” – O(n)“Linear” – O(n log n)“n log n” (less common: “Linearithmic”) – O(n^2)“Quadratic” – O(n^p)“Polynomial” – O(2^n)“Exponential” This only indicates the dominant term of the algorithm’s growth. – For example, the function 3n^2 + 10n + 5 is O(n^2). We don’t take constant terms into account. – y =.0001x^2 is worse than y = 10000x. – This is because as x gets large, the x^2 term will dominate. Therefore, even if algorithm B takes twice as long as algorithm A, they would still belong to the same complexity class. – There is no such thing as “O(2n)”; it’s just O(n). Constants are not counted. Best Worst
Big-O visualized
How bad is exponential time? This bad: O(n log n) O(2^n)
Optimality Unfortunately, exponential time is not always avoidable. Certain problems can’t be solved any faster. Thus, an “optimal” solution might not be fast… – It’s just the fastest one that can exist. The study of how quickly problems can be solved is called complexity theory. – We unfortunately won’t have time to cover it. – It will probably be covered in CS512, or you can look into it yourself. – There is a famous unsolved problem in this field known as “P=NP?” There’s a $1 million prize for solving it. But that’s because it’s an extremely difficult question.
The Guessing Game: Writing a simple Java program. Problem: – Guess a random number from 1 to 100. – Prompt the user to input guesses. – Print out whether the number is actually higher, lower, or equal to the user’s guess. – Repeat until the user guesses correctly. How would you do this in Java? – The answer is in Thursday’s notes.
Decomposition of the problem It helps to make problems simpler; to iteratively take them down to the level at which you can implement them. “Higher”/“Lower”/“Equal” translate into “if” comparisons between the number and the guess. “Repeat until…” usually means “use a loop”. “Prompt the user” means you’ll be using Java’s input facilities (hint: look up the Scanner class). Random numbers can be generated using Math.random(). When in doubt about how to use a class, consult the Java API documentation!
That’s all for now! The lesson: – Algorithmic thought exists outside of computer science. You can apply what you will learn in this class in many unexpected places. Next class: The guessing game, review of asymptotic analysis, arrays. We are half a class ahead of schedule, so we have more time to review. – If we can keep this up, we can explore some really interesting topics towards the end.