algorithm

Non recursive depth first search on graphs for Delphi

I am searching for a non-recursive depth first search algorithm on graphs in Pascal (Delphi). I need DFS for computing strongly or bi-connected components of large graphs. Currently I am using a recursive variant of the algorithm: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm The problem is that for ...

Find if a line intersects a sphere

Trying to make a very simple boolean function that will find whether a line intersects a sphere. This did not seem to be what I want, even though the question was similar: http://stackoverflow.com/questions/910565/intersection-of-a-line-and-a-sphere Also I have tried the algorithms listed at: http://www.docstoc.com/docs/7747820/Inters...

How is CPU usage calculated?

I hope this is the right place to ask this question, as I think it is related to algorithms and performance. On my desktop, I have a little widget that tells me my current CPU usage. It also shows the usage for each of my two cores. I always wondered, how does the CPU calculate how much of its processing power is being used? Also, if ...

What algorithms / optimisations are there for calculating a conditional subset of elements from an array?

My program often deals with large quantities of data, and one particular component of it creates a subset of that data, based on a condition. You could view this as, having the string, 10457038005502 the problem is to return the first five (say) non-0 elements, that is to return: 14573 In actual fact, each element of this string i...

Finding partial substrings within a string

I have two strings which must be compared for similarity. The algorithm must be designed to find the maximal similarity. In this instance, the ordering matters, but intervening (or missing) characters do not. Edit distance cannot be used in this case for various reasons. The situation is basically as follows: string 1: ABCDEFG string...

listing all interesting sections of a tetrahedron

An interesting 2d section is plane that goes through the center of a regular 3d simplex and 2 other points each of which is a centroid of some non-empty subset of vertices. It is defined by two subsets of vertices. For instance {{1},{1,2}} gives a plane defined by 3 points -- center of the tetrahedron, first vertex, and average of first ...

Selecting items from a list to achieve a sum

I have a list of items that has numeric values and I need to achieve a sum using these items. I need your help to build such an algorithm. Below, there is a sample that describes my problem, written in C#: int sum = 21; List<Item> list = new List<Item>(); list.Add(new Item() { Id = Guid.NewGuid(), Value = 3 }); list.Add(new Item() { Id...

find inorder successor in BST without using any extra space.

I am looking a way to find out inorder successor of a node in BST withut using extra space. ...

Basic encryption to authenticate users

I'm writing a web service and I need to make sure only valid applications will use it (before I start managing a session for their users). In order to achieve that I thought of using asymmetric key algorithm, but I'm not really sure how - what data to encrypt, how to manage the keys, etc. (my web service's data isn't that sensitive, I...

Optimization of variable multiplication

[This was initially on matrices, but I guess it applies to any variable generically] Say we have Var1 * Var2 * Var3 * Var4. One of them sporadically changes, which one of them is random. Is it possible to minimize multiplications? If I do In case Var1 changes: newVar1 * savedVar2Var3Var4 I noticed that then I need to recalculate ...

Best way to create moving map

I'm looking for a good way to create a moving map app on an semi-embedded device, comparable to a netbook. The source images are 400MB tiff files with associated world and projection files. The current approach I've taken is to create a tiled dataset for the desired zoomlevels in OSM map format. It works, but uses up way too much disks...

asymptotic complexity

suppose a computer executes one instruction in a microsecond and an algorithm is known to have a complexity of O(2^n), if a maximum of 12 hours of computer time is given to this algorithm, determine the largest possible value of n for which the algorithm can be used ...

How to limit item position into Canvas?

[EDIT] Alright, I edited this post since the code I posted back then had no real links with what I'm trying to do now, but the question is the same. When I'm talking about limiting objects to a Canvas it was more like a Mouse Clipping, but as I read on many threads, this feature doesn't exist in SL. So I searched a bit around all foru...

Popularity, How to make new hits count more than old hits?

Each product a product_date_added which is a Date field contained the date it was added. They also have a product_views which is an int field containing how many times a product has been viewed. To display products by popularity, I us an algorithm to calculate how many hits per day a product has. SELECT AVG(product_views / DATEDI...

Finding border of a binary tree

We are given a binary search tree; we need to find out its border. So, if the binary tree is 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / / \ 15 35 120 155 250 It should print out 50 25 15 35 120 155 250 20 150 10. If the binary tree is ...

Counting sort - implementation differences

I heard about Counting Sort and wrote my version of it based on what I understood. public void my_counting_sort(int[] arr) { int range = 100; int[] count = new int[range]; for (int i = 0; i < arr.Length; i++) count[arr[i]]++; int index = 0; for (int i = 0; i < count.Length; i++) { ...

Pythonic way to check if a list is sorted or not

Is there a pythonic way to check if a list is already sorted in AESC or DESC. listtimestamps=[1,2,3,5,6,7] something like listtimestamps.isSorted() that returns True or False. EDIT: I want to input a list of timestamps for some messages and check if the the transactions appeared in the correct order. and I can write a custom function...

Maximum interval overlaps using an interval tree

Here is an interesting question: Given a set of N intervals ([start, end]), use an interval tree to find the maximum number of overlapping intervals. A similar question on StackOverflow provided an O(N) solution, but if we can pre-process the intervals into an interval tree, perhaps we can find the solution in logarithmic time. In fact, ...

Efficient database query for ancestors on an acyclic directed graph

Let's say I have an acyclic directed graph such as a family "tree" (not really a tree since a child has 2 parents). I want to place a representation of this graph in a relational database so that it's fast to compute all ancestors of a node, and all descendants of a node. How would you represent this graph? How would you query for all de...

Traversing and inserting into a weird binary tree [SOLVED]

Ok, I ran into this tree question which LOOKED simple, but started driving me nuts. The tree given is LIKE a binary search tree, but with one difference: For a Node, leftNode.value < value <= rightNode.value. However, (by their omission of any further info, and by their example below) this ONLY applies to the immediate children, no...