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

Algorithms Complexity Reviewer Lahat Ng Ni Lesson

The document covers fundamental concepts of algorithms and complexity, defining an algorithm as a finite sequence of instructions for problem-solving. It distinguishes between algorithms and programs, outlines criteria for algorithms, and discusses performance metrics such as time and space complexity. Additionally, it introduces Python data types and their functionalities, emphasizing the importance of algorithms and flowcharts in programming.

Uploaded by

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

Algorithms Complexity Reviewer Lahat Ng Ni Lesson

The document covers fundamental concepts of algorithms and complexity, defining an algorithm as a finite sequence of instructions for problem-solving. It distinguishes between algorithms and programs, outlines criteria for algorithms, and discusses performance metrics such as time and space complexity. Additionally, it introduces Python data types and their functionalities, emphasizing the importance of algorithms and flowcharts in programming.

Uploaded by

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

Algorithms and Complexity

Week 1 Lesson 1- Algorithm Basic Concepts

What is an Algorithm?

• Algorithm is a procedure or formula for solving a problem, based on conducting a sequence of


specified actions.
• finite sequence of instructions, each of which has a clear meaning and can be performed with a finite
amount of effort in a finite length of time.
• Derives from the name of the mathematician, Mohammed ibn-Musa al-Khwarizmi
• An encryption algorithm transforms data according to specified actions to protect it.
o Encrypt – to secure
o Decrypt – Removing the Security (If the algorithm is sufficiently sophisticated, no one can decrypt
the data.)

Algorithm Vs. Program

• Algorithm is a set of instructions while Program is consist of various algorithms.

Every algorithm must satisfy the following criteria:

✓ 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.

We represent algorithm using a pseudo language that is a combination of the constructs of a


programming language together with informal English statements.
Performance of a program:

• The performance of a program is the amount of computer memory and time needed to run a program.

o In performance analysis we use analytical methods.


o In performance measurement we conduct experiments.

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:

• The amount of memory it needs to run to completion.

The space need by a program has the following components:

• 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.

Algorithm Design Goals

The three basic design goals that one should strive for in a program are:

1. Try to save Time


2. Try to save Space
3. Try to save Face

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.

2. Average Case: The expected value of f(n).

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:

11. Big–OH (O) 1

2. Big–OMEGA (Ω)

3. Big–THETA (θ) and

4. Little–OH (o)

HOW TO WRITE ALGORITHMS

Step 1: Define your algorithms input.

Step 2: Define the variables: ex. HEIGHT and WIDTH (or H & W).

Step 3: Outline the algorithm's operations.

Step 4: Output the results of your algorithm's operations.

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:

✓ Execellent way of communicating the logic of a program.


✓ Easier and efficient to analyze problem.
✓ During program development cycle, the flowchart plays the role of a blueprint, which makes program
development process easier.
✓ The flowchart makes program or system maintenance easier.
✓ Easier to convert into any programming language code.
Example Problems

Problem 1: Find the area of a Circle of radius r.

Inputs to the algorithm: Algorithm:

Radius r of the Circle. Step1: Start

Expected output: Step2: Read\input the Radius (r) of the Circle

Area of the Circle Step3: Area PI*r*r // calculation of area

Step4: Print Area

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

Sum of the two numbers. Step5: Print Sum

Step6: End

Problem 3: Convert temperature Fahrenheit to Celsius

Inputs to the algorithm: Algorithm:


Temperature in Fahrenheit Step1: Start

Expected output: Step 2: Read Temperature in Fahrenheit F

Temperature in Celsius Step 3: C 5/9*(F32)

Step 4: Print Temperature in Celsius: C

Step5: End

Branching Selection Flowchart

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

S2: Read A,B

S3: Condition: True

Print A

S4: Condition: False

Print B

S5: End
Week 2 Lesson 2: RECALL OF PROGRAMS AND RULE OF SUMS AND ITS ALGORITHM EXAMPLES

What is a Computer Program?

• Detailed procedure for solving a problem with a computer.


• The distinction between computer programs and equipment is often made by referring to the former
as software and the latter as hardware.

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.

Similarities Between Applications and Programs

The main similarity between applications and programs is the fact that they are both used to perform various
functions or tasks on a computer.

Program Vs. Application

➢ 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.

Differences Between an Application and a Program

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.

Week 3 Lesson 3: Geeks for Geeks Data Types


Python Data types

- 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

1. Numeric Data Types in Python

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.

- Sequences allow storing of multiple values in an organized and efficient fashion.

There are several sequence data types of Python:

• String Data Type


- Strings in Python are arrays of bytes representing Unicode characters.
- A string is a collection of one or more characters put in a single quote, double-quote, or triple-quote.
- In Python, there is no character data type Python, a character is a string of length one. It is represented by str
class.
Accessing elements of String
In Python programming , individual characters of a String can be accessed by using the method of Indexing. Negative
Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last
character, -2 refers to the second last character, and so on.

• List Data Type


- Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very flexible
as the items in a list do not need to be of the same type.
Creating a List in Python - Lists in Python can be created by just placing the sequence inside the square brackets[].

Python Access List Items


In order to access the list items refer to the index number. Use the index operator [ ] to access an item in a list.
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc.
• Tuple Data Type
- Just like a list, a tuple is also an ordered collection of Python objects.
-The only difference between a tuple and a list is that tuples are immutable i.e. tuples cannot be modified after it is
created. It is represented by a tuple class.
Creating a Tuple in Python
In Python Data Types, tuples are created by placing a sequence of values separated by a ‘comma’ with or without the
use of parentheses for grouping the data sequence.
Tuples can contain any number of elements and of any datatype (like strings, integers, lists, etc.).
Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is
not sufficient, there must be a trailing ‘comma’ to make it a tuple.

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

3. Boolean Data Type in Python

- 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.

4. Set Data Type in Python

- 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.

Create a Set in Python

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.

Access Set Items

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

5. Dictionary Data Type in Python

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.

Key-value is provided in the dictionary to make it more optimized.


Each key-value pair in a Dictionary is separated by a colon :

, whereas each key is separated by a ‘comma’.

Create a Dictionary in Python

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.

Accessing Key-value in Dictionary

In order to access the items of a dictionary refer to its key name.

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.

Block can be regarded as the grouping of statements for a specific purpose.

Python Arrays

What is an Array in Python?

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).

- are a data structure that can hold a collection of items.

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.

Create an Array in Python

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.

What is the difference between Lists and Arrays in Python?

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.

How do you access elements in a Python Array?

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 “`

How do you create an Array in Python?

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.

Complexities for Creation of Arrays:

Time Complexity: O(n)


Auxiliary Space: O(n)

Some of the data types are mentioned below which will help in create array in Python 3.8 of different data types.

Adding Elements to an Array

Elements can be added to the Python Array by using built-in insert() function.

Insert is used to insert one or more data elements into an array.


Based on the requirement, a new element can be added at the beginning, end, or any given index of
array. append() is also used to add the value mentioned in its arguments at the end of the Python array.

Accessing Elements from the Array

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.

Removing Elements from the Array

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].

Searching Element in an Array

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.

Different Operations on Python Arrays

Counting Elements in an Array

In order to count elements in an array we need to use count method.

Reversing Elements in an Array

In order to reverse elements of an array we need to simply use reverse method.

Extend Element from Array

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.

Syntax of list extend()

The syntax of the extend() method:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy