Dwi Putri Wahyuningsih 2019390004 Midterm - DAA Mid-Term (70 Minutes) Answer All Questions and Submit Using MD-Doc or PDF File (100 Points)
Dwi Putri Wahyuningsih 2019390004 Midterm - DAA Mid-Term (70 Minutes) Answer All Questions and Submit Using MD-Doc or PDF File (100 Points)
2019390004
Midterm – DAA
Answer all questions and submit using MD-Doc or PDF file [100 points].
Design at least 3 algorithms for solving the following formula of ∑ni=02i and compare the
total basic operation for those three algorithms. [15 points]
a. n3 + 7n2 - 7n + 15
b. n2 + 12n + 20
c. n2 - 12n + 16
d. (n2 + n + 1)/14
e. n2 + 12n - 9
The following is the non-recursive code of Euclid algorithm in C Language to find the
CGD: problem-3.jpg
Code
import java.util.*;
import java.lang.*;
class GCDrecursive
{
// extended Euclidean Algorithm
public static int gcd(int a, int b)
{
if (a == 0)
return b;
// Driver Program
public static void main(String[] args)
{
int a = 45, b = 35, g;
g = gcd(a, b);
System.out.println("GCD(" + a + " , " + b+ ") = " +
g);
a = 1989; b = 1590;
g = gcd(a, b);
System.out.println("GCD(" + a + " , " + b+ ") = " +
g);
}
}
Result
In a scheduling problem, given nine jobs, with running times of 3, 6, 7, 10, 11, 16, 17, 18,
and 20 minutes You have three processors on which you can run these jobs [15 points]:
Write a program using Bruto Force String Matching to calculate how many times the
string can be found? Calculate the complexity of your program [ 15 points].
Code
//brute force algorithm
//string matching
import java.io.*;
import java.util.Scanner;
class Bruteforce{
//called function
public static int bruteforce(String text,String
tobematched){
int length = text.length();//length of the text
int plength = tobematched.length();//length of the
pattern;
//loop condition
for(int i=0;i<length-plength;i++){
//initialization of j
int j=0;
while((j < plength) && (text.charAt(i+j) ==
tobematched.charAt(j))){
j++;
}
if(j == plength){
return i;
}
}
return -1;
}
//process
Bruteforce obj = new Bruteforce();
Scanner sc = new Scanner(System.in);
//text
String text = "Bruto Force String Matching";
//word that want to be matched in the text
String tobematched = "Force";
//calling the function
int position = obj.bruteforce(text,tobematched);
int endindex = position+1;
//condition to check whether the pattern is
matched are not
if(position == -1){
System.out.println("Pattern is not matched in
the text");
}else{
System.out.println("Found at position:" +
(position+1));
System.out.println("End at the position:" +
(endindex + tobematched.length()));
}
// finish time taken
long finishtime = System.nanoTime();
NO TIME
1 31026500 nanoseconds
2 32875400 nanoseconds
3 54410400 nanoseconds
4 54722800 nanoseconds
5 30677200 nanoseconds
6 51271800 nanoseconds
7 37752200 nanoseconds
8 36596400 nanoseconds
9 41223800 nanoseconds
10 49818400 nanoseconds
Big-oh = 54722800 nanoseconds
Write a code using Bruto force (Exhaustive Search) to solve the following Knapsack
Problem [15 points]:
Knapsack capacity W= 19
1 2 Rp 120
2 4 Rp 320
3 9 Rp 450
4 7 Rp 707
5 5 Rp 500
Code
class Knapsack {
Write a code using Bruto force (Exhaustive Search) to solve the following Assignment
Problem [15 points]:
9 2 6 7 8
6 4 3 4 7
4 5 8 1 8
7 4 6 9 6
3 2 4 6 5
Code
class AssignmentProblem{
}
}
}
}
System.out.println("\n\nCheapest Assignments: ");
System.out.println(optimalIndex[0] + " + " +
optimalIndex[1] + " + " + optimalIndex[2] + " + " +
optimalIndex[3] + " = " + min_cost);
}
}
Result