algorithm

Easy way to determine whether a given graph is subgraph of some other graph?

Hello, I'm looking for an algorithm to check whether a given graph is subgraph of another given graph. I have few conditions to make this NP complete problem bit more feasible.. The graphs have approx <20 vertices. The graphs are DAG. All vertices are non-uniquely labeled, and the corresponding vertices in the main graph and the subg...

Detecting consecutive integers in a list

I have a list containing data as such: [1, 2, 3, 4, 7, 8, 10, 11, 12, 13, 14] I'd like to print out the ranges of consecutive integers: 1-4, 7-8, 10-14 Is there a built-in/fast/efficient way of doing this? ...

Implementing a parallel algorithm to compute pi

I would like to implement a parallel version of the code below using threads in OpenMP,is there any better way to do this? /* Program to compute Pi using Monte Carlo methods */ #include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #define SEED 35791246 int main(int argc, char* argv) { int ni...

Generating a URL pattern when provided a set of 5 or so URLs

Provided with a set of URLs, I need to generate a pattern, For example: http://www.buy.com/prod/disney-s-star-struck/q/loc/109/213724402.html http://www.buy.com/prod/samsung-f2380-23-widescreen-1080p-lcd-monitor-150-000-1-dc-8ms-1920-x/q/loc/101/211249863.html http://www.buy.com/prod/panasonic-nnh765wf-microwave-oven-countertop-1-6-ft...

Code for Variations with repetition (combinatorics)?

Does anyone have Java code for generating all VARIATIONS WITH REPETITION? There are plenty of permutation and combination examples available, and variations must be the easiest one... It feels stupid to waste time to reinvent the wheel (it must be plenty of code written for this). An example of VARIATIONS WITH REPETITION could be like...

Designing this algorithm a better way?

I am working a much more complex version of this (with vehicle moving in both X and Y directions) I made this example to get ideas on better ways to accomplish this. I have a vehicle moving in the X direction at a speed (24.5872 mps) I am simulating this by incrementing the X value every 100 ms using an executor (To keep its X positio...

All valid combinations of points, in the most (speed) effective way

I know there are quite some questions out there on generating combinations of elements, but I think this one has a certain twist to be worth a new question: For a pet proejct of mine I've to pre-compute a lot of state to improve the runtime behavior of the application later. One of the steps I struggle with is this: Given N tuples of t...

Mergesort to sort three input arrays

A Merge algorithm merges two sorted input arrays into a sorted output array, by repeatedly comparing the smallest elements of the two input arrays, and moving the smaller one of the two to the output. Now we need to merge three sorted input arrays (A1, A2, and A3) of the same length into a (sorted) output array, and there are two method...

Inserting a new value in binary search tree

Using an algorithm Tree-Insert(T, v) that inserts a new value v into a binary search tree T, the following algorithm grows a binary search tree by repeatedly inserting each value in a given section of an array into the tree: Tree-Grow(A, first, last, T) 1 for i ← first to last 2 do Tree-Insert(T, A[i]) If the tree is i...

Ordering concurrent tasks to minimise waiting

In a system with multiple concurrent tasks operating on data, I want to order the tasks such that there is a minimum amount of waiting time involved. Each task in the system uses a certain set of resources, tasks are issued in a certain order (this order is what I want to calculate), a task will not start until it can get a lock on all r...

Why are hash table expansions usually done by doubling the size?

I've done a little research on hash tables, and I keep running across the rule of thumb that when there are a certain number of entries (either max or via a load factor like 75%) the hash table should be expanded. Almost always, the recommendation is to double (or double plus 1, i.e., 2n+1) the size of the hash table. However, I haven'...

find the position of a string in another string

Possible Duplicate: substring algorithm Given two strings, A and B, how to find the first position of B in A? For instance, A = " ab123cdefgcde"; B= "cde" Then the first position of B in A is 5. Is there any trick to solve this problem or just search A from the start? ...

shortest digest of a string

[Description] Given a string of char type, find a shortest digest, which is defined as: a shortest sub-string which contains all the characters in the original string. [Example] A = "aaabedacd" B = "bedac" is the answer. [My solution] Define an integer table with 256 elements, which is used to record the occurring times for each ki...

Modifying a list from order A to order B using only push and remove operations

Is there any well-known algorithm (or obvious solution) for transforming a list from order A to order B using only push and remove operations? For instance, from a b c d to b c a d could be done using the following operations: remove(a) remove(d) push(a) push(d) Or, from c b a to a b would be remove(c) remove(b) push(b) Or, from a ...

Athletic Result Rating System

I'm trying to develop an alternative rating system for athletic results. We're all aware of the traditional first past the post rating system for races. Think of the 100m final in the Olympics. First gets gold, second silver, etc. This system only benefits the top three. In my system, there is a series/league of races, where all eight ...

Which is the best way to randomly generate a list of fixtures?

I have an even number of teams and I need to create a list of fixtures. The rule is that a team that plays a fixture at home will play the next one away. What do you think is the best aproach for this problem? So the problem is to generate half of the fixtures, the other will be generated reversing home - away. So, if I have 6 teams {A...

How to implement strlen as fast as possible

Assume that you're working a x86 32-bits system. Your task is to implement the strlen as fast as possible. There're two problems you've to take care: 1. address alignment. 2. read memory with machine word length(4 bytes). It's not hard to find the first alignment address in the given string. Then we can read memory once with the 4 by...

search an element in a heap

Hi I remembered that heap can be used to search whether an element is in it or not with O(logN) time complexity. But suddenly I can't get the details. I can only find getmin delete add and so on. Can anybody give a hint? ...

Java: How to determine programmatically that a dataset doesn't follow a normal distribution?

In a Java program, how can I determine if a dataset I have is following or not a normal distribution? Is it possible? Is there an API or an algorithm that I can use that determines this? ...

grouping points when they are close to each other

I have 2d-points (x,y) with float coordinates, when I draw them, I need to group points if they are close to each other, and they should be grouped iwith help of rectangle with fixed size. And the problem is that these rectangles should not intersect, and all points-neighbours should be grouped. If you have a paper nearby, you can draw a...