algorithm

Weighted voting system with karma

This question is more logic than programming at the moment.. once I understand what algorithm(s) I need to use I'll be looking into how to implement it. I've got a list of items in a database that need to be voted up or down by users to determine if they are correct or not. The aim is to provide a % for each item to show how reliable th...

Can anyone point me toward a content relevance algorithm?

A new project with some interesting requirements has arrived on my desk. I need to develop a searchable directory of businesses, with a focus on delivering relevant results based on arbitrary search queries. The businesses can be of any niche; there's no one area that is more represented than another. When googling for things like "se...

Calculating the smallest possible tree

Given a set of nodes, how can I construct a tree which links together all the nodes in a way which minimises max(max(degree), max(depth))? For example, given a set of five nodes, I could connect them like so: However, this is not minimal, since max(degree) == 4 and max(depth) == 1, a better tree would be: which has max(degree) == ...

Finding the border of a string

Firstofall let me tell you what a border of string is. Let x = abacab. The borders of x are ε, ab . That is a border of a string is a substring which is both a prefix and suffix of it. for e.g. In string abcde hgrab edcba abcde is prefix as well as suffix .Thus it is also the border of above string. Guys please could somebody help me...

Calculating a minimal tree for file transfer

I'm constructing a file sharing system which needs to transmit a single file to many peers. One single root peer has the entire file and needs to transfer it to all other peers. How can I best construct a tree of file transfers such that total waiting time is minimised? For example: In this tree, we need to wait 4 times before the tr...

Algorithm to find the lines bracketing one point

I have following task: In the program we should draw lines on a bit mapped display. An array of n pairs of reals (ai,bi) defined the n lines yi = ai*x + bi. The lines were ordered in the x-interval [0, 1] in the sense that yi < yi+1 for all values of i between 0 and n-2 and for all values of x in [0, 1] Less formally, the lines do...

Help me understand linear separability in a binary SVM

I'm cross-posting this from math.stackexchange.com because I'm not getting any feedback and it's a time-sensitive question for me. My question pertains to linear separability with hyperplanes in a support vector machine. According to Wikipedia: ...formally, a support vector machine constructs a hyperplane or set of hyperplane...

Formula for calculating an Ephemeris table

Here's an obscure question: I need to be able to determine historical astrological periods (semi) accurately. I am looking for a way to calculate the start and end dates of a given zodiac sign period given a geographical coordinate and a year. The purpose of the algorithm is to determine the possible dates of birth for a person whose a...

problem in understanding the hill cipher

i want to implement hill cipher but i think i have problem in understanding the algorithm itself. the key i'll use it 2X2 matrix and i'll encode 2 charachter each time. i'll multiply the key matrix with the matrix of 2 charachters then modulus the result on 26 as this equation C = E(K, P) = KP mod 26 where: K:key P:plain text ...

What is the best way to extract a diagonal from a matrix in Haskell?

I was asked to write a function that would extract the diagonal of a matrix stored as a list of lists. The first version was to extract the number by indexing the lists, but I soon concluded it isn't a good algorithm for Haskell and wrote another function: getDiagonal :: (Num a) => [[a]] -> [a] getDiagonal [[]] = [] getDiagonal (x...

How can I take the modulus of two very large numbers?

I need an algorithm for A mod B with A is a very big integer and it contains digit 1 only (ex: 1111, 1111111111111111) B is a very big integer (ex: 1231, 1231231823127312918923) Big, I mean 1000 digits. ...

Testing whether a polygon is simple or complex

For a polygon defined as a sequence of (x,y) points, how can I detect whether it is complex or not? A complex polygon has intersections with itself, as shown: Is there a better solution than checking every pair which would have a time complexity of O(N2)? ...

Nice way to store the number of elements between braces?

Given an input like a { b c d { e f } g } I want to parse it one token at at time (letter or brace). When I hit the first closing brace } I need to know how many elements there were since the last opening brace (e and f = 2). And then when I hit the one after that, I need 4 (b,c,d,g). Grabbing the tokens 1 by 1 is easy, but... I don'...

a simple formula

Hi, I have a for loop which gives a given integer sequence, for fixed parameters N and D : int i = 0, j = 0; for (int k=0; k<N; k++) { sequence[k] = i; if ((i += D) >= N) i = ++j; } I'd like to find a simple formula which reproduces this sequence, depending only on N and D (and the index k), like s...

A traveling salesman like problem

Given a set of points in the 2D Euclidean plane: P={V1,V2,..,Vn}, and we assume that there are K different types of points:T1,T2,..,TK, every point Vi in P belongs to exactly one of the K types. Definition of KTSP tour: Given an arbitrary location point V0 in the same 2D Euclidean plane (V0 has no type), a path V0->V'1->V'2->V'3->....-...

Algorithm Recurrence formula

Hi All, I am reading Algorithms in C++ by Robert Sedgewick. Basic recurrences section it was mentioned as This recurrence arises for a recursive program that loops through the input to eliminate one item Cn = cn-1 + N, for N >=2 with C1 = 1. Cn is about Nsquare/2. Evaluating the sum 1 + 2 +...+ N i...

What is the formal name of the number-picking game?

You know that game, where you and an opponent take turns choosing numbers, and the last person to take their turn before the cumulative total passes a preset value, is the winner? What's the formal name of that game? I know it's a solved game in that there's an algorithm to generate the winning moves, but, given that google doesn't under...

Is there a simpler way than this to calculate a straight in poker?

I have an algorithm for calculating whether a player's hand holds a straight in Texas Hold'em. It works fine, but I wonder if there is a simpler way to do it that does not involve array/string conversions, etc. Here's a simplified version of what I have. Say the player is dealt a hand that is a 52-element array of card values: var rawH...

sequential search analysis

Hi All, I am reading algorithms in C++ by Robert Sedwick it was mentioned as follows Sequential search in an ordered table examines N numbers for each search in the worst case and about N/2 numbers for each search on average. This result follows from assuming that the search is equally likely to terminate at any ...

Algorithm for counting connected components of a graph in Python

Hi, I try to write a script that counts connected components of a graph and I can't get the right solution. I have a simple graph with 6 nodes (vertexes), nodes 1 and 2 are connected, and nodes 3 and 4 are connected (6 vertexes; 1-2,3-4,5,6). So the graph contains 4 connected components. I use following script to count connected componen...