Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices - PowerPoint PPT Presentation

1 / 138
About This Presentation
Title:

Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices

Description:

The Fundamentals: Algorithms, the Integers, and Matrices (c) ... Definiteness. Precisely defined. Correctness. Outputs correctly relate to inputs. Finiteness. ... – PowerPoint PPT presentation

Number of Views:1390
Avg rating:3.0/5.0
Slides: 139
Provided by: Michael1807
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices


1
Chapter 3The Fundamentals Algorithms, the
Integers, and Matrices
2
3.1 Algorithms
3.1 Algorithms
3
Algorithms
  • The foundation of computer programming.
  • Most generally, an algorithm just means a
    definite procedure for performing some sort of
    task.
  • A computer program is simply a description of an
    algorithm in a language precise enough for a
    computer to understand, requiring only operations
    the computer already knows how to do.
  • We say that a program implements (or is an
    implementation of) its algorithm.

3.1 Algorithms
4
Programming Languages
  • Some common programming languages
  • Newer Java, C, C, Visual Basic, JavaScript,
    Perl, Tcl, Pascal
  • Older Fortran, Cobol, Lisp, Basic
  • Assembly languages, for low-level coding.
  • In this class we will use an informal,
    Pascal-like pseudo-code language.
  • You should know at least 1 real language!

3.1 Algorithms
5
Algorithm Example (English)
  • Task Given a sequence aia1,,an, ai?N, say
    what its largest element is.
  • Set the value of a temporary variable v (largest
    element seen so far) to a1s value.
  • Look at the next element ai in the sequence.
  • If aigtv, then re-assign v to the number ai.
  • Repeat previous 2 steps until there are no more
    elements in the sequence, return v.

3.1 Algorithms
6
Executing an Algorithm
  • When you start up a piece of software, we say the
    program or its algorithm are being run or
    executed by the computer.
  • Given a description of an algorithm, you can also
    execute it by hand, by working through all of its
    steps on paper.
  • Before WWII, computer meant a person whose job
    was to run algorithms!

3.1 Algorithms
7
Executing the Max algorithm
  • Let ai7,12,3,15,8. Find its maximum
  • Set v a1 7.
  • Look at next element a2 12.
  • Is a2gtv? Yes, so change v to 12.
  • Look at next element a2 3.
  • Is 3gt12? No, leave v alone.
  • Is 15gt12? Yes, v15

3.1 Algorithms
8
Algorithm Characteristics
  • Some important features of algorithms
  • Input. Information or data that comes in.
  • Output. Information or data that goes out.
  • Definiteness. Precisely defined.
  • Correctness. Outputs correctly relate to inputs.
  • Finiteness. Wont take forever to describe or
    run.
  • Effectiveness. Individual steps are all do-able.
  • Generality. Works for many possible inputs.
  • Efficiency. Takes little time memory to run.

3.1 Algorithms
9
Our Pseudocode Language A2
  • procedurename(argument type)
  • variable expression
  • informal statement
  • begin statements end
  • comment
  • if condition then statement else statement
  • for variable initial value to final value
    statement
  • while condition statement
  • procname(arguments)
  • Not defined in book
  • return expression

3.1 Algorithms
10
procedure procname(arg type)
  • Declares that the following text defines a
    procedure named procname that takes inputs
    (arguments) named arg which are data objects of
    the type type.
  • Exampleprocedure maximum(L list of
    integers) statements defining maximum

3.1 Algorithms
11
variable expression
  • An assignment statement evaluates the expression
    expression, then reassigns the variable variable
    to the value that results.
  • Examplev 3x7 (If x is 2, changes v
    to 13.)
  • In pseudocode (but not real code), the expression
    might be informal
  • x the largest integer in the list L

3.1 Algorithms
12
Informal statement
  • Sometimes we may write a statement as an informal
    English imperative, if the meaning is still clear
    and precise swap x and y
  • Keep in mind that real programming languages
    never allow this.
  • When we ask for an algorithm to do so-and-so,
    writing Do so-and-so isnt enough!
  • Break down algorithm into detailed steps.

3.1 Algorithms
13
begin statements end
  • Groups a sequence of statements togetherbegin
    statement 1 statement 2 statement
    n end
  • Allows sequence to be used like a single
    statement.
  • Might be used
  • After a procedure declaration.
  • In an if statement after then or else.
  • In the body of a for or while loop.

3.1 Algorithms
14
comment
  • Not executed (does nothing).
  • Natural-language text explaining some aspect of
    the procedure to human readers.
  • Also called a remark in some real programming
    languages.
  • Example
  • Note that v is the largest integer seen so far.

3.1 Algorithms
15
if condition then statement
  • Evaluate the propositional expression condition.
  • If the resulting truth value is true, then
    execute the statement statement otherwise, just
    skip on ahead to the next statement.
  • Variant if cond then stmt1 else stmt2Like
    before, but iff truth value is false, executes
    stmt2.

3.1 Algorithms
16
while condition statement
  • Evaluate the propositional expression condition.
  • If the resulting value is true, then execute
    statement.
  • Continue repeating the above two actions over and
    over until finally the condition evaluates to
    false then go on to the next statement.

3.1 Algorithms
17
while condition statement
  • Also equivalent to infinite nested ifs, like so
    if condition begin statement
    if condition begin
    statement (continue
    infinite nested ifs) end end

3.1 Algorithms
18
for var initial to final stmt
  • Initial is an integer expression.
  • Final is another integer expression.
  • Repeatedly execute stmt, first with variable var
    initial, then with var initial1, then with
    var initial2, etc., then finally with var
    final.
  • What happens if stmt changes the value that
    initial or final evaluates to?

3.1 Algorithms
19
for var initial to final stmt
  • For can be exactly defined in terms of while,
    like so

begin var initial while var ? final
begin stmt
var var 1 endend
3.1 Algorithms
20
procedure(argument)
  • A procedure call statement invokes the named
    procedure, giving it as its input the value of
    the argument expression.
  • Various real programming languages refer to
    procedures as functions (since the procedure call
    notation works similarly to function application
    f(x)), or as subroutines, subprograms, or methods.

3.1 Algorithms
21
Max procedure in pseudocode
  • procedure max(a1, a2, , an integers)
  • v a1 largest element so far
  • for i 2 to n go thru rest of elems
  • if ai gt v then v ai found
    bigger?
  • at this point vs value is the same as
    the largest integer in the list
  • return v

3.1 Algorithms
22
Another example task
  • Problem of searching an ordered list.
  • Given a list L of n elements that are sorted into
    a definite order (e.g., numeric, alphabetical),
  • And given a particular element x,
  • Determine whether x appears in the list,
  • and if so, return its index (position) in the
    list.
  • Problem occurs often in many contexts.
  • Lets find an efficient algorithm!

3.1 Algorithms
23
Search alg. 1 Linear Search
  • procedure linear search(x integer, a1, a2, ,
    an distinct integers)i 1while (i ? n ? x ?
    ai) i i 1if i ? n then location ielse
    location 0return location index or 0 if not
    found

3.1 Algorithms
24
Search alg. 2 Binary Search
  • Basic idea On each step, look at the middle
    element of the remaining list to eliminate half
    of it, and quickly zero in on the desired element.

ltx
gtx
ltx
ltx
3.1 Algorithms
25
Search alg. 2 Binary Search
  • procedure binary search(xinteger, a1, a2, ,
    an distinct integers) i 1 left endpoint of
    search intervalj n right endpoint of
    search intervalwhile iltj begin while
    interval has gt1 item m ?(ij)/2?
    midpoint if xgtam then i m1 else j
    mendif x ai then location i else location
    0return location

3.1 Algorithms
26
Sorting alg. Bubble Sort
  • procedure bubblesort(a1, a2, , an)for i 1
    to n-1 for j 1 to n-i
  • if ajgtaj1 then interchange aj and
    aj1
  • a1, a2, , an is in increasing order

3.1 Algorithms
27
Sorting alg. Insertion Sort
  • procedure insertionsort(a1, a2, , an)
  • for j 2 to n
  • begin i 1
  • while aj gt ai
  • i i1
  • m aj
  • for k 0 to j-i-1
  • aj-k aj-k-1
  • ai m
  • end a1, a2, , an are sorted

3.1 Algorithms
28
Greedy Change-Making Alg.
  • procedure change(c1, c2, , cr c1gt c2gt gt crn)
  • for i 1 to r
  • while n ? ci
  • begin
  • add a coin with value ci to the
    change
  • n n- ci
  • end

3.1 Algorithms
29
Practice exercises
  • 3.1.3 Devise an algorithm that finds the sum of
    all the integers in a list. 2 min
  • procedure sum(a1, a2, , an integers) s 0
    sum of elems so far for i 1 to n go
    thru all elems s s ai add current
    item at this point s is the sum of all
    items return s

3.1 Algorithms
30
3.2 The Growth of Functions
3.2 The Growth of Functions
31
Orders of Growth
  • For functions over numbers, we often need to know
    a rough measure of how fast a function grows.
  • If f(x) is faster growing than g(x), then f(x)
    always eventually becomes larger than g(x) in the
    limit (for large enough values of x).
  • Useful in engineering for showing that one design
    scales better or worse than another.

3.2 The Growth of Functions
32
Orders of Growth - Motivation
  • Suppose you are designing a web site to process
    user data (e.g., financial records).
  • Suppose database program A takes fA(n)30n8
    microseconds to process any n records, while
    program B takes fB(n)n21 microseconds to
    process the n records.
  • Which program do you choose, knowing youll want
    to support millions of users?

3.2 The Growth of Functions
33
Visualizing Orders of Growth
  • On a graph, asyou go to theright, a
    fastergrowingfunctioneventuallybecomeslarger.
    ..

fA(n)30n8
Value of function ?
fB(n)n21
Increasing n ?
3.2 The Growth of Functions
34
Concept of order of growth
  • We say fA(n)30n8 is order n, or O(n). It is,
    at most, roughly proportional to n.
  • fB(n)n21 is order n2, or O(n2). It is roughly
    proportional to n2.
  • Any O(n2) function is faster-growing than any
    O(n) function.
  • For large numbers of user records, the O(n2)
    function will always take more time.

3.2 The Growth of Functions
35
Definition O(g), at most order g
  • Let g be any function R?R.
  • Define at most order g, written O(g), to be
    fR?R ?c,k ?xgtk f(x) ? cg(x).
  • Beyond some point k, function f is at most a
    constant c times g (i.e., proportional to g).
  • f is at most order g, or f is O(g), or
    fO(g) all just mean that f?O(g).
  • Sometimes the phrase at most is omitted.

3.2 The Growth of Functions
36
Points about the definition
  • Note that f is O(g) so long as any values of c
    and k exist that satisfy the definition.
  • But The particular c, k, values that make the
    statement true are not unique Any larger value
    of c and/or k will also work.
  • You are not required to find the smallest c and k
    values that work. (Indeed, in some cases, there
    may be no smallest values!)

However, you should prove that the values you
choose do work.
3.2 The Growth of Functions
37
Big-O Proof Examples
  • Show that 30n8 is O(n).
  • Show ?c, k ?ngtk 30n8 ? cn.
  • Show that n21 is O(n2).
  • Show ?c, k ?ngtk n21 ? cn2.

3.2 The Growth of Functions
38
Big-O example, graphically
  • Note 30n8 isntless than nanywhere (ngt0).
  • It isnt evenless than 31neverywhere.
  • But it is less than31n everywhere tothe right
    of n8.

30n8
30n8?O(n)
Value of function ?
n
Increasing n ?
3.2 The Growth of Functions
39
Useful Facts about Big O
  • Big O, as a relation, is transitive f?O(g) ?
    g?O(h) ? f?O(h)
  • O with constant multiples, roots, and logs...? f
    (in ?(1)) constants a,b?R, with b?0, af, f
    1-b, and (logb f)a are all O(f).
  • Sums of functionsIf g?O(f) and h?O(f), then
    gh?O(f).

3.2 The Growth of Functions
40
More Big-O facts
  • ?cgt0, O(cf)O(fc)O(f?c)O(f)
  • f1?O(g1) ? f2?O(g2) ?
  • f1 f2 ?O(g1g2)
  • f1f2 ?O(g1g2) O(max(g1,g2))
    O(g1) if g2?O(g1) (Very useful!)

3.2 The Growth of Functions
41
Orders of Growth (3.2) - So Far
  • For any gR?R, at most order g,O(g) ? fR?R
    ?c,k ?xgtk f(x) ? cg(x).
  • Often, one deals only with positive functions and
    can ignore absolute value symbols.
  • f?O(g) often written f is O(g)or fO(g).
  • The latter form is an instance of a more general
    convention...

3.2 The Growth of Functions
42
Order-of-Growth Expressions
  • O(f) when used as a term in an arithmetic
    expression means some function f such that
    f?O(f).
  • E.g. x2O(x) means x2 plus some function
    that is O(x).
  • Formally, you can think of any such expression as
    denoting a set of functions x2O(x) ? g
    ?f?O(x) g(x) x2f(x)

3.2 The Growth of Functions
43
Order of Growth Equations
  • Suppose E1 and E2 are order-of-growth expressions
    corresponding to the sets of functions S and T,
    respectively.
  • Then the equation E1E2 really means
    ?f?S, ?g?T fgor simply S?T.
  • Example x2 O(x) O(x2) means ?f?O(x)
    ?g?O(x2) x2f(x)g(x)

3.2 The Growth of Functions
44
Useful Facts about Big O
  • ? f,g constants a,b?R, with b?0,
  • af O(f) (e.g. 3x2 O(x2))
  • fO(f) O(f) (e.g. x2x O(x2))
  • Also, if f?(1) (at least order 1), then
  • f1-b O(f) (e.g. x?1 O(x))
  • (logb f)a O(f). (e.g. log x O(x))
  • gO(fg) (e.g. x O(x log x))
  • fg ? O(g) (e.g. x log x ? O(x))
  • aO(f) (e.g. 3 O(x))

3.2 The Growth of Functions
45
Definition ?(g), exactly order g
  • If f?O(g) and g?O(f) then we say g and f are of
    the same order or f is (exactly) order g and
    write f??(g).
  • Another equivalent definition?(g) ? fR?R
    ?c1c2k ?xgtk c1g(x)?f(x)?c2g(x)
  • Everywhere beyond some point k, f(x) lies in
    between two multiples of g(x).

3.2 The Growth of Functions
46
Rules for ?
  • Mostly like rules for O( ), except
  • ? f,ggt0 constants a,b?R, with bgt0, af ? ?(f),
    but ? Same as with O. f ? ?(fg)
    unless g?(1) ? Unlike O.f 1-b ? ?(f), and
    ? Unlike with O. (logb f)c ? ?(f).
    ? Unlike with O.
  • The functions in the latter two cases we say are
    strictly of lower order than ?(f).

3.2 The Growth of Functions
47
? example
  • Determine whether
  • Solution

3.2 The Growth of Functions
48
Other Order-of-Growth Relations
  • ?(g) f g?O(f)The functions that are at
    least order g.
  • o(g) f ?cgt0 ?k ?xgtk f(x) lt cg(x)The
    functions that are strictly lower order than g.
    o(g) ? O(g) ? ?(g).
  • ?(g) f ?cgt0 ?k ?xgtk cg(x) lt f(x)The
    functions that are strictly higher order than g.
    ?(g) ? ?(g) ? ?(g).

3.2 The Growth of Functions
49
Relations Between the Relations
  • Subset relations between order-of-growth sets.

R?R
?( f )
O( f )
f
?( f )
?( f )
o( f )
3.2 The Growth of Functions
50
Why o(f )?O(x)??(x)
  • A function that is O(x), but neither o(x) nor
    ?(x)

3.2 The Growth of Functions
51
Strict Ordering of Functions
  • Temporarily lets write f?g to mean f?o(g),
    fg to mean
    f??(g)
  • Note that
  • Let kgt1. Then the following are true1 ? log
    log n ? log n logk n ? logk n ? n1/k ? n ? n
    log n ? nk ? kn ? n! ? nn

3.2 The Growth of Functions
52
Review Growth of Functions (3.2)
  • Definitions of order-of-growth sets, ?gR?R
  • O(g) ? f ? cgt0 ?k ?xgtk f(x) lt cg(x)
  • o(g) ? f ?cgt0 ?k ?xgtk f(x) lt cg(x)
  • ?(g) ? f g?O(f)
  • ?(g) ? f g?o(f)
  • ?(g) ? O(g) ? ?(g)

3.2 The Growth of Functions
53
3.3 Complexity of Algorithms
3.3 Complexity of Algorithms
54
What is complexity?
  • The word complexity has a variety of technical
    meanings in different fields.
  • There is a field of complex systems, which
    studies complicated, difficult-to-analyze
    non-linear and chaotic natural artificial
    systems.
  • Another concept Informational complexity the
    amount of information needed to completely
    describe an object. (An active research field.)
  • We will study algorithmic complexity.

3.3 Complexity of Algorithms
55
Algorithmic Complexity
  • The algorithmic complexity of a computation is
    some measure of how difficult it is to perform
    the computation.
  • Measures some aspect of cost of computation (in a
    general sense of cost).
  • Common complexity measures
  • Time complexity of ops or steps required
  • Space complexity of memory bits reqd

3.3 Complexity of Algorithms
56
An aside...
  • Another, increasingly important measure of
    complexity for computing is energy complexity -
    How much total energy is used in performing the
    computation.
  • Motivations Battery life, electricity cost...
  • I develop reversible circuits algorithms that
    recycle energy, trading off energy complexity for
    spacetime complexity.

3.3 Complexity of Algorithms
57
Complexity Depends on Input
  • Most algorithms have different complexities for
    inputs of different sizes. (E.g. searching a
    long list takes more time than searching a short
    one.)
  • Therefore, complexity is usually expressed as a
    function of input length.
  • This function usually gives the complexity for
    the worst-case input of any given length.

3.3 Complexity of Algorithms
58
Complexity Orders of Growth
  • Suppose algorithm A has worst-case time
    complexity (w.c.t.c., or just time) f(n) for
    inputs of length n, while algorithm B (for the
    same task) takes time g(n).
  • Suppose that f??(g), also written .
  • Which algorithm will be fastest on all
    sufficiently-large, worst-case inputs?

3.3 Complexity of Algorithms
59
Example 1 Max algorithm
  • Problem Find the simplest form of the exact
    order of growth (?) of the worst-case time
    complexity (w.c.t.c.) of the max algorithm,
    assuming that each line of code takes some
    constant time every time it is executed (with
    possibly different times for different lines of
    code).

3.3 Complexity of Algorithms
60
Complexity analysis of max
  • procedure max(a1, a2, , an integers)
  • v a1 t1
  • for i 2 to n t2
  • if ai gt v then v ai t3
  • return v t4
  • Whats an expression for the exact total
    worst-case time? (Not its order of growth.)

3.3 Complexity of Algorithms
61
Complexity analysis, cont.
  • procedure max(a1, a2, , an integers)
  • v a1 t1
  • for i 2 to n t2
  • if ai gt v then v ai t3
  • return v t4
  • w.c.t.c.

Times for each execution of each line.
3.3 Complexity of Algorithms
62
Complexity analysis, cont.
  • Now, what is the simplest form of the exact (?)
    order of growth of t(n)?

3.3 Complexity of Algorithms
63
Example 2 Linear Search
  • procedure linear search (x integer, a1, a2, ,
    an distinct integers)i 1 t1while (i ? n
    ? x ? ai) t2 i i 1 t3 if i ? n then
    location i t4 else location 0 t5
    return location t6

3.3 Complexity of Algorithms
64
Linear search analysis
  • Worst case time complexity order
  • Best case
  • Average case, if item is present

3.3 Complexity of Algorithms
65
Review 3.3 Complexity
  • Algorithmic complexity cost of computation.
  • Focus on time complexity (space energy are also
    important.)
  • Characterize complexity as a function of input
    size Worst-case, best-case, average-case.
  • Use orders of growth notation to concisely
    summarize growth properties of complexity fns.

3.3 Complexity of Algorithms
66
Example 3 Binary Search
  • procedure binary search (xinteger, a1, a2, ,
    an distinct integers) i 1 j nwhile iltj
    begin m ?(ij)/2? if xgtam then i m1 else
    j mendif x ai then location i else
    location 0return location

Key questionHow many loop iterations?
?(1)
?(1)
?(1)
3.3 Complexity of Algorithms
67
Binary search analysis
  • Suppose n2k.
  • Original range from i1 to jn contains n elems.
  • Each iteration Size j?i1 of range is cut in
    half.
  • Loop terminates when size of range is 120 (ij).
  • Therefore, number of iterations is k log2n
    ?(log2 n) ?(log n)
  • Even for n?2k (not an integral power of 2),time
    complexity is still ?(log2 n) ?(log n).

3.3 Complexity of Algorithms
68
Names for some orders of growth
  • ?(1) Constant
  • ?(logc n) Logarithmic (same order ?c)
  • ?(logc n) Polylogarithmic
  • ?(n) Linear
  • ?(nc) Polynomial
  • ?(cn), cgt1 Exponential
  • ?(n!) Factorial

(With ca constant.)
3.3 Complexity of Algorithms
69
Problem Complexity
  • The complexity of a computational problem or task
    is (the order of growth of) the complexity of the
    algorithm with the lowest order of growth of
    complexity for solving that problem or performing
    that task.
  • E.g. the problem of searching an ordered list has
    at most logarithmic time complexity. (Complexity
    is O(log n).)

3.3 Complexity of Algorithms
70
Tractable vs. intractable
  • A problem or algorithm with at most polynomial
    time complexity is considered tractable (or
    feasible). P is the set of all tractable
    problems.
  • A problem or algorithm that has more than
    polynomial complexity is considered intractable
    (or infeasible).
  • Note that n1,000,000 is technically tractable,
    but really impossible. nlog log log n is
    technically intractable, but easy. Such cases
    are rare though.

3.3 Complexity of Algorithms
71
Unsolvable problems
  • Turing discovered in the 1930s that there are
    problems unsolvable by any algorithm.
  • Or equivalently, there are undecidable yes/no
    questions, and uncomputable functions.
  • Example the halting problem.
  • Given an arbitrary algorithm and its input, will
    that algorithm eventually halt, or will it
    continue forever in an infinite loop?

3.3 Complexity of Algorithms
72
P vs. NP
  • NP is the set of problems for which there exists
    a tractable algorithm for checking solutions to
    see if they are correct.
  • We know P?NP, but the most famous unproven
    conjecture in computer science is that this
    inclusion is proper (i.e., that P?NP rather than
    PNP).
  • Whoever first proves it will be famous!

3.3 Complexity of Algorithms
73
Computer Time Examples
(125 kB)
(1.25 bytes)
  • Assume time 1 ns (10?9 second) per op, problem
    size n bits, ops a function of n as shown.

3.3 Complexity of Algorithms
74
Things to Know
  • Definitions of algorithmic complexity, time
    complexity, worst-case complexity names of
    orders of growth of complexity.
  • How to analyze the worst case, best case, or
    average case order of growth of time complexity
    for simple algorithms.

3.3 Complexity of Algorithms
75
3.4 The Integers and Division
3.4 The Integers and Division
76
The Integers and Division
  • Of course you already know what the integers are,
    and what division is
  • But There are some specific notations,
    terminology, and theorems associated with these
    concepts which you may not know.
  • These form the basics of number theory.
  • Vital in many important algorithms today (hash
    functions, cryptography, digital signatures).

3.4 The Integers and Division
77
Divides, Factor, Multiple
  • Let a,b?Z with a?0.
  • ab ? a divides b ? ?c?Z bacThere is an
    integer c such that c times a equals b.
  • Example 3??12 ? True, but 3?7 ? False.
  • Iff a divides b, then we say a is a factor or a
    divisor of b, and b is a multiple of a.
  • b is even 2b. Is 0 even? Is -4?

3.4 The Integers and Division
78
Facts re the Divides Relation
  • ?a,b,c ? Z
  • 1. a0
  • 2. (ab ? ac) ? a (b c)
  • 3. ab ? abc
  • 4. (ab ? bc) ? ac
  • Proof of (2) ab means there is an s such that
    bas, and ac means that there is a t such that
    cat, so bc asat a(st), so a(bc) also.

3.4 The Integers and Division
79
More Detailed Version of Proof
  • Show ?a,b,c ? Z (ab ? ac) ? a (b c).
  • Let a, b, c be any integers such that ab and
    ac, and show that a (b c).
  • By defn. of , we know ?s bas, and ?t cat.
    Let s, t, be such integers.
  • Then bc as at a(st), so ?u bcau,
    namely ust. Thus a(bc).

3.4 The Integers and Division
80
The Division Algorithm
  • Really just a theorem, not an algorithm
  • The name is used here for historical reasons.
  • For any integer dividend a and divisor d?0, there
    is a unique integer quotient q and remainder r?N
    ? a dq r and 0 ? r lt d.
  • ?a,d?Z, dgt0 ?!q,r?Z 0?rltd, adqr.
  • We can find q and r by q?a?d?, ra?qd.

(such that)
3.4 The Integers and Division
81
The mod operator
  • An integer division remainder operator.
  • Let a,d?Z with dgt1. Then a mod d denotes the
    remainder r from the division algorithm with
    dividend a and divisor d i.e. the remainder when
    a is divided by d. (Using e.g. long division.)
  • We can compute (a mod d) by a ? d?a/d?.
  • In C programming language, mod.

3.4 The Integers and Division
82
Modular Congruence
  • Let Zn?Z ngt0, the positive integers.
  • Let a,b?Z, m?Z.
  • Then a is congruent to b modulo m, written a?b
    (mod m), iff m a?b .
  • Also equivalent to (a?b) mod m 0.
  • (Note this is a different use of ? than the
    meaning is defined as Ive used before.)

3.4 The Integers and Division
83
Spiral Visualization of mod

Example shownmodulo-5arithmetic
0(mod 5)
20
15
1(mod 5)
10
4(mod 5)
21
5
19
14
16
9
11
0
4
6
1
3
2
8
7
13
12
18
17
2(mod 5)
22
3(mod 5)
3.4 The Integers and Division
84
Useful Congruence Theorems
  • Let a,b?Z, m?Z. Then a?b (mod m) ? ?k?Z
    abkm.
  • Let a,b,c,d?Z, m?Z. Then if a?b (mod m) and
    c?d (mod m), then
  • ? ac ? bd (mod m), and
  • ? ac ? bd (mod m)

3.4 The Integers and Division
85
Applications of Congruences
  • Hash Function h(k)k mod m a?b (mod m) ? ?k?Z
    abkm.
  • Let a,b,c,d?Z, m?Z. Then if a?b (mod m) and
    c?d (mod m), then
  • ? ac ? bd (mod m), and
  • ? ac ? bd (mod m)

3.4 The Integers and Division
86
3.5 Primes and Greatest Common Divisors
3.5 Primes and Greatest Common Divisors
87
Prime Numbers
  • An integer pgt1 is prime iff it is not the product
    of any two integers greater than 1 pgt1 ?
    ??a,b?N agt1, bgt1, abp.
  • The only positive factors of a prime p are 1 and
    p itself. Some primes 2,3,5,7,11,13...
  • Non-prime integers greater than 1 are called
    composite, because they can be composed by
    multiplying two integers greater than 1.

3.5 Primes and Greatest Common Divisors
88
Fundamental Theorem of Arithmetic
  • Every positive integer has a unique
    representation as the product of a non-decreasing
    series of zero or more primes.
  • 1 (product of empty series) 1
  • 2 2 (product of series with one element 2)
  • 4 22 (product of series 2,2)
  • 2000 2222555 2001 32329
  • 2002 271113 2003 2003

3.5 Primes and Greatest Common Divisors
89
An Application of Primes
  • When you visit a secure web site (https
    address, indicated by padlock icon in IE, key
    icon in Netscape), the browser and web site may
    be using a technology called RSA encryption.
  • This public-key cryptography scheme involves
    exchanging public keys containing the product pq
    of two random large primes p and q (a private
    key) which must be kept secret by a given party.
  • So, the security of your day-to-day web
    transactions depends critically on the fact that
    all known factoring algorithms are intractable!
  • Note There is a tractable quantum algorithm for
    factoring so if we can ever build big quantum
    computers, RSA will be insecure.

3.5 Primes and Greatest Common Divisors
90
Greatest Common Divisor
  • The greatest common divisor gcd(a,b) of integers
    a,b (not both 0) is the largest (most positive)
    integer d that is a divisor both of a and of b.
  • d gcd(a,b) max(d da ? db) ? da ? db ?
    ?e?Z, (ea ? eb) ? d e
  • Example gcd(24,36)?Positive common divisors
    1,2,3,4,6,12Greatest is 12.

3.5 Primes and Greatest Common Divisors
91
GCD shortcut
  • If the prime factorizations are written as
    and
    ,then the GCD is given by
  • Example
  • a842237 223171
  • b96222223 253170
  • gcd(84,96)

3.5 Primes and Greatest Common Divisors
92
Relative Primality
  • Integers a and b are called relatively prime or
    coprime iff their gcd 1.
  • Example Neither 21 and 10 are prime, but they
    are coprime. 2137 and 1025, so they have no
    common factors gt 1, so their gcd 1.
  • A set of integers a1,a2, is (pairwise)
    relatively prime if all pairs ai, aj, i?j, are
    relatively prime.

3.5 Primes and Greatest Common Divisors
93
Least Common Multiple
  • lcm(a,b) of positive integers a, b, is the
    smallest positive integer that is a multiple both
    of a and of b. E.g. lcm(6,10)30
  • m lcm(a,b) min(m am ? bm) ? am ? bm
    ? ?n?Z (an ? bn) ? (m n)
  • If the prime factorizations are written as
    and , then the
    LCM is given by

3.5 Primes and Greatest Common Divisors
94
3.6 Integers and Algorithms
3.6 Integers and Algorithms
95
Integers Algorithms
  • Topics
  • Euclidean algorithm for finding GCDs.
  • Base-b representations of integers.
  • Especially binary, hexadecimal, octal.
  • Also Twos complement representation of negative
    numbers.
  • Algorithms for computer arithmetic
  • Binary addition, multiplication, division.

3.6 Integers and Algorithms
96
Euclids Algorithm for GCD
  • Finding GCDs by comparing prime factorizations
    can be difficult if the prime factors are
    unknown.
  • Euclid discovered For all integers a, b, gcd(a,
    b) gcd((a mod b), b).
  • Sort a,b so that agtb, and then (given bgt1) (a
    mod b) lt a, so problem is simplified.

Euclid of Alexandria325-265 B.C.
3.6 Integers and Algorithms
97
Euclids Algorithm Example
  • gcd(372,164) gcd(372 mod 164, 164).
  • 372 mod 164 372?164?372/164? 372?1642
    372?328 44.
  • gcd(164,44) gcd(164 mod 44, 44).
  • 164 mod 44 164?44?164/44? 164?443 164?132
    32.
  • gcd(44,32) gcd(44 mod 32, 32) gcd(12, 32)
    gcd(32 mod 12, 12) gcd(8,12) gcd(12 mod 8, 8)
    gcd(4,8) gcd(8 mod 4, 4) gcd(0,4) 4.

3.6 Integers and Algorithms
98
Euclids Algorithm Pseudocode
  • procedure gcd(a, b positive integers)
  • while b ? 0
  • r a mod b a b b r
  • return a

Sorting inputs not needed b/c order will be
reversed each iteration.
Fast! Number of while loop iterationsturns out
to be O(log(max(a,b))).
3.6 Integers and Algorithms
99
Base-b number systems
  • Ordinarily we write base-10 representations of
    numbers (using digits 0-9).
  • 10 isnt special any base bgt1 will work.
  • For any positive integers n,b there is a unique
    sequence ak ak-1 a1a0 of digits ailtb such that

See module 12 for summation notation.
3.6 Integers and Algorithms
100
Particular Bases of Interest
  • Base b10 (decimal)10 digits
    0,1,2,3,4,5,6,7,8,9.
  • Base b2 (binary)2 digits 0,1. (Bitsbinary
    digits.)
  • Base b8 (octal)8 digits 0,1,2,3,4,5,6,7.
  • Base b16 (hexadecimal)16 digits
    0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Hex digits give groups of 4 bits
3.6 Integers and Algorithms
101
Converting to Base b
  • (Algorithm, informally stated)
  • To convert any integer n to any base bgt1
  • To find the value of the rightmost (lowest-order)
    digit, simply compute n mod b.
  • Now replace n with the quotient ?n/b?.
  • Repeat above two steps to find subsequent digits,
    until n is gone (0).

Exercise for student Write this out in
pseudocode
3.6 Integers and Algorithms
102
Addition of Binary Numbers
  • procedure add(an-1a0, bn-1b0 binary
    representations of non-negative integers a,b)
  • carry 0
  • for bitIndex 0 to n-1 go through bits
  • bitSum abitIndexbbitIndexcarry 2-bit
    sum
  • sbitIndex bitSum mod 2 low bit of
    sum
  • carry ?bitSum / 2? high bit of sum
  • sn carry
  • return sns0 binary representation of integer s

3.6 Integers and Algorithms
103
Twos Complement
  • In binary, negative numbers can be conveniently
    represented using twos complement notation.
  • In this scheme, a string of n bits can represent
    any integer i such that -2n-1 i lt 2n-1.
  • The bit in the highest-order bit-position (n-1)
    represents a coefficient multiplying -2n-1
  • The other positions i lt n-1 just represent 2i, as
    before.
  • The negation of any n-bit twos complement number
    a an-1a0 is given by an-1a0 1.

3.6 Integers and Algorithms
104
Correctness of Negation Algorithm
  • Theorem For an integer a represented in twos
    complement notation, -a a 1.
  • Proof

3.6 Integers and Algorithms
105
Subtraction of Binary Numbers
  • procedure subtract(an-1a0, bn-1b0 binary twos
    complement representations of integers a,b)
  • return add(a, add(b,1)) a (-b)
  • This fails if either of the adds causes a carry
    into or out of the n-1 position, since 2n-22n-2
    ? -2n-1, and -2n-1 (-2n-1) -2n isnt
    representable!

3.6 Integers and Algorithms
106
Multiplication of Binary Numbers
  • procedure multiply(an-1a0, bn-1b0 binary
    representations of a,b?N)
  • product 0
  • for i 0 to n-1
  • if bi 1 then
  • product add(an-1a00i, product)
  • return product

3.6 Integers and Algorithms
107
Binary Division with Remainder
  • procedure div-mod(a,d ? Z) Quotient rem. of
    a/d.
  • n max(length of a in bits, length of d in
    bits)
  • for i n-1 downto 0
  • if a d0i then Can we subtract at this
    position?
  • qi 1 This bit of quotient is 1.
  • a a - d0i Subtract to get remainder.
  • else
  • qi 0 This bit of quotient is 0.
  • r a
  • return q,r q quotient, r remainder

3.6 Integers and Algorithms
108
3.7 Applications of Number Theory
3.7 Applications of Number Theory
109
Some Useful Results
  • Theorem 1 If a and b are positive integers, then
    there exist integers s and t such that gcd(a,
    b)satb.
  • Example Express gcd(252, 198) as a linear
    combination of 252 and 198.

3.7 Applications of Number Theory
110
Some Useful Results
  • Lemma 1 If a, b and c are positive integers such
    that gcd(a, b)1 and abc, then ac.
  • Pf by Theorem 1, 1satb, ? csactbc
  • Lemma 2 If p is a prime and pa1a2?an, where
    each ai is an integer, then pai for some i.

3.7 Applications of Number Theory
111
Some Useful Results
  • Theorem 2 Let m be a positive integer and let a,
    b and c be integers. If ac ? bc (mod m) and
    gcd(c, m)1, then a ? b (mod m).
  • Pf
  • Example 14 ? 8 (mod 6), then 7 ? 4 (mod 6) ?


3.7 Applications of Number Theory
112
Linear Congruences
  • Theorem 3 If a and m are relatively prime
    integers amd m gt 1, then an inverse of a modulo m
    exists. Furthermore, this inverse is unique
    modulo m.
  • Pf


3.7 Applications of Number Theory
113
Linear Congruences
  • Example Find an inverse of 3 modulo 7.
  • Example Solve 3x ? 4 (mod 7).
  • Sol


3.7 Applications of Number Theory
114
Extended Euclids Algorithm
  • EXTENDED EUCLID(m, b)
  • (A1, A2, A3)(1, 0, m) (B1, B2, B3)(0,
    1, b)
  • while ( (B3!0) (B3!1))
  • Q A3 div B3
  • (T1, T2, T3)(A1QB1, A2QB2,
    A3QB3)
  • (A1, A2, A3)(B1, B2, B3)
  • (B1, B2, B3)(T1, T2, T3)
  • if (B3 0) return gcd(m, b) A3 no
    inverse
  • if (B3 1) return gcd(m, b)1 b1
    mod m B2

3.7 Applications of Number Theory
115
Extended Euclids Algorithm
  • Example Find the inverse of 550 mod 1759.

3.7 Applications of Number Theory
116
Chinese Remainder Theorem
  • Theorem 4 Let m1, m2,? , mn be pairwise
    relatively prime positive integers and a1, a2,? ,
    an arbitrary integers. Then the system
  • has a unique solution modulo m m1m2? mn .

3.7 Applications of Number Theory
117
Fermats Little Theorem
  • Theorem 5 If p is a prime positive integers and
    a is an integer not divisible by p, then
  • Furthermore, for every integer a we have
  • Example The integer 341 is a pseudoprime to the
    base 2 because it is composite (11?31) and 2340
    ?1 (mod 341).

3.7 Applications of Number Theory
118
Public Key Cryptography
  • RSA Cryptosystem There are a private key and a
    public key. It is an exponentiation algorithm.
    (Also known as MIT algorithm)
  • RSA Encryption C M e mod n.
  • RSA Decryption M C d mod n.
  • where n pq , p and q are two large primes,
    and
  • ed mod ?(n) 1 with ?(n)
    (p-1)(q-1).

3.7 Applications of Number Theory
119
Public Key Cryptography
  • Example Let n 15, e 3, encrypt M 7.
  • Example Encrypt the message STOP with p 43,
  • q 59, with e 13..
  • Sol

3.7 Applications of Number Theory
120
3.8 Matrices
3.8 Matrices
121
Matrices
  • A matrix (say MAY-trix) is a rectan-gular array
    of objects (usually numbers).
  • An m?n (m by n) matrix has exactly m horizontal
    rows, and n vertical columns.
  • Plural of matrix matrices (say MAY-trih-sees)
  • An n?n matrix is called a square matrix,whose
    order is n.

Notourmeaning!
Note The singular formof matrices is
matrix,not MAY-trih-see!
3.8 Matrices
122
Applications of Matrices
  • Tons of applications, including
  • Solving systems of linear equations
  • Computer Graphics, Image Processing
  • Models within Computational Science Engineering
  • Quantum Mechanics, Quantum Computing
  • Many, many more

3.8 Matrices
123
Matrix Equality
  • Two matrices A and B are equal iff they have the
    same number of rows, the same number of columns,
    and all corresponding elements are equal.

3.8 Matrices
124
Row and Column Order
  • The rows in a matrix are usually indexed 1 to m
    from top to bottom. The columns are usually
    indexed 1 to n from left to right. Elements are
    indexed by row, then column.

3.8 Matrices
125
Matrices as Functions
  • An m?n matrix A ai,j of members of a set S
    can be encoded as a partial function
    fA N?N?S, such that for iltm, jltn, fA(i, j)
    ai,j.
  • By extending the domain over which fA is defined,
    various types of infinite and/or multidimensional
    matrices can be obtained.

3.8 Matrices
126
Matrix Sums
  • The sum AB of two matrices A, B (which must have
    the same number of rows, and the same number of
    columns) is the matrix (also with the same shape)
    given by adding corresponding elements.
  • AB ai,jbi,j

3.8 Matrices
127
Matrix Products
  • For an m?k matrix A and a k?n matrix B, the
    product AB is the m?n matrix
  • I.e., element (i,j) of AB is given by the vector
    dot product of the ith row of A and the jth
    column of B (considered as vectors).
  • Note Matrix multiplication is not commutative!

3.8 Matrices
128
Matrix Product Example
  • An example matrix multiplication to practice in
    class

-
ù
é
0
1
1
0
-
ù
0
é
1
1
ú
ê
-

0
2
0
2
ê
ú
ú
ú
ê
3
0
2
û
ë
ú
ê
1
3
0
1
û
ë
3.8 Matrices
129
Identity Matrices
  • The identity matrix of order n, In, is the
    order-n matrix with 1s along the upper-left to
    lower-right diagonal and 0s everywhere else.

3.8 Matrices
130
Review Matrices, so far
  • Matrix sums and products
  • AB ai,jbi,j
  • Identity matrix of order nIn ?ij, where
    ?ij1 if ij and ?ij0 if i?j.

3.8 Matrices
131
Matrix Inverses
  • For some (but not all) square matrices A, there
    exists a unique multiplicative inverse A-1 of A,
    a matrix such that A-1A In.
  • If the inverse exists, it is unique, and A-1A
    AA-1.
  • We wont go into the algorithms for matrix
    inversion...

3.8 Matrices
132
Matrix Multiplication Algorithm
  • procedure matmul(matrices A m?k, B k?n)
  • for i 1 to m
  • for j 1 to n begin
  • cij 0
  • for q 1 to k
  • cij cij aiqbqj
  • end Ccij is the product of A and B

Whats the ? of itstime complexity?
Answer?(mnk)
3.8 Matrices
133
Powers of Matrices
  • If A is an n?n square matrix and p?0, then
  • Ap ? AAAA (A0 ? In)
  • Example

3.8 Matrices
134
Matrix Transposition
  • If Aaij is an m?n matrix, the transpose of A
    (often written At or AT) is the n?m matrix given
    by At B bij aji (1?i?n,1?j?m)

Flipacrossdiagonal
3.8 Matrices
135
Symmetric Matrices
  • A square matrix A is symmetric iff AAt. I.e.,
    ?i,j?n aij aji .
  • Which is symmetric?

3.8 Matrices
136
Zero-One Matrices
  • Useful for representing other structures.
  • E.g., relations, directed graphs (later in
    course)
  • All elements of a zero-one matrix are 0 or 1
  • Representing False True respectively.
  • The meet of A, B (both m?n zero-one matrices)
  • A?B ? aij?bij aij bij
  • The join of A, B
  • A?B ? aij?bij

3.8 Matrices
137
Boolean Products
  • Let Aaij be an m?k zero-one matrix, let
    Bbij be a k?n zero-one matrix,
  • The boolean product of A and B is like normal
    matrix ?, but using ? instead in the row-column
    vector dot product.

A?B
3.8 Matrices
138
Boolean Powers
  • For a square zero-one matrix A, and any k?0, the
    kth Boolean power of A is simply the Boolean
    product of k copies of A.
  • Ak ? A?A??A

k times
3.8 Matrices
Write a Comment
User Comments (0)
About PowerShow.com