Master Methode MATHLAB ?
7 views (last 30 days)
Show older comments
function T = master_method(a, b, k)
% Master Method to solve recurrence T(n) = a * T(n/b) + n^k
% Check input values
if a <= 0 || b <= 1 || k < 0
error('Invalid input values: a must be positive, b must be greater than 1, and k must be non-negative.');
end
% Base cases of the Master Method
if k > log(b) / log(a)
% Case 1: T(n) = Θ(n^k)
T = 'Θ(n^k)';
elseif k == log(b) / log(a)
% Case 2: T(n) = Θ(n^k * log(n))
T = 'Θ(n^k * log(n))';
else
% Case 3: T(n) = Θ(n^(log_b(a)))
T = 'Θ(n^(log_b(a)))';
end
% Display the asymptotic time complexity
disp(['The asymptotic time complexity T(n) is: ', T]);
end
% Beispielaufrufe
master_method(6, 6, 1);
master_method(3, 2, 2);
master_method(2, 3, 1);
1 Comment
Ganesh
on 20 Jun 2024
Define the function at the end.
master_method(6, 6, 1);
master_method(3, 2, 2);
master_method(2, 3, 1);
function T = master_method(a, b, k)
.....
end
Answers (0)
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!