Introduction to Algorithms - ieu.edu.tr
Transcript of Introduction to Algorithms - ieu.edu.tr
![Page 1: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/1.jpg)
Introduction to Algorithms
A brief tutorial to be used in SE115, by İlker Korkmaz
![Page 2: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/2.jpg)
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
![Page 3: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/3.jpg)
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
![Page 4: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/5.jpg)
algorithm, pseudocode, program code
“Machine code is a set of instructions executed directly by the CPU.” Reference: https://en.wikipedia.org/wiki/Machine_code
![Page 6: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/6.jpg)
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
![Page 7: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/7.jpg)
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)
![Page 8: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/8.jpg)
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
![Page 9: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/9.jpg)
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
![Page 10: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/10.jpg)
some examples for activity diagrams using Unified Modeling Language (UML)
![Page 11: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/11.jpg)
UML activity diagram of ‘if’ single-selection statement
![Page 12: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/12.jpg)
UML activity diagram of ‘if-else’ double-selection statement
![Page 13: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/13.jpg)
flow diagram of ‘while’ repetition statement
![Page 14: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/14.jpg)
flow diagram of ‘for’ repetition statement
![Page 15: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/15.jpg)
Simple Algorithms
![Page 16: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/16.jpg)
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 .
![Page 17: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/17.jpg)
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 =
![Page 18: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/18.jpg)
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|
![Page 19: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/19.jpg)
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
![Page 20: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/20.jpg)
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
![Page 21: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/21.jpg)
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}
![Page 22: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/22.jpg)
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
![Page 23: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/23.jpg)
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}
![Page 24: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/24.jpg)
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
![Page 25: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/25.jpg)
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
![Page 26: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/26.jpg)
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?
![Page 27: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/27.jpg)
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
![Page 28: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/28.jpg)
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
![Page 29: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/29.jpg)
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}}
![Page 30: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/30.jpg)
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
![Page 31: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/31.jpg)
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}}
![Page 32: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/32.jpg)
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
![Page 33: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/33.jpg)
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
![Page 34: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/34.jpg)
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
![Page 35: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/35.jpg)
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
![Page 36: Introduction to Algorithms - ieu.edu.tr](https://reader035.fdocuments.net/reader035/viewer/2022062301/62a4d523ef754e3bf42e01f4/html5/thumbnails/36.jpg)
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 )