29
Department of CSE,Coimbatore Algorithms 2 CTPS 2018 LN #11 (2 Hrs)

Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Department of CSE,Coimbatore

Algorithms 2

CTPS 2018

LN #11

(2 Hrs)

Page 2: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Fibonacci Sequence

The sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The first two terms 0 and 1 are often chosen by default.

New term (c) = preceding term (a) + term before preceding term

(b).

a = 1; b = 0 then the third number is c = a + b = 0 + 1 = 1.

Similarly consecutive terms can be generated iteratively but making

necessary assignments to the preceding and term before preceding

terms!!

Department of CSE,Coimbatore

Page 3: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Given the first two numbers

a = 0

b = 1

the third number is c = a + b = 0 + 1 = 1.

Now to generate the fourth number the following reassignments

have to be made so that we can slide the preceding terms forward.

a = b

b = c

the fourth number is c = a + b = 1 + 1 = 2.

Let us work through

Department of CSE,Coimbatore

Page 4: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

0, 1, 1, 2, 3, 5, 8, 13, 21, …

So, we can generate consecutive Fibonacci numbers in pairs using

a = a+b and b = a+b.

a b

Thinking in terms of alternating a's

and b's our new a is a+b.

Our new b is our old b +

new a which is a+b again!!

Room for Improvement

Department of CSE,Coimbatore

Page 5: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Given the first two numbers

a = 0

b = 1

third and fourth numbers are

a = a + b = 0 + 1 = 1

b = a + b = 1(remember a is now 1) + 1 = 2

fifth and sixth numbers are

a = a + b = 1 + 2(remember b is now 2) = 3

b = a + b = 1(remember a is now 3) + 2 = 5

seventh and eighth numbers are

a = a + b = 3 + 5(remember b is now 5) = 8

b = a + b = 1(remember a is now 8) + 5 = 13

Let us check...

Department of CSE,Coimbatore

Page 6: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

1.Get n the number of Fibonacci numbers to be generated.

2.Initialize the first two Fibonacci numbers as a = 0 and b = 1 .

3.Initialize count of Fibonacci number generated .

4.While less than n Fibonacci numbers generated do

a) generate the next number as a = a+b.

b) generate the next to next number as b = a+b.

c) update count of number of Fibonacci numbers generated.

5. If n is even display all numbers else display all numbers except the

last.

Algorithm to Fibonacci Sequence

Department of CSE,Coimbatore

Page 7: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Department of CSE,Coimbatore

Set i = 1

While (i <= n) {

Display t1

nextTerm = t1 + t2

t1 = t2

t2 = nextTerm= i +1

i

}

Page 8: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Number pattern

Department of CSE,Coimbatore

12345

1234

123

12

1

Set i = 5

While( i>=1)

{

Set j = 1

While(j<=i)

{

Display j

j = j +1

}

Goto Next Line

i = i -1

}

Page 9: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

for(i=5;i>=1;i=i-1) {

for(j=1;j<=i;j = j+1){

Display j

}

Goto Next Line

}

Department of CSE,Coimbatore

Page 10: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Number pattern

Department of CSE,Coimbatore

Set i =5

While(i>=1) {

k = i

Set j = 1

While (j<=5) {

if(k <= 5) then Display k

else Display 5

k= k +1

j = j +1

}

i = i -1

Goto Next Line

}

55555

45555

34555

23455

12345

Page 11: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Department of CSE,Coimbatore

for(i=5;i>=1;i-1) {

k = i;

for(j=1;j<=5;j=j+1) {

if(k <= 5)

then Display k

else Display 5

k = k +1

}

Goto Next Line

}

Page 12: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Number pattern

Department of CSE,Coimbatore

for(i=4;i>=1;i =i-1) {

for(j=1;j<=4;j=j+1) {

if(j<=i)

then Print j

else Print “*”

}

for(j=4;j>=1;j=j-1) {

if(j<=i)

then Print j

else Print “*”

}

Goto Next Line

}

12344321

123**321

12****21

1******1

Page 13: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Fibonacci triangle

Department of CSE,Coimbatore

Enter the limit:9

1

1 1

1 1 2

1 1 2 3

1 1 2 3 5

1 1 2 3 5 8

1 1 2 3 5 8 13

1 1 2 3 5 8 13 21

1 1 2 3 5 8 13 21 34

Enter the limit:5

1

1 1

1 1 2

1 1 2 3

1 1 2 3 5

Page 14: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

for(i=1;i<=n; i= i+1) {

a=0;

b=1;

Display b

for(j=1;j<i;j=j+1) {

c=a+b;

Display c

a=b;

b=c;

}

Goto Next Line

} Department of CSE,Coimbatore

Page 15: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Matrix : Formatted o/p

Department of CSE,Coimbatore

for(i=0; i<r; i = i+1){

for(j=0; j<c; j = j +1){

Display a[i][j])

if (j == c-1)

then Goto Nextline

}

}

Page 16: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Matrix Transpose

Department of CSE,Coimbatore

for(i=0; i<r; i = i +1){

for(j=0; j<c; j = j +1 ){

transpose[j][i] = a[i][j]

}

}

Page 17: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Symmetric Matrix

Department of CSE,Coimbatore

for(i=0;i<row;i = i+1) {

for(j=0;j<col;j = j+1){

(q[j][i]!=a[i][j])

}

}

Page 18: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Department of CSE,Coimbatore

symm_flag=1

for(i=0;i<row;i = i+1) {

for(j=0;j<col;j = j+1){

if(q[i][j]!=p[i][j])

then symm_flag= symm_flag +1

}

}

if (symm_flag is equal to 1)

then Display “Symmetric”

else Display “ Not Symmetric”

Page 19: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Reverse an array

Department of CSE,Coimbatore

Declare i, temp

for(i=0;i < size/2; i++)

{

temp=a[i]

a[i]=a[((size-1)-i)]

a[((size-1)-i)]=temp

}

Page 20: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Department of CSE,Coimbatore

First run (when number of elements are EVEN)

Array elements before reverse : 11 22 33 44 55 66

Array elements After reverse : 66 55 44 33 22 11

Second run (when number of elements are

ODD)

Array elements before reverse : 11 22 33 44 55

Array elements After reverse : 55 44 33 22 11

Page 21: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Given the following array

The array after duplicate removal becomes

2 1362323231582

2 1323158 6

✓What has happened to the given ordered array?

✓What, do you think, is required to achieve this?

Duplicates in ordered array

Department of CSE,Coimbatore

Page 22: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Except the first element, all unique elements have shifted their

positions to as far to left as possible in the array.

A pair wise comparison to identify unique elements must be there

and it leads to two situations.

• A pair of duplicates has been encountered.

• Two elements are different.

Department of CSE,Coimbatore

Page 23: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

13623232315 136231513623232315

1 2 6543 41 2 3

j i

Consider the following case.

The position in the array where each most recently encountered

unique element must be located is determined at each instance by the

number of unique elements met so far!

Can be modeled by the assignment a[j] = a[i].

While all adjacent pairs have not been compared do

if they are not equal, shift the rightmost element to the array

position determined by the current unique element count.

Department of CSE,Coimbatore

Page 24: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Initialization choices: first element should remain untouched; first

two elements may be identical too!

• i = 1 and is a[i] equal to a[i+1]?

• i = 2 and is a[i-1] equal to a[i]? (direct termination)

The assignment a[j] = a[i] should be invoked only when duplicates

are encountered. This can be realized using a loop that compares

pair of elements until a duplicate is encountered.

while a[i-1] is not equal to a[i] do i = i +1

Loop can be terminated with a condition i<n to ensure that

termination works well even if there are no duplicates .

Department of CSE,Coimbatore

Page 25: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

1.Get an array a[1,...n] of n elements where n ≥ 1.

2. Set loop index i to 2 to allow correct termination.

3.Compare successive pairs of elements until a duplicate is encountered

then set unique element count j .

4.While all pairs have not been examined do

a)If next not duplicates then

⚫ Add one to unique element count j.

⚫ Move later element of pair to array position determined by

the unique element count j.

5. Return the array.

Algorithm to find duplicates

Department of CSE,Coimbatore

Page 26: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

Figure: Initial configuration of the puzzle

A playing board

• A playing board consists of 7 squares in a row numbered 0 to 6. Initial

positions of three white and three black pieces are as shown below. Pieces can

either move by sliding into an adjacent empty square or by jumping a single

adjacent piece into the empty square beyond. Aim: Swap the positions of the

black and white pieces

Department of CSE,Coimbatore

Page 27: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

✓ How many instructions your algorithm has?

✓ Did you observe that there may be varying number of

instructions still all solving the puzzle?

✓ Needless to say, do you realize that lesser of number of

instructions solving the puzzle is often preferred?Department of CSE,Coimbatore

Page 28: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

HCF and LCM of two numbers

Step 1 : Start

Step 2 : Read A, B

Step 3 : Assign A to X and B to Y

Step 4 : Divide A by B.

Step 5 : Is remainder zero? If yes, go to step 7.

Step 6 : Assign A to B and B to remainder. And go

to step 4

Step 7 : Assign B to HCF

Step 8 : Multiply X and Y and divide by HCF. And

assign result to LCM.

Step 9 : Display HCF and LCM

Step 10 : Stop

Page 29: Algorithms 2 CTPS 2018 · 2019. 5. 20. · •Programming Languages,2nd edition,Tucker and Noonan ... (Fall 2004) •Computing Without Computers,A Gentle Introduction to Computer

What has been described?

• Characteristics of algorithms

• Algorithms for various problems using combinations of

control structures

Credits

•Programming Languages,2nd edition,Tucker and Noonan

•www.cse.msu.edu/ Organization of Programming Languages-Cheng (Fall 2004)

•Computing Without Computers,A Gentle Introduction to Computer

• Programming,Data Structures and Algorithms,Version 0.15,Paul Curzon

•Google imagesDepartment of CSE,Coimbatore