Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for an algorithm in vb.net or c# but I don't know it's name!

I'll do my best to explain what the algorithm is supposed to do:

There's a class 'Recipe'. Each Recipe can include other Recipes but cannot include itself or any other Recipe that includes it.

So, a simple example is we have just two Recipes A & B.

If A adds B first, then later on B cannot add A because it will cause a loop.

A more complicated example is:

A,B,C

(1) Recipe C Adds B
(2) Recipe B Adds A
(3) Recipe A attempts to add C, but can't because of the relationship. C - B - A.

I can do this myself, I just wondered if this was a standard named algorithm and I could grab the optimal solution.

Thanks

like image 396
Jules Avatar asked Apr 14 '10 08:04

Jules


People also ask

What is an algorithm in VB?

Visual Basic Algorithms is both a solid working introduction to the subject and a sourcebook packed with valuable, ready-to-run code. You'll learn the basics of how algorithms work, how to analyze the usefulness of any algorithm, and how to incorporate algorithms into Visual Basic programs.

What is linear search in Visual Basic?

The user can enter a name and corresponding number into two parralel separate arrays. The user then enters a number and the number array is searched to see if that number lies there. I have created a basic algorithm which goes as follows: VB.

When would you use linear search?

Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an un-ordered list. When many values have to be searched in the same list, it often pays to pre-process the list in order to use a faster method.


1 Answers

In the Maths/Computer science jargon your structure is called a directed graph. You want a "Directed Acyclic Graph" - that is one with no cycles in it.

To find out if there are cycles in a graph you can use an algorithm called Topological sorting. It tries to rearrange a graph so that if A refers to B then A always occurs before B in order. It stops if the graph has cycles.

If you want to find all cycles in the graph (which is helpful for error messages) then this stackoverflow question and answer gives lots of help.

As background:
Graph = anything with nodes linked by edges (in your case nodes are recipes and references are edges).
Directed = the edges have directions. In your case this is true because 'A' refers to 'B', not 'A' and 'B' to each other.

like image 87
Nick Fortescue Avatar answered Oct 13 '22 13:10

Nick Fortescue