algorithm

is there a difference in the result from these two algorithms?

hi there, these two algorithms are used to check valid member numbers the first is the one I was given by the company, the second is one I devised, from my tests I can't see any difference between them functionally, are there any cases anyone can see where they would return different outputs? test input: 6014355021355010 or 601435506...

Algorithms or theory for merging portions of two graph structures?

I have a directed acyclic graph, an example of which may look like: | (1) / | \ / | \ (3) (2) (4) / / | \ \ / / | \ \ / / | \ \ (6)(7) (5) (8)(9) | | | ...

Efficient priority list

I am looking for an efficient data structure to represent a priority list. Specifically I need to assign a priority to a set of items and return only the top scoring items. I have looked into priority queues which operate on heaps, but they don't seem to really suit my needs. They will reorganize the heap structure as soon as I will poll...

Transform one integer to another

Suppose there are given two integers, a and b, and we know that a>b. I want to calculate how many operations I should make on b to get a (by operation I mean that bitwise operations change a bit from 1 to 0 and vice versa. How can I count the number of operation for such a transform? ...

Lexicographical ordering of multiple doubles

Consider a class of type doubles class path_cost { double length; double time; }; If I want to lexicographically order a list of path_costs, I have a problem. Read on :) If I use exact equal for the equality test like so bool operator<(const path_cost& rhs) const { if (length == rhs.length) return time < rhs.time; return...

Algorithm for searching grid in diffrent direction

Hi, Today a work mate posed a question to me. Given a grid of characters and a list of words one would have to find every occurrence of that word in the grid both horizontally, vertically and diagonally. The solution we came up with works and does the trick but I am interested to see how other peoples minds tick. ...

is there any problem with this algorithm?

I have a problem with this algorithm for Heap-Sort Heap_Sort(A) Build_Heap(A) for i<--n down to 2 swap (A[1],A[n]) n<--n-1 MaxHeapify(A,1) I think instead of this algorithm we should write: Heap_Sort(A) Build_Heap(A) for i<-- n down to 1 Delete_Max(A) ...

Is this how rotation about a point is done?

Let's say I have a polygon with points: (0,0) (100,0) (100,100) (0,100) Lets also let it's center be (50,50). To rotate it would I add 50 to each component of each point, then do x' = cos(theta)*x - sin(theta)*y y' = sin(theta)*x + cos(theta)*y Then subtract 50 from each component of each point? Thanks ...

Scaling a polygon toward an edge?

I know that to scale verticies I simply have to multiply by a scale factor. But I noticed that most vector drawing applications show the shapes bounding box and by dragging one of the edge it will scale the geometry toward the opposite edge, then if you go past this edge it will end up mirroring the geometry on that axis. How is scaling ...

all possible combinations ...

I have a math problem that goes as follows: I have a container which holds a total of 21000 kilos. I have 4 items A,B,C,D . Item A weights 1 kilo. Item B weights 4 kilos. Item C weight 5 kilos. Item D weights 5 kilos also. I am looking for an algorithm that will iterate through all possible combinations keeping the above equation. for...

how to simulate a rectangle union starting with a rectangle intersection

Given rectangle_A intersecting rectangle_B, Which has a union defined such that it is the rectangle containing both rectangles: I want to determine the coordinates of the (not overlapping) rectangles required to add to rectangle_A to create the union of rectangle_A and rectangle_B: (note: this is just one configuration of the so...

Finding the largest palindrome of the product of two three digit numbers problem.

Hi, So on Project Euler the Problem 4 states the following: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers. I have tried the following: #include <stdio.h> ...

Automatically recognize patterns in images

Recently I downloaded some flags from the CIA world factbook. Now I want to "classify them. Get the colors Get some shapes (stars, moons etc.) While browsing I came across the Python Image Library which allows me to extract the colors (i.e. for Austria: #!/usr/bin/env python import Image bild = Image.open("au-lgflag.gif").convert("R...

What is a good algorithm to traverse a Trie to check for spelling suggestions?

Assuming that a general Trie of dictionary words is built, what would be the best method to check for the 4 cases of spelling mistakes - substitution, deletion, transposition and insertion during traversal? One method is to figure out all the words within n edit distances of a given word and then checking for them in the Trie. This isn'...

A recursive method to traverse a multi-dimensional array in PHP

I need a method to traverse a multidimensional array in PHP without using function calls itself. Basically I have an array like this: $data = array( 'hoge' => 123, 'foo' => 456, 'bar' => 789, 'aaa' => array( 'abc' => 111, 'bcd' => 222, 'cde' => 333 ), 'bbb' => array( 'def' => arra...

Correct implementation of Boyer Moore algorithm

I tried to use several implementations, but all of them had bugs. Search at SO gave me http://www-igm.univ-mlv.fr/~lecroq/string/node14.html - looks nice, but this implementation gave me wrong results - sometimes it doesn't found a string. I spent couple hours to find the bug. Following line looks fine: j += MAX(bmGs[i], bmBc[y[i + j]]...

enumerate grouped columns vertically

If I have a matrix that is iterated horizontally and then vertically it would enumerate like this: 0 1 2 3 4 5 6 7 8 9 ------------------------------ 0 | 1 2 3 4 5 6 7 8 9 10 1 | 11 12 13 14 15 16 17 18 19 20 2 | 21 22 23 24 25 26 27 28 29 30 if I want to enumerate vertically I could do this: total_rows * ...

Java: Using adjacency list to compute all pairs shortest path?

I am writing a Graph library that has both adjacency list and matrix implementations. Here is some code I came across in a Java data structures textbook: static void floyd(Graph<V,E> g) // post: g contains edge (a,b) if there is a path from a to b { Iterator<V> witer = g.iterator(); //vertex iterator while (witer.hasNext()...

algorithm for DFS

Can anybody tell me the algorithm for depth first search (DFS)? I need to write the code for DFS in python. ...

Java:Internal data structure in Map

I am trying to create a class that implements the Map interface. So I am writing code that will check if the calling object is empty or not. However I am a little confused as to which data structure I should use internally. At present I am using a Hash Table. Thanks in advance ...