algorithm

Find all chordless cycles in an undirected graph

How to find all chordless cycles in an undirected graph? For example, given the graph 0 --- 1 | | \ | | \ 4 --- 3 - 2 the algorithm should return 1-2-3 and 0-1-3-4, but never 0-1-2-3-4. (Note: [1] This question is not the same as small cycle finding in a planar graph because the graph is not necessarily planar. [2] I have...

Is there one type of set-like data structure supporting merging in O(logn) time and k-th search in O(logn) time?(n is the size of this set)

Is there one type of set-like data structure supporting merging in O(logn) time and k-th element search in O(logn) time? n is the size of this set. ...

Count the number of "holes" in a bitmap

Consider a MxN bitmap where the cells are 0 or 1. '1' means filled and '0' means empty. Find the number of 'holes' in the bitmap, where a hole is a contiguous region of empty cells. For example, this has two holes: 11111 10101 10101 11111 ... and this has only one: 11111 10001 10101 11111 What is the fastest wa...

Array comparisons and relevancy scores

In a script I am working on, I calculate how relevant every item in one array is to each item in another array by comparing similarities in keywords and keyphrases. End the end, I select the top 4 most relevant items for each item in that second array. I know this is a very vague background, but is there any way to avoid making the algo...

What is the name of the algorithm that returns order statistics quickly?

I have an idea of what I want the algorithm to do, but I'm looking to see if this has been implemented for me in python and I need to know the algorithm's name. I want to insert a set of values in the range [0,65535] into the container. I want to then ask the container for an arbitrary order statistic. Both insert and query should be ...

Algorithm for matching all items with another item in same list, where some have restrictions

Given array [a, b, c, d, e, f] I want to match each letter with any other letter except itself, resulting in something like: a - c b - f d - e The catch is that each letter may be restricted to being matched with one or more of the other letters. So let's say for example, a cannot be matched with c, d c cannot be matched with e,...

I need to diff two images to see what color(s) are different. Any medium level algorithms?

If I have two images which are both the left side view of a the same shoe in different styles, how can I determine by which color(s) they differ? Perhaps it's a shoe in two styles, one style has pink laces and a white side, the other has white laces and a yellow side. I want: Image One Colors: C1=Pink, C2=White Image Two Colors: C1=W...

How can Interval be solved differently in php

I need to solve a problem like below. I'm not sure, can you suggest better method? switch(true){ case $price>100: tooHigh(); break; case (($price<=100) && ($price>70)): negotiate(); break; case (($price<=70) && ($price>20)): accept(); break; case ($price<=20): thankAndEscape...

Group most similar elements together

I have a two-dimensional array of ints, for example: int [][] board= { {23,17,3,29,12,10}, {17,4,11,12,10,19}, {32,33,25,25,28,35}, {27,29,24,25,23,37}, {29,40,34,26,24,39}, {23,37,29,36,31,3} } I don't want to change the columns of this array at all; however, I would like to swap the rows so that the most similar rows are grouped tog...

Learning Graph Algorithms

Hi SO, In algorithms, I've mostly been self-taught and that's largely been fine. However, I'm having trouble grasping graph algorithns. I'm looking for some kind of reference that has concepts and actual code so I can not only learn the theory (which I usually do ok with) but also get a feel for how graphs are represented and manipulate...

How to give each object in a document a unique ID?

I'm making a bitmap editor where a document consists of several layers where each layer represents a bitmap. Each layer must have a unique ID compared to all other layers that currently exist in the document. I also need to take into account that I need to save and load documents along with the layer IDs. I'm using the command pattern t...

sort numbers by digit

why this code does not work? it does not show me output #include <stdlib.h> #include <iostream> #include <string.h> void Sort(int *arr,int length){ int *iter=arr; char buf[12],buf1[12]; while ((iter++)< (arr+length)){ if (iter==arr || (strcmp(itoa(*iter,buf,10),itoa(*(iter-1),buf1,10))>=0)){ iter++; ...

Runtime vertex generation

Is it possible to create an infinite world, where the world is generated using an algorithms? Does XNA support vertex loading during runtime? ...

Can I use implementations of SIFT algorithm?

Hello, I'm interested in image recognition. From what I have found I can say that SIFT algorithm is one of the best of the available solutions. However, SIFT is protected with a US patent, so I wonder if I can use this algorithm in my code? I could found many implementation of SIFT, some of them are open source, what makes me think tha...

Finding Eulerian Path In Perl

I have a code that try to find the Eulerian path like this. But somehow it doesn't work. What's wrong with the code? use strict; use warnings; use Data::Dumper; use Carp; my %graphs = ( 1 => [2,3], 2 => [1,3,4,5], 3 =>[1,2,4,5], 4 => [2,3,5], 5 => [2,3,4]); my @path = eulerPath(%graphs); sub eulerPath { my %graph = @_; # ...

How to create skill tree?

I would like to create a skill tree for my own and a community usage. I have data in format like below: skill_1 [description etc.] requires: none skill_2 [...] requires: skill_1 skill_3 [...] requires: skill_1, skill_2 skill_4 [...] requires: skill_1 OR skill_2 (data format from Civilization 5 units promotion) now i want to change th...

Objective C- How to add digits in a number?

How do I add the digits in a particular number for example if the number is 3234 the result should be 3+2+3+4 = 12? ...

How to find all words that have the same appearence in two different languages?

The russian alphabet includes many letters that are the same in the English alphabet. Here is the list of common letters: L='acekopuxy' Now, given two huge lists R and E, each in the form [word_A, word_B, ...], where each word_N is a lowercase word, I want to create a list C, which should contain only those words that have the same sp...

Does Peterson's algorithm satisfy starvation?

I've been searching information on Peterson's algorithm but have come across references stating it does not satisfy starvation but only deadlock. Is this true? and if so can someone elaborate on why it does not? Peterson's algorithm: flag[0] = 0; flag[1] = 0; turn; P0: flag[0] = 1; turn ...

Need strategy for managing aggregated data during large database table creation

Imagine collecting all of the world's high-school students' grades each month into a single table and in each student's record, you're required to include the final averages for the subject across the student's class, city and country. This can be done in a post-process, but your boss says it has to be done during data collection. Cons...