Lab Session 06-2000031745
Lab Session 06-2000031745
Sriyaa Narisety
2000031745
Pre-Lab:
1) Explain why 0-1 Knapsack problems cannot be solved using greedy method unlike fractional
knapsack.
Solution:
0-1 knapsack problem cannot be solved by the greedy method because it is
enabled to fill the knapsack to full capacity .
2) Categorize the Following as single source or multiple source shortest path algorithms.
Floyd-Warshall algorithm
Dijkstra’s algorithm –
Bellman-Ford algorithm –
Solution:
Floyd-Warshall Algorithm : It is an algorithm for finding the shortest path
between all the pairs of vertices in a weighted
graph. It is an single source shortest path algorithm.
Bellman-Ford algorithm : This algorithm works correctly when some of the edges of
directed graph G may have negative weight. It is
a single-source shortest path algorithm .
Solution :
• Prim’s Minimal Spanning Tree Algorithm
• Travelling Salesman Problem
• Graph – Map Coloring
• Kruskal’s Minimal Spanning Tree Algorithm
• Dijkstra’s Minimal Spanning Tree Algorithm
• Graph – Vertex Cover
• Knapsack Problem
• Job Scheduling Problem
In-Lab:
1) Given an array of size n that has the following specifications:
a. Each element in the array contains either a police officer or a thief.
b. Each police officer can catch only one thief.
c. A police officer cannot catch a thief who is more than K units away from the police officer.
Solution:
import java.util.*;
import java.io.*;
class Main
{
// Returns maximum number of thieves
// that can be caught.
static int policeThief(char arr[], int n, int k)
{
int res = 0;
ArrayList<Integer> thi = new ArrayList<Integer>();
ArrayList<Integer> pol = new ArrayList<Integer>();
// can be caught
if (Math.abs(thi.get(l) - pol.get(r)) <= k) {
res++;
l++;
r++;
}
}
}
2) Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n
vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines,
which, together with the x-axis forms a container, such that the container contains the most water.
Notice that you may not slant the container.
Input
height = [1,8,6,2,5,4,8,3,7]
Output
49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the
max area of water (blue section) the container
Solution:
Post-Lab:
1) Given an array of jobs where every job has a deadline and associated profit if the job is finished
before the deadline. It is also given that every job takes a single unit of time, so the minimum possible
deadline for any job is 1. How to maximize total profit if only one job can be scheduled at a time.
Input
4
Job ID Deadline Profit
a 4 20
b 1 10
c 1 40
d 1 30
Output
Following is maximum
profit sequence of jobs is c, a
Solution:
import java.util.*;
class Main
{
// Each job has a unique-id,
// profit and deadline
char id;
int deadline, profit;
// Constructors
public Main() {}
// Driver code
public static void main(String args[])
{
ArrayList<Main> arr = new ArrayList<Main>();
// Calling function
job.printJobScheduling(arr, 3);
}
}
2) There are N Mice and N holes are placed in a straight line. Each hole can accommodate
only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or
move one step left from x to x − 1. Any of these moves consumes 1 minute.Assign mice
to holes so that the time when the last mouse gets inside a hole is minimized.
Example: positions of mice are: 4 -4 2
Positions of holes are: 4 0 5
Input:
A: list of positions of mice
B: list of positions of holes
Output:
single integer value
Solution:
import java.util.* ;
return Math.abs(max);
}