If2225d - Tugas 6 & 7 - 3411181167
If2225d - Tugas 6 & 7 - 3411181167
If2225d - Tugas 6 & 7 - 3411181167
Disusun oleh :
Anissa Azhari
3411181167
DAFTAR ISI..................................................................................................................i
1. Soal 1.....................................................................................................1
2. Soal 2.....................................................................................................4
3. Soal 3.....................................................................................................7
4. Soal 4.....................................................................................................8
5. Soal 5...................................................................................................12
6. Soal 6...................................................................................................14
i
1. Soal 1
a. Source Code
package tugas8;
import java.util.ArrayList;
import java.util.List;
1
for (int j = 0; j <= capacity; j++) {
if (items[i - 1].weight > j)
matrix[i][j] = matrix[i-1]
[j];
else
// we maximize value at this
rank in the matrix
matrix[i][j] =
Math.max(matrix[i-1][j], matrix[i-1][j - items[i-1].weight]
+ items[i-1].value);
}
}
int res = matrix[NB_ITEMS][capacity];
int w = capacity;
List<Item> itemsSolution = new ArrayList<>();
for (int i = NB_ITEMS; i > 0 && res >
0; i--) {
if (res != matrix[i-1][w]) {
itemsSolution.add(items[i-
1]);
// we remove items value and
weight
res -= items[i-1].value;
w -= items[i-1].weight;
}
}
return new Solution(itemsSolution,
matrix[NB_ITEMS][capacity]);
}
package tugas8;
2
public Item(String name, int value, int weight) {
this.name = name;
this.value = value;
this.weight = weight;
}
package tugas8;
import java.util.List;
3
2. Soal 2
a. Source Code
package tugas8;
import java.util.*;
public class Job {
4
System.out.print("Jobs: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Profit: ");
for(int i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.println();
System.out.print("DeadLine:");
for(int i=0;i<n;i++){
System.out.print(c[i]+" ");
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(b[i]<b[j]){
int temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=c[i];
c[i]=c[j];
c[j]=temp;
String temp1=a[i];
a[i]=a[j];
a[j]=temp1;
}
}
}
System.out.println();
System.out.println("--Sorted Order--");
System.out.print("Jobs: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Profit: ");
for(int i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.println();
System.out.print("DeadLine:");
for(int i=0;i<n;i++){
System.out.print(c[i]+" ");
}
System.out.println();
int max=c[0];
for(int i=0;i<n;i++){
if(c[i]>max){
max=c[i];
}
}
String x[]=new String[max];
int xx[]=new int[max];
int profit=0;
for(int i=0;i<n;i++){
int pp=c[i];
pp=pp-1;
if(x[pp]==null ){
5
x[pp]=a[i];
profit+=b[i];
}else{
while(pp!=-1){
if(x[pp]==null){
x[pp]=a[i];
profit+=b[i];
break;
}
pp=pp-1;
}
}
}
for(int i=0;i<max;i++){
System.out.print("-->"+x[i]);
}
System.out.println();
System.out.print("Profit Earned"+profit);
}
}
6
3. Soal 3
a. Source Code
package tugas8;
import java.util.Scanner;
import java.util.PriorityQueue;
public class Merge {
//Function to find minimum computation
static int minComputation(int size, int files[]){
// create a min heap
PriorityQueue<Integer> pq = new
PriorityQueue<>();
for (int i = 0; i < size; i++) {
// add sizes to priorityQueue
pq.add(files[i]);
}
// variable to count total computations
int count = 0;
while (pq.size() > 1) {
int temp = pq.poll() + pq.poll();
count += temp;
pq.add(temp);
}
return count;
7
}
4. Soal 4
a. Source Code
package tugas8;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeMap;
8
static TreeMap<Character, String> codes = new
TreeMap<>();
static String text = "";
static String encoded = "";
static String decoded = "";
static int ASCII[] = new int[128];
printCodes();
System.out.println("-- Encoding/Decoding --");
encodeText();
decodeText();
return false;
}
9
flag = false;
break;
}
return flag;
}
10
}
}
if (node.left != null)
generateCodes(node.left, s + "0");
class Node {
Node left, right;
double value;
String character;
11
5. Soal 5
a. Source Code
package tugas8;
import java.io.*;
import java.util.*;
import java.lang.*;
class MST{
private static final int V = 5;
int minKey(int key[], Boolean mstSet[]){
// Initialize min value
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++){
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}
12
}
}
key[0] = 0;
parent[0] = -1;
13
6. Soal 6
a. Source Code
14
package tugas8;
import java.util.*;
import java.lang.*;
import java.io.*;
class ShortestPath {
// Fungsi utilitas untuk menemukan simpul dengan
nilai jarak minimum,
// dari himpunan simpul yang belum termasuk dalam
pohon jalur terpendek
static final int V = 6;
int minDistance(int dist[], Boolean sptSet[])
{
// Inisialisasi nilai min
int min = Integer.MAX_VALUE, min_index = -1;
return min_index;
}
15
sptSet[i] = false;
}
// Driver method
public static void main(String[] args)
{
/* Mari kita buat contoh grafik yang dibahas
di atas */
int graph[][] = new int[][] {
{ 0,
2, 0, 4, 0, 0},
{ 2,
0, 7, 1, 0, 0},
{ 0,
7, 0, 0, 2, 1},
{ 4,
1, 0, 0, 3, 0},
{ 0,
0, 2, 3, 0, 5},
{ 0,
16
0, 1, 0, 5, 0},
};
ShortestPath t = new ShortestPath();
t.dijkstra(graph, 0);
}
}
b. Hasil Eksekusi Program
17