lecture 1asd`a

Upload: theakholic

Post on 03-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Lecture 1asd`a

    1/22

    CS305/503, Spring 2009

    Introduction

    Michael Barnathan

  • 7/28/2019 Lecture 1asd`a

    2/22

    Welcome

    Who I am: Michael Barnathan Monmouth University CS alumnus, now an adjunct professor.

    This is a course I originally taught at Monmouth University during Spring 2009.

    I now donate the slides to the Polymath Foundation in hopes that they will edify additionalstudents. The slides may periodically contain references to a class that no longer exists

    Ill record voiceovers, but only as I have the time. It may take a while. Please be patient.

    What this course will be about: Data Structures Algorithms

    Java

    Prereq: Basic Java and object-oriented programming experience.

    What I expect of you: Its like learning to play an instrument:

    View the lecturesdont miss your lessons. Keep programming on your ownpractice.

    You can eventually learn how to play by practicing without taking lessons (though it will take longer),but youll never learn how if you go to lessons but dont practice!

    Much as I like teaching, this class is offered for your own benefit! Data Structures andAlgorithms form the core of CS. You will use these skills whenever you touch software designin any context, and this is the best opportunity you will ever receive to learn them.

    Your goals: Let them guide your learning.

  • 7/28/2019 Lecture 1asd`a

    3/22

    Syllabus

    Its online.

    If you have any questions about it, feel free to

    email me.

    http://rockhopper.monmouth.edu/~mbarnath/2009/CS305/syllabus.dochttp://rockhopper.monmouth.edu/~mbarnath/2009/CS305/syllabus.doc
  • 7/28/2019 Lecture 1asd`a

    4/22

    Java

    This is the first time this course is being taught in Java (it was previouslytaught in C++).

    Have any of you worked with Java before?

    Because Java may be new to some of you, we will cover it in some detail.

    However: You should also make an effort to learn the language on your own (its fairly

    similar to C++).

    You willbe expected to know the language on the assignments and exams.

    You will also be expected to know how to write, compile, and execute a Javaprogram without using an IDE.

    The systems in this lab are unreliable when it comes to running IDEs. They may not

    always work. Even when they do, you may find yourself struggling with the IDE longerthan it would have taken you to just write the code!

    I recommend learning how to use vim or emacs, as these are very powerful editors, butfailing that, at least know how to use nano (which is like Notepad for the Linux terminal).

    Know how to invoke thejavac andjava commands to compile and run your programs.

  • 7/28/2019 Lecture 1asd`a

    5/22

    What is a Computer Program?

    What is a computer?

    What is a program?

    What are the key components of a program?

    Mustthings be this way?

    Must a computer be a machine?

    Need a program run on a computer?

    Can a program be created while avoiding thosecomponents?

  • 7/28/2019 Lecture 1asd`a

    6/22

    The Class Model

    According to the class, a computer:

    Is a machine (whats a machine?)

    Performs tasks designated by a user.

    Has security in place to protect its integrity.

    And a program:

    Interacts with the computer/user (I/O).

    Consumes system resources.

    Accomplishes a goal.

  • 7/28/2019 Lecture 1asd`a

    7/22

    What is a computer?

    Given infinite time and space, you CAN

    build a computer using rocks*.

    * Warning: do not attempt in a finite universeunless otherwise recommended by a physicist.

    Or neurons

    --xkcd

    Computer systems are actually organized in

    a fairly similar manner to brains.

    http://www.xkcd.com/505/http://www.xkcd.com/505/http://www.xkcd.com/505/http://www.xkcd.com/505/
  • 7/28/2019 Lecture 1asd`a

    8/22

  • 7/28/2019 Lecture 1asd`a

    9/22

    Turing Machines

    Computers can actually be implemented with abstract models that dontexist at all

    One such example is a Turing machine.

    This model consists of: An infinite length oftape(memory, storage)

    A head, which can read or write a single strip of tape and move one strip leftor right at a time.

    A stack, containing instructions and a stack pointer.

    Why implement a fake computer? Because Turing machines are able to perform any calculation a real machine

    can.

    Therefore, things that cannot be done on a Turing machinecant be done on

    real machines either.

    A machine or programming language capable of performing everycomputation that can be performed on a Turing machine is called turingcomplete.

  • 7/28/2019 Lecture 1asd`a

    10/22

    Data Structures and Algorithms:

    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 dont 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 one well discuss today).

  • 7/28/2019 Lecture 1asd`a

    11/22

    What is the difference between an

    algorithm and a program? The differences are slight.

    Programs are implementations of data structures andalgorithms.

    Data structures and algorithms are abstract concepts.

    Just as the Turing machine was an abstract computer.

    Programs are generally directly interpretable, and cansomehow be used directly to solve problems. Very often you will need to translate an algorithm, given

    in pseudocode or by a diagram, into a program. Sometimes youll derive an algorithm but leave the

    implementation to others.

    Often youll be expected to do both!

  • 7/28/2019 Lecture 1asd`a

    12/22

    How do we measure algorithms?

    Correctness: Produces the correct answer given the correct input.

    Halting: Stops after it does what its supposed to.

    Doesnt crash.

    Performance: Time (e.g. CPU time)

    Space (e.g. Memory)

    Development timedont overlook it. Is saving 5 seconds per run worth a week of your time? Unless the

    app. is time-critical, probably not. Complexity:

    Is the solution simple, intuitive, easy to understand?

  • 7/28/2019 Lecture 1asd`a

    13/22

    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 whatseparates good from great.

  • 7/28/2019 Lecture 1asd`a

    14/22

    Measuring Time and Space

    An abstract notion, not usually measured in seconds, but unitsthat only have meaning relative to one another.

    For example, 2 takes twice as long as 1, but we have no idea howlong 1 takes.

    We care only about the order of growth.

    Not how long something takes. Instead, how much longer it takes with more data.

    Well start with Worst-case analysis. Murphys Law: assume that everything that can slow the algorithm

    down will. If your algorithm runs slower when it reads in the number 2, worst-case

    analysis would have you assume every element in the dataset is 2! However unlikely this may be it is the worst case, not the worstprobable

    case.

    The purpose of this is to find out the absolute longest amount of time/ largest amount of space that an algorithm might use.

    Theres also a best-case, which assumes the opposite.

  • 7/28/2019 Lecture 1asd`a

    15/22

    Big-O (Asymptotic) 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 algorithms growth. For example, the function 3n^2 + 10n + 5 is O(n^2).

    We dont 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, theywould still belong to the same complexity class. There is no such thing as O(2n); its just O(n). Constants are not counted.

    Best

    Worst

  • 7/28/2019 Lecture 1asd`a

    16/22

    Big-O visualized

  • 7/28/2019 Lecture 1asd`a

    17/22

    How bad is exponential time?

    This bad:

    O(n log n)

    O(2^n)

  • 7/28/2019 Lecture 1asd`a

    18/22

    Other Symbols

    Big-O is only one of the symbols used in asymptotic analysis.

    Others are and . O represents an upper bound(no worse than).

    represents a lower bound(no better than).

    represents a tight bound(exactly; no better or worse than).

    It is not wrong to say a linear function is O(n2); it just means wedont know enough to refine our estimate. O(n) represents atighter bound and a more complete state of knowledge aboutthe running time of the algorithm. It is wrong to say the function is anything other than (n), however. An

    (n) bound, if correctly proven, will neverchange.

    For the next few weeks, we will use O and interchangeably (manysources do, although its not precise to do so) and will avoid altogether. But do be aware of the difference; well start todistinguish between them later.

  • 7/28/2019 Lecture 1asd`a

    19/22

    Optimality

    Unfortunately, exponential time is not always avoidable.

    Certain problems cant be solved any faster.

    Thus, an optimal solution might not be fast Its just the fastest one that can exist.

    Our subject is the study ofalgorithms: how to solve problemsefficiently.

    The study of how quickly problems can be solved by any algorithmis called complexity theory. We unfortunately wont have time to cover it.

    There is a famous unsolved problem in this field known as P=NP?

    This has to do with whether a certain class of problems whose solutions canbe verified in polynomial time can also be solved in polynomial time.

    If not, these algorithms are all exponential (or worse!)

    Theres a $1 million prize for solving it, but its an extremely difficult question!

  • 7/28/2019 Lecture 1asd`a

    20/22

    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 users guess.

    Repeat until the user guesses correctly.

    How would you do this in Java?

    The answer is in Thursdays notes.

  • 7/28/2019 Lecture 1asd`a

    21/22

    Decomposition of the problem

    It helps to make problems simpler; to iteratively takethem down to the level at which you can implementthem.

    Higher/Lower/Equal translate into if

    comparisons between the number and the guess. Repeat until usually means use a loop.

    Prompt the user means youll be using Javas inputfacilities (hint: look up the Scanner class).

    Random numbers can be generated usingMath.random() or the Random class.

    When in doubt about how to use a class, consult theJava API documentation!

  • 7/28/2019 Lecture 1asd`a

    22/22

    Thats 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.