Simonas D.
Simonas D.

Reputation: 2582

C# Graph Traversal recursion

enter image description here

What algorithm with recursion I need to use to traverse this graph and get this sequence (Output):

IF01
IF04
IF02
IF03
IF08 
IF05 
IF06 
IF07 
IF09 

Initial data (Input) (first is code, second is how much codes you have, third one is what code you have to take in order to use it, for example to print IF02, you have to have already printed IF01, to print IF05, you have to print IF04 before. and so on)

IF01 0
IF02 1 IF01
IF03 2 IF01 IF02
IF04 0
IF05 1 IF04
IF06 1 IF05
IF07 2 IF03 IF06
IF08 1 IF03
IF09 2 IF07 IF08

Upvotes: 1

Views: 1173

Answers (1)

Felix Av
Felix Av

Reputation: 1254

The most trivial solution to this problem I can think of is to run Topological Sorting, you can see the algorithm here: https://en.wikipedia.org/wiki/Topological_sorting

It should be no problem to implement it in c#

Here's a great article that contains implementation:

http://www.codeproject.com/Articles/869059/Topological-sorting-in-Csharp

Upvotes: 1

Related Questions