algorithm

c programming puzzle

Given an array all of whose elements are positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7). I tried it using all possible allowed sums and then fin...

Is it possible to get an appriximtion to a seed based on a finite sequance of pseudo random numbers?

suppose I have some numbers that form a series for example : 652,328,1,254 and I want to get a seed that if I ,for example ,do srand(my_seed); I will get some kind of approximation with bounded error to my origonal sequance , when all numbers appearing in the same order. thanks, Alex ...

PROJECT EULER #29

Well, after solving this problem by naive STL set,I was reading the forum entries,there I find this entry : #include <iostream> #include <cmath> #define MAX 100 using namespace std; int main(){ int res=(MAX-1)*(MAX-1); for(int i=2;i<MAX;i++) for(int j=i*i;j<=MAX;j=j*i) res = res-int(MAX*(log(i)/log(...

How to create a unique order number

Algorithmically speaking, how could I generate a unique, human readable, reasonably lengthed - order number for SQL Server column. The only requirement is that it references the Customer Number and can be easily repeated over the phone. Something like: Customer Number - XXXXXXXX - XXXXXXXX RT65-XXXXXXXX-XXXXXXXX How would I genera...

Formally verifying the correctness of an algorithm

First of all, is this only possible on algorithms which have no side effects? Secondly, where could I learn about this process, any good books, articles, etc? ...

Randomly Generate Letters According to their Frequency of Use?

How can I randomly generate letters according to their frequency of use in common speech? Any pseudo-code appreciated, but an implementation in Java would be fantastic. Otherwise just a poke in the right direction would be helpful. Note: I don't need to generate the frequencies of usage - I'm sure I can look that up easily enough. ...

Finding ideal utilization of rentable items in ruby

# Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec rentable_items = [ %w(x x x - - - x x x - - -), # item1 %w(- - - - - - - - - - - -), # item2 %w(x x x x x x x - - - - -), # item3 %w(x x x - - - - x x x x x) ]# item4 Ok,given that data-structure (which represents rent-slots in months of the items) where "x" stands f...

how to manage a "resource" array efficiently

The senario of my question is that one need to use a fixed size of array to keep track of certain number of "objects" . The object here can be as simply as a integer or as complex as very fancy data structure. And "keep track" here means to allocate one object when other part of the app need one instance of object and recyle it for fut...

How to use the Stanford GraphBase

Hi all I am reading the Donald Knuth's Volume 4 about the combination which mentions that using the Stanford GraphBase to learn the algorithms. I have downloaded the SGB and install the CWEB. But I do not know what to begin with. Does anyone do it before? Could you give me some suggestion about? ...

Algorithm for a strategy game

This is a question I have been toying with for a week or so, proposed by a colleague: Imagine a game played on a 36x36 grid. The goal of the game is to create four corners of a square of any size (eg., 2x2, 3x3, 4x4, and so on). The first player places a game-piece anywhere except the center four grid spaces. After the first move, p...

Computing different terms for a given pair of exponentiation having the same result

To understand the problem,let us consider these examples first:                                  46 = (22)6 = 212 = (23)4 = 84 = 163 = 4096. Thus,we can say that 46,212,84 and 163 are same.                                  273 = 39 = 19683 so, both 273 and 39 are identical. Now the problem is, for any given pair of ab how to comput...

C/C++ implementation of a subset sum kind of problem

I think that stackoverflow is rendering me even lazier than usual, but... The problem is simpler than knapsack (or a type of it, without values and only positive weights). It consists on checking if a number can be a combination of others, and the function shall return true or false. Ex: 112 and a list with { 17, 100, 101 } should retu...

Is there any Algorithm for converting 2d video into 3d video?

Is there any Algorithm for converting 2d video into 3d video? (for viewing using glasses) (a-la turning Avatar into Avatar for An IMAX 3D Experience ) or at least turn it into video prepared for feeling some 3d viewing using a-la or I know my question is asked(expressed) not in the best way so feel free to edit. ...

Which topic in Discrete Mathematics is considered as a prerequisite for data structures course?

Hello, I want to read a book on Data Structures and Algorithms, but i would like to know if there is any specific topic in Discrete Mathematics considered very important as a prerequisite in order to understand the materials presented in Data Structure Book. Thank you in advance for your help P.S i am self-thought programmer, i didn't...

InsertionSort vs. InsertionSort vs. BinaryInsertionSort

I have a couple of questions concerning different implementations of insertion sort. Implementation 1: public static void insertionSort(int[] a) { for (int i = 1; i < a.length; ++i) { int key = a[i]; int j = i - 1; while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; --j; } ...

TSP - branch and bound

Hi. I'm trying to solve the TSP with branch and bound algorithm. I'm must bulid a matrix with cost but i have one big problem. I have city with coordinates x and y. The cost of travel is ceil(ceil(sqrt((x1-x2)^2+(y1-y2)^2))/v) + days in city. V is speed. Days in city depend from day when w come to city. For example when we arrived on...

BFS and DFS in SML

I need BFS and DFS sml code. does anyone how should I write it? ...

Algorithm to generate unique (possibly auto incremented) ids

I need to generate unique ids for my application and I am looking for suitable algorithms. I would prefer something like this -- YYYY + MM + DD + HH + MM + SS + <random salt> + <something derived from the preceding values> F.ex. - 20100128184544ewbhk4h3b45fdg544 I was thinking about using SHA-256 or something but the resultant string...

Python linked list O(1) insert/remove

Hey, I am looking for a linked list and related algorithms implementation for Python. Everyone I ask just recommends using built in Python lists, but performance measurements indicate that list insertion and removal is a bottleneck for our application. It's trivial to implement a simple linked list, but I wonder if there is a mature lib...

A simple but challenging SQL Question, at least I couldn't find a way out except doing it externally (c#).

Hello All, I have an SQL Table which consists of 1 column only Column Name A A A B B B B C D D E I need an SQL Code that returns the cut points. For the table above, it will return this: Column Name 3 7 8 10 11 3 is the end of A's and 7 is the end of B's and 8 is the end of C's and so on... Let's see what can...