0% found this document useful (0 votes)
15 views

Lecture 1 Algorithms

ai

Uploaded by

alieslam20014
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 1 Algorithms

ai

Uploaded by

alieslam20014
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

The Design and

Analysis of
Algorithms
Lecture 1

CHAPTER 1: INTRODUCTION
What is an Algorithm
 Algorithm is more than a branch of computer science. It is
the core of computer science, and, in all fairness, can be
said to be relevant to most science, business, and
technology.
 An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required output
for any legitimate input in a finite amount of time.

• has input data, and is expected to produce


output data
• each instruction can be carried out in a finite
amount of time in a deterministic way

2
 What the Difference Between Algorithm and
Program?

1. Writing an algorithm is simpler (we don’t need to


worry about the detailed implementation, or the
language syntax).
2. An algorithm is easier to read than a program
written in, for instance, C.

3
Problems vs Algorithms vs Programs
 For each problem or class of problems,
there may be many different algorithms.
 For each algorithm, there may be many
different implementations (programs).
Difference between Algorithm,
Pseudocode and Program
 Algorithm : Systematic logical approach which is a
well-defined, step-by-step procedure that allows a
computer to solve a problem.
 Pseudocode : It is a simpler version of a programming
code in plain English which uses short phrases to write
code for a program before it is implemented in a specific
programming language.
 Program : It is exact code written for problem following
all the rules of the programming language.

5
Algorithmic Representation of
Computer Functions
 Input
 Get information Get (input command)
 Storage
 Store information Given/Result
Intermediates/Set
 Process
 Arithmetic Let (assignment command)
 Repeat instructions Loop
 Branch conditionals If
 Output
 Give information Give (output command)
Properties of an Algorithm
An algorithm must possess the following
properties:
 finiteness: The algorithm must always
terminate after a finite number of steps.
 definiteness: Each step must be precisely
defined; the actions to be carried out must
be rigorously and unambiguously specified
for each case.
 input: An algorithm has zero or more inputs,
taken from a specified set of objects.
 output: An algorithm has one or more
outputs, which have a specified relation to
the inputs.
 effectiveness: All operations to be
performed must be sufficiently basic that
they can be done exactly and in finite
length
Expressing Algorithms
An algorithm may be expressed in a number of ways,
including:
 natural language: usually verbose and ambiguous
 flow charts: avoid most (if not all) issues of ambiguity;
difficult to modify w/o specialized tools; largely
standardized
 pseudo-code: also avoids most issues of ambiguity;
vaguely resembles common elements of programming
languages; no particular agreement on syntax
 programming language: tend to require expressing low-
level details that are not necessary for a high-level
understanding
Common Elements of Algorithms
 acquire data (input): some means of reading
values from an external source; most algorithms
require data values to define the specific problem
(e.g., coefficients of a polynomial)
 Computation: some means of performing
arithmetic computations, comparisons, testing
logical conditions, and so forth...
 Selection: some means of choosing among two
or more possible courses of action, based upon
initial data, user input and/or computed results
 Iteration: some means of repeatedly
executing a collection of instructions, for a
fixed number of times or until some logical
condition holds
 report results (output): some means of
reporting computed results to the user, or
requesting additional data from the user
Understand the problem

Decide on computational means


Exact vs approximate solution
Data structures
Algorithm design technique

Design an algorithm

Prove correctness

Analyze the algorithm

Code the algorithm

12
What does it mean to understand
the problem?
 What are the problem objects?
 What are the operations applied to the objects?

Deciding on computational means


 How the objects would be represented?
 How the operations would be implemented?

13
Design an algorithm

• Build a computational model of


the solving process

Prove correctness
• Correct output for every
legitimate input in finite time
• Based on correct math formula
• By induction 14
Analyze the algorithm
Efficiency: time and space
Simplicity
Generality: range of inputs, special
cases
Optimality:
no other algorithm can do
better
Coding
How the objects and operations in the 15
pseudo-Language
 See the posted notes on pseudo-language notation.
Testing Correctness
 How do we know whether an algorithm is actually
correct?
 First, the logical analysis of the problem we
performed in order to design the algorithm should
give us confidence that we have identified a valid
procedure for finding a solution.
 Second, we can test the algorithm by choosing
different sets of input values, carrying out the
algorithm, and checking to see if the resulting
solution does, in fact, work.
Proving Correctness
 We can attempt to construct a formal,
mathematical proof that, if the algorithm is given
valid input values then the results obtained from
the algorithm must be a solution to the problem.
 We should expect that such a proof be provided
for every algorithm.
 In the absence of such a proof, we should view the
purported algorithm as nothing more than a
heuristic procedure, which may or may not yield
the expected results
gcd(m,n)

while n ≠0 do 1. t ← min (m ,n)


r ← m mod
n 2. if m % t = 0 goto 3,
m←n
n ←r else goto
return m 4

3. if n % t = 0 return
t,
else goto
4
20
21
Figure 8-6

Three constructs
Figure 8-7

Flowcharts for three constructs


Figure 8-8

Pseudocode for three constructs


Example 1

Write an algorithm in pseudocode that finds the


average of two numbers

Solution
See Algorithm on the next slide.
Algorithm : Average of two
AverageOfTwo
Input: Two numbers
1.Add the two numbers
2.Divide the result by 2
3.Return the result by step 2
End
Example 2

Write an algorithm to change a numeric grade to a


pass/no pass grade.

Solution
See Algorithm on the next slide.
Algorithm :Pass/no pass Grade
Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “no pass”
End if
2. Return the grade
End
Example 3

Write an algorithm to change a numeric grade


to a letter grade.

Solution

See Algorithm on the next slide.


Algorithm : Letter grade
LetterGrade
Input: One number
1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Continues on the next slide
Algorithm Letter grade
(continued)
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if

Continues on the next slide


Algorithm Letter grade
(continued)
5. If (the number is less than 60)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
Example 4

Write an algorithm to find the largest of a


set of numbers. You do not know the
number of numbers.

Solution

See Algorithm on the next slide.


Algorithm: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the
integer
End if
End while
3. Return Largest
End
Example 5

Write an algorithm to find the largest of


1000 numbers.

Solution

See Algorithm on the next slide.


Algorithm : Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
Important problem types
 Sorting
 Searching
 String processing
 Graph problems
 Combinatorial problems
 Geometric problems
 Numerical problems

37
Tasks

38

You might also like