algorithm

Interview question: How to detect a loop in a linked list?

Say you have a linked list structure in Java. It's made up of Nodes: class Node { Node next; // some user data } and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a referenc...

Graph and permutation problem

I have a graph (with nodes and edges) containing symmetry and a group of permutations to label the nodes so no edges are changed (automorphisms). Now I would like to determine for which nodes a permutation exchanges two equivalent (i.e. nodes with the same color or symmetry class) neighboring nodes. When the nodes with equivalent neighb...

How does heap compaction work quickly?

They say that compacting garbage collectors are faster than traditional memory management because they only have to collect live objects, and by rearranging them in memory so everything's in one contiguous block, you end up with no heap fragmentation. But how can that be done quickly? It seems to me that that's equivalent to the bin-pa...

What is a convenient base for a bignum library & primality testing algorithm?

Hi, I am to program the Solovay-Strassen primality test presented in the original paper on RSA. Additionally I will need to write a small bignum library, and so when searching for a convenient representation for bignum I came across this specification: struct { int sign; int size; int *tab; } bignum; I will also be writing a m...

Fastest way to calculate an X-bit bitmask?

I have been trying to solve this problem for a while, but couldn't with just integer arithmetic and bitwise operators. However, I think its possible and it should be fairly easy. What am I missing? The problem: to get an integer value of arbitrary length (this is not relevant to the problem) with it's X least significant bits sets to 1 ...

Non recursive way to position a genogram in 2D points for x axis. Descendant are below

I currently was tasked to make a genogram for a family consisting of siblings, parents with aunts and uncles with grandparents and greatgrandparents for only blood relatives. My current algorithm is using recursion. but I am wondering how to do it in non recursive way to make it more efficient. it is programmed in c# using graphics to dr...

Algorithm that will grab all nodes attached to a particular node

I am creating an algorithm that is based on directed graphs. I would like a function that will grab all the nodes that are attached to a particular node. public List<Node> GetNodesInRange(Graph graph, int Range, Node selected) { var result = new List<Node>(); result.Add(selected); if (Range > 0) { foreach (Node ...

Revision, Quadratic Time

I am not sure if you can post revision programming questions in here but i am stuck with some algorithms revision If an algorithm is quadratic it takes time proportional to the number of n^2 ? So if the slides say its almost 1/2 the square of n records is this the same as saying (n^2 * 0.5) Thanks ...

How to write an algorithm to check if the sum of any two numbers in an array/list matches a given number?

How can I write an algorithm to check if the sum of any two numbers in an array/list matches a given number with a complexity of nlogn? ...

Is regex too slow? Real life examples where simple non-regex alternative is better

I've seen people here made comments like "regex is too slow!", or "why would you do something so simple using regex!" (and then present a 10+ lines alternative instead), etc. I haven't really used regex in industrial setting, so I'm curious if there are applications where regex is demonstratably just too slow, AND where a simple non-reg...

Mergesort : Revision

Does merge sort work by; taking a list of values splitting it in to two take the first element of each list, the lowest value one goes in to a new list(and i guess removed from the original). comare the next two numbers - do this until one list is empty, then place the rest of the other list at the end ofthe nw list? Also, what are ...

How to determine the best case and worst case of an program(algorithm)?

Suppose I have this program, I want to compare 2 input lists. Assume array A and array B. How do I determine the best case and worst case of the function? Here is my code in [php]: foreach($array_1 as $k){ if(!in_array($k, $array_2)){ array_push($array_2, $k); } } What is the best case and worst case of the for loo...

Find recipes that can be cooked from provided ingridients

Sorry for bad English :( Suppose i can preliminary organize recipes and ingredients data in any way. How can i effectively conduct search of recipes by user-provided ingredients, preferably sorted by max match - so, first going recipes that use maximum of provided ingridients and do not contain any other ingrs, after them recipes that u...

Latex: Listing all figures (tables, algorithm) once again at the end of the document

Hi all, I have been writhing a rather large document with latex. Now I would like to list all the figures / tables / algortihms once again at the end of the file so that I can check if they all look the same. For example, if every algorithm has the same notation. How can I do this? I know about \listofalgorithms and \listoffigures b...

How to find all characters in a string whose appearance is greater than 2

Hi folks: I have a question about algorithm: How to find all characters in a string whose appearance is greater than a specific number, say 2 for example efficiently? Regards. ...

How to find a binary logarithm very fast? (O(1) at best)

Is there any very fast method to find a binary logarithm of an integer number? For example, given a number x=52656145834278593348959013841835216159447547700274555627155488768 such algorithm must find y=log(x,2) which is 215. x is always a power of 2. The problem seems to be really simple. All what is required is to find the position of...

An approximate algorithm for finding Steiner Forest.

Hello. Consider a weighted graph G=(V,E,w). We are given a family of subsets of vertices V_i. A Steiner Forest is a forest that for each subset of vertices V_i connects all of the vertices in this subset with a tree. Example: only one subset V_1 = V. In this case a Steiner forest is a spanning tree of the whole graph. Example: a grap...

Strlen of MAX 16 chars string using bitwise operators

The challenge is to find the fastest way to determine in C/C++ the length of a c-string using bitwise operations in C. char thestring[16]; The c-string has a max size of 16 chars and is inside a buffer If the string is equal to 16 chars doesn't have the null byte at the end. I am sure can be done but didn't got it right yet. I am wo...

Finding the product of each of the (n-1) subsets of a given array

I'm sorry for deleting the original question, here it is: We have a bag or an array of n integers, we need to find the product of each of the (n-1) subsets. e.g: S = {1, 0, 3, 6} ps[1] = 0*3*6 = 0; ps[2] = 1*3*6 = 18; etc. After discussions, we need to take care of the three cases and they are illustrated in the following: 1. S is a se...

Detect winning game in nought and crosses

I need to know the best way to detect a winning move in a game of noughts and crosses. Source code doesn't matter, I just need a example or something I can start with. The only thing I can come up with is to use loops and test every direction for every move a player makes, to search for e.g five in a row. Is there a faster and more effi...