algorithm

Calculate total batch upload transfer percent with limited information

Hi there, I have a system which uploads to a server file by file and displays a progress bar on file upload progress, then underneath a second progress bar which I want to indicate percentage of batch complete across all files queued to upload. Information and algorithms I can work out are: Bytes Sent / Total Bytes To Send = First pro...

How to scale rotated objects properly in Actionscript 3?

This is unfortunately a quite complex issue to explain, so please don't get discouraged by the wall of text - it's there for a reason. ;) I'm working on a transformation manager for flash, written with Actionscript 3. Users can place objects on the screen, for example a rectangle. This rectangle can then be selected and transformed: mo...

Substring and its reverse in a string

My professor was talking about this in a Dynamic programming class and asked us to think over it. She gave us some examples as well. Given a string, we were to find the longest continuous subsequence whose reverse is also a subsequence present in the given string. Example: INPUT: pqrstuvtsrv OUTPUT: i=3, k=2 rst -> tsr (rst found firs...

How would I use for_each to delete every value in an STL map?

Suppose I have a STL map where the values are pointers, and I want to delete them all. How would I represent the following code, but making use of std::for_each? I'm happy for solutions to use Boost. for( stdext::hash_map<int, Foo *>::iterator ir = myMap.begin(); ir != myMap.end(); ++ir ) { delete ir->second; // delete all t...

Output from OouraFFT correct sometimes but completely false other times. Why ?

Hi I am using Ooura FFT to compute the FFT of the accelerometer data in windows of 1024 samples. The code works fine, but then for some reason it produces very strange outputs, i.e. continuous spectrum with amplitudes of the order of 10^200. Here is the code: OouraFFT *myFFT=[[OouraFFT alloc] initForSignalsOfLength:1024 NumWindows...

Formula needed: Sort array to array-"snaked"

After the you guys helped me out so gracefully last time, here is another tricky array sorter for you. I have the following array: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] I use it for some visual stuff and render it like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Now I want to sort the array to have a "snake" l...

Algorithm to find the next number in a sequence

Ever since I started programming this has been something I have been curious about. But seems too complicated for me to even attempt. I'd love to see a solution. 1, 2, 3, 4, 5 // returns 6 (n + 1) 10, 20, 30, 40, 50 //returns 60 (n + 10) 10, 17, 31, 59, 115 //returns 227 ((n * 2) - 3) ...

Lawler's Algorithm Implementation Assistance

UPDATE2: I think I got it now: <?php /* * @name Lawler's algorithm PHP implementation * @desc This algorithm calculates an optimal schedule of jobs to be * processed on a single machine (in reversed order) while taking * into consideration any precedence constraints. * @author Richard Knop * */ $jobs = array(1 => a...

Quicksort vs heapsort

Both quicksort and heapsort do in-place sorting. Which is better? What are the applications and cases in which either is preferred? ...

Finding a prime number after a given number

How can I find the least prime number greater than a given number? For example, given 4, I need 5; given 7, I need 11. I would like to know some ideas on best algorithms to do this. One method that I thought of was generate primes numbers through the Sieve of Eratosthenes, and then find the prime after the given number. ...

OOP vs PP for algorithms

Which paradigm is better for design and analysis of algorithms? Which is faster? Because I have a subject called Design and Analysis of Algorithms in university and have a time limit for programs. Is OOP slower than Procedure programming? Or the time difference is not big? ...

How to build, sort and print a tree of a sort?

This is more of an algorithmic dilemma than a language-specific problem, but since I'm currently using Ruby I'll tag this as such. I've already spent over 20 hours on this and I would've never believed it if someone told me writing a LaTeX parser was a walk in the park in comparison. I have a loop to read hierarchies (that are prefixed ...

How to spread changes in oriented graph?

Hello, I have oriented graph. Graph can be strongly connected. Every vertex can have a set of anything, for example letters. The set is user editable. Every vertex makes intersection of sets in previous vertices (only one step back). But now, there is problem: When I update set of one vertex, the change should expand to all vertices an...

Best and easiest algorithm to search for a vertex on a Graph?

Hi, After implementing most of the common and needed functions for my Graph implementation, I realized that a couple of functions (remove vertex, search vertex and get vertex) don't have the "best" implementation. I'm using adjacency lists with linked lists for my Graph implementation and I was searching one vertex after the other unti...

How to find sum of elements from given index interval (i, j) in constant time?

Given an array. How can we find sum of elements in index interval (i, j) in constant time. You are allowed to use extra space. Example: A: 3 2 4 7 1 -2 8 0 -4 2 1 5 6 -1 length = 14 int getsum(int* arr, int i, int j, int len); // suppose int array "arr" is initialized here int sum = getsum(arr, 2, 5, 14); sum should be 1...

Trend analysis using iterative value increments

We have configured iReport to generate the following graph: The real data points are in blue, the trend line is green. The problems include: Too many data points for the trend line Trend line does not follow a Bezier curve (spline) The source of the problem is with the incrementer class. The incrementer is provided with the data p...

Python: speed up removal of every n-th element from list.

I'm trying to solve this programming riddle and although the solution (see code below) works correctly, it is too slow for succesful submission. Any pointers as how to make this run faster (removal of every n-th element from a list)? Or suggestions for a better algorithm to calculate the same; seems I can't think of anything else than ...

List of Big-O for PHP functions

After using PHP for a while now, I've noticed that not all PHP built in functions as fast as expected. Consider the below two possible implementations of a function that finds if a number is prime using a cached array of primes. //very slow for large $prime_array $prime_array = array( 2, 3, 5, 7, 11, 13, .... 104729, ... ); $result_arra...

How to find the longest continuous subsequence whose reverse is also a subsequence

Suppose I have a sequence x1,x2,x3.....xn, and I want to find the longest continuous subsequence xi,xi+1,xi+2......xi+k, whose reverse is also a subsequence of the given sequence. And if there are multiple such subsequences, then I also have to find the smallest i. ex:- consider the sequences: abcdefgedcg here i=3 and k=2 aabcdddd he...

optimized grid for rectangular items

I have N rectangular items with an aspect ratio Aitem (X:Y). I have a rectangular display area with an aspect ratio Aview The items should be arranged in a table-like layout (i.e. r rows, c columns). what is the ideal grid rows x columns, so that individual items are largest? (rows * colums >= N, of course - i.e. there may be "unused" ...