algorithm

Substring algorithm

Can someone explain to me how to solve the substring problem iteratively? The problem: given two strings S=S1S2S3…Sn and T=T1T2T3…Tm, with m is less than or equal to n, determine if T is a substring of S. ...

What is an Algorithm to Diff the Two Strings in the Same Way that SO Does on the Version Page?

I'm trying to diff two strings by phrase, similar to the way that StackOverflow diffs the two strings on the version edits page. What would be an algorithm to do this? Are there gems, or other standard libraries that accomplish this? EDIT: I've seen other diffing algorithms (Differ with Ruby) and they seem to result in the following: >...

How to turn Prim's algorithm into Kruskal's algorithm?

I've implemented Prim's algorithm in C (www.bubblellicious.es/prim.tar.gz) but I was just wondering how to transform this into Kruskal's algorithm. It seems they're quite similar, but I can't imagine how can I modify my old code into that new one. It'd be delicious if you give some advices or something. I know that's easy, but I'm still...

Shift elements in string array to left to fill 'holes'

I have a list of names and phone numbers like so: var phonelist = List<string[]> { new string[] {"Bill", "1234", "12345", "12314" }, new string[] {"Bob", "", "12345", "12314" }, new string[] {"Chris", "", "", "12314" }, new string[] {"Dave", "1234", "", "12314" }, new string[] {"Andy", "1234", "12345", "" }, } ...

Whats the name of this game?

This is not a programming question per se, although the ultimate goal is to devise an algorithm. I'm looking for references or at least the name of a type of game. It is pretty widespread on television game-shows. The game is as follows: You have a number of slots, each slot contains an item (from some finite set), which you don't kno...

Algorithm to filter a set of all phrases containing in other phrase

Given a set of phrases, i would like to filter the set of all phrases that contain any of the other phrases. Contained here means that if a phrase contains all the words of another phrase it should be filtered out. Order of the words within the phrase does not matter. What i have so far is this: Sort the set by the number of words in ...

How do I scale one rectangle to the maximum size possible within another rectangle?

I have a source rectangle and a destination rectangle. I need to find the maximum scale to which the source can be scaled while fitting within the destination rectangle and maintaining its original aspect ratio. Google found one way to do it but I'm not sure if it works in all cases. Here is my home-brewed solution: Calculate Height/W...

Why is this merge sort implementation not working?

I am missing something embarrassingly basic here in my Merge Sort implementation: # include <math.h> # include <stdio.h> int temp[10]; void merge_sort(int *indices, int x, int z); void m_merge(int *indices, int x, int y, int z); void merge_sort(int *indices, int x, int z ) { int y; if(x<z){ y = (x+z)/...

Looking for space efficient algorithms and data structures

Preferably in Java. I am interested in implementations of data structures such as Sets and Maps, and algorithms such as sorting, that are memory-efficient, not necessarily fast. I could live with O(n^2) fetch and store if the amount of memory and number of allocations was low. Anything out there? ...

How to quickly find maximal element of a sum of vectors?

I have a following code in a most inner loop of my program struct V { float val [200]; // 0 <= val[i] <= 1 }; V a[600]; V b[250]; V c[250]; V d[350]; V e[350]; // ... init values in a,b,c,d,e ... int findmax(int ai, int bi, int ci, int di, int ei) { float best_val = 0.0; int best_ii = -1; for (int ii = 0; ii < 200; ii++) { ...

A draw-encoding algorithm similar to one in Google maps

As some of you may know, there is an encoding algorithm in Google maps API which optimizes polyline drawings in different zoom levels. It removes-adds the coordinates of a path according to the current zoom level and decreases-increases the drawing computation time. Specifically, I mean the algorithm in the GPolyline.fromEncoded method. ...

How to calculate an arbitrary power/root?

I have a application which needs to raise a number to a fractional power. The target platform is an FPGA and I can get estimates on an FPU size for it, but I need an algorithm for raising a number to a fractional power just for a feasibility study. I'm assuming floating point as a worst case, I expect in practice we will be able to use...

Is there a nearest-key map datastructure?

Hello all, I have a situation where I need to find the value with the key closest to the one I request. It's kind of like a nearest map that defines distance between keys. For example, if I have the keys {A, C, M, Z} in the map, a request for D would return C's value. Any idea? ...

C# Many To Many Relationship

So, I need some way to implement an undirected network ( I think this is the correct term ) in C# Let's say I have the following data: Foo1 <-> Bar1 Foo2 <-> Bar1 Foo2 <-> Bar2 Foo2 <-> Bar3 Foo3 <-> Bar2 Foo3 <-> Bar3 How would I implement something that could support this? One way to do this would be to create a class containing ...

Min Value from Stack

I have a stack which contains some integer data. I want to find out the min value from Stack in O(1) time. Any idea? PS: There is no ordering (increasing/decreasing) of data in Stack. Thanks, Naveen ...

Why is squaring a number faster than multiplying two random numbers?

Multiplying two binary numbers takes n^2 time, yet squaring a number can be done more efficiently somehow. (with n being the number of bits) How could that be? Or is it not possible? This is insanity! ...

Finding all non-conflicting combinations of values from multiple lists of values

I have the following array which contains arrays of values: $array = array( array('1', '2'), array('a', 'b', 'c'), array('x', 'y'), ); There can be any number of arrays and an array can contain any number of values. I currently have a piece of code which will generate all combinations where one value is taken from each arr...

Coding brain teaser to update an array (language agnostic)

All, I need a clever way to implement this algorithm (for work) as quickly and cleanly as possible: I think I've removed all the language specific issues and boiled it down to this: I have two arrays: A and B. A has a list of names in it {Apple, Apple, Banana, Banana, Banana, Carrot, ...} each i-th value has no upper limit on the num...

Mathematical problem help.

I have some code where, if a user has referred X number of people, he will get X number of credits. For example, referring 2 people = 1 credit. 4 people = 2 credits, and so on. However where this gets tricky is, the numbers can be changed so he gets 1 credit per person, or 1 credit per 3 people, 1 credit for 5 people, etc. If he gets ...

How might I find the largest number contained in a JavaScript array?

I have a simple JavaScript Array object containing a few numbers. [267, 306, 108] Is there a function that would find the largest number in this array? ...