algorithm

Dictionary of Primes

I was trying to create this helper function in C# that returns the first n prime numbers. I decided to store the numbers in a dictionary in the <int,bool> format. The key is the number in question and the bool represents whether the int is a prime or not. There are a ton of resources out there calculating/generating the prime numbers(SO ...

double or half coin toss expectation

Came across an interesting problem today. You are given a coin and x money, you double money if you get heads and lose half if tails on any toss. What is the expected value of your money in n tries What is the probability of getting more than expected value in (1) This is how I approached it. The probability of heads and tails is sam...

How to link four points to a convex polygon

Hi guys, How to link four points to a convex polygon? I mean how to identify the order of these four points. Thanks. Zhong ...

Make nested for loop algorithm - dynamic

I have an algorithm that goes something like this: for m = 1 to 2 initialize(work_item(m)) for l = 1 to 2 initialize(work_item(l)) for k = 1 to 2 initialize(work_item(k)) for j = 1 to 2 initialize(work_item(j)) for i = 1 to 2 initialize(work_item(i)) doSomething(work_item(i)) ...

Can a non binary tree be tranversed in order?

Hi, We are dealing with a Most similar neigthbour algorithm here. Part of the algorithm involves searching in order over a tree. The thing is that until now, we cant make that tree to be binary. Is there an analog to in order traversal for non binary trees. Particularly, I think there is, just traversing the nodes from left to right (...

How could I group duplicates from a collection?

I'm creating a program that parses a log file for a user's name and its GUID (Global unique identifier) using regular expressions. So far, my program extracts the data properly, and stores it in a two-column DataTable. Outputting its content with this code: foreach (DataRow dr in guids.Select("","guid")) { Console.WriteLine(...

Most memory-efficient way of holding base64 data in Python?

Suppose you have a MD5 hash encoded in base64. Then each character needs only 6 bits to store each character in the resultant 22-byte string (excluding the ending '=='). Thus, each base64 md5 hash can shrink down to 6*22 = 132 bits, which requires 25% less memory space compared to the original 8*22=176 bits string. Is there any Python...

Recommended Open Source C# algorithms & data structures libraries

What algorithms libraries can you recommend? The basic criteria are: Capabilities Object Oriented Design Performance Algorithms libraries I have used and can recommend: QuickGraph - for graph algorithms C5 - for data structures that have not been included in .NET framework NetTopologySuite - for spatial processing and algorithms ...

Help needed for writing an algorithm

Here is the description of the the problem Given an integer N, write a function which returns an integer array of size N, containing the numbers from 1 to N in a random order. Each number from 1 to N must appear once and must not repeat. What is the running time of your algorithm? Can your algorithm be improved? For example: if you ...

Algorithm to find the optimal way of covering a plane with colored rectangles

Suppose that I open up MS Paint, draw a bunch of solid rectangles, save it as a png, and give it to you: Now you have to find out how I drew these rectangles. For this image, your algorithm would generate instructions like, Draw the green rectangle (filling up the entire space) Draw the pink rectangle Draw the yellow rectangle Draw ...

Kalman filter for car's tracking path

Hi, I am having a set of Points like Point(x,y). After the car gone through so many ways in the same road it is almost messing the resulting map. I heard that kalman filter can make a sigle path from the available paths. Can any body say, how to make it. I am not from computer science. So please explain me about that concept and those ma...

Does JPEG use a row-major compression algorithm?

A JPEG image, if it is non-progrssive loads from top-to-bottom, and not from left-to-right or any other manner. Doesn't that imply that jpeg uses some row-wise compression technique? Does it (use a row-wise compression technique)? ...

String Algorithm Question - Word Beginnings

Hi guys. I have a problem, and I'm not too sure how to solve it without going down the route of inefficiency. Say I have a list of words: Apple Ape Arc Abraid Bridge Braide Bray Boolean What I want to do is process this list and get what each word starts with up to a certain depth, e.g. a - Apple, Ape, Arc, Abraid ab - Abraid ar -...

Possible "boat loads" question

You know the river-crossing problems. Here is a sort description: Once upon a time, three cannibals were guiding three missionaries through a jungle. They were on their way to the nearest mission station. After some time, they arrived at a wide river, filled with deadly snakes and fish. There was no way to cross the river without a b...

Algorithm for covering population with minimum tags

My data is of people and tags, with a many-to-many relationship. I need an algorithm that will find a minimal set of tags such that the union of the groups of people tagged with each tag in the solution will cover the entire population. Any ideas? ...

Algorithm for seating people with most shared hobbies

Hi My data is of people and hobbies, with a many-to-many relationship. Each person has at least one hobby. I need to find a way to seat all of the people around N seat tables, in such a way that there is a maximum number of shared hobbies between the people in each table. It is not required that each table has at least one shared hobby...

Algorithm for finding all permutations for n alphabets

I wrote the following algorithm for finding all possible permutations of n unique alphabets. Set<String> results = new HashSet<String>(); int size = 1; //find the total permutations possible for(int i=0;i<array.length;i++){ size*=(i+1); } // i is the number of items remaining to be shuffl...

Algorithm for determining largest set of successively evenly spaced value of numbers in a sorted array?

For example, in the input array [0,1,4,6,8,9,12] the largest set of successively evenly spaced numbers is {0,4,8,12} and the next largest that isn't a subset of the largest is {4,6,8}. ...

Substring search algorithms (very large haystack, small needle)

I know there are already several similar questions here, but I need some recommendations for my case (couldn't find anything similar). I have to search a very large amount of data for a substring that would be about a billion times smaller (10 bytes in 10 billion bytes). The haystack doesn't change, so I can bear with large precomputati...

remove duplicate elements in an array in O(n) in C/C++

is there any method to remove the duplicate elements in an array in place in C/C++ in 0(n)? Suppose elements are a[5]={1,2,2,3,4} then resulting array should contain {1,2,3,4} the solution can be achieved using two for loops but that would involve O(n^2) i believe. ...