Algorithms Complexity Reviewer Lahat Ng Ni Lesson
Algorithms Complexity Reviewer Lahat Ng Ni Lesson
What is an Algorithm?
✓ Input: there are zero or more quantities, which are externally supplied.
✓ Output: at least one quantity is produced.
✓ Definiteness: each instruction must be clear and unambiguous.
✓ Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will
terminate after a finite number of steps.
✓ Effectiveness: every instruction must be sufficiently basic that it can in principle be carried out by a
person using only pencil and paper. It is not enough that each operation be definite, but it must also be
feasible.
In formal computer science, one distinguishes between an algorithm, and a program. A program
does not necessarily satisfy the fourth condition.
One important example of such a program for a computer is its operating system, which never
terminates (except for system crashes) but continues in a wait loop until more jobs are entered.
• The performance of a program is the amount of computer memory and time needed to run a program.
Time Complexity:
• The time complexity of a program is the amount of computer time it needs to run to completion.
• The time needed by an algorithm expressed as a function of the size of a problem is called the time
complexity of the algorithm.
• Asymptotic time complexity - The limiting behavior of the complexity as size increases. It determines the
size of problems that can be solved by the algorithm.
Space Complexity:
• Instruction space:
Instruction space is the space needed to store the compiled version of the program instructions.
• Data space:
Data space is the space needed to store all constant and variable values.
Data space has two components:
o Space needed by constants and simple variables in program.
o Space needed by dynamically allocated objects such as arrays and class instances.
The three basic design goals that one should strive for in a program are:
A program that runs faster is a better program, so saving time is an obvious goal. Likewise, a
program that saves space over a competing program is considered desirable. We want to “save face” by
preventing the program from locking up or generating reams of garbled data.
Complexity of Algorithms
The complexity of an algorithm M is the function f(n) which gives the running time and/or storage
space requirement of the algorithm in terms of the size “n‟ of the input data.
Mostly, the storage space required by an algorithm is simply a multiple of the data size “n‟. Complexity shall
refer to the running time of the algorithm.
The function f(n), gives the running time of an algorithm, depends not only on the size “n‟ of the
input data but also on the data. The complexity function f(n) for certain cases are:
1. Best Case: The minimum possible value of f(n) is called the best case.
3. Worst Case: The maximum value of f(n) for any key possible input.
Algorithms can be evaluated by a variety of criteria. Most often we shall be interested in the rate of growth of
the time or space required to solve larger and larger instances of a problem.
We will associate with the problem an integer, called the size of the problem, which is a measure of the
quantity of input data.
Rate of Growth:
The following notations are commonly use notations in performance analysis and used to
characterize the complexity of an algorithm:
2. Big–OMEGA (Ω)
4. Little–OH (o)
Step 2: Define the variables: ex. HEIGHT and WIDTH (or H & W).
While writing algorithms we will use following symbol for different operations:
+ Addition * Multiplication
- Subtraction / Division
FLOWCHART
Algorithms and flowchart are the powerful tools for learning programming.
An algorithm is a step-by-step analysis of the process, while a flowchart explains the steps of a program in a
graphical way.
• The first design of flowchart goes back to 1945 which was designed by John Von Neumann.
• Often considered as a blueprint of a design used for solving a specific problem.
Advantages of flowchart:
Step5: End
Problem2: Write an algorithm to read two numbers and find their sum.
Algorithm:
Inputs to the algorithm:
Step1: Start
First num1.
Step2: Read\input the first num1.
Second num2.
Step3: Read\input the second num2.
Expected output:
Step4: Sum num1+num2 // calculation of sum
Step6: End
Step5: End
In branch control, there is a condition and according to a condition, a decision of either TRUE or FALSE is
achieved. Ex. If A > B
S1: Start
Print A
Print B
S5: End
Week 2 Lesson 2: RECALL OF PROGRAMS AND RULE OF SUMS AND ITS ALGORITHM EXAMPLES
What is a Program?
• A program is prepared by first formulating a task and then expressing it in an appropriate computer
language, presumably one suited to the application.
• The specification thus rendered is translated, commonly in several stages, into a coded program
directly executable by the computer on which the task is to be run.
• Programs are a collection or sequence of executable coded instructions that a computer can
recognize to solve a problem.
What is an Application?
• An app is a program or a collection of programs that have been designed for the end-users. They help
you to perform a set of coordinated tasks, functions, or activities.
The main similarity between applications and programs is the fact that they are both used to perform various
functions or tasks on a computer.
➢ All apps are programs, but not all programs are necessarily apps.
➢ Apps can’t run on their own, and they depend on system software to operate.
Program Application
1. Installation • Requires an installer that is packaged • Single file that a user downloads from the
with necessary files for it to be installed on internet and installs it on their device.
a computer. • Don’t need a separate uninstaller to
• Seek to download more data from the uninstall apps from your computer.
web before it installs
2. Target Users • To help a computer execute a specific • To help users complete a given function,
purpose, and it can run on the background task, or activity.
without the end-users intervention.
3. User • Don’t necessarily need to have a UI for • Since an app is designed to be utilized by
Interface them to be considered complete. real individuals, it boasts a user interface
Ex. viruses and malware that are used by (UI).
hackers to infect your computer are
programs, but they don’t have a UI
4. Development • Can be created by a single individual. • Since an application can feature several
programs, it is usually developed through a
structured and documented process that
may involve several individuals.
5. Operation • Don’t need an app to execute their roles. • Requires various programs to exist and
operate
Scripts VS. Programs
• Scripts tend to be small (no more than a few hundred or a few thousand lines of code) and do not do any
significant computation of their own.
• Real programs may be quite large and may implement complex computational algorithms. Hence, they
need to be fast and as a result are usually written in compiled languages.
- A data type in programming defines the type of data that a variable can hold.
- It determines the operations that can be performed on the data and the way it is stored in memory.
Examples in Python include integers ( int ), floating-point numbers ( float ), strings ( str ), lists ( list ), dictionaries ( dict ), etc.
- are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed
on a particular data.
• Numeric
• Sequence Type
• Boolean
• Set
• Dictionary
• Binary Types( memoryview , bytearray , bytes )
type() function - to define the values of various data types of Python and check their data types
The numeric data type in Python represents the data that has a numeric value.
• Integers –It contains positive or negative whole numbers (without fractions or decimals). In Python, there is no limit to
how long an integer value can be.
• Float –. It is a real number with a floating-point representation. It is specified by a decimal point. Optionally, the
character e or E followed by a positive or negative integer may be appended to specify scientific notation.
• Complex Numbers –It is specified as (real part) + (imaginary part)j . For example – 2+3j
2. Sequence Data Types in Python
- The sequence Data Type in Python is the ordered collection of similar or different Python data types.
Note – The creation of a Python tuple without the use of parentheses is known as Tuple Packing.
Access Tuple Items
In order to access the tuple items refer to the index number. Use the index operator [ ] to access an item in a tuple. The
index must be an integer. Nested tuples are accessed using nested indexing
- Python Data type with one of the two built-in values, True or False.
Boolean objects that are equal to True are truthy (true), and those equal to False are falsy (false).
However non-Boolean objects can be evaluated in a Boolean context as well and determined to be true or false. It is denoted by
the class bool.
Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw an error.
Example: The first two lines will print the type of the boolean values True and False, which is <class ‘bool’>. The third line will
cause an error, because true is not a valid keyword in Python. Python is case-sensitive, which means it distinguishes between
uppercase and lowercase letters. You need to capitalize the first letter of true to make it a boolean value.
- In Python Data Types, a Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements. The
order of elements in a set is undefined though it may consist of various elements.
Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly
braces, separated by a ‘comma’.
The type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set.
Set items cannot be accessed by referring to an index, since sets are unordered the items have no index. But you can loop through
the set items using a for loop, or ask if a specified value is present in a set, by using the in the keyword.
Example: This Python code creates a set named set1 with the values “Geeks” , “For” and “Geeks” . The code then prints the
initial set, the elements of the set in a loop, and checks if the value “Geeks” is in the set using the ‘ in’ operator
A dictionary in Python is an unordered collection of data values, used to store data values like a map, unlike other
Python Data Types that hold only a single value as an element, a Dictionary holds a key: value pair.
In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces, separated by
‘comma’.
Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be
immutable.
The dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing
it in curly braces{}.
Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly.
Key can be used inside square brackets. There is also a method called get() that will also help in accessing the
element from a dictionary.
Indentation in Python
Indentation is a very important concept of Python because without properly indenting the Python code, you will
end up seeing IndentationError and the code will not get compiled.
Python indentation refers to adding white space before a statement to a particular block of code. In other words,
all the statements with the same space to the left, belong to the same code block.
Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block
of code. A block is a combination of all these statements.
Python Arrays
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the
same type together. This makes it easier to calculate the position of each element by simply adding an offset to a
base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
Unlike lists, which can hold items of different data types, arrays in Python are typically used to store items of the
same data type. This makes them more efficient for numerical computations. Python does not have a built-in array
data type, but the ‘array’ module provides an array type that can be used for this purpose.
Array in Python can be created by importing an array module. array( data_type , value_list ) is used to create array
in Python with data type and value list specified in its arguments.
The main difference between lists and arrays in Python is that lists can hold items of different data types, while
arrays are designed to hold items of the same data type. Arrays are more memory efficient and provide better
performance for numerical operations. However, lists are more flexible and easier to use for general-purpose
programming.
You can access elements in a Python array using indexing, similar to how you would access elements in a list. The
index starts at 0 for the first element. For example: “`python import array arr = array.array(‘i’, [1, 2, 3, 4]) element =
arr[2] # Accessing the third element print(element) # Output: 3 “`
To create an array in Python, you can use the ‘array’ module. First, you need to import the module, and then you can
create an array by specifying the type code and the initial values. For example: “`python import array arr =
array.array(‘i’, [1, 2, 3, 4]) # ‘i’ indicates an array of integers “` This creates an array of integers with the values 1, 2, 3,
and 4
In below code Python create array : one of integers and one of doubles . It then prints the contents of each array to
the console.
Some of the data types are mentioned below which will help in create array in Python 3.8 of different data types.
Elements can be added to the Python Array by using built-in insert() function.
In order to access the array items refer to the index number. Use the index operator [ ] to access an item in a array in
Python. The index must be an integer.
Elements can be removed from the Python array by using built-in remove() function but an Error arises if element
doesn’t exist in the set.
Remove() method only removes one element at a time, to remove range of elements, iterator is used.
pop() function can also be used to remove and return an element from the array, but by default it removes only the
last element of the array, to remove element from a specific position of the array, index of the element is passed as
an argument to the pop() method.
Note – Remove method in List will only remove the first occurrence of the searched element.
Slicing of an Array
In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific range of
elements from the array, we use Slice operation .
Slice operation is performed on array with the use of colon(:). To print elements from beginning to a range use
[:Index], to print elements from end use [:-Index], to print elements from specific Index till the end use [Index:], to
print elements within a range, use [Start Index:End Index] and to print whole List with the use of slicing operation,
use [:]. Further, to print whole array in reverse order, use [::-1].
In order to search an element in the array we use a python in-built index() method. This function returns the index of
the first occurrence of value mentioned in arguments.
Updating Elements in an Array
In order to update an element in the array we simply reassign a new value to the desired index we want to update.
In Python, an array is used to store multiple values or elements of the same datatype in a single variable.
The extend() function is simply used to attach an item from iterable to the end of the array. In simpler terms, this
method is used to add an array of values to the end of a given or existing array.