0% found this document useful (0 votes)
31 views15 pages

Basic Algoritm

Uploaded by

Ivania Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views15 pages

Basic Algoritm

Uploaded by

Ivania Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structures

& Algorithm Analysis


Problem Solving: Main Steps
1. Problem definition

2. Algorithm design / Algorithm specification

3. Algorithm analysis

4. Implementation

5. Testing

6. [Maintenance]
1. Problem Definition
• What is the task to be accomplished?
 Calculate the average of the grades for a given student
 Understand the talks given out by politicians and translate them in
Chinese

• What are the time / space / speed / performance requirements ?


2. Algorithm Design /
Specifications
• Algorithm: Finite set of instructions that, if followed,
accomplishes a particular task.
• Describe: in natural language / pseudo-code / diagrams / etc.
• Criteria to follow:
 Input: Zero or more quantities (externally produced)
 Output: One or more quantities
 Definiteness: Clarity, correctness of each instruction
 Finiteness: The algorithm has to stop after a finite (may be very
large) number of steps
 Effectiveness: Each instruction has to be basic enough and feasible
4,5,6: Implementation, Testing,
Maintainance
• Implementation
 Decide on the programming language to use
 C, C++, Lisp, Java, Perl, Prolog, assembly, etc. , etc.
 Write clean, well documented code

• Test, test, test

• Integrate feedback from users, fix bugs, ensure


compatibility across different versions 
Maintenance
3. Algorithm Analysis
• Complexity Analysis
 The complexity of an algorithm is a function describing the efficiency of the algorithm
in terms of the amount of data the algorithm must process.
3. Algorithm Analysis (Cont)
• There are two main complexity measures of the efficiency of an algorithm:
 Space complexity
 How much space is required
 Time Complexity
 How much time does it take to run the algorithm
Space Complexity
• Function describing the amount of memory (space) an
algorithm takes in terms of the amount of input to the
algorithm.

• This is essentially the number of memory cells which


an algorithm needs. A good algorithm keeps this
number as small as possible, too.

• We often speak of "extra" memory needed, not


counting the memory needed to store the input itself
Space Complexity (cont’d)
1. Fixed part: The size required to store certain
data/variables, that is independent of the size
of the problem:
- e.g. name of the data collection
- same size for classifying 2GB or 1MB of texts

2. Variable part: Space needed by variables,


whose size is dependent on the size of the
problem:
- e.g. actual text
- load 2GB of text VS. load 1MB of text
Time Complexity
• The number of (machine) instructions which a program executes
during its running time is called its time complexity in computer
science. This number depends primarily on the size of the
program's input

• Often more important than space complexity


 space available (for computer programs!) tends to be larger and larger
 time is still a problem for all of us

• Algorithms running time is an important issue


Experimental Approach
• Write a program that implements the algorithm
• Run the program with data sets of varying size.
• Determine the actual running time using a system call to measure
time (e.g. system (date) );

• Problems?
Experimental Approach
• It is necessary to implement and test the algorithm in order to
determine its running time.
• Experiments can be done only on a limited set of inputs, and may
not be indicative of the running time for other inputs.
• The same hardware and software should be used in order to
compare two algorithms. – condition very hard to achieve!
Algorithm Description
• How to describe algorithms independent of a programming
language
• Pseudo-Code = a description of an algorithm that is
 more structured than usual prose but
 less formal than a programming language
• (Or diagrams)
• Example: find the maximum element of an array.
Algorithm arrayMax(A, n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax  A[0]
for i 1 to n -1 do
if currentMax < A[i] then currentMax  A[i]
return currentMax
Pseudo Code
• Expressions: use standard mathematical symbols
 use  for assignment ( ? in C/C++)
 use = for the equality relationship (? in C/C++)

• Method Declarations: -Algorithm name(param1, param2)


• Programming Constructs:
 decision structures: if ... then ... [else ..]
 while-loops while ... do
 repeat-loops: repeat ... until ...
 for-loop: for ... do
 array indexing: A[i]

• Methods
 calls: object method(args)
 returns: return value

• Use comments
• Instructions have to be basic enough and feasible!
Example
Algorithm arrayMax(A, n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i  1 to n -1 do
if currentMax < A[i] then
currentMax  A[i]
return currentMax

How many operations ?

You might also like