0% found this document useful (0 votes)
202 views18 pages

Dwi Putri Wahyuningsih 2019390004 Midterm - DAA Mid-Term (70 Minutes) Answer All Questions and Submit Using MD-Doc or PDF File (100 Points)

The document contains the details of a midterm assignment for a Design and Analysis of Algorithms (DAA) course. It includes questions on analyzing the complexity of algorithms, designing recursive and non-recursive versions of the Euclidean algorithm to find the greatest common divisor (GCD) of two numbers, solving scheduling and knapsack problems using brute force algorithms, and solving an assignment problem also using a brute force approach. Code snippets are provided for some questions.

Uploaded by

Yuyun Joe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
202 views18 pages

Dwi Putri Wahyuningsih 2019390004 Midterm - DAA Mid-Term (70 Minutes) Answer All Questions and Submit Using MD-Doc or PDF File (100 Points)

The document contains the details of a midterm assignment for a Design and Analysis of Algorithms (DAA) course. It includes questions on analyzing the complexity of algorithms, designing recursive and non-recursive versions of the Euclidean algorithm to find the greatest common divisor (GCD) of two numbers, solving scheduling and knapsack problems using brute force algorithms, and solving an assignment problem also using a brute force approach. Code snippets are provided for some questions.

Uploaded by

Yuyun Joe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 18

Dwi Putri Wahyuningsih

2019390004

Midterm – DAA

Mid-term (70 Minutes)

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]

Show the complexity of the following [10 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

Design the recursive of Euclid algorithm? [15 points]

 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;

return gcd(b%a, a);


}

// 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]:

a. Computing the longest-running jobs first, on whatever processor is available

b. Computing the shortest job first, on whatever processor is available

c. Find the optimum solution

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;
}

public static void main(String[] args){


// start time taken
long starttime = System.nanoTime();

//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();

// finish - start time


long timeelapse = finishtime - starttime;

// print result time


System.out.println("\n" + "Time Efficiency = " +
timeelapse + " nanosecond" );
}
}
 Result
Time in nanoseconds

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

item weight value

1 2 Rp 120

2 4 Rp 320

3 9 Rp 450

4 7 Rp 707

5 5 Rp 500

 Code
class Knapsack {

// A utility function that returns


// maximum of two integers
static int max(int a, int b)
{
return (a > b) ? a : b;
}

// Returns the maximum value that


// can be put in a knapsack of
// capacity W
static int knapSack(
int W, int wt[],
int val[], int n)
{
// Base Case
if (n == 0 || W == 0)
return 0;

// If weight of the nth item is


// more than Knapsack capacity W,
// then this item cannot be included
// in the optimal solution
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);

// Return the maximum of two cases:


// (1) nth item included
// (2) not included
else
return max(
val[n - 1] + knapSack(W - wt[n - 1],
wt, val, n -
1),
knapSack(W, wt, val, n - 1));
}

// Driver program to test


// above function
public static void main(String args[])
{
int val[] = new int[] {120, 320, 450, 707, 500};
int wt[] = new int[] {2, 4, 9, 7, 5};
int W = 19;
int n = val.length;
System.out.println("The maximum value is " +
knapSack(W, wt, val, n));
}
}
 Result

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{

public static void main(String[] args)


{

int min_cost = Integer.MAX_VALUE; int


currentCost = 0; int optimalIndex[] = new int[4];
int cost[][] =
{{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}};
int count = 1;

for (int i = 0; i < 4; i++)


{ for (int j = 0; j < 4; j++)
{ for(int k = 0; k < 4; k++)
{ for(int l = 0; l < 4;
l++)
{
currentCost = cost[i][0] + cost[j]
[1] + cost[k][2] +cost[l][3];
System.out.println("Assignment #"+
count + ": " + cost[i][0] + " + " + cost[j][1] + " + " +
cost[k][2] + " + " + cost[l][3] + " = " + currentCost);
count++;

if(currentCost < min_cost)


{ min_co
st = currentCost;
optimalIndex[0] = cost[i][0];
optimalIndex[1] = cost[j][1];
optimalIndex[2] = cost[k][2];
optimalIndex[3] = cost[l][3];
}

}
}
}
}
System.out.println("\n\nCheapest Assignments: ");
System.out.println(optimalIndex[0] + " + " +
optimalIndex[1] + " + " + optimalIndex[2] + " + " +
optimalIndex[3] + " = " + min_cost);
}
}
 Result

You might also like