EECE7205 Fundamental of Computer Engineering: Project 1
EECE7205 Fundamental of Computer Engineering: Project 1
Engineering
Project 1
Problem Description:
You are given an input array [1, … , ]. A grouping of the array is described by
an array [1, … , ], where the array is partitioned into groups, the 1st group
consists of the first [1] elements of array , the 2nd group consists of the next [2]
elements, and so forth. Define array [1, … , ] such that [ ] is the summation of
the elements in the -th group of array . Use a dynamic programming algorithm to
find a grouping of array with groups such that we maximize the minimum
element of array .
Max-min-grouping( , , )
{
return [1, … , ]
}
Hint:
• The optimal subproblem property: suppose the optimal solution to Max-min-
grouping( , , ) is [1, … , ] = [ 1, 2, … , M-1, M] . Then [1, … , − 1] is
the optimal solution to the subproblem Max-min-grouping( , – 1, − 1).
• See Algorithm 2 in the paper.
Project Report
Max_Min_Tabulation(A, N, M)
let C[0 . . M - 1, 0 . . N - 1] and Sum[0 . . N - 1, 0 . . N - 1] be new tables
Sum = Sum_Tabulation(A, N)
for i = 0 to N - 1
C[0][i] = Sum[0][i]
for j = 1 to M - 1
for i = j to N - 1
max_num = 0
min_num = 0
for k = j – 1 to i – 1
min_num = min(C[j – 1][k], Sum[k + 1][i])
max_num = max(max_num, min_num)
C[j][i] = max_num
return C
Max_Min_Grouping(A, N, M)
let C[0 . . M - 1, 0 . . N - 1] be a new table
let G[0 . . M – 1] be a new array
C = Max_Min_Tabulation(A, N, M)
for j = 0 to M – 1
for i = j to N – 1
if C[j][i] == C[M – 1][N – 1]
G[j] = i + 1
else if C[j][i] < C[M – 1][N – 1]
G[j] = i + 2
for m = M – 1 to 1
G[m] = G[m] – G[m – 1]
return G
(2) Analysis of the running time
In the algorithm, clearly there is a 3-nested for loop, where asymptotically the time
complexity is O(MN2). To save an extra for loop calculating the sum, we tabulate the
sum within a separate loop, where the complexity there is O(N2). So formulate the
grouping, we have yet another loop with complexity O(M). So overall the complexity
is O(MN2).
fig.1 test1
fig.2 test2
fig.3 test3
Result: G = {2, 2, 3, 2, 3}.
fig.4 test4
#include <iostream>
#include <vector>
#include <algorithm>
}
}
for (int m = M - 1; m >= 1; m--)
{
G[m] = G[m] - G[m - 1];
}
return G;
}
int main()
{
vector<int> A = {3, 9, 7, 8, 2, 6, 5, 10, 1, 7, 6,
4};
int M = 3;
int N = A.size();
vector<int> G(M);
G = Max_Min_Grouping(A, N, M);