Você está na página 1de 37

CSCI20

Prelim

References:
Introduction to Algorithms, Thomas H. Cormen, 2nd edition,
MIT Press

Instructor: Priyanka S
What are the Algorithms?

Algorithm:

 Is any well-defined computational procedure that takes some


value, or set of values, as input and produces some value, or set
of values, as output.

 It is a tool for solving a well-specified computational problem.

 They are written in a pseudo code which can be implemented in


the language of programmer’s choice.
From Algorithms to Programs
Problem
Algorithm:
Algorithm A sequence
of instructions describing
how to do a task (or
process)

C++ Program
3
Example: sorting numbers.

􀂄 Input: A sequence of n numbers {a3 , a1, a2,...,an }

􀂄 Output: A reordered sequence of the input


{a1 , a2, a3,...,an } such that a1≤a2 ≤a3… ≤an .

􀂄 Input instance: {5, 2, 4, 1, 6, 3}.


􀂄 Output : {1, 2, 3, 4, 5, 6}.

􀂄 An instance of a problem consists of the input (satisfying


whatever constraints are imposed in the problem statement)
needed to compute a solution to the problem.
Example: sorting numbers.

􀂄 Sorting is a fundamental operation.

􀂄 Many algorithms existed for that purpose.

􀂄 The best algorithm to use depends on:


􀂄 The number of items to be sorted.
􀂄 possible restrictions on the item values
􀂄 kind of storage device to be used: main memory, disks, or tapes.
Correct and incorrect algorithms
• Algorithm is correct if, for every input instance, it
ends with the correct output. We say that a correct
algorithm solves the given computational problem.

• An incorrect algorithm might not end at all on some


input instances, or it might end with an answer other
than the desired one.

• We shall be concerned only with correct algorithms.


Practical Examples

Internet and Networks.


􀂄 The need to access large amount of information with the
shortest time.
􀂄 Problems of finding the best routs for the data to travel.
􀂄 Algorithms for searching this large amount of data to quickly
find the pages on which particular information resides.

Electronic Commerce.
􀂄 The ability of keeping the information (credit card numbers,
passwords, bank statements) private, safe, and secure.
􀂄 Algorithms involves encryption/decryption techniques.
• These lists are far from exhaustive, but exhibit
tow characteristics that are common to many
interesting algorithms.
(1)There are many candidate solutions, most of
which are not we want. Finding one that we do
want can present quite a challenge.
(2)There are practical applications.
Hard problems
􀂄 We can identify the Efficiency of an algorithm
from its speed (how long does the algorithm
take to produce the result).

􀂄 Some problems have unknown efficient


solution.

􀂄 These problems are called NP-complete


problems.
Hard problems
􀂄 Nobody has proven that an efficient algorithm
for one cannot exist.

􀂄 Several NP-complete problems are similar, but not


identical.

􀂄 If we can show that the problem is NP-complete, we


can spend our time developing an efficient algorithm
that gives a good, but not the best possible solution.

􀂄 Example of NP-complete problem: “traveling


salesman”.
Why do we need the algorithms?
􀂄 One always need to proof that his solution method terminate and does
the correct answer.

􀂄 Computers may be fast but they are not infinitely fast, and memory
may be cheap but it is not free. This resources should be used wisely.

As an example:

􀂄 There is two algorithms for sorting; merge sort algorithm, and


insertion sort algorithm

􀂄 Insertion sort takes an execution time equal c1*n2 to sort n items.


􀂄 Merge sort takes an execution time equal c2*n log2n to sort n items.
Algorithms as a technology
Suppose computers were infinitely fast and computer
memory was free. Would you have any reason to study
algorithms?
The answer is yes, if for no other reason than that you
would still like to demonstrate that your solution method
terminates and does so with the correct answer.
If computers were infinitely fast, any correct method for
solving a problem would do. You would probably want
your implementation to be within the bounds of good
software engineering practice (i.e., well designed and
documented), but you would most often use whichever
method was the easiest to implement
Why do we need algorithms?- cont.

􀂄 c1 and c2 are constants.

􀂄 Insertion sort usually has a smaller constant factor than merge


sort, so that c1< c2

􀂄 Two computers; computer A running insertion sort, and


computer B running merge sort.

􀂄 Assuming Computer A executes one billion instructions per


second and computer B executes only ten million instructions
per second, so that computer A is 100 times faster than
computer B.
Why do we need algorithms?- cont.

• Suppose (c1 =2, and c2 =50).

• To sort one million number, computer A takes,


2 x (106 ) 2 instruction /109 instruction/sec = 2000 sec.

• While computer B takes, 50 x 106 x log2 106 instruction /107


instruction/sec = 100 sec.
Algorithms and other technologies
Algorithms are truly that important on
contemporary computers in light of other
advanced technologies, such as
Hardware with high clock rates, pipelining, and
superscalar architectures,
Easy-to-use, intuitive graphical user interfaces
(GUIs),
Object-oriented systems, and
Local-area and wide-area networking.
Expressing algorithms
• Algorithms can be expressed in many
kinds of notation, including
– natural languages,
– pseudocode,
– flowcharts,
– programming languages or
– control tables (processed by interpreters).
• Natural Language: used for complex
algorithm.
• Pseudocode and Flowcharts: structured ways
used to express algorithms.
• Programming Languages: Structured English
for expressing algorithms in a form that can be
executed by a computer system.
• One of the most common tools for defining
algorithms is pseudocode, which is part
English, part structured code.
Algorithm vs Pseudo-code vs
Program-code
• Algorithm:
Step 1: Declare two integers and variable that
computes the sum. (num1, num2, sum)
Step 2: Accept the first number.
Step 3: Accept the second number.
Step 4: Computes for the sum of the first and
second numbers.
Step 5: Display the computed sum of the two
numbers.
• Pseudo-code:
declare num1 as int
declare num2 as int
declare sum as double
accept num1
accept num2
sum = num1 + num2
display sum
• Flowchart: Is an algorithm that uses symbols
to describe a method or a program. Used
usually instead of pseudo-code.
• Program
Import java.util.Scanner;
Public class sample
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
num1 = sc.nextInt();
num2 = sc.nextInt();
sum = num1 + num2;
System.out.println(“The sum is “+sum);
}
}
Algorithm analysis
• To analyze an algorithm is to determine the
amount of resources (such as time and storage)
necessary to execute it.
• Algorithm analysis requires a set of rules to
determine how operations are to be counted.
• There is no generally accepted set of rules for
algorithm analysis.
• In some cases, an exact count of operations is
desired, in other cases, a general approximation
is sufficient.
• Not every line of code will be explained .The
analysis examines only those points that either
need to be emphasized or that may require
some clarification.
Comparing algorithms
• Time complexity
– The amount of time that an algorithm needs to
run to completion.
• Space complexity
– The amount of memory an algorithm needs to
run
• The better algorithm is the one which
runs faster (has smaller time
complexity)
Coding an Algorithm

• Coding an algorithm consists of


implementing the algorithm using a
computer programming language.
Algorithm examples
• To find the sum of two numbers
1. declare n1,n2 as integers
2. declare sum as integers
3. input values for n1,n2
4. sum  n1+n2
5. print “Sum is”, sum
6. halt
• Average of 3 numbers
1. declare n1,n2,n3 as integers
2. declare sum,avg as float
3. input values for n1,n2,n3
4. sum  n1+n2+n3
5. avg  sum/3
6. print “Average is”, avg
7. halt
• Write an algorithm to calculate the final price
as the product of itemprice and tax.
declare itemprice,tax as double
declare finalprice as double
input values for itemprice, tax
finalprice = itemprice + tax
print “ final price is” , finalprice
halt
• To find the largest of two numbers
1. Declare a, b as integers
2. Input values for a,b
3. if a> b then
3.1 print “a is larger than b”
4. else
4.1 print “b is larger than a”
5. halt
Loops
• Write an algorithm to display the first
20 natural numbers
declare I as integer
for I=1;I<=20;I++
print I
halt
• To find the sum of first n natural numbers.
Declare i, n,sum as integers
Input value for n
Assign sum =0
for i=1;i<=n;i++
sum=sum+i
print “sum is”,sum
halt
Euclid’s Algorithms
• The Euclidean algorithm (also called Euclid's
algorithm) is an algorithm to determine the
greatest common divisor (gcd) of two integers.
• In mathematics, the Euclidean algorithm(also
called Euclid's algorithm) is an efficient method
for computing the greatest common divisor
(GCD), also known as the greatest common
factor (GCF) or highest common factor (HCF). It
is named after the Greek mathematician Euclid.
GCD
• 3 divides 15 (15/3 is a whole number), and is called a
divisor of 15. 1, 3, 5, and 15 are all of the divisors of
15.
• Prime numbers have only themselves and 1 as
divisors.
– Example 7 has 1 and 7 as divisors
• Example: Take number 36 and 15
– Divisors of 15 are 1,3,5,15
– Divisors of 36 are 1,2,3,4,6,9,12,18,36
– 36 and 15 have some "common divisors," 1, and 3. You can
list the divisors of each, and you will find that 1 and 3 are on
both lists.
– Sometimes you need to know the greatest common divisor
(gcd), the greatest number that is a common divisor of two
or more whole numbers.
Euclid’s Algorithm
• In Euclid's Elements we find a way of
calculating the gcd of two numbers, without
listing the divisors of either number. It is now
called Euclid's Algorithm.
• [An algorithm is a step by step process (or
recipe) for doing something.]
• We will find the gcd of 36 and 15.
– Divide 36 by 15 (the greater by the smaller), getting 2
with a remainder of 6.
– Then we divide 15 by 6 (the previous remainder) and
we get 2 and a remainder of 3.
– Then we divide 6 by 3 (the previous remainder) and
we get 2 with no remainder.
– The last non-zero remainder (3) is our gcd.
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.
• //Computes gcd(m,n)
• //Input: Two nonnegative, not-both-zero integers
m and n
• //output: Greatest common divisor of m and n
• While n ≠0 do
• r←m mod n
• m←n
• n←r
• return m
Example
• Using Euclid’s algorithm find the gcd of 24, 33
• Euclid's algorithm comes in handy with
computers, because listing divisors is more
difficult than the above algorithm. Large
numbers are difficult to factor, while they are
relatively easy to divide.
• Euclid’s algorithm is considered to be one of the
best examples of an efficient algorithm.
– 33/24 =1 remainder 9
– 24/9 = 2 remainder 6
– 9/6=1 remainder 3
– 6/3=2 remainder 0
• The last non-zero remainder is 3
1. Write an algorithm to check if a given number is even or
odd
2. Write an algorithm to find the smallest of 3 numbers.
3. To find the smallest of 3 numbers
4. To check if a given number is a positive or negative
number
5. To check if a number is divisible by 5
6. Write an algorithm to print the sum and count of non-
negative numbers out of a list of 50 numbers
7. Write an algorithm to read the gender and age of 50
voters and count how many male voters of age above
18 are present in the list
8. Write an algorithm to find the largest number out of a list
of 100 numbers.

Você também pode gostar