CO34563 Assignment 4
CO34563 Assignment 4
1. For the following problems, write pseudocode solutions and state the worse case running time
(in terms of Θ or O where appropriate). Also give the correctness of the yours algorithms. You
will be graded on the efficiency of your solutions. After that implement the algorithms in any
programming language and verified the complexity of the yours algorithm.
Problem 1: Assume that we have N workers and N jobs that should be done. For each pair
(worker, job) we know salary that should be paid to worker for him to perform the job.
Our goal is to complete all jobs minimizing total inputs, while assigning each worker to
exactly one job and vice versa. Converting this problem to a formal mathematical
definition we can form the following equations:
C is a cost matrix of NxN where cij is cost of worker i to perform job j.
X is resulting binary matrix, where xij = 1 if and only if ith worker is assigned to jth job.
] one worker to one job assignment.
] one job to one worker assignment.
is a total cost function and it should be minimize.
We can also rephrase this problem in terms of graph theory. Let’s look at the job and
workers as if they were a bipartite graph, where each edge between the ith worker and jth
job has weight of cij. Write a program to find minimum-weight matching in the graph (the
matching will consists of N edges, because our bipartite graph is complete).
Solutions:
Approach 1: Hungarian Algorithm (Munkres Algorithm):
Pseudocode:
The Hungarian algorithm, aka Munkres assignment algorithm, utilizes the following
theorem for polynomial runtime complexity (worst case O(n3)) and guaranteed
optimality: If a number is added to or subtracted from all of the entries of any one row or
column of a cost matrix, then an optimal assignment for the resulting cost matrix is also
an optimal assignment for the original cost matrix.
Time complexity : O(n^3),This is because the algorithm implements the Hungarian
algorithm, which is known to have a time complexity of O(n^3).
Space complexity : O(n^2), where n is the number of workers and jobs.
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
1
0801CS211072 Raj Gupta
void init_labels()
{
memset(lx, 0, sizeof(lx));
memset(ly, 0, sizeof(ly));
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
lx[x] = max(lx[x], cost[x][y]);
}
void update_labels()
{
int x, y;
int delta = 99999999; //init delta as infinity
for (y = 0; y < n; y++) //calculate delta using slack
if (!T[y])
delta = min(delta, slack[y]);
for (x = 0; x < n; x++) //update X labels
if (S[x])
lx[x] -= delta;
for (y = 0; y < n; y++) //update Y labels
if (T[y])
ly[y] += delta;
for (y = 0; y < n; y++) //update slack array
if (!T[y])
slack[y] -= delta;
}
2
0801CS211072 Raj Gupta
int q[31], wr = 0, rd = 0; //q - queue for bfs, wr,rd - write and read
//pos in queue
memset(S, false, sizeof(S)); //init set S
memset(T, false, sizeof(T)); //init set T
memset(prev_ious, -1, sizeof(prev_ious)); //init set prev_ious - for the alternating tree
wr = rd = 0;
3
0801CS211072 Raj Gupta
4
0801CS211072 Raj Gupta
return ret;
}
int assignmentProblem(int Arr[], int N) {
n = N;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
cost[i][j] = -1*Arr[i*n+j];
return ans;
}
};
int main()
{
int n=3;
int Arr[3*3]={1500,4000,4500,2000,6000,3500,2000,4000,2500}; /*1500 4000 4500
5
0801CS211072 Raj Gupta
current_assignment[job] = None
return None
Problem 2: Suppose you are given an array A[1 .. n] of integers, each of which may be positive,
negative, or zero. A contiguous subarray A[i .. j] is called a positive interval if the sum of its
entries is greater than zero. Describe and analyze an algorithm to compute the minimum
number of positive intervals that cover every positive entry in A. For example, given the
following array as input, your algorithm should output the number 3.
Approach
One approach to solving this problem is to use a greedy algorithm. The algorithm
scans the array from left to right, maintaining a running sum. Whenever the running
sum becomes positive, it starts a new positive interval. Whenever the running sum
becomes negative or zero, it ends the current positive interval. By doing this, we
can count the number of positive intervals needed to cover every positive entry in
the array.
Pseudocode:
function min_positive_intervals(A):
n = length of array A
intervals = 0
running_sum = 0
for i from 1 to n:
running_sum += A[i]
if running_sum > 0:
intervals++
running_sum = 0
return intervals
Time Complexity:
This algorithm has a time complexity of O(n) since it scans the array once.
Problem 3: Consider the following bridge crossing problem where n people with speeds s1; : : : ;
sn wish to cross the bridge as quickly as possible. The rules remain:
6
0801CS211072 Raj Gupta
time_taken = 0
crossing_sequence = []
if length of people_speeds is 2:
// Two people left, they cross together
time_taken += max(people_speeds[0], people_speeds[1])
crossing_sequence.append([people_speeds[0], people_speeds[1]])
break
7
0801CS211072 Raj Gupta
Program:
Complexity Analysis
Sorting O(nlogn) time, (n) iterations in the worst case.
The overall time complexity O(nlogn)
To prove the correctness of this algorithm, we need to show that it always produces an optimal
solution. We can argue that selecting the fastest pairs to cross at each step minimizes the total
time taken for all crossings.
Since the two people in each crossing pair must walk together at the rate of the slower person's
pace, it's optimal to pair the fastest person with the slowest person available. This way, the faster
8
0801CS211072 Raj Gupta
person's speed is effectively "wasted" by matching them with the slower person, minimizing the
total time taken for each crossing.
Therefore, by always selecting the fastest pairs to cross, the algorithm ensures that each trip takes
the least possible time, resulting in an optimal overall solution.
Program:
9
0801CS211072 Raj Gupta
Analysis:
The time complexity of this approach is O(rows×cols)
Problem 10: There are N floors and N persons each one is tagged with some random unique
number between 1 to N (represents floor number). We have a lift which can
accommodate one person at a time. Every person is in some random floor. Initially lift is at
floor 1 and all floors have single person. Design an algorithm to move the persons to their
corresponding floor with minimum number of lift movements. [Restriction: Lift can have
at most one person at a time. While moving persons, at some point of time, we can keep
more than one person at one floor.
One approach to solving is a Greedy Algorithm, specifically the "Nearest Neighbor" approach. This
approach involves always moving the lift to the nearest floor where a person needs to be dropped off. Here's
the pseudocode for this approach:
10
0801CS211072 Raj Gupta
function elevatorScheduling(persons):
n = length of persons
lift_position = 1
Program:
11
0801CS211072 Raj Gupta
Algorithm Analysis:
The time complexity of this approach is O(N log N).
12