algorithm

How to solve the recurrence equation T(n)=T(n/2)+T(n/4)+\Theta(n)?

How to solve the recurrence equation 1.T(n)=T(n/2)+T(n/4)+\Theta(n) 2.T(1)=1 Use Big-Theta notation to give the result ...

Finding the kth quantiles of an n-elements set. (From cormen)

The kth quantiles of an n-element set are the k - 1 order statistics that divide the sorted set into k equal-sized sets (to within 1). Give an O(n lg k)-time algorithm to list the kth quantiles of a set. The straight forward solution would be to select every k, 2k, 3k .. ik the smallest element, whose running time is O(kn) (k calls t...

How to understand the knapsack problem is NP-complete?

We know that the knapsack problem can be solved in O(nW) complexity by dynamic programming. But we say this is a NP-complete problem. I feel it is hard to understand here. (n is the number of items. W is the maximum volume.) ...

Optimal median of medians selection - 3 element blocks vs 5 element blocks?

I'm working on a quicksort-variant implementation based on the Select algorithm for choosing a good pivot element. Conventional wisdom seems to be to divide the array into 5-element blocks, take the median of each, and then recursively apply the same blocking approach to the resulting medians to get a "median of medians". What's confusi...

Need effective greedy for covering a line segment

Given n segments of line (into the X axis) with coordinates [li; ri]. You are to choose the minimum number of segments that cover the segment [0;M]. Design a greedy algorithm to solve this problem. Here what I did: sorting them by starting points in increasing order, then I choose the longest one, second longest.... But here is a probl...

How to quicksort over multiple columns

I'm looking to quicksort some objects in php. i'm sorting an array of OBJECTS $object->x; $object->y; $object->z; I want to first sort by x, then y, then z. This is my quicksort function Where it accepts an array of jobjects, and sorts by a particular sortkey (x, y, or z column) The function returns a sorted array of objects, that h...

Looking for a fast outlined line rendering algorithm

I'm looking for a fast algorithm to draw an outlined line. For this application, the outline only needs to be 1 pixel wide. It should be possible, whether by default or through an option, to make two lines connect together seemlessly, if they share a common point. Excuse the ASCII art but this is probably the best way to demonstrate it....

How to find a cycle containing a set of nodes in a graph?

Given a graph undirected G = (V,E) and a set of nodes P. I need to find a cycle (not the shortest length cycle) containing these nodes? How do I find this cycle? ...

Last word in a sentence: In SQL (regular expressions possible?)

I need this to be done in Oracle SQL (10gR2). But I guess, I would rather put it plainly, any good, efficient algorithm is fine. Given a line (or sentence, containing one or many words, English), how will you find the last word of the sentence? Here is what I have tried in SQL. But, I would like to see an efficient way of doing this. ...

Efficiently determining the relationship between rows in a spreadsheet

This is a problem I've just run into, or rather its a simplification that captures the core problem. Imagine I have a spreadsheet containing a number of columns, each of them labeled, and a number of rows. I want to determine when the value in one column can be inferred from the value in another. For example, we might find that every ...

Algorithm for sorting multisets

Hi I'm looking for an algorithm (preferably in C/C++/Java or similar) to sort a multiset. By scouting the internet I've come to the conclusion that I should be able to do it in O(n log h) time. With h being the number of distinct elements and n the total number of elements. I wasn't however able to find an algorithm that utilizes the fa...

How to generate a predictable shuffling of a sequence without generating the whole sequence in advance?

The following python code describes exactly what I want to achieve for a sequence of arbitrary size (population): import random fixed_seed = 1 #generate the same sequence every time with a fixed seed population = 1000 sample_count = 5 #demonstration number num_retries = 3 #just enough to show the repeatable behaviour for trynum in xran...

Tiling patterns

I'm looking for efficient solution/algorithm for tiling : the layout of the decking tiles. We want to build system that will calculate the mutual placement of columns and beams (beams, joist) with support of irregular shape. Thank you in advance for any help. ...

MATLAB coding problem

Hey guys, I got this error message when I tried to trigger the function below. Can anybody help me out? Thanks! >> changeYuv('tilt.yuv',352,288,1:40,40); ??? Index exceeds matrix dimensions. Error in ==> changeYuv at 32 j=histogram(imgYuv(:,:,1,k+1)); >> [x,y,z,a]=size(imgYuv) x = 288 y = 352 z = 3 a = 40...

Find cycle of shortest length in a directed graph with positive weights

Hi guys, I was asked this question in an interview but i couldnt come up with any decent solution. So, i told them the naive approach of finding all the cycles then picking the cycle with the least length. I'm curious to know what is an efficient solution to this problem. Thanks, Chander ...

What algorithm should I use to maximize resource utilization in production planning?

Hi, What kind of algorithm should I use to solve this problem? Ore is blended in a particular ratio to produce metal with the desired quality characteristics. The ore is blended in batches of 5, the batches are to be mixed in a particular ratio that will maximize ore utilization as well as produce an output of the desired quality. 1. ...

The book about writing search engine. Recommend some...

I want to learn more about the full-text search. Recommend please a few good books in which would be well described used algorithms and data structures, which must describe how to write simple search engine. I use C++. Thanks! ...

how to represent a Binary Search Tree as a database schema?

I need to represent Binary Search Tree as a DB schema? How do I do it? Its kind of problem. I dont have any code. I need direction how to show BST as a DB schema. ...

Swap Integer variables without using any assignment

Hi, all! Could someone please help me to solve such a task. I need to swap variable values without using any assignment signs. I tried to do it with a while loop but I coudn't store counter value anywhere. Thank you all in advance. ...

how to find number of nodes in loop of linked list?

how to find the number of nodes in a loop of linked list? for e.g A ----> B ----> C -----> D -----> E Λ | | | | V H <----- G <----- F Find the number of nodes in a loop from C to H Fundamental problem is how to find poin...