algorithm

algorithm to find complementary solution to this problem

I had this coding question in an interview.I couldnt find an optimum solution to this. what I did was, for(i=0;i<n;i++) for(j=i;j<n;j++) if(a[i]+a[j]==k) print a[i], a[j] But that would give rise to a O(n2) complexity. Is there a better way to solve this?? Array A contains n integers. A pair (i,j) of indexes of the array A is called ...

books or sites for design of algorithms!

Hi I want to start my new semester. and one of my lessons is Design of Algorithms also I have passed Data Structure And Algorithms last semester. Please help me that for preparing myself for new semester what should I read for Design of Algorithms? (Also we use Java programming language.) Thanks ...

Proof of detecting the start of cycle in linked list

From several posts inside stackoverflow and outside, I have come to know how to detect cycles in a linked list, the length of a cycle. I also found the method on how to detect the start of the loop. Here are the steps again for reference. Detecting Loop: Have two pointers, classically called hare and tortoise. Move hare by 2 steps an...

Comparison of 2 text files: what and where changes were made?

Hi, imagine you have 2 texfiles (let's say 500kB - 3 MB large): the first is original, the second is the update of this original. How can I find out, what was changed (inserted, deleted) and where the changes took place (in the update file in comparison to original)? Is there any tool or library somewhere? Resides this function in an...

Example of O(n!)?

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I'm asking about time complexity. ...

Word coloring and syntax analyzing

Hey! I want to colorize the words in a text according to their classification (category/declination etc). I have a fully working dictionary, but the problem is that there is a lot of ambiguity. foedere, for instance, can be forms of either the verb "fornicate" or the noun "treaty". What the general strategies for solving these ambiguit...

Is there an simple algorithm for calculating maximum inscribed circle into a convex polygon?

I found some solutions, but they're too messy. ...

What makes this bucket sort function slow?

The function is defined as void bucketsort(Array& A){ size_t numBuckets=A.size(); iarray<List> buckets(numBuckets); //put in buckets for(size_t i=0;i!=A.size();i++){ buckets[int(numBuckets*A[i])].push_back(A[i]); } ////get back from buckets //for(size_t i=0,head=0;i!=numBuckets;i++){ //size_t bucket_size=buckets[...

hashing algorithm for strings

I came across a situation where i had to count the number of occurences of each word in a string. I decided hashing would be the best way to do it (Find the hash value for each word that is encountered and increment the count at the position indexed by the hash value - assuming i use an array). What hashing algorithm can i use to ensure...

question, the best data structure and algorithm

I have a question about implementing a data structure (with algorithm) that has these features: There are too many places (like stores), and each place can store too many items, I want to store items in the places. The point is we need to have optimized insertion (or deletion) and optimized search feature. Search can be done based on p...

How to implement minimax in Tictactoe

I read this answer, and it just confused me: http://stackoverflow.com/questions/1869096/tictactoe-ai-making-incorrect-decisions#answer-1869226 Could somebody help me understand how I could apply this to Tictactoe? 1) How would I "work my way up through the tree? 2) How do I even create a tree of moves? Note: I currently have a Board c...

How to check if my AVL tree implementation is correct?

Hello, guys. I think I've created an AVL tree implementation, but as AVL Tree is quite a complex structure, I need to test it. So the question is - how can I test it? Have you got any ideas? Up to this moment I have the following tests: basic sanity check - checks that for every node height equals max. height of child nodes + 1...

Can some one help solving this recurrence relation?

T(n) = 2T(n/2) + 0(1) T(n) = T(sqrt(n)) + 0(1) first one I use substitution method for n, logn, etc, all gave me wrong answers. Recurrence trees: I dont know if I can apply as the root will be a constant Can some one help? T ...

How to start implementing a running text algorithm?

I have a dialog implementation that receives a length of text from the user and I'd like to format it over multiple lines in a visually pleasing way. The user can include their own line breaks in the string which I would also like to account for. Is anyone aware of pseudocode or something else publicly available I could use as a referen...

Algorithm to select a set of numbers to reach a minimum total

Given A set of numbers n[1], n[2], n[3], .... n[x] And a number M I would like to find the best combination of n[a] + n[b] + n[c] + ... + n[?] >= M The combination should reach the minimum required to reach or go beyond M with no other combination giving a better result. Will be doing this in PHP so usage of PHP libraries is ok. I...

Determine if a Python list is 95% the same?

This question asks how to determine if every element in a list is the same. How would I go about determining if 95% of the elements in a list are the same in a reasonably efficient way? For example: >>> ninety_five_same([1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) True >>> ninety_five_same([1,1,1,1,1,1,2,1]) # only 80% the same Fals...

Sorting algorithm for large datasets

Hi, I don't know whether the term sort is most appropriate but anyway I have a large datasets which consists of columns userA, userB, date, interactionDuration. In other words the dataset contains rows which describe which users were interacting, how long the interaction lasted and the date of interaction. The goal is to divide these i...

Non biased return a list of n random positive numbers (>=0) so that their sum == total_sum

I'm either looking for an algorithm or a suggestion to improve my code to generate a list of random numbers that their sum equals some arbitrary number. With my code below, it'll always be biased as the first numbers will tend to be higher. Is there a way to have the number selection more efficient? #!/usr/bin/python ''' Generate a...

Efficient 2D FFT on real input data?

I'm currently implementing a two dimensional FFT for real input data using opencl (more specifically a fast 2D convolution using FFTs, so I only need something which behaves similary enough to apply the convolution to). The 2D FFT is implemented using an 1D FFT on the rows and afterwards an 1D FFT on the cols. To make this more efficien...

Making a timetable schedule

So.. someone recently asked me to make a timetable for them and I agreed. When I sat down to do it I realized it was harder than I thought. It's just a timetable to give shifts to 4 people for either day or night. I thought of something like this: for Monday to Saturday { for(i=0;i<people.length;i++){ if (person[i].available()){ ...