Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Introduction to Algorithms
A brief tutorial to be used in SE115, by İlker Korkmaz
algorithm, pseudocode, program code
“Both in mathematics and computer science as well, an algorithm is a step-by-step set of operations to be performed for calculating a partial function or a whole task.” Reference: https://en.wikipedia.org/wiki/Algorithm
algorithm, pseudocode, program code
“Pseudocode is an informal high-level description of the operating principle of an algorithm and is mainly intended for human reading rather than machine reading.” Reference: https://en.wikipedia.org/wiki/Pseudocode
algorithm, pseudocode, program code
A program code can refer to either a source code or a machine code.
“In computing, source code is any collection of computer instructions written using a programming language.” Reference: https://en.wikipedia.org/wiki/Source_code
“Source code (also referred to as source or code) is the version of software as it is originally written (i.e., typed into a computer) by a human in plain text (i.e., human readable alphanumeric characters).” Reference: http://www.linfo.org/source_code.html
algorithm, pseudocode, program code
“Machine code is a set of instructions executed directly by the CPU.” Reference: https://en.wikipedia.org/wiki/Machine_code
The fundamental phases of a program’s lifecycle
“ ... edit time, compile time, distribution time, installation time, link time, load time, run time, ... “
Reference: https://en.wikipedia.org/wiki/Program_lifecycle_phase
The relation among the memory (RAM), the processor (CPU), and the program (process)
“Prior to execution, a program must first be written. This is generally done in source code, which is then compiled at compile time (and statically linked at link time) to an executable. This executable is then invoked, most often by an operating system, which loads the program into memory (load time), possibly performs dynamic linking. At this point execution begins and the program enters run time. The program then runs until it ends, either normal termination or a crash.” Reference: https://en.wikipedia.org/wiki/Execution_(computing)
flowchart (or flow chart)
“A flowchart is a type of diagram that represents an algorithm, workflow or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows. This diagrammatic representation illustrates a solution model to a given problem.” Reference: https://en.wikipedia.org/wiki/Flowchart
activity diagrams
Activity diagrams are graphical representations of workflows of stepwise activities and actions. Activity diagrams show the flow of the activities.
Some favorite shape types used in activity diagrams: - rounded rectangles represent actions - diamonds represent decisions - arrows (or transition arrows) represent the order in which the activities occur Reference: https://en.wikipedia.org/wiki/Activity_diagram
some examples for activity diagrams using Unified Modeling Language (UML)
UML activity diagram of ‘if’ single-selection statement
UML activity diagram of ‘if-else’ double-selection statement
flow diagram of ‘while’ repetition statement
flow diagram of ‘for’ repetition statement
Simple Algorithms
problem/input/output/solution/complexity
There are many standard algorithms used in various tasks to solve a part or a whole of a problem given. Here in this tutorial, we present some of them, which are most widely used to solve some basic problems on a list of numbers given. Assuming that a list –or an array- of numbers are given as input, the solution as an algorithm will find the requested output using basic mathematics. In this tutorial an algorithm for a particular problem will be given with its time complexity .
finding sum
problem: Find the sum of the values of a list (data set) given.
input: A list of integers given
{i1, i2, i3, ... , in}
output: Sum of the values
= i1 + i2 + i3 + ... + in =
finding sum algorithm:
procedure ComputeSum(Array)
sum = 0
for each integer i in Array do
sum = sum + i
end for
return sum
end procedure
complexity: O(n) , where n is the size of Array, |Array|
finding average
problem: Find the arithmetic mean (average) of the values of a list given.
input: A list of integers given
{i1, i2, i3, ... , in}
output: Average (mean) of the values
= 1/n * Sum = 1/n
finding average algorithm: procedure ComputeAverage(Array) sum = 0 cnt = 0.0 average = 0.0 for each integer i in Array do sum = sum + i cnt = cnt + 1 end for average = sum / cnt return average end procedure
complexity: O(n), where n is the size (cardinality) of Array
finding minimum
problem: Find the minimum of the values of a list given.
input: A list of integers given
{i1, i2, i3, ... , in}
output: minimum of the values
= min{i1, i2, i3, ... , in}
finding minimum
algorithm: procedure FindMin(Array) min = first integer in Array for each integer i in Array do if i < min then min = i end if end for return min end procedure
complexity: O(n), where n is the size (cardinality) of Array
finding maximum
problem: Find the maximum of the values of a list given.
input: A list of integers given
{i1, i2, i3, ... , in}
output: maximum of the values
= max{i1, i2, i3, ... , in}
finding maximum
algorithm: procedure FindMax(Array) max = first integer in Array for each integer i in Array do if i > max then max = i end if end for return max end procedure
complexity: O(n), where n is the size (length) of Array
searching a value
problem: Find a key value searched in a list given.
input: A list of integers and a key
{i1, i2, i3, ... , in} , key
output: Yes/No
= 1 (True) if key is found in list, 0 (False) otherwise
searching a value
algorithm: procedure SearchKey(Array, key) flag = 0 for each integer i in Array do if i == key then flag = 1 end if end for return flag end procedure
complexity: O(n), where n is the size of Array
IF THE ARRAY IS SORTED, CAN YOU SEARCH IN ANY WAY FASTER THAN ABOVE?
quantity counting
problem: Find the quantity (number of times occured) of a key value in a list given.
input: A list of integers and a key
{i1, i2, i3, ... , in} , key
output: The quantity of key in the list
quantity counting
algorithm: procedure FindTheFrequency(Array, key) cnt = 0 for each integer i in Array do if i == key then cnt = cnt + 1 end if end for return cnt end procedure
complexity: O(n), where n is the size of Array
finding second minimum
problem: Find the 2nd minimum of the values of a list given.
input: A list of integers given
{i1, i2, i3, ... , in}
output: 2nd minimum of the values
= min{i1, i2, i3, ... , in - min{i1, i2, i3, ... , in}}
finding second minimum
algorithm: procedure FindSecondMin(Array) min1 = first integer in Array min2 = second integer in Array if min1 > min2 then temp = min1 min1= min2 min2 = temp end if for each integer i in Array do if i < min1 then min2 = min1 min1 = i else if min1 <= i AND i < min2 then min2 = i end if end for return min2 end procedure
complexity: O(n), where n is the size of Array
finding second maximum
problem: Find the 2nd maximum of the values of a list given.
input: A list of integers given
{i1, i2, i3, ... , in}
output: 2nd maximum of the values
= max{i1, i2, i3, ... , in - max{i1, i2, i3, ... , in}}
finding second maximum
algorithm: procedure FindSecondMax(Array) max1 = first integer in Array max2 = second integer in Array if max1 < max2 then temp = max1 max1= max2 max2 = temp end if for each integer i in Array do if i > max1 then max2 = max1 max1 = i else if max1 >= i AND i > max2 then max2 = i end if end for return max2 end procedure
complexity: O(n), where n is the size of Array
finding the index of a value
problem: Find the index of a key value searched in a list given.
input: A list of integers and a key
{i1, i2, i3, ... , in} , key
output: the index of the key / -1
= index if key is found in list, -1 otherwise
finding the index of a value
algorithm: procedure SearchIndexKey(Array, key) index = -1 cnt = 0 for each integer i in Array do if i == key then index = cnt end if cnt = cnt + 1 // WHAT IF DUPLICATES ? end for return index end procedure
complexity: O(n), where n is the size of Array
sorting an array (this topic is out-of-scope of this tutorial)
problem: Sort the elements of an array in an ascending/descending order.
input: Array given
{i1, i2, i3, ... , in}
output: ordered version of the Array
sorting an array (this topic is out-of-scope of this tutorial)
There are lots of well-known algorithms available to sort an array and they may be classified by various criteria with different complexities (please check https://en.wikipedia.org/wiki/Sorting_algorithm )