&6(°±±²
$lgorithms
,ntroduction to $lgorithms

$lgorithm 'efinition
• $ finite set of statements that guarantees an optimal solution in
finite interval of time
• $lgorithmic thinking and problem solving skill are vital in making
efficient solutions³
• 7he (nglish word ´$/*25,7+0´ derives from the /atin word $/°
$/°.+:$5,=0,’6 name³ +e developed the concept of an
algorithm in 0athematicsµ and thus sometimes being called the
“*randfather of &omputer 6cience´³
±

*lance of $lgorithm
• $n algorithm is a finite set of instructions or logicµ written in orderµ
to accomplish a certain predefined task³
• $lgorithm is not the complete code or program
• &an be expressed either as an informal high level description as
pseudocode or using a flowchart³
¶

$lgorithm 6pecifications
• Input
±
(very $lgorithm must take zero or more number of input values from
external³
• Output
±
(very $lgorithm must produce an output as result³
• +**Definiteness
°
(very statement·instruction in an algorithm must be clear and
unambiguous ¸only one interpretation¹
• ,,Finiteness
±
)or all different casesµ the algorithm must produce result within a
finite number of steps³
• ,++Effectiveness
±
(very ,nstruction must be basic enough to be carried out and it
also must be feasible³
º

*ood $lgorithms
"
• 5un in less time
• &onsume less memory
%ut computational resources ¸time complexity¹ usually important
»

¼
$nalyzing $lgorithms
•
3redict the amount of resources required½
•
memory
½ how much space is needed%
•
computational time
½ how fast the algorithm runs%
•
)$&7½ running time grows with the size of the input
•
,nput size ¸number of elements in the input¹
–
6ize of an arrayµ polynomial degreeµ ¾ of elements in a matrixµ ¾ of bits in the binary
representation of the inputµ vertices and edges in a graph
+**Def%!
5unning time (4 the number of primitive operations ±steps° executed before termination
–
$rithmetic operations ¸¿µ °µ À¹µ data movementµ controlµ decision making ¸
NOK± \]\MNOQRJ
¹µ
comparison

Á
$lgorithm $nalysis½ (xample
• (''lg°%!
0,1 ¸a>²@µ …µ a>[email protected]¹
m ← a>²@ !
for i ← ± to n
if a>[email protected] !" m
then m ← a>[email protected] !
• EZuaSaS\NaSZL Yt\N`RX
½
– the number of primitive operations ¸steps¹ executed before termination
T±n² ($##³
>first [email protected] ¿
±n²
>for [email protected] ¿
±n´³²
>if [email protected] ¿
±n´³²
>the assignment in [email protected] "#
µn ´ ³
• 2rder ¸rate¹ of growth½
– 7he leading term of the formula
– (xpresses the asymptotic behavior of the algorithm

Â
7ypical 5unning 7ime )unctions
• ² ¸constant running time¹½
– ,nstructions are executed once or a few times
• log1 ¸logarithmic¹
– $ big problem is solved by cutting the original problem in smaller sizesµ by a constant
fraction at each step
• 1 ¸linear¹
– $ small amount of processing is done on each input element
• 1 log1
– $ problem is solved by dividing it into smaller problemsµ solving them independently and
combining the solution

Ã
7ypical 5unning 7ime )unctions
• 1
±
¸quadratic¹
– 7ypical for algorithms that process all pairs of data items ¸double nested loops¹
• 1
¶
¸cubic¹
– 3rocessing of triples of data ¸triple nested loops¹
• 1
.