algorithm

Find two numbers in a binary search tree that add up to a third number

You are given a BST of numbers. You have to find two numbers (a, b) in it such that a + b = S, in O(n) time and O(1) space. What could be the algorithm? One possible way could be two convert the BST to a doubly linked List and then start from the front and end: if front + end > S then end-- Or: if front + end < S then front++ ...

Simulating "Wheel of fortune" (Monte Carlo Simulation Hit or Miss Method)

Hello, I'm trying to make a randomizer that will use the Monte Carlo Hit or Miss Simulation. I have a Key-Value pair that represents the ID and the probability value: ID - Value 2 - 0.37 1 - 0.35 4 - 0.14 3 - 0.12 When you add all of those values, you will get a total of 1.0. You can imagine those values as the total area of a "sl...

Token Suffix Tree Tutorial

Can someone please point to tutorials on - "Token Suffix Trees". Google, gives only links to research papers that are already using them! :( Thanks in advance. ...

How to implement a double linked list with only one pointer?

How to implement a double linked list with only one pointer? It takes O(1) time to find the prev and next Node. struct Node { int val; Node* p; }; ...

How do I delete Perl hash or array items based on their age?

How do I delete array items I'm not interested in? If I would leave themmy memory would get overflowed with unnecessary items. I need to implement in Perl one task. One file is being continuously filled with messages containing: "IP - URL" I need to continuously read this file and measure if there were more than, say five, same IP -...

Getting suggestions on how to get the best code coverage in java

Several tools exists that allow to calculate path coverage for a set of tests, but is there a tool (or an algorithm) that could suggest values to get the best path coverage with the smallest number of tests possible? For example with the following classes: public class User { private boolean isAdmin = false; private String nam...

How do i go about matching three fields from two tables in order to update the same tables in case of match

Given two tables A and B in a MySQL Database, how can one update a field in a row of table A if and only if certain fields in the fore-mentioned row in table A exactly match fields in a distinct row of table B. The rows in table B must be used once and only once for each comparison. As such, a row in Table B that has fields matching a ro...

Spelling Suggestions for Conjoined Words

I am working on implementing a spell check function for a web-based WYSIWYG editor. I am currently using the Damerau-Levenshtein distance algorithm to produce a list of spelling suggestions. This is all working out nicely, but I am curious as to how I might improve the functionality. Specifically, my implementation does not currently ...

Fastest way to pick a random element from a list that fulfills certain condition

I need to pick a random element from a list, that fulfills certain condition. The approach I've been using works, but I'm sure is not every efficient. What would be the most efficient way to do it? The following code is inside a while (true) loop, so obviously is not very efficient to shuffle the list on every iteration. Foo randomPick...

Help Determining This Sort Algorithm

I came across this sort algorithm when looking for a function to sort a collection of objects in VBA by a property of the object. In order to make sure that I understand all aspects of the code that I implement, I wanted to validate my thoughts on this algorithm. To me it looks like an Insertion Sort, but I wanted to check with the com...

What is a "safety variable"?

I was just reading an article about different ranking algorithms. One thing I'm a bit confused about is stumbleupon's algoritm: (Initial stumbler audience / # domain) + ((% stumbler audience / # domain) + organic bonus – nonfriend) – (% stumbler audience + organic bonus) + N N is a "safety variable" so that the assumed al...

Algorithm Implementation to Improve

I believe that out there we have algorithms implementations (e.g. a c++ implementation of a particular sorting algorithm) which might not be as efficient as they could be. I would like to write a research paper discussing how such an implementation might be improved. This could be in any programming language, however C, C++, Python, Jav...

Sorting a binary 2D matrix?

I'm looking for some pointers here as I don't quite know where to start researching this one. I have a 2D matrix with 0 or 1 in each cell, such as: 1 2 3 4 A 0 1 1 0 B 1 1 1 0 C 0 1 0 0 D 1 1 0 0 And I'd like to sort it so it is as "upper triangular" as possible, like so: 4 3 1 2 B 0 1 1 1 A 0 1 0 1 D 0 0 1 1 C 0 0 0 1 The row...

What’s An Algorithm or code for the obtaining ordinal position of an element in a list sorted by value in c++

Hi, This is similar to a recent question. I will be maintaining sorted a list of values. I will be inserting items of arbitrary value into the list. Each time I insert a value, I would like to determine its ordinal position in the list (is it 1st, 2nd, 1000th). What is the most efficient data structure and algorithm for accomplishing...

Running time of minimum spanning tree? ( Prim method )

I have written a code that solves MST using Prim method. I read that this kind of implementation(using priority queue) should have O(E + VlogV) = O(VlogV) where E is the number of edges and V number of Edges but when I look at my code it simply doesn't look that way.I would appreciate it if someone could clear this up for me. To me it s...

Is there an algorithm to find unique combinations of 2 lists? 5 lists?

I have N Lists I'd like to find unique combinations of. I've written it out on my whiteboard and it all seems to have a pattern, I just haven't found it yet. I feel I can express a brute-force method and that will certainly be something I pursue. Is there an alternative? Would a different data structure (binary tree?) make a job like thi...

Tips for Project Euler Problem #78

This is the problem in question: Problem #78 This is driving me crazy. I've been working on this for a few hours now and I've been able to reduce the complexity of finding the number of ways to stack n coins to O(n/2), but even with those improvements and starting from an n for which p(n) is close to one-million, I still can't reach the...

Fast algorithm for searching for substrings in a string

I'd like an efficient algorithm (or library) that I can use in Java to search for substrings in a string. What I would like to do is: Given an input string - INSTR: "BCDEFGH" And a set of candidate strings - CAND: "AB", "CDE", "FG", "H", "IJ" Find any CAND strings that match as substrings within INSTR In this example I wo...

A more efficient approach to Verbal arithmetic / Alphametics?

Perhaps most of you know the Send + More = Money. Well, I'm currently learning java and one of the exercises is I have to solve HES + THE = BEST. Now, so far I can/should use if-for-while-do loops, nothing else. Although I'm sure there are different methods to solve it, that's not the point of the exercise I'm going through. I have to b...

where can I find graph input resources/files?

Hi! I want to test various algorithms on graphs. Does anyone know a web where I can get lots of examples in text files? I've found many examples but they are always images. I want a text description of a graph, by edge listing or whatever... do you know one such source? Thanks! Manuel ...