EECE7205 Fundamental of Computer
Engineering
Project 1
Name: Guohao Yao
NUID: 001306652
Submission instructions:
This is an individual project, and each student must implement the codes
independently. (Project 2 will be teamwork of at most 2 students.) In your first
submission attempt in Blackboard, upload a zip file including your source codes and
executable file. In your second submission attempt, upload a pdf file as project report.
The project report should have the following parts: (1) pseudo codes of your dynamic
programming algorithm. (2) Analysis of the running time asymptotically. (3)
Grouping results of several input examples including the one that A = {3, 9, 7, 8, 2, 6,
5, 10, 1, 7, 6, 4} and M = 3. (4) Source codes.
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
(1) Pseudo codes
Sum_Tabulation(A, N)
let Sum[0 . . N - 1, 0 . . N - 1] be a new table
for i = 0 to N - 1
for j = i to N - 1
if j == 0
Sum[i][j] = A[j]
else Sum[i][j] = Sum[i][j – 1] + A[j]
return Sum
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).
(3) Grouping results of several input examples
<1> Input: A = {3, 9, 7, 8, 2, 6, 5, 10, 1, 7, 6, 4}, M = 3.
fig.1 test1
Result: G = {3, 4, 5}.
<2> Input: A = {3, 9, 7, 8, 2, 6, 5, 10, 1}, M = 3.
fig.2 test2
Result: G = {3, 3, 3}.
<3> Input: A = {3, 9, 7, 8, 2, 6, 5, 10, 1, 7, 6, 4}, M = 5.
fig.3 test3
Result: G = {2, 2, 3, 2, 3}.
<4> Input: A = {3, 9, 7, 8, 2, 6, 5, 10, 1, 7, 6, 4, 5, 6, 7}, M = 5.
fig.4 test4
Result: G = {3, 3, 3, 4, 2}.
(4) Source codes
(Source codes are also packed in the ZIP file.)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> Sum_Tabulation(vector<int> &A, int
N)
{
vector<vector<int>> Sum(N, vector<int> (N, 0));
for (int i = 0; i <= N - 1; i++)
{
for (int j = i; j <= N - 1; j++)
{
if (j == 0)
{
Sum[i][j] = A[j];
}
else Sum[i][j] = Sum[i][j - 1] + A[j];
}
}
return Sum;
}
vector<vector<int>> Max_Min_Tabulation(vector<int> &A,
int N, int M)
{
vector<vector<int>> C(M, vector<int> (N, 0));
vector<vector<int>> Sum(N, vector<int> (N, 0));
Sum = Sum_Tabulation(A, N);
for (int i = 0; i <= N - 1; i++)
{
C[0][i] = Sum[0][i];
}
for (int j = 1; j <= M - 1; j++)
{
for (int i = j; i <= N - 1; i++)
{
int max_num = 0, min_num = 0;
for (int k = j - 1; k < i; k++)
{
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;
}
vector<int> Max_Min_Grouping(vector<int> &A, int N, int
M)
{
vector<int> G(M);
vector<vector<int>> C(M, vector<int> (N, 0));
C = Max_Min_Tabulation(A, N, M);
for (int j = 0; j <= M - 1; j++)
{
for (int i = j; i <= N - 1; i++)
{
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 (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);
cout << "The given array is A = { ";
for (int i = 0; i <= N - 2; i++)
{
cout << A[i] << ", ";
}
cout << A[N - 1] << " }" << '\n' << endl;
G = Max_Min_Grouping(A, N, M);
cout << "The objective array is G = { ";
for (int m = 0; m <= M - 2; m++)
{
cout << G[m] << ", ";
}
cout << G[M - 1] << " }" << endl;
return 0;
}