Elements of Algorithms - Georgetown...
Transcript of Elements of Algorithms - Georgetown...
![Page 1: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/1.jpg)
Elements of Algorithms
Mark MaloofDepartment of Computer Science
Washington, DC 20057
September 22, 2016
![Page 2: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/2.jpg)
Four Critical Elements
I Algorithms have some subset of the following criticalelements:1. simple statements, including but not limited to:
I input statementsI assignment statementsI output statementsI return statements
2. sequences of statements, which are also statements3. branching statements4. looping statements
![Page 3: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/3.jpg)
Algorithm for Simple Interest
1: input r , b, and m . input statement2: i ← r · b ·m . assignment statement3: output i . output statement
![Page 4: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/4.jpg)
Another Algorithm for Simple Interest
1: input r . input statement2: input b . input statement3: input m . input statement4: i ← r . assignment statement5: i ← i · b . assignment statement6: i ← i ·m . assignment statement7: output i . output statement
![Page 5: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/5.jpg)
Branching: If-then Statement
if some condition is true thenstatement (or sequence)
end if
![Page 6: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/6.jpg)
Flowchart for an if-then Statement
statementtrue
false
condition
![Page 7: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/7.jpg)
Example of an if-then Statement
1: input grade2: if grade > 64 then3: output pass4: end if5: if grade ≤ 64 then6: output fail7: end if
![Page 8: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/8.jpg)
Branching: If-then-else Statement
if some condition is true thenstatement (or sequence)
elsestatement (or sequence)
end if
![Page 9: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/9.jpg)
Flowchart for an if-then-else Statement
true
false
condition statement
statement
![Page 10: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/10.jpg)
Example of an if-then-else Statement
1: input grade2: if grade > 64 then3: output pass4: else5: output fail6: end if
![Page 11: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/11.jpg)
Looping: While Statement, While Loop
while some condition is true dostatement (or sequence)
end while
![Page 12: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/12.jpg)
Flowchart for a While Loop
false
true
condition
statement
![Page 13: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/13.jpg)
Example of a While Loop
1: input grade2: while there is a grade do3: if grade > 64 then4: output pass5: else6: output fail7: end if8: input grade9: end while
![Page 14: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/14.jpg)
Repeat-until Loop
repeatstatement (or sequence)
until some condition is true
Equivalent to:
statement (or sequence)while some condition is false do
statement (or sequence)end while
![Page 15: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/15.jpg)
For Loop
for i ← b, e dostatement (or sequence)
end for
Equivalent to:
i ← bwhile i ≤ e do
statement (or sequence)i ← i + 1
end while
![Page 16: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/16.jpg)
For-each Loop
for each element of some collection dostatement (or sequence)
end for
Equivalent to:
i ← 1e ← the number of elements in the collectionwhile i ≤ e doelement ← ith element of the collection
statement (or sequence)i ← i + 1
end while
![Page 17: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/17.jpg)
Example of a For-each Loop
1: Let Grades be a sequence or list of grades2: input Grades3: for each grade in Grades do4: if grade > 64 then5: output pass6: else7: output fail8: end if9: end for
![Page 18: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/18.jpg)
Algorithm for Binary-to-Decimal Conversion
1: Let D be a decimal number, set to zero2: Let B be a binary number, set to zero3: input B4: Let B ′ be B with its digits reversed5: i ← 06: for each binary digit b ∈ B ′ do7: D ← D + b · 2i8: i ← i + 19: end for
10: output D
![Page 19: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/19.jpg)
Program for B2D Conversion in Julia
D = 0
B = {1,1,0,0,1}
BPrime = B[end:-1:1]
i = 0
for b in BPrime
D = D + b * 2^i
i = i + 1
end
println( D )
![Page 20: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/20.jpg)
Program for B2D Conversion in C
#include <stdio.h>
#include <math.h>
int main()
{
int b[] = { 1, 0, 0, 1, 1 };
int n = 5;
int d = 0;
int i = 0;
for ( i = n - 1; i >= 0; i = i - 1 ) {
d = d + b[i] * (int) pow( 2.0, i );
}
printf( "%d\n", d );
return 0;
}
![Page 21: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/21.jpg)
Algorithm for Decimal-to-Binary Conversion
1: Let B be an empty sequence of binary digits2: Let D be a decimal number, set to zero3: input D4: while D 6= 0 do5: r ← D mod 26: Add r as the left-most digit of B7: D ← D ÷ 2 (integer division)8: end while9: output B
![Page 22: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/22.jpg)
Program for D2B Conversion in Julia
B = {}
D = 25
while D > 0
r = D % 2
unshift!( B, r )
D = div( D, 2 )
end
println( B )
![Page 23: Elements of Algorithms - Georgetown Universitypeople.cs.georgetown.edu/~maloof/idst010.f16/algorithms... · 2016-09-22 · Four Critical Elements I Algorithms have some subset of](https://reader033.fdocuments.in/reader033/viewer/2022060409/5f10084f7e708231d4471bac/html5/thumbnails/23.jpg)
Program for D2B Conversion in C
#include <stdio.h>
#include <math.h>
int main()
{
int d = 25;
int n = (int) ceil( log2( d ) );
int b[n];
int i = n - 1;
int r = 0;
while ( d > 0 ) {
r = d % 2;
b[i] = r;
i = i - 1;
d = d / 2;
}
for ( i = 0; i < n; i = i + 1 ) {
printf( "%d", b[i] );
}
printf( "\n" );
return 0;
}