algorithms and data structures

44
Algorithms and data Algorithms and data structures structures Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/

Upload: may-buckner

Post on 01-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Algorithms and data structures. Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/. Creative Commons. You are free to: share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material Under the following terms: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithms and data structures

Algorithms and data structuresAlgorithms and data structures

Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/

Page 2: Algorithms and data structures

Creative CommonsCreative Commons

You are free to:You are free to: shareshare — copy and redistribute the material in any medium or format — copy and redistribute the material in any medium or format adaptadapt — remix, transform, and build upon the material — remix, transform, and build upon the material

Under the following terms: Under the following terms: Attribution Attribution — You must give— You must give appropriate credit, appropriate credit, provide a link to the license, and provide a link to the license, and

indicate if changes were madeindicate if changes were made. You may do so in any reasonable manner, but not . You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. in any way that suggests the licensor endorses you or your use.

NonCommercial NonCommercial — You may not use the material for— You may not use the material for commercial purposes commercial purposes. . ShareAlike ShareAlike — If you remix, transform, or build upon the material, you must — If you remix, transform, or build upon the material, you must

distribute your contributions under the same license as the original.distribute your contributions under the same license as the original.

No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.

Text copied from http://creativecommons.org/licenses/by-nc-sa/3.0/

Algorithms and data structures, FER

Notices:

You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.

No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.

2 / 43

Page 3: Algorithms and data structures

RecursionRecursion

Page 4: Algorithms and data structures

The main idea of recursionThe main idea of recursion

A procedure calls another instance of itselfA procedure calls another instance of itself There must be a terminationThere must be a termination!!

Why a debt on a credit card cannot be paid using the same credit Why a debt on a credit card cannot be paid using the same credit cardcard??

RecursiveRecursive program programss are shorterare shorter, , but the execution is more time but the execution is more time consumingconsuming Some languagesSome languages ( (e.g.e.g. old versions ofold versions of FORTRAN) FORTRAN) do not allow recursiondo not allow recursion

For storing of results and for enabling the return from recursion, the For storing of results and for enabling the return from recursion, the stackstack data structure is used data structure is used

recursionrecursion::

see undersee under: : recursionrecursion

recursion:recursion:

iif still not clear, f still not clear, look under: recursionlook under: recursion

Algorithms and data structures, FER 4 / 43

Page 5: Algorithms and data structures

Elementary recursion and the system stackElementary recursion and the system stack

......

f(1);f(1);

......

void f(int i)void f(int i)

{{

int v;int v;

f(i+1);f(i+1);

return;return;

}}

void f(int i)void f(int i)

{{

int v;int v;

f(i+1);f(i+1);

return;return;

}}

void f(int i)void f(int i)

{{

int v;int v;

f(i+1);f(i+1);

return;return;

}}

void f(int i)void f(int i)

{{

int v;int v;

f(i+1);f(i+1);

return;return;

}}

11ret.addr.ret.addr.

vv

11ret.addr.ret.addr.

vv22

ret.addr.ret.addr.vv

11ret.addr.ret.addr.

vv22

ret.addr.ret.addr.vv33

ret.addr.ret.addr.vv

11ret.addr.ret.addr.

vv22

ret.addr.ret.addr.vv33

ret.addr.ret.addr.vv44

ret.addr.ret.addr.vv

ElementarnaRekurzija ElementarnaRekurzija

((ElementaryRecursion)ElementaryRecursion)

mainmain ff f’f’ f’’f’’ f’’’f’’’

Algorithms and data structures, FER 5 / 43

Page 6: Algorithms and data structures

Calculation of factorialsCalculation of factorials

Among the simplest recursive algorithms is the calculation ofAmong the simplest recursive algorithms is the calculation of n!n! for for n >= 0n >= 0 0! = 10! = 1 1! = 11! = 1 n! = n* (n-1)!n! = n* (n-1)!

ExampleExample: 4!: 4!k = fak = facct (4);t (4);

= 4 * fa= 4 * facct (3);t (3);

= 3 * fa= 3 * facct (2);t (2);

= 2 * fa= 2 * facct (1);t (1);

= 1 = 1

int fact(int n){int fact(int n){

if (n <= 1) {if (n <= 1) {

return 1;return 1;

} else {} else {

return n * fact(n-1);return n * fact(n-1);

}}

}}

Algorithms and data structures, FER 6 / 43

Page 7: Algorithms and data structures

Calculation of factorialsCalculation of factorials

......

i=fact(3);i=fact(3);

......

int fact(int n){int fact(int n){

if (n <= 1) {if (n <= 1) {

return 1;return 1;

} else {} else {

return return

n * fact(n-1);n * fact(n-1);

}}

}}

33

Faktorijeli(Factorials )Faktorijeli(Factorials )

mainmainint fact(int n){int fact(int n){

if (n <= 1) {if (n <= 1) {

return 1;return 1;

} else {} else {

return return

n * fact(n-1);n * fact(n-1);

}}

}}

int fact(int n){int fact(int n){

if (n <= 1) {if (n <= 1) {

return 1;return 1;

} else {} else {

return return

n * fact(n-1);n * fact(n-1);

}}

}}

factfact fact’’fact’’fact’fact’

3322

332211

1122

66

Algorithms and data structures, FER 7 / 43

Page 8: Algorithms and data structures

ExercisesExercises

Write the function that inputs two integer arguments Write the function that inputs two integer arguments xx and and yy and in and in its name returns the value for its name returns the value for xxyy.. PotencijaRekurzijom (PowerByRecursion)PotencijaRekurzijom (PowerByRecursion)

Function call:Function call:k = pot(2,5);k = pot(2,5);

= 2*pot(2,4)= 2*pot(2,4)

= 2*pot(2,3)= 2*pot(2,3)

= 2*pot(2,2)= 2*pot(2,2)

= 2*pot(2,1)= 2*pot(2,1)

= 2*pot(2,0)= 2*pot(2,0)

= 1= 1

Algorithms and data structures, FER 8 / 43

Page 9: Algorithms and data structures

Stack contentsStack contents

Algorithms and data structures, FER

pot(2,5)pot(2,5) pot(2,4)pot(2,4) pot(2,3)pot(2,3) pot(2,2)pot(2,2) pot(2,1)pot(2,1) pot(2,0)pot(2,0) returnreturn

11return return

2*12*1return return

2*22*2return return

2*42*4return return

2*82*8return return 2*162*16

(2,0)(2,0) 11

(2,1)(2,1) (2,1)(2,1) (2,1)(2,1) 22

(2,2)(2,2) (2,2)(2,2) (2,2)(2,2) (2,2)(2,2) (2,2)(2,2) 44

(2,3)(2,3) (2,3)(2,3) (2,3)(2,3) (2,3)(2,3) (2,3)(2,3) (2,3)(2,3) (2,3)(2,3) 88

(2,4)(2,4) (2,4)(2,4) (2,4)(2,4) (2,4)(2,4) (2,4)(2,4) (2,4)(2,4) (2,4)(2,4) (2,4)(2,4) (2,4)(2,4) 1616

(2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) (2,5)(2,5) 3232

9 / 43

Page 10: Algorithms and data structures

ExercisesExercises

What happens if the following line is omitted:What happens if the following line is omitted:if (y <= 0) return 1;if (y <= 0) return 1;

The function would call itself incessantly and it would never return any value to the main The function would call itself incessantly and it would never return any value to the main programprogram

For the above example, it would result in the following:For the above example, it would result in the following:

pot(2,5);pot(2,5); = 2*pot(2,4)= 2*pot(2,4) = 2*pot(2,3)= 2*pot(2,3) = 2*pot(2,2)= 2*pot(2,2) = 2*pot(2,1)= 2*pot(2,1) = 2*pot(2,0)= 2*pot(2,0) = 2*pot(2,-1)= 2*pot(2,-1) = 2*pot(2,-2)= 2*pot(2,-2) = 2*pot(2,-3)= 2*pot(2,-3) ......

Algorithms and data structures, FER 10 / 43

Page 11: Algorithms and data structures

ExercisesExercises

Solution without recursion:Solution without recursion:

Write the recursive functions to print the numbers from 1 to n, or from n to 1 in Write the recursive functions to print the numbers from 1 to n, or from n to 1 in different waysdifferent ways RekurzivniIspisRedom (RecursiveSequentialPrint)RekurzivniIspisRedom (RecursiveSequentialPrint)

Write the recursive function to calculate the Write the recursive function to calculate the nnthth member of the arithmetic seriesmember of the arithmetic series AritmetickiNiz (ArithmeticSeries)AritmetickiNiz (ArithmeticSeries)

int pot(long x, long y) {int pot(long x, long y) {

int retval = 1;int retval = 1;

int i;int i;

for (i = 0; i < y; i++) retval *= x;for (i = 0; i < y; i++) retval *= x;

return retval;return retval;

}}

Algorithms and data structures, FER 11 / 43

Page 12: Algorithms and data structures

Leonardo Pisano Fibonacci Leonardo Pisano Fibonacci

born: 1170 (probably) Pisa born: 1170 (probably) Pisa died: 1250 (probably) Pisadied: 1250 (probably) Pisa

In year 1202In year 1202 Liber abaci Liber abaci : : Introduction of Hindu-Arabic Introduction of Hindu-Arabic

numbers (in Europe)numbers (in Europe) Simultaneous linear equationsSimultaneous linear equations Commercial mathematical Commercial mathematical

problemsproblems Calculation of profitCalculation of profit Exchange rates for currenciesExchange rates for currencies

Algorithms and data structures, FER 12 / 43

Page 13: Algorithms and data structures

Arabic numbersArabic numbers

Do not match exactly Do not match exactly our “Arabic” numbersour “Arabic” numbers

The main achievement: The main achievement: introduction of zero and introduction of zero and of weighted positions of of weighted positions of digits within numbersdigits within numbers

٠٠ 0 Sifer 0 Sifer ١١ 1 Wahid1 Wahid٢٢ 2 Ithinin 2 Ithinin ٣٣ 3 Thalatha 3 Thalatha ٤٤ 4 Arba'a 4 Arba'a ٥٥ 5 Kamisa 5 Kamisa ٦٦ 6 Sita 6 Sita ٧٧ 7 Saba'a 7 Saba'a ٨٨ 8 Thamania 8 Thamania ٩٩ 9 Tisa'a 9 Tisa'a ٠٠ ١١ 10 Ashara10 Ashara

Algorithms and data structures, FER 13 / 43

Page 14: Algorithms and data structures

Fibonacci numbersFibonacci numbers

1, 1, 2, 3, 5, 8, 13, 21, 34,... (1, 1, 2, 3, 5, 8, 13, 21, 34,... (nextnext?)?)FF00=F=F11=1=1

FFii=F=Fi-2i-2+F+Fi-1i-1; i>1; i>1 The program is very short and completely corresponds to the mathematical The program is very short and completely corresponds to the mathematical

definitiondefinition– Low efficiencyLow efficiency int F(int n) {int F(int n) {

if (n <= 1) if (n <= 1)

return 1;return 1;

elseelse

return F(n-2) + F(n-1);return F(n-2) + F(n-1);

}}

Algorithms and data structures, FER 14 / 43

Page 15: Algorithms and data structures

Fibonacci numbers – program executionFibonacci numbers – program execution

F(0) is calculated 5 timesF(0) is calculated 5 timesF(1) is calculated 8 timesF(1) is calculated 8 timesF(2) is calculated 5 timesF(2) is calculated 5 timesF(3) is calculated 3 timesF(3) is calculated 3 timesF(4) is calculated 2 timesF(4) is calculated 2 timesF(5) is calculated 1 timeF(5) is calculated 1 timeF(6) is calculated 1 timeF(6) is calculated 1 time

Total : 25Total : 25

Fibonacci

F(4)F(4)

F(6)F(6)

F(5)F(5)

F(0)F(0)

F(2)F(2)

22F(1)F(1)11

33 F(3)F(3)

F(1)F(1) F(2)F(2)44

F(0)F(0) F(1)F(1)55 66

77

88

99

F(3)F(3)

F(1)F(1) F(2)F(2)

F(0)F(0) F(1)F(1)1111

1313

1414 F(4)F(4)

F(0)F(0)

F(2)F(2)

1616F(1)F(1)1515

1717 F(3)F(3)

F(1)F(1) F(2)F(2)1818

F(0)F(0) F(1)F(1)1919 2020

2121

2222

2323

1212

1010

2424

2525

11 11

22

11 11

2211

33

55

11 11 11 11

11 11

11 22

33222211

33

88

1313

55

Algorithms and data structures, FER 15 / 43

Page 16: Algorithms and data structures

Largest common divisorLargest common divisor

One of the oldest known algorithms is the Euclid‘s method for finding One of the oldest known algorithms is the Euclid‘s method for finding of the of the largest common divisor largest common divisor ((lcdlcd) of two nonnegative integers:) of two nonnegative integers:

Euklid (Euclid)

ifif bb = 0 = 0

lcdlcd = = aa

elseelse

lcdlcd = = largest common divisor oflargest common divisor of bb

and rest of divisionand rest of division a a withwith bb

Algorithms and data structures, FER 16 / 43

Page 17: Algorithms and data structures

Largest common divisor Largest common divisor – – example and the functionexample and the function

ExampleExample::lcd(22,8) = lcd(8,6) = lcd(6,2) = lcd(2,0) = 2lcd(22,8) = lcd(8,6) = lcd(6,2) = lcd(2,0) = 2

lcd(21,13) = lcd(13,8) = lcd(8,5) = lcd(5,3) = lcd(21,13) = lcd(13,8) = lcd(8,5) = lcd(5,3) = lcd(3,2) = lcd(2,1) = lcd(1,0) lcd(3,2) = lcd(2,1) = lcd(1,0) = 1= 1

lcd (21,0) = 21lcd (21,0) = 21

lcd (0,21) = lcd (21,0) = 21lcd (0,21) = lcd (21,0) = 21 Recursive functionRecursive function::

int lcd (int a, int b) {int lcd (int a, int b) {

if(b == 0) return a;if(b == 0) return a;

return lcd (b, a % b);return lcd (b, a % b);

}}

Algorithms and data structures, FER 17 / 43

Page 18: Algorithms and data structures

Searching for a member of an arraySearching for a member of an array

Recursive search for the index of the first member of one-Recursive search for the index of the first member of one-dimensional array of dimensional array of nn members with the value members with the value xx. If nonexistent, the . If nonexistent, the result is result is -1-1..

The search is initiated by the function call The search is initiated by the function call search(A, x, n, 0)search(A, x, n, 0)..

Rekurzija (Recursion)

int search(tint search(typeype A[], t A[], tyyppee x, int n, int i) { x, int n, int i) {

if(i >= n) return -1;if(i >= n) return -1;

if(A[i] == x) return i;if(A[i] == x) return i;

return search(A, x, n, i+1);return search(A, x, n, i+1);

}}

Algorithms and data structures, FER 18 / 43

Page 19: Algorithms and data structures

Search with sentinelSearch with sentinel

The search can become faster if beforehand the array is extended The search can become faster if beforehand the array is extended with a member called with a member called sentinelsentinel A[n] = x;A[n] = x;

The callThe call: : ttyyppee i; i;

A[n] = x;A[n] = x;

if ((i = if ((i = searchsearch1 (A, x, 0)1 (A, x, 0)) == n) ...) == n) ...

int search1 (type A[], type x, int i){int search1 (type A[], type x, int i){

if(A[i] == x)if(A[i] == x) return i;return i;

return search1 (A, x, i+1)return search1 (A, x, i+1)

}}

Algorithms and data structures, FER 19 / 43

Page 20: Algorithms and data structures

Searching for the largest array memberSearching for the largest array member

Finding the index of the largest member in array ofFinding the index of the largest member in array of nn membersmembers

Rekurzija (Recursion)

int maxmem (int A[], int i, int n) {int maxmem (int A[], int i, int n) {

int imax;int imax;

if (i >= n-1) return n-1;if (i >= n-1) return n-1;

imax = maxmem (A, i + 1, n);imax = maxmem (A, i + 1, n);

if (A[i] > A[imax]) return i;if (A[i] > A[imax]) return i;

return imax;return imax;

}}

Algorithms and data structures, FER 20 / 43

Page 21: Algorithms and data structures

Searching for the largest array memberSearching for the largest array member

Finding the largest member in array ofFinding the largest member in array of nn membersmembers Why is this implementation inefficientWhy is this implementation inefficient??

#define maxof(a,b) ((a) > (b) ? (a) : (b)) #define maxof(a,b) ((a) > (b) ? (a) : (b))

int maxmem2 (int A[], int i, int n) {int maxmem2 (int A[], int i, int n) {

if (i >= n-1) return A[i];if (i >= n-1) return A[i];

else return maxof(A[i], maxmem2 (A, i + 1, n));else return maxof(A[i], maxmem2 (A, i + 1, n));

}}

Rekurzija (Recursion)

Algorithms and data structures, FER 21 / 43

Page 22: Algorithms and data structures

Recursion characteristicsRecursion characteristics

Basic casesBasic cases Basic cases to be solved without recursion must always existBasic cases to be solved without recursion must always exist

ProgressionProgression For cases to be solved recursively, each consecutive recursive call must For cases to be solved recursively, each consecutive recursive call must

progress towards the basic casesprogress towards the basic cases Design ruleDesign rule

It is understood without questioning that every recursive call is functioningIt is understood without questioning that every recursive call is functioning No repetition ruleNo repetition rule

It should not be allowed to solve the same problem in separate recursive It should not be allowed to solve the same problem in separate recursive callscalls

This results in multiplication of processing, e.g.This results in multiplication of processing, e.g. see Fibonacci numberssee Fibonacci numbers

Algorithms and data structures, FER 22 / 43

Page 23: Algorithms and data structures

Example for errorExample for error

For the valueFor the value n = 1n = 1 recursive call is again withrecursive call is again with with the argumen with the argumentt 11 There is nThere is noo progressing towards the basic case progressing towards the basic case

The The program program does not work neither for other argument valuesdoes not work neither for other argument values: : E.g.E.g. for for n = 4n = 4,,thethe recursive call ofrecursive call of badbad has thehas the argument argument 4/3 +1 = 4/3 +1 = 22, , then then 2/3 +1 = 12/3 +1 = 1 and further on constantly and further on constantly 1/3 +1 = 11/3 +1 = 1

int bad (int n) {int bad (int n) {

if (n == 0) return 0;if (n == 0) return 0;

return bad (n / 3 + 1) + n - 1;return bad (n / 3 + 1) + n - 1;

}}

Algorithms and data structures, FER 23 / 43

Page 24: Algorithms and data structures

RecursionRecursion

ExercisesExercises

Page 25: Algorithms and data structures

InterestInterest

A given amount of money is deposited in the bank for a given A given amount of money is deposited in the bank for a given number of years with a given interest rate. Write a program to number of years with a given interest rate. Write a program to calculate the sum after the deposit is withdrawn. calculate the sum after the deposit is withdrawn.

Kamate (Interest)Kamate (Interest)

Algorithms and data structures, FER 25 / 43

Page 26: Algorithms and data structures

AnagramAnagram

AnagramAnagram – a word constructed from the starting word by a permutation of – a word constructed from the starting word by a permutation of lettersletters anagrams of DOG: DOG, DGO, ODG, OGD, GOD, GDOanagrams of DOG: DOG, DGO, ODG, OGD, GOD, GDO Word consisting of Word consisting of nn different letters - different letters - n!n! possible permutations possible permutations

How to construct them:How to construct them: Permutation of Permutation of n-1n-1 letters on the right hand side is always performed letters on the right hand side is always performed All the All the nn letters must rotate, whereby all the letters are shifted for one place letters must rotate, whereby all the letters are shifted for one place

to the left, except the first one, which goes to the furthest place on the right to the left, except the first one, which goes to the furthest place on the right These steps are repeated These steps are repeated nn times times

By rotation of letters, it is achieved that each letter may become the first oneBy rotation of letters, it is achieved that each letter may become the first one While the selected letter is the first one, all the other letters are permutatedWhile the selected letter is the first one, all the other letters are permutated

Basic case: permutation of a single letterBasic case: permutation of a single letter PremetaljkaPremetaljka (Anagram) (Anagram)

Algorithms and data structures, FER 26 / 43

Page 27: Algorithms and data structures

PalindromePalindrome

Write a program to check whether the given word or phrase is a Write a program to check whether the given word or phrase is a palindromepalindrome. In the input string blanks and punctuation should be . In the input string blanks and punctuation should be neglected. neglected. Examples: Examples:

– AmoreAmore,, Roma Roma – A man, a plan, a canal: Panama A man, a plan, a canal: Panama – A Toyota! Race fast, safe car! A Toyota! A Toyota! Race fast, safe car! A Toyota!

Hint: if the first and the last letter are omitted, the remaining text Hint: if the first and the last letter are omitted, the remaining text is also a palindromeis also a palindrome

ObrtaljkaObrtaljka (Palindrome) (Palindrome)

Algorithms and data structures, FER 27 / 43

Page 28: Algorithms and data structures

Towers of HanoiTowers of Hanoi

There are rods There are rods II (from Croatian (from Croatian IIzvor zvor = Source), = Source), OO (from Croatian (from Croatian OOdredištedredište = = Destination), Destination), PP (from Croatian (from Croatian PPomoćni omoćni = Auxiliary). On the first rod (= Auxiliary). On the first rod (II) there are ) there are nn disks of different sizes which can slide onto any rod. Disks are always disks of different sizes which can slide onto any rod. Disks are always ordered so that a larger one never tops a smaller one.ordered so that a larger one never tops a smaller one.

To cite the Wikipedia (http://en.wikipedia.org/wiki/Tower_of_Hanoi)To cite the Wikipedia (http://en.wikipedia.org/wiki/Tower_of_Hanoi): : The The objective of the puzzle is to move the entire stack to another rod, obeying the objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:following simple rules:

1.1. Only one disk can be moved at a time.Only one disk can be moved at a time.

2.2. EachEach move consists of taking the upper disk from one of the stacks and move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.uppermost disk on a stack.

3.3. No disk may be placed on top of a smaller diskNo disk may be placed on top of a smaller disk.. HanoiHanoi

Algorithms and data structures, FER 28 / 43

Page 29: Algorithms and data structures

Towers of HanoiTowers of Hanoi

Solution algorithm:Solution algorithm:Ignore the lowest (largest) disk, and solve the problem for Ignore the lowest (largest) disk, and solve the problem for n-1n-1

disks, moving them from the rod disks, moving them from the rod II to the rod to the rod PP using using OO as an as an auxiliary rodauxiliary rod

Now the largest disk is on Now the largest disk is on II, while the remaining , while the remaining n-1 n-1 are on are on PPMove the largest disk from Move the largest disk from II to to OOMove Move n-1n-1 disks from disks from PP to to OO using using II as as auxiliary (the problem is auxiliary (the problem is

already solved for already solved for n-1n-1 disks) disks) With three disks, the puzzle can be solved in seven moves. The With three disks, the puzzle can be solved in seven moves. The

minimum number of moves required to solve a Tower of Hanoi minimum number of moves required to solve a Tower of Hanoi puzzle is 2puzzle is 2nn - 1, where n is the number of disks. - 1, where n is the number of disks.

HanoiHanoi

Algorithms and data structures, FER 29 / 43

Page 30: Algorithms and data structures

Eight queensEight queens

Write the function to find positions of 8 queens on a chess board, so Write the function to find positions of 8 queens on a chess board, so that that none of them attacks any otherthat that none of them attacks any other

Solution algorithm:Solution algorithm: We inspect columns on the chess board from the first to the lastWe inspect columns on the chess board from the first to the last In each column we place a queenIn each column we place a queen We inspect the chessboard when We inspect the chessboard when ii queens have already been placed queens have already been placed (in (in ii

different columnsdifferent columns)),, but so that they do not attack each otherbut so that they do not attack each other We try to place the We try to place the ((ii ++ 11))thth queen queen so that she does not attack any of so that she does not attack any of

the already placed queens, and so that the already placed queens, and so that the rest of the queens can be the rest of the queens can be placed, satisfying the non attacking requirementplaced, satisfying the non attacking requirement

NoteNote:: There are There are 92 92 different solutionsdifferent solutions

KraljiceKraljice (Queens) (Queens)

Algorithms and data structures, FER 30 / 43

Page 31: Algorithms and data structures

Knight’s TourKnight’s Tour

Write the program to print the tour of knight onWrite the program to print the tour of knight on a a chess board chess board, , so so that he visits any field only oncethat he visits any field only once. . The knight’s tour starts in the upper The knight’s tour starts in the upper left cornerleft corner (A8). (A8). The knight moves in form of the letter L. In the figure, the fields the knight The knight moves in form of the letter L. In the figure, the fields the knight

can visit in his next movecan visit in his next move are markedare marked redred There are few billons of solutionsThere are few billons of solutions 122 122 million are closed pathsmillion are closed paths

KonjKonj (Knight) (Knight)

Algorithms and data structures, FER 31 / 43

Page 32: Algorithms and data structures

RecursionRecursion

Complexity in recursionComplexity in recursion

Page 33: Algorithms and data structures

An example for different complexities in solving An example for different complexities in solving a a same problem (M.A. Weiss) – 1 same problem (M.A. Weiss) – 1

An array of integers is given: AAn array of integers is given: A00, A, A22,…,A,…,An-1n-1. . e.g. e.g. int A [] = {4, -3, 5, -2, -1, 2, 6, -2};int A [] = {4, -3, 5, -2, -1, 2, 6, -2};

Numbers can be also negative. The Numbers can be also negative. The largest value of the largest value of the sum of consecutive numberssum of consecutive numbers is to be found. It is supposed is to be found. It is supposed that the that the maximum sum is 0 if all the numbers are negativemaximum sum is 0 if all the numbers are negative..

Practical example: Practical example: Find the maximum necessary capacity of the Find the maximum necessary capacity of the wallet if the owner comes to a market with an empty wallet and buys wallet if the owner comes to a market with an empty wallet and buys and sells after the consecutive prices. If the wallet is empty, payment and sells after the consecutive prices. If the wallet is empty, payment is by a credit card.is by a credit card.

RazneSlozenostiRazneSlozenosti (DifferentComplexities) (DifferentComplexities)

Algorithms and data structures, FER 33 / 43

Page 34: Algorithms and data structures

An example for different complexities in solving An example for different complexities in solving a a same problem (M.A. Weiss) – 2 same problem (M.A. Weiss) – 2

MaxSubSumArray3MaxSubSumArray3 O(nO(n33)) i = 1, n-1i = 1, n-1 j = i, n-1j = i, n-1 k = i, jk = i, j

All the possible subsequences are checked. In the outer loop the All the possible subsequences are checked. In the outer loop the first member of the subarray is iterated through its all possible first member of the subarray is iterated through its all possible values. In the first inner loop, the index of the last member is iterated values. In the first inner loop, the index of the last member is iterated from the index of the outer loop to the end. The second inner loop from the index of the outer loop to the end. The second inner loop runs from the index of the first loop to the index of the second loop. runs from the index of the first loop to the index of the second loop. In this way all the possible subarrays are exhaustively checked.In this way all the possible subarrays are exhaustively checked.

All the 3 loops are repeated All the 3 loops are repeated nn times in the worst case. Therefore the times in the worst case. Therefore the complexity is complexity is O(nO(n33) ) ..

Algorithms and data structures, FER 34 / 43

Page 35: Algorithms and data structures

An example for different complexities in solving An example for different complexities in solving aa same problem same problem (M.A. Weiss) – (M.A. Weiss) – 33

MaxSubSumArrayMaxSubSumArray22 O(nO(n22))If we noticeIf we notice: :

the complexity can be reduced by removingthe complexity can be reduced by removing one loop.one loop.

1j j

k j kk i k i

A A A

RazneSlozenostiRazneSlozenosti (DifferentComplexities) (DifferentComplexities)

Algorithms and data structures, FER 35 / 43

Page 36: Algorithms and data structures

An example for different complexities in solving a same problemAn example for different complexities in solving a same problem (M.A. Weiss) – (M.A. Weiss) – 44

MaxSubSumArray MaxSubSumArray O(nlogO(nlog22n)n) Rather complex recursive algorithmRather complex recursive algorithm If there were not a better (linear) solution, this would be a good example for If there were not a better (linear) solution, this would be a good example for

the power of recursion and the divide-and-conquer method.the power of recursion and the divide-and-conquer method. The input array is divided approximately in two halves, the solution might be The input array is divided approximately in two halves, the solution might be

found in the left hand side part, or in the right hand side part, or it results found in the left hand side part, or in the right hand side part, or it results from a contiguous sequence extending from left to right hand sides of the from a contiguous sequence extending from left to right hand sides of the array. The first two cases can be solved recursively. The last case can be array. The first two cases can be solved recursively. The last case can be checked by finding the maximum sum in the left hand side part, including its checked by finding the maximum sum in the left hand side part, including its last member and the maximum sum of the right hand side of the array, such last member and the maximum sum of the right hand side of the array, such that includes its first member. These two sums are then added up and that includes its first member. These two sums are then added up and compared to the previous two sums. compared to the previous two sums.

Algorithms and data structures, FER 36 / 43

Page 37: Algorithms and data structures

An example for different complexities in solving a same problemAn example for different complexities in solving a same problem (M.A. Weiss) – (M.A. Weiss) – 55

The largest left sum is formed from members 0 to 2 and amounts 6. The largest left sum is formed from members 0 to 2 and amounts 6. The largest right sum results in 8 from the sequence of members 5 The largest right sum results in 8 from the sequence of members 5 to 6. The maximum left sum including the last member is built from to 6. The maximum left sum including the last member is built from members 0 to 3 and equals 4, while on the right hand side it is the members 0 to 3 and equals 4, while on the right hand side it is the sum from members 4-6 and equals 7. Adding left and right gives 11 sum from members 4-6 and equals 7. Adding left and right gives 11 and that is the maximum sum.and that is the maximum sum. The calling program defines the initial array edges asThe calling program defines the initial array edges as 00 andand n-1n-1

44 -3-3 55 -2-2 -1-1 22 66 -2-2

00 11 22 33 44 55 66 77

Right partRight partLeft partLeft part

Algorithms and data structures, FER 37 / 43

Page 38: Algorithms and data structures

An example for different complexities in solving a same problemAn example for different complexities in solving a same problem (M.A. Weiss) (M.A. Weiss) – – 66

The source code is rather complicated, but it performs for an order of The source code is rather complicated, but it performs for an order of magnitude better than the previous one. It demonstrates that a magnitude better than the previous one. It demonstrates that a shorter code does not imply a better code!shorter code does not imply a better code!

If If nn were a power of were a power of 2 2 it can be intuitively seen that there would be it can be intuitively seen that there would be loglog22 n n successive halving. A more detailed proof will be given later successive halving. A more detailed proof will be given later

while discussing binary trees. As while discussing binary trees. As nn data are processed, the data are processed, the complexity is complexity is O(n logO(n log22n)n)..

It can be generally stated that an algorithm complexity is It can be generally stated that an algorithm complexity is O(logO(log22n)n)

if it divides the problem in time if it divides the problem in time O(O(11)) (usually that is halving). (usually that is halving). If each step reduces the problem sizeIf each step reduces the problem size for for 11, the complexity is , the complexity is O(n)O(n).. ..

Algorithms and data structures, FER 38 / 43

Page 39: Algorithms and data structures

An example for different complexities in solving a same problemAn example for different complexities in solving a same problem (M.A. Weiss) (M.A. Weiss) – – 77

MaxSubSumArrayMaxSubSumArray11 O(n)O(n)All the array members are added sequentially, and the sum which All the array members are added sequentially, and the sum which

was the largestwas the largest during the whole process is remembered.during the whole process is remembered.

Algorithms and data structures, FER 39 / 43

Page 40: Algorithms and data structures

AA posteriori posteriori analysis analysis

Example: calculate the Example: calculate the mode mode of a sorted integer array, i.e. find the of a sorted integer array, i.e. find the most frequent value and its frequency.most frequent value and its frequency. ModPolja (ModeArray)ModPolja (ModeArray)mode0mode0 direct solvingdirect solvingrmode0rmode0 recursive solving recursive solvingrmode1rmode1 recursive algorithm transformed into a direct one recursive algorithm transformed into a direct one

All the three algorithms have the execution time ofAll the three algorithms have the execution time of (n) (n). Which . Which one is the best?one is the best?

Algorithms and data structures, FER 40 / 43

Page 41: Algorithms and data structures

rmode0rmode0

Suppose in the array of Suppose in the array of nn members members a[0:n-1]a[0:n-1] the mode and its frequency the mode and its frequency f f have been calculated for the first have been calculated for the first n-1n-1 array members. Under what array members. Under what circumstances can the last array member change the mode? If circumstances can the last array member change the mode? If a[n-1] != a[n-1] != a[n-2]a[n-2] neither the mode, nor the frequency change. If they are equal, how neither the mode, nor the frequency change. If they are equal, how to distinguish among 3 possible cases:to distinguish among 3 possible cases: a) new mode has been founda) new mode has been found b) mode remains the same but the frequency b) mode remains the same but the frequency f f is incrementedis incremented c) neither mode nor frequency change c) neither mode nor frequency change

It depends whether It depends whether a[n-1] == a[n-1 - f]a[n-1] == a[n-1 - f] If yes, then there are If yes, then there are n-1 - (n-1 - f) +1 = f + 1n-1 - (n-1 - f) +1 = f + 1

appearances of the value contained in appearances of the value contained in a[n-1]a[n-1]. This means this value is . This means this value is either the new mode or the old mode but with increased frequency either the new mode or the old mode but with increased frequency ff..

Algorithms and data structures, FER 41 / 43

Page 42: Algorithms and data structures

ExercisesExercises

Examples for recursive function callsExamples for recursive function calls PrimjeriRekurzijePrimjeriRekurzije (ExamplesForRecursion) (ExamplesForRecursion)

Algorithms and data structures, FER 42 / 43

Page 43: Algorithms and data structures

ExercisesExercises

Write the program for calculation of binomial coefficient using the Write the program for calculation of binomial coefficient using the expression:expression:a) a) BINOM(BINOM(mm, , nn) = ) = mm!/(!/(nn!(!(mm--nn)!))!)

b) b) BINOM(BINOM(mm, , nn) = BINOM() = BINOM(mm-1, -1, nn) + BINOM() + BINOM(mm-1, -1, nn-1);-1); BINOM(BINOM(mm, 0) = BINOM(, 0) = BINOM(mm, , mm) = 1) = 1

BinomniKoeficijenti (BinomialCoefficient)BinomniKoeficijenti (BinomialCoefficient)

Write the function to print on the screen the first Write the function to print on the screen the first nn rows of the rows of the Pascal’s triangle. What is the complexityPascal’s triangle. What is the complexity - - a priori? a priori? PascalovTrokutRekurzija (PascalTriangle)PascalovTrokutRekurzija (PascalTriangle)

Algorithms and data structures, FER 43 / 43

Page 44: Algorithms and data structures

Travelling salesman problemTravelling salesman problem

Travelling Salesman Problem (TSP): Let Travelling Salesman Problem (TSP): Let GG be a set of be a set of nn towns, and towns, and ccijij fees fees for travelling from the town for travelling from the town i to the town to the town j.. Starting from a given city, it is Starting from a given city, it is required to visit all the cities exactly once, while the travelling cost should be at required to visit all the cities exactly once, while the travelling cost should be at minimum. minimum. cij = cji

TSP(Gi, G) = min (cij + TSP (Gj, G \ Gi))

jj

TSP(Gi, {Gk}) = cik

Complexity: O(n!), ~(n!/2)

TSPTSP

Algorithms and data structures, FER 44 / 43