Untitled
Untitled
Introduction
Algorithm
An algorithm is a computational method for solving
a problem.
It is a sequence of steps that take us from the input
to the output.
An algorithm must be
Correct
It should provide a correct solution according to the specifications.
Finite
It should terminate.
General
It should work for every instance of a problem
Efficient
It should use few resources (such as time or memory).
Pseudocode
1. Sequence
• a series of statements that do not alter the
execution path within an algorithm.
2. Selection
• Evaluates one or more alternative
3. Loop
• Iterates a block of code.
Data Structure
a way of organizing the data considering not only the
items stored but also their releationship from each
other
an aggregation of atomic and composite data types
into a set with defined relationships.
a combination of elements each of which is either a
data type or another data structure.
a group of data elements grouped together under
one name
Data Structure (cont…)
Stack ADT
A Stack contains elements of same type arranged in sequential
order. All operations takes place at a single end that is top of
the stack and following operations can be performed:
push() – Insert an element at one end of the stack called top.
pop() – Remove and return the element at the top of the stack,
if it is not empty.
peek() – Return the element at the top of the stack without
removing it, if the stack is not empty.
size() – Return the number of elements in the stack.
isEmpty() – Return true if the stack is empty, otherwise return
false.
isFull() – Return true if the stack is full, otherwise return false.
Introduction
Queues - insertions made at the back and removals made from the front
Linear Relationship
If t=cn, then an increase in the size of data increases the
execution time by the same factor
Logarithmic Relationship
logarithm n. Mathematics. The power to which a base, such as
10, must be raised to produce a given number
If t=log2n then doubling the size ‘n’ increases ‘t’ by one
time unit.
Asymptotic Complexity
41
Typical Complexities (2)
Complexity Notation Description
• Running time is usually the thing we care most about. But it can be as well
important to analyze the amount of memory used by a program. If a
program takes a lot of time, you can still run it, and just wait longer for the
result.
However if a program takes a lot of memory, you may not be able to run it
at all, so this is an important parameter to understand.
• We analyze things differently for recursive and iterative programs.
•
For an iterative program, it is usually just a matter of looking at the variable
declarations and storage allocation calls, e.g., array of n numbers.
•
Analysis of recursive program space is more complicated: the space used at
any time is the total space used by all recursive calls active at that time.
• Each recursive call takes a constant amount of space: some space for local
variables and function arguments, and also some space for remembering
Algorithm Efficiency, f(n)
A. Linear Logarithmic
1. i=1
2. loop (i <= 10)
1. j=1
2. loop (j <=10)
1. application code
2. j=jx2
3. end loop
4. i=i+1
3. end loop
.:. f(n) = n log2 n
Nested Loops …
B. Quadratic
1. i=1
2. loop (i <= 10)
1. j=1 .:. f(n) = n2
2. loop (j <= 10)
1. application code
2. j=j+1
3. end loop
4. i=i+1
3. end loop
Big-O Notation (“on the order of”)
Picking midpoints;
Method 2 is actually like searching a binary tree, so we
will leave a full calculation until week 6, as right now the
maths could get complicated.
But for n=10, you should be able to calculate the
average case – try it! (When n=10 I make it 1.9 times as
efficient)
Average Case Complexity
1i=1
2 loop (i <= n)
1j=1
2 loop (j <= n)
1k=1
2 loop (k <= n)
1 print (i, j, k)
2k=k+1
3 end loop
4j=j+1
3 end loop
4 i = i+1
3 end loop