
function [R_max, tau_opt] = Brute_force(k, gamma, step, flag)
% this function finds the optimal value of tau by using brute force
% flag = 1 indicates that we want to solve the max sum-throughput problem;
% flag = 2 indicates that we want to maximize the common throughput

tau = zeros(1, k);
num = 1 / step + 1;
R_max = 0;
for i = 1 : num^k - 1
    % generate the values of tau for all the k users
    tau(1) = floor(i/num^(k - 1));
    step_sum = 0;
    for j = 2 : k
        step_sum = step_sum + tau(j-1)*num^(k - j + 1);
        tau(j) = floor((i - step_sum)/num^(k - j));
    end
    tau =tau*step;
    % for tau0 > 0 and tau_i > 0 for all the users, calculate the
    % corresponding throughput 
    if sum(tau) < 1
        tau0 = 1 - sum(tau);
        R = zeros(1, k);
        for m = 1 : k
            if tau(m) ~= 0
                R(m) = tau(m)*log2(1+gamma(m)*tau0/tau(m));
            end
        end
        % find the optimal tau which leads to the optimal sum or common throughput 
        if flag == 1 && sum(R) > R_max
            R_max = sum(R);
            tau_opt = [tau0, tau];
        elseif flag == 2 && min(R) > R_max
            R_max = min(R);
            tau_opt = [tau0, tau];
        end
    end
end

