-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Useful Functions for 2D Lists #502
Conversation
@czgdp1807 For issue number #376 |
for i in range(n): | ||
|
||
for j in range(m): | ||
|
||
if search_list[i][j] == val: | ||
|
||
return [i, j] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this would take O(m*n) steps. This generalises to O(number_of_elements_in_the_input_container) for arbitrary dimensional array. MultiDimensionalArray._data
in arrays.py
represents data internally as a 1-D list. So, the above two nested loops would be equivalent to a single loop traversing MultiDimensionalArray._data
. Hence, according to your implementation stair case search for 2-D arrays is same as linear search for 1-D arrays time complexity wise. I am unable to see the advantages of a special function like this.
Could you please explain how would it be beneficial to add a special function, stair_case_search
for 2-D arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was extremely stupid to put this here...A better function would be to search the Kth smallest or largest element in sorted 2d list in O(N+M) time...I think this is better and much practical..I will update this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Is there any algorithm for multi-dimensional arrays for this purpose (finding k-th smallest/largest element in a given dimension)? Wouldn't that be something unique and interesting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per my knowledge, multidimensional arrays are pretty much restricted to 2D...as there isn't really any specific algorithm for larger dimensions, divide an conquer recursion would help..but I think not more than that..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this topic wouldn't help much as a contribution, because the further restriction that is caused is that, array must be sorted...and that is pretty rare in most use cases...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's okay. But how will we use output of stair case search? I think it just produces the first index tuple where the given number/object is found? So, basically you can find out whether a given number/object exists in a matrix or not? What's the use of this information. If I have to answer million queries just to check whether a given number/object exists in a matrix or not. I would spend time to pre-process a matrix into a dict (key = matrix element, value = vector of indices) and then answer million queries using that dict. So, the complexity would be O(NM + Q) instead of O(Q*(N + M)). If N = M = 1000, then O(NM + Q) = (million * 2) but O(Q(N + M)) = O(million * 2000). No? In fact, O(Q*(log(N) + log(M)) would be O(million * 2 * log(1000)). In fact, dict would help in providing all the indices where a given value would be present in a single query. Any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right...but the problem with python packages is that..it uses a Hash Map..and that only guarantees an average case time of O(1), and can lead to bad hash collisions in worst cases..which would essentially make it linear...
And yes, use case related it's quite less..but that way many functions already existing have their specific situations...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it uses a Hash Map..and that only guarantees an average case time of O(1)
Not a big problem. You can define your own hash function (__hash__
method). There are very good functions out there that one can use to lower collision.
Overall, I am -1 for this stair case algorithm. Thanks for discussion anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so...actually this wasn't my idea...as I mentioned, this was put because someone had put this issue..and I thought of solving that..because he/she might have put for a certain use case...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries. I would still recommend to go for graph algorithms. That should be easy yet impactful work. Should require little discussion. Though, I would recommend to go through the exisiting implementations in graph/algorithms.py
.
Added StairCase Search for 2D Lists under /linear_data_structures