Lecture # 01 - New
Lecture # 01 - New
■ Machine Learning
■ Pattern Recognition
Data
■ Web: CUOnline
■ Textbook: See next slide
■ Grading on 100
■ Collaborative-learning:
■ You will do small group discussion activities.
Introduction
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
2 1 0
0 1
1001011
Department of Computer Science
Procedure for Decimal to Binary conversion
1. Write the decimal number
2. Divide by 2; write quotient and remainder
3. Repeat step 2 on the quotient; keep on repeating until the
quotient becomes zero
4. Write all remainder digits in the reverse order (last remainder
first) to form the final result
■ What is it?
■ A procedural solution to given problem
■ Briefly speaking, algorithms are procedural
solutions to problems
■ Algorithms are not answers, but rather
precisely defined procedures for getting
answers. (e.g., sorting 3 numbers)
Department of Computer Science
Formal
Definition
problem
algorithm
Is there a
relationship
between
input and
output?
L
o
g
i
c
■ Statement of problem:
■ Input: a sequence of N numbers <a1, a2, …, aN>
■ Output: a reordering of the input sequence <a’ 1, a’2,
…, a’N> so that a’i ≤ a’j whenever i < j
■ Instance: the sequence <5, 3, 2, 8, 3> becomes
<2, 3, 3, 5, 8> after sorting
■ Algorithms:
■ Bubble Sort
■ Selection sort
■ Insertion sort
■ Merge sort
■ Quick sort and many more
OF
ALGORITHM
Department of Computer Science
Characteristics of an Algorithm
■ Not all procedures can be called algorithms.
■ An algorithm should have the following characteristics
■ Input: An algorithm must take zero or more inputs
values from a specified set.
■ Output: From each set of input values an algorithm
produces output value(s) from a specified set.
■ Definiteness/precision: The steps to be performed
in the algorithm must be clear and unambiguous.
■ Determinism: The intermediate results of each step
of execution are unique and are determined only by the
inputs and results of the preceding steps.
■ Correctness: An algorithm should produce the correct
output values for each set of input values.
■ Procedure:
■ Step 1: Find prime factors of m
■ Step 2: Find prime factors of n
■ Step 3: Identify al l common prime factors of m and n (if p is a prime factor occurring p m
and pn times in m and n, it should be repeated min{pm, pn } times)
■ Step 4: Compute product of al l common factors and return product as the answer.
■ Input(Y/N)
■ Yes: Because two nonnegative, not-both-zero integers m and n
are given.
■ Output(Y/N):
■ Yes: Because procedure return product of all the common factors
as the GCD of m and n.
■ Procedure:
■ Step 1: Find prime factors of m
■ Step 2: Find prime factors of n
■ Step 3: Identify al l common prime factors of m and n (if p is a prime factor occurring p m
and pn times in m and n, it should be repeated min{pm, pn } times)
■ Step 4: Compute product of al l common factors and return product as the answer.
■ Determinism(Y/N)
■ No : Because the prime factorization steps(Step 1 and 2) are
defined ambiguously: they require a list of prime numbers, and we
did not explain how to obtain such a list.
■ Step 3 is also not defined clearly enough. Its ambiguity is much
easier to rectify than that of the factorization steps, however. How
would you find common elements in two sorted lists?
■ Procedure:
■ Step 1: Find prime factors of m
■ Step 2: Find prime factors of n
■ Step 3: Identify al l common prime factors of m and n (if p is a prime factor occurring p m
and pn times in m and n, it should be repeated min{pm, pn } times)
■ Step 4: Compute product of al l common factors and return product as the answer.
■ Finiteness (Y/N)
■ Yes : Because in order to find prime factors pi in step 1, we
perform at most iterations and in step 2 we do at most
iterations.
■ Correctness (Y/N)
■ Yes : Because it produces correct output against legitimate input
■ Generality(Y/N)
■ Yes: Because it can find GCD of any two positive integers
Upshot
■ Pseudo-code
■ A mixture of natural language and programming
language-like structures
■ Precise and brief
■ also avoids most issues of ambiguity
■ No particular agreement on syntax
■ programming language
■ tend to require expressing low-level details that are
not necessary for a high-level understanding
Process
Input or output
Decision
Flowchart
Symbols Flow line
Connector
Off-page connector
Flowchart counte
Symbols Automatic- A
r
B
counter loop
S
situation is used:
■ Expressions
■ Control flow ← Assignment
■ if … then … [else …] (like = in Java)
■ while … do … ● Equality testing
■ repeat … until … (like == in Java)
■ for … do … n2 Superscripts and
■ Indentation replaces braces other mathematical
■ Method declaration formatting allowed
Algorithm method ( arg [, arg…])
Input …
Output …
Input
2. Divide by 2; write quotient Numb Step 1
er
and remainder Base
Quatient=Number / Base
3. Repeat step 2 on the Remainder = Number %
Base
Step 2
quotient; keep on Step 3 Record Remainder to
End
2. Divide by 2; write
quotient and remainder
■ Idea:
■ if n ≠ 0, gcd(m, n) = gcd(n, m mod n);
■ if n = 0, gcd(m, n) = m.
■ Euclid’s algorithm for computing gcd(m, n)
■ Step1 If n=0, return the value of m as the answer
and stop; otherwise proceed to Step2.
■ Step2 Divide m by n and assign the value of the
remainder to r.
■ Step3 Assign the value of n to m and the value of r
to n. Go to Step1.
■ What is an algorithm?
■ Understand the concept and properties of an
algorithm.
■ Define and use the three constructs for
developing
algorithms: sequence, decision, and repetition.
■ What properties an algorithm must have?
■ Understand the properties of an algorithm.
■ How to specify an algorithm?
■ Understand and use three tools to represent
algorithms:
flowchart, pseudocode, and structure.
■ One problem different algorithms* having
Department of Computer Science 88
Summary
Great thinkers
wil l always be needed.