algorithm

How can I group an array of rectangles into "Islands" of connected regions?

The problem I have an array of java.awt.Rectangles. For those who are not familiar with this class, the important piece of information is that they provide an .intersects(Rectangle b) function. I would like to write a function that takes this array of Rectangles, and breaks it up into groups of connected rectangles. Lets say for examp...

formulation of general dynamic programming problem

I wonder if the objective function of a general dynamic programming problem can always be formulated as in dynamic programming on wiki, where the objective function is a sum of items for action and state at every stage? Or that is just a specical case and what is the general formulation? EDIT: By "dynamic programming problem", I mean...

Teleporting Traveler, Optimal Profit over time Problem

I'm new to the whole traveling-salesman problem as well as stackoverflow so let me know if I say something that isn't quite right. Intro: I'm trying to code a profit/time-optimized multiple-trade algorithm for a game which involves multiple cities (nodes) within multiple countries (areas), where: The physical time it takes to travel...

Good book for THINKING in terms of algorithms?

Before you mark this is a duplicate, let me explain why this is different. Most of the books on algorithms are more of a reference. You basically have a list of algorithms at your disposal. But what happens when you need to create a new algorithm for something? These books don't teach how to think in terms of algorithms. So I'm looking f...

What operations can be performed on disjoint sets?

I just studied the disjoint set data structure and I know that it is also called "union-find data structures", union and find are two main operations of this data structure. We can can perform union on disjoint sets, similarly we can perform find operations; I want to know what other operations we can perform on disjoint sets except unio...

How does PDF417 barcode decoding recover from damaged labels?

I recently learned about PDF417 barcodes and I was astonished that I can still read the barcode after I ripped it in half and scanned only a fragment of the original label. How can the barcode decoding be that robust? Which (types of) algorithms are used during encoding and decoding? EDIT: I understand the general philosophy of introdu...

What are the "hardest" problems which can be solved in polynomial time?

Recently I read a seminar work which says: The matching algorithm [for general graphs] can be extended to the weighted case, which appears to be one of the "hardest" combinatorial optimization problems that can be solved in polynomial time. Immediately the following question came to my mind: Do you know other "P-hard" pro...

Traversal of a tree to find a node

Hi again.... I'm either really stupid or my program hates me. Probably both. Problem: I am searching through a tree to find a value that is passed. Unfortunately, it does not work. I started debugging it with prints and stuff, and what is weird is it actually finds the value... But skips the return statement!!! /** * Returns...

Rotating a point about another point (2D)

I'm trying to make a card game where the cards fan out. Right now to display it Im using the Allegro API which has a function: al_draw_rotated_bitmap(OBJECT_TO_ROTATE,CENTER_X,CENTER_Y,X ,Y,DEGREES_TO_ROTATE_IN_RADIANS); so with this I can make my fan effect easily. The problem is then knowing which card is under the mouse. To...

How to find the last element of array in binary search

In binary Search algorithm, the upper-bound element is array.length-1, then how can I find the last element of an array? If the lower-bound and upper-bound for element of an array of length 8 is 6 and 7 respectively, then my mid element coming out as: mid=(6+7)/2 i.e. 6 in java ...

Project Euler Problem# 276 - Primitive Triangles

Hi, I tried to solve problem# 276 from Project Euler, but without success so far. Consider the triangles with integer sides a, b and c with a ≤ b ≤ c. An integer sided triangle (a,b,c) is called primitive if gcd(a,b,c)=1. How many primitive integer sided triangles exist with a perimeter not exceeding 10 000 000? The bo...

Fast way to get the bounding rectangle of a flood fill

I need to perform a flood fill on a region of an image. However I do not actually need the resulting image, I only need to know the smallest rectangle containing all pixels that would be changed by the flood fill. Is there a variant of a flood fill algorithm that can compute this rectangle more cheaply than doing a full flood fill? Ex...

Finding the minimum length RLE

The classical RLE algorithm compresses data by using numbers to represent how many times the character following a number appears in the text at that position. For example: AAABBAAABBCECE => 3A2B3A2B1C1E1C1E However, in the above example, that method results in even more space being used by the compressed text. A better idea would be t...

Binary tree to get minimum element in O(1)

I'm accessing the minimum element of a binary tree lots of times. What implementations allow me to access the minimum element in constant time, rather than O(log n)? ...

Error with my program

Okay I have the queue program that I have been working on and I finally figured most of it out. The problem I am having now is that everytime I enter numbers into the keyboard and then access them I get the same number. if I enter 5 ones when it goes to add them together it says the answer is 37 which is not right. here is my code again:...

Does anybody know how B-Tree got its name?

I'm reading CLRS 2nd and is studying B-Tree now. CLRS claims that B-Tree naming is not clear yet: [Bayer, McCreight, 1972] doesn't offer the reason that B-Tree is named to "B-Tree". I haven't investigated this issue any further... but does anyone know the reason? :) ...

How to create a biased number generator using a pair of six sided dice

What is the most efficient way to use a pair of six sided dice to generate a random number in [1, 4] unevenly: it should produce 1 in 40% of the time, 2 in 30%, 3 in 20%, and 4 in 10%. Please justify the correctness of the method and give an algorithm. Dice could be of different colors. Note: the only random number generators availa...

In a triangulated isometric grid, what triangle is a given point in?

I have a triangulated isometric grid, like this: In my code, triangles are grouped by columns. As I hover the mouse, I want to calculate what triangle the mouse coordinates are in. Is there a simple algorithm to do that? ...

How I can get sorted list in way of nearer matching in Tcl/tk?

Hi, which sorting algorithm can be used to get nearer and aproximate matching list with given string input: 1. list of strings 2. tobesearch_str ...

Monitor brands with common words

Let's say you should monitor the brand "ONE" online. What algorithms can be used to separate pages about the brand ONE from pages containing the common word ONE? I'm thinking maybe Bayes could work, but are there other ways to do this? ...