algorithm

Code for identifying programming language in a text file

Hi all, i'm supposed to write code which when given a text file (source code) as input will output which programming language is it. This is the most basic definition of the problem. More constraints follow: I must write this in C++. A wide variety of languages should be recognized - html, php, perl, ruby, C, C++, Java, C#... Amount o...

what is the best way to create all pairs of objects of two, from a huge list of objects (say a million strings)?

lets say I've a list of 10 strings (lets just call it "str1", "str2", ... "str10" etc). I want to be able to generate all pairs from this ("str1", "str2") ("str1", "str3") . . . etc upto ("str9", "str10"). That is easy, with two loops. How to do the same thing with a million strings? Is there anyway to put it in a table, and run a q...

What are some quality graph libraries for C?

Hey All, I'm writing some C where I'm going to need to store a very large graph as an adjacency matrix. I was going to hack up a quick graph implementation, but wanted to ask first if there are any good graph libraries for C (not c++) that people like. I'm going to import the graph in some standard format (probably GML, but thats not a...

Ultra symmetrical line algorithm?

I ran into special case where I need to produce ultra symmetrical line or ray in 2D grid in order from (x0, y0) through (x1, y1) like this: void drawSymmetricalLine(int x0, int y0, int x1, int y1) { // loop and handle each (x, y)... } The actual problem lies in points where popular line drawing algorithms does NOT draw both coordi...

Traversal of cyclic directed graph

I have a cyclic directed graph. Starting at the leaves, I wish to propagate data attached to each node downstream to all nodes that are reachable from that node. In particular, I need to keep pushing data around any cycles that are reached until the cycles stabilise. I'm completely sure that this is a stock graph traversal problem. Howe...

What is the best algorithm for closest word

What is the best algorithm for closest word. Possible word dictionary is given and first characters in the input word can be wrong. ...

optimized algorithm to synchronize two arrays

hi... am looking for an efficient algorithm to synchronize two arrays.. Lets say a1 and a2 are two arrays given as input... a1 - C , C++ , Java , C# , Perl a2 - C++ , Python , Java , Cw , Haskel output 2 arrays: Output A1 : C , C++ , Java Output A2 : Cw , Haskell , Python Output A1 : 1) items common to both arrays 2) items only i...

Finding largest connected tree in a matrix

Assume I have a matrix MxN, filled with values between 0 and 5. I now want to determine the largest connected tree in that matrix, where the values of the matrix are considered to be the nodes. A pair of nodes is said to be connected if it's nodes are adjacent to each other either horizontally or vertically, and if the value of both node...

Which language for rapid-prototyping of heuristics in combinatorial optimization?

I have to develop a fast heuristic-like algorithm for a hard combinatorial problem. Therefore I think, it would be the best to learn a more expressive language than C++ first for tackling the problem, because I think a lot of different algorithm attempts are needed to solve my assignment. Some personal views about some languages which a...

3D rotation algorithm needed

Given two orthogonal unit vectors, A and B, and two different orthogonal unit vectors C and D, I need the 3x3 direction cosine matrix or a quaternion which will rotate A to align with C AND will rotate B to align with D. The vectors are all 3-vectors (x, y, z). I have a brute force algorithm, but I am almost certain there is a much sim...

Calculate fractional exponent in for loop without power function.

My maths in this area is a bit shaky. Does anybody know how I can calculate a power such as 10^2.2 using no maths functions other than */-+ and a for loop? I don't have access to a maths library (and can't import/include it), but need to calculate these things. Hmm.. maybe I should just look how the math library does it. ...

Towers of Hanoi with K pegs

The Towers of Hanoi problem is a classic problem for recursion. You are given 3 pegs with disks on one of them, and you must move all the disks from one peg to another, by following the given rules. You must also do this with the minimum number of moves. Here's a recursive algorithm that solves the problem: void Hanoi3(int nDisks, char...

Simple Javascript encrypt, PHP decrypt with shared secret key

This is not about security. It is also not to make it hard to break. I'm looking for a simple algorithm to change a string (a url) in a way it does not resemble the original. The encryption will be done with javascript. Then I want to feed the encrypted string to a PHP function to change it back to the original. Both ends could share a s...

Algorithm for two-dimensional rebinning

I have a 12-by-50 array that needs rebinning. The array represents a bivariate probability distribution, p(a,b), where a and b are non-Cartesian coordinates. However, I want to rebin it so that I have a distribution in Cartesian coordinates, p(x,y). a and b are (mildly) nonlinearly related to x and y, however I make the simplifying ass...

how to write a function to find the time complexity and space complexity of any algorithm ?

Possible Duplicates: Program/algorithm to find the time complexity of any given program. Programmatically obtaining Big-O efficiency of code Hi, i am eager to develop a function(say computeComplexity()) which can determine the time complexity and space complexity of any algorithm(say bubbleSort()). Usage of computeComplexit...

Finding the highest-n values in a Map

I have a large map of String->Integer and I want to find the highest 5 values in the map. My current approach involves translating the map into an array list of pair(key, value) object and then sorting using Collections.sort() before taking the first 5. It is possible for a key to have its value updated during the course of operation. I...

How to compute shortest unique prefixes of a set of strings?

It's a pretty common algorithm in command line parsing. Given a set of predefined long option names -- compute the shortest prefix that uniquely identifies one of these options. So for instance, for the following options: -help -hostname -portnumber -name -polymorphic This would be the output: -he -ho -por -n -pol I'm thinking tw...

Interview question - Finding numbers

Hi, I just got this question on a SE position interview, and I'm not quite sure how to answer it, other than brute force: Given a natural number N, find two numbers, A and P, such that: N = A + (A+1) + (A+2) + ... + (A+P-1) P should be the maximum possible. Ex: For N=14, A = 2 and P = 4 N = 2 + (2+1) + (2+2) + (4+2-1) N = 2 + 3 ...

Need an algorithm to pixelate an n-dimensional hypersphere.

I would like to bin vectors in n-dimensional space. This can be done by pixelating the surface of an n-dimensional hypersphere. Does anyone know any good algorithms for pixelating a hypersphere in C? I would like constant bin sizes. My space consists of only positive integers. ...

Algorithm to convert a multi-dimensional array to a one-dimensional array

It's easy enough to convert a 2-dimensional array to a single-dimensional array but how can I convert a multi-dimensional array of more than 2 dimensions to a one-dimensional array? For example, let's assume I have int [5][5][5] x and int [125] y and I want to place the value at x[3][4][2] in its right place in y? Hope that makes sens...