Time and space complexity of determinant of a matrix - matrix

Can anybody please explain the calculation of time and space complexity of this code(finding determinant of a matrix) in detail?
I am not able to understand the exact time complexity of this program using recurrence tree.
class Solution
{
static void getCofactor(int arr[][], int temp[][], int n, int i, int j){
int x = 0, y = 0;
for(int row=0; row<n; row++){
for(int col=0; col<n; col++){
if(row!=i && col!=j){
temp[x][y++] = arr[row][col];
if(y == n-1){
y = 0;
x++;
}
}
}
}
}
static int determinantOfMatrix(int matrix[][], int n)
{
// code here
if(n == 1)
return matrix[0][0];
int temp[][] = new int[n-1][n-1];
int determinant = 0;
int sign = 1;
for(int i=0; i<n; i++){
getCofactor(matrix, temp, n, 0, i);
determinant+=sign*matrix[0][i]*determinantOfMatrix(temp, n-1);
sign=-sign;
}
return determinant;
}
}

Related

Having some trouble with solving for runtime

In Big-Θ notation, analyze the running time of the following three pieces of code/pseudo-code, describing it as a function of the input, n.
1.
void f1(int n)
{
int t = sqrt(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
// do something O(1)
}
n -= t;
}
}
2.
Assume A is an array of size n+1.
void f2(int* A, int n)
{
for(int i=1; i <= n; i++){
for(int k=1; k <= n; k++){
if( A[k] == i){
for(int m=1; m <= n; m=m+m){
// do something that takes O(1) time
// Assume the contents of the A[] array are not changed
}
}
}
}
}
3.
void f3(int* A, int n)
{
if(n <= 1) return;
else {
f3(A, n-2);
// do something that takes O(1) time
f3(A, n-2);
}
}
Any help would be appreciated.

Runtime error for large inputs for sorting ( quicksort)

This is a very simple program where the user inputs (x,y) coordinates and distance 'd' and the program has to find out the number of unrepeated coordinates from (x,y) to (x+d,y).
For eg: if input for one test case is: 4,9,2 then the unrepeated coordinates are (4,9),(5,9) and (6,9)(x=4,y=9,d=2). I have used a sorting algorithm as mentioned in the question (to keep track of multiple occurrences) however the program shows runtime error for test cases beyond 30. Is there any mistake in the code or is it an issue with my compiler?
For a detailed explanation of question: https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorithm/missing-soldiers-december-easy-easy/
#include <stdio.h>
#include <stdlib.h>
int partition(int *arr, int p, int r) {
int x;
x = arr[r];
int tmp;
int i = p - 1;
for (int j = p; j <= r - 1; ++j) {
if (arr[j] <= x) {
i = i + 1;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[i + 1];
arr[i + 1] = arr[r];
arr[r] = tmp;
return (i + 1);
}
void quicksort(int *arr, int p, int r) {
int q;
if (p < r) {
q = partition(arr, p, r);
quicksort(arr, p, q - 1);
quicksort(arr, q + 1, r);
}
}
int count(int A[],int ct) {
int cnt = 0;
for (int i = 0; i < ct; ++i) {
if (A[i] != A[i + 1]) {
cnt++;
}
}
return cnt;
}
int main() {
int t;
scanf("%d", &t);
long int tmp, y, d;
int ct = 0;
int i = 0;
int x[1000];
int j = 0;
for (int l = 0; l < t; ++l) {
scanf("%d%d%d", &tmp, &y, &d);
ct = ct + d + 1; //this counts the total no of coordinates for each (x,y,d)
for (int i = 0; i <= d; ++i) {
x[j] = tmp + i; //storing all possible the x and x+d coordinates
j++;
}
}
int cnt;
int p = ct - 1;
quicksort(x, 0, p); //quicksort sorting
for (int l = 0; l < ct; ++l) {
printf("%d ", x[l]); //prints sorted array not necessary to question
}
cnt = count(x, ct); //counts the number of non-repeated vertices
printf("%d\n", cnt);
}
The problem was the bounds of the array int x[1000] is not enough for the data given below.

Pset3 Sorting-sort. Program doesn't execute through all the code lines

I was rewriting the sort function in helper.c. The program compiled, but upon running, it froze. To simplify the problem, I've separated the sort function into another program with a certain set of ints to be sorted. Still, the execution froze at the sorting loop.
Can anyone help?
#include <cs50.h>
#include <stdio.h>
void sort(int values[], int n);
int main(void)
{
int randoms[] = {5,486,48,89,78,164,57,54,9};
sort(randoms, 9);
for(int i = 0; i < 9; i++)
{
printf("%i\n", randoms[i]);
}
}
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
for(int i = 0, counter; i < n - 1; i++)
{
counter = 0;
for(int j = 0, extra; j < n; i++)
{
//swap adjacent values in wrong order
if (values[j] > values[j+1])
{
extra = values[j];
values[j] = values[j+1];
values[j+1] = extra;
counter++;
}
}
if (counter == 0)
break;
}
return;
}
In your second for loop, you are increasing the wrong variable, it should be j++ instead of i++.

How to calculate number of partitions of n?

How can I calculate number of partitions of n mod 1e9+7, where n<=50000.
See http://oeis.org/A000041 .
Here is the source problem http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259 (In Chinese)
Simply applying the formula: a(n) = (1/n) * Sum_{k=0..n-1} d(n-k)*a(k) gave me an O(n^2) solution.
typedef long long ll;
ll MOD=1e9+7;
ll qp(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%MOD;
a=a*a%MOD;
b>>=1;
}
return ans;
}
ll a[50003],d[50003];
#define S 1000
int main()
{
for(int i=1; i<=S; i++)
{
for(int j=1; j<=S; j++)
{
if(i%j==0) d[i]+=j;
}
d[i]%=MOD;
}
a[0]=1;
for(int i=1; i<=S; i++)
{
ll qwq=0;
for(int j=0; j<i; j++) qwq=qwq+d[i-j]*a[j]%MOD;
qwq%=MOD;
a[i]=qwq*qp(i,MOD-2)%MOD;
}
int n;
cin>>n;
cout<<a[n]<<"\n";
}
I would solve it with a different approach.
Dynamic Programming:
DP[N,K] = number of partitions of N using only numbers 1..K
DP[0,k] = 1
DP[n,0] = 0
DP[n,k] when n<0 = 0
DP[n,k] when n>0 = DP[n-k,k] + DP[n,k-1]
Recursive implementation using memoization:
ll partition(ll n, ll max){
if (max == 0)
return 0;
if (n == 0)
return 1;
if (n < 0)
return 0;
if (memo[n][max] != 0)
return memo[n][max];
else
return (memo[n][max] = (partition(n, max-1) + partition(n-max,max)));
}
Live-Example

Euler's Totient function permutation

I was doing this problem on SPOJ. www.spoj.com/problems/TIP1.
I have written this code but I am getting time limit exceeded when judged. Can anyone help me with any optimization or a better approach.
if N is a positive integer, then PHI(N) is the number of integers K for which GCD(N, K) = 1 and 1 ≤ K ≤ N. We denote GCD the Greatest Common Divisor. For example, we have PHI(9)=6.
#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10000010
#define MAXN 10000010
int phi[MAXN + 1], prime[MAXN/10], sz=0;
vector<bool> mark(MAXN + 1);
int ans[10000011];
vector<int> a(10);
vector<int> b(10);
bool isprm(long int x)
{
for(int s=0; s<10; s++)
{
a[s]=b[s]=0;
}
long int y=phi[x];
int i=0,j=0;
while(x>0)
{
int rem=x%10;
x=x/10;
a[i]=rem;
i++;
}
while(y>0)
{
int rem=y%10;
y=y/10;
b[j]=rem;
j++;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(i!=j)
return false;
for(int s=0; s<10; s++)
{
if(a[s]!=b[s])
return false;
}
return true;
}
void precompute_again()
{
for(int i=0; i<=20; ++i)
ans[i]=0;
ans[21]=21;
for(long int i=22; i<10000005; ++i){
bool chk=false;
chk=isprm(i);
if(chk==true)
{
if(i*phi[ans[i-1]]==phi[i]*ans[i-1])
{
ans[i]=i;
}
else
{
if(i*phi[ans[i-1]]>phi[i]*ans[i-1])
{
ans[i]=ans[i-1];
}
else
{
ans[i]=i;
}
}
}
else
{
ans[i]=ans[i-1];
}
}
}
int main()
{
phi[1] = 1;
for (int i = 2; i <= MAXN; i++ ){
if(!mark[i]){
phi[i] = i-1;
prime[sz++]= i;
}
for (int j=0; j<sz && prime[j]*i <= MAXN; j++ ){
mark[prime[j]*i]=1;
if(i%prime[j]==0){
int ll = 0;int xx = i;
while(xx%prime[j]==0)
{
xx/=prime[j];
ll++;
}
int mm = 1;
for(int k=0;k<ll;k++)mm*=prime[j];
phi[i*prime[j]] = phi[xx]*mm*(prime[j]-1);
break;
}
else phi[i*prime[j]] = phi[i]*(prime[j]-1 );
}
}
precompute_again();
int t;
scanf("%d",&t);
while(t--)
{
long int m;
scanf("%ld",&m);
cout<<ans[m]<<endl;
}
return 0;
}
Try to use a variant of Sieve of Eratosthenes.
The following code computes all phi[N] up to MAXN. For MAXN = 1e7 it runs in the blink of an eye.
int i, j;
int * phi = new int [MAXN];
for (i = 1; i < MAXN; i ++)
phi[i] = i;
for (i = 2; i < MAXN; i ++)
{
if (phi[i] != i) continue;
for (j = i; j < MAXN; j += i)
phi[j] = phi[j] / i * (i - 1);
}

Resources