Thread: Can someone help with the space-time complexity of my matrix peak finder algorithm?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    6

    Can someone help with the space-time complexity of my matrix peak finder algorithm?

    I'm having a little trouble calculating the space-time complexity with a peak finding algorithm for matrices. I've already taken a class in algorithm analysis, but I'm afraid my professor didn't really explain anything that well in that class so I'm sort of stuck in figuring out my algorithm's complexity.

    Regardless here's my code:

    Code:
    int matrix_peak(vector< vector<int> > m, int j)
    {
    	int i;
    
    
    
    
    	/* 1D array brute force peak function, 
    	modified to work with column iteration on 
    	matrices. Time complexity is O(n)*/
    	if(m.size() == 1)
    	{
    		return recursive_peak_finder(m[0], 0, m[0].size());
    	}
    
    
    	for(i = 0; i < m.size() ; i++)
    	{
    		if(i > 0 && i < m.size()-1 &&
    			m[i][j] >= m[i+1][j] && m[i][j] >= m[i-1][j])
    		{
    			cout << m[i][j] << endl;
    			break;
    		}
    		else if(i == 0 && m[i][j] >= m[i+1][j])
    		{
    			cout << m[i][j] << endl;
    			break;
    		}
    		else if(i == m.size() - 1  && m[i][j] >= m[i-1][j])
    		{
    			cout << m[i][j] << endl;
    			break;
    		}
    		
    	}
    	/* If peak is found in a middle column, it is returned
    	otherwise the function is called on a column which contains
    	a number greater than the greatest number in the current column. */
    	if(m[i][j] >= m[i][j+1] && m[i][j] >= m[i][j-1])
    	{
    		return m[i][j];
    	}
    	else if(m[i][j] < m[i][j+1])
    	{
    		return matrix_peak(m, j+1);
    	}
    	else if(m[i][j] < m[i][j-1])
    	{
    		return matrix_peak(m, j-1);
    	}
    }
    There's a function that's called in case the matrix has just one row, which is calling my peak finder for arrays, which is basically a binary search, meaning it has O(log n) time.

    I'm not exactly sure how I'm supposed to write this I thought my algorithm could be determined with this:

    T(nm, m/2) = O(n) + T(nm, m/2 -+1)

    Can someone help me out, or point me to some tutorial that explains how to solve recurrences properly? What I'm not getting is how I can figure out which parameters T() is supposed to have.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well at the moment, it's probably O(n2), since matrix_peak() makes a copy of m for each recursive invocation.

    > There's a function that's called in case the matrix has just one row, which is
    > calling my peak finder for arrays, which is basically a binary search, meaning it
    > has O(log n) time.
    How can it be a binary search if the elements are unordered?
    You can't look at the middle element, and instantly know that the peak will be in the first half or the second half.
    I mean, if the elements were ordered, the peak would be at one end or the other, in which case the answer would be trivially O(1).

    > Can someone help me out, or point me to some tutorial that explains how to solve recurrences properly?
    Recursion is just a loop in disguise.

    Since you basically have to examine each element of m at least once, I'd go with O(n2)
    You only count the most dominant term.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Space Complexity of Radix Sort
    By gaurav_13191 in forum C Programming
    Replies: 1
    Last Post: 09-02-2013, 12:09 PM
  2. space complexity of recursive function
    By daniig in forum C Programming
    Replies: 2
    Last Post: 01-23-2013, 12:35 PM
  3. Trouble with peak finding in canny algorithm
    By DaNxTh3xMaNx in forum C Programming
    Replies: 3
    Last Post: 01-29-2012, 10:42 PM
  4. Time complexity of algorithm
    By viblic in forum C Programming
    Replies: 2
    Last Post: 01-11-2012, 11:22 AM
  5. Implementation of path finder algorithm in C
    By user_name_void in forum C Programming
    Replies: 2
    Last Post: 11-23-2009, 12:31 AM

Tags for this Thread