Top Banner

of 24

c01intro

May 29, 2018

Download

Documents

Abhilash Ks
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/9/2019 c01intro

    1/24

  • 8/9/2019 c01intro

    2/24

    CSE 202 - Intro2

    What well studyGeneral Techniques

    Divide and Conquer, Dynamic Programming, Hashing,

    Greedy Algorithms, Reduction to other problems, ...Specific Problems

    Sorting, sorting, shortest paths, max flow, sorting, ...

    Various Paradigms

    Probabilistic algorithms

    Alternate models of computationNP Completeness

  • 8/9/2019 c01intro

    3/24

    CSE 202 - Intro3

    Sounds like my undergrad course ...

    Going over same material twice is good!

    Well probably go deeper mathematical formalisms

    modified assumptions

    assorted topics every computer scientist shouldknow.

  • 8/9/2019 c01intro

    4/24

    CSE 202 - Intro4

    Logistics Textbook: Introduction to Algorithms, 2ndedition

    Cormen, Leiserson, Rivest & Stein

    First edition probably OK (new chapter K ~ old chapter (K+1).)

    Written work Individual homeworks

    Group homeworks

    In-class Midterm Final (oral? take-home?)

    Grades

    A: Demonstrate mastery of material. B (or B+ or B-): Typical grade. Understand most of material,

    solve most routine problems and some hard ones.

    C: Really dont get it.

    D,F: Gave up.

  • 8/9/2019 c01intro

    5/24

    CSE 202 - Intro5

    Logistics Classes will include lecture (with overheads),

    discussion (at board), and exercises (at your seat).

    Id prefer you pay attention, think, ask questions, andparticipate in discussions rather than taking notes.

    Website: //www.cs.ucsd.edu/classes/fa02/cse202-b

    .pdf and .ps of lectures. (Sometimes available before class.)

    Homeworks, announcements, etc. too You are responsible for checking website

    Be sure to register for the class email list

    Ill use it to send out urgent messages, like HW corrections

    Directions on class webpage

  • 8/9/2019 c01intro

    6/24

  • 8/9/2019 c01intro

    7/24

    CSE 202 - Intro7

    Formal Analysis of Algorithms

    Algorithm: a step-by-step method of producingoutput of instances given input

    The steps are instructions for some model of computation. Standard model is the Random Access Machine (RAM)

    Memory

    ALU

    One needs to be careful about what operations are allowed- E.g. operations on very long numbers arent one step.

    - For this course anything reasonable is OK.

  • 8/9/2019 c01intro

    8/24

  • 8/9/2019 c01intro

    9/24

    CSE 202 - Intro9

    Mathematical notation

    P.T. Barnum asserted, You can fool all of the peoplesome of the time.

    Suppose on Monday, I fooled all the men, and on Tuesday,I fooled all the women (but not vice versa).

    Is this an example of Barnums assertion?

    Mathematics is (or should be) preciseLet P = set of all the people, T = {Monday,Tuesday},

    Let F(p,t) mean I fooled person p at time t

    Is "p e P $t e T F(p,t) true?

    Is $t e T "p e P F(p,t) true?

    " means for all

    $ means there exists

  • 8/9/2019 c01intro

    10/24

    CSE 202 - Intro10

    Classifying (complexity) functionsWe focus on functions from to , though definitions are general.

    Given function g, O(g) (pronounced Oh of g or

    Big Oh of g) is the set of functions (from to )for which g is an asymptotic upper bound. Thismeans:

    O(g) = {f : $n0e $c>0 "n>n0 0 f(n) c g(n) }

    Example: 3n log n + 20 e O(n2)

    Note: Since O(n2) is a set,

    using e is technically correct.

    But people often use = .

  • 8/9/2019 c01intro

    11/24

    CSE 202 - Intro11

    Classifying (complexity) functions

    W(g) (pronounced Omega or Big Omega) is theset of functions for which g is an asymptotic

    lower bound. Formally:W(g) = {f : $n0e $c>0 "n>n0 0 c g(n) f(n) }

    Q(g) (Theta) is the set of functions for which gis an asymptotic tight bound. This means:

    Q(g) = {f : $n0e $c1,c2>0 "n>n0 0 c1g(n) f(n) c2g(n) }

    Theorem: Q(g) = W(g) O(g).

    Note: we wont use little-o or little-omega notation.

  • 8/9/2019 c01intro

    12/24

    CSE 202 - Intro12

    Review

    Consider the decision problem, Is x prime?

    How should we define size?

    Consider the algorithm:if(i

  • 8/9/2019 c01intro

    13/24

    CSE 202 - Intro13

    What is a proof?

    Informal Proof (Hand waving) Anything that convinces the reader

    Formal Proof (First few classes) A sequence of statements, each being:

    a hypothesis of the theorem

    a definition, axiom, or known theorem a statement that follows from previous statements via a

    rule of inference

    Proof (Rest of course) A subset or summary of a formal proof that convinces a

    literate but sleepy reader that the writer could write aformal proof if forced to.

  • 8/9/2019 c01intro

    14/24

    CSE 202 - Intro14

    Notes about proofs

    Use complete sentences. Sentences have a subject (you may be understood), a

    verb, a period at the end.

    If sentence is too long, introduce notation or definitions.

    Each statement should indicate whether its anassumption, a definition, a known fact, ...

    Dont just write, x e A. Instead write:

    Let x e A. if youre introducing x and want it to be in A.

    Suppose x e A. if x has already been introduced, and

    youre seeing what would happen if it were in A. Thus, x e A. if it follows from earlier statements.

    Make sure each variable is properly introduced Like declaring variables in a program.

  • 8/9/2019 c01intro

    15/24

    CSE 202 - Intro15

    Some proof schemas Literate readers even sleepy ones know that:

    To show A B, can write Let x e A. (blah ...). Thus, x e B.

    To show A = B (for sets A and B), show A B and B A.

    To show P implies Q, write Assume P. (blah ...). Thus Q.

    To show $x P, write Let x=(whatever). (blah ...) Thus P.

    To show "x P, write Given any x, (blah ...). Thus P.

    To show an algorithm has complexity O(f), you willprobably construct a positive constant c and an integer n0and then show that if n>n0 and I is an instance of size n,then the algorithm requires time at most c f(n) on I.

    These schemas can be mostly implicit. Thus, after writingLet x e A. (blah blah). Thus, x e B. you often dont needto write This shows that A B.

  • 8/9/2019 c01intro

    16/24

    CSE 202 - Intro16

    Example formal proof

    Thm: Suppose feO(g) and geO(h). Then feO(h).

    Proof:

    Since fe O(g), $n0e $c0>0 "n>n0 0 f(n) c0 g(n).Similarly, $n1e $c1>0 "n>n1 0 g(n) c1 h(n).

    Let n2 = max(n0, n1) and c2 = c0 c1. Note that c2 is positive

    since both c0 and c1 are.Suppose n>n2. Then n>n0 and so 0 f(n) c0 g(n).

    But we also have n>n1 and so g(n) c1 h(n).Thus, 0 f(n) c0 c1 h(n) = c2 h(n).

    Q.E.D.

    Just the definition of O(g), but introduces c0 and n0.

    Setup for proving $n2e $c2>0 ...

    Setup for proving "n>n2 ...

    Means, Weve proved what we intended to.

  • 8/9/2019 c01intro

    17/24

    CSE 202 - Intro17

    Your turn ...

    Thm: If feO(g) then geW(f).

  • 8/9/2019 c01intro

    18/24

    CSE 202 - Intro18

    Mathematical Induction

    Suppose for " ie, Pi is a statement.

    Suppose also that we can prove P0, and we canprove " ie, Pi implies Pi+1

    Then the Principle of Mathematical Induction

    allows us to conclude "

    ie

    Pi.

    P3P0

    P1P2

    P4...

  • 8/9/2019 c01intro

    19/24

    CSE 202 - Intro19

    Mathematical Induction

    Alternatively, suppose we can prove P0, and wecan prove " ie, (P0 & P1 & ... & Pi) implies Pi+1

    Again, the Principle of Mathematical Inductionallows us to conclude " ie Pi.

    P3P0

    P1

    P2

    P4

    ...P5

    P6

    P7

  • 8/9/2019 c01intro

    20/24

    CSE 202 - Intro20

    Induction Example

    Definitions from CLRS pg. 1088:

    A binary tree is a structure on a finite number of

    nodes that either contains no nodes or is composedof three disjoint subsets: a root node, a binarytree called the left subtree and a binary treecalled the right subtree.

    The height of a non-empty tree is the maximumdepth of its nodes. The depth of a node is thelength of the path from the root to the node.

    Thm: If T is a non-empty binary tree of height h,then T has fewer than 2h+1 nodes.

  • 8/9/2019 c01intro

    21/24

    CSE 202 - Intro21

    Induction Example

    Let Ph be the statement, If T is a binary tree ofheight h, then T has at most 2h+11 nodes.

    We will prove " he Ph by induction.

    Base Case (h=0): T is non-empty, so it has a root

    node r. Let s be any node of T. Since the heightof T is 0, the depth of s must be 0, so s = r.Thus, T has only one node (which is 20+1-1 =1).

  • 8/9/2019 c01intro

    22/24

    CSE 202 - Intro22

    Induction Example

    Let Ph be the statement, If T is a binary tree ofheight h, then T has at most 2h+11 nodes.

    We will prove " he Ph by induction.

    Base Case (h=0): T is non-empty, so it has a root

    node r. Let s be any node of T. Since the heightof T is 0, the depth of s must be 0, so s = r.Thus, T has only one node (which is 20+1-1).

    Obviously, the only binary tree of height 0 isthe tree of one node, so P0 is true.

  • 8/9/2019 c01intro

    23/24

    CSE 202 - Intro23

    Induction Example

    Induction step: Assume that Ph is true.

    Let T be a tree of height h+1. Then the left subtree L is a

    binary tree.If L is empty, it has 0 nodes.

    Otherwise, each node in L is has depth one less than its

    depth in T. Thus, L is a non-empty binary tree of depthat most h. By assumption

    OOOPS!Ive only assumed Ph. But L may have smaller height.

  • 8/9/2019 c01intro

    24/24

    CSE 202 - Intro24

    Induction Example

    Induction step: Assume that Pi is true " ih.

    Let T be a tree of height h+1. Then the left subtree L is a

    binary tree.If L is empty, it has 0 nodes.

    Otherwise, each node in L is has depth one less than its

    depth in T. Thus, L is a non-empty binary tree of depthat most h. By assumption, L has at most 2h+11 nodes.

    Similarly, the right subtree R has at most 2h+11 nodes.

    Thus, T has at most 1 + 2h+11 + 2h+11 = 2 2h+11= 2(h+1)+11 nodes.

    This shows that Ph+1 is true, and completes our proof.

    for the root for L for R