Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need algorithm for Sequence calculation

I am trying to find the solution for a problem where i have something like

  1. A > B
  2. B > C
  3. B > D
  4. C > D

And I should get the answer as A > B > C > D.

Conditions for this problem

  1. The output will involve all the elements.
  2. The problem will not have any bogus inputs. for example, (A>B) (C>D) is a bogus input, since we cannot determine the output.
  3. The inputs can be of any size but never bogus and there will always be a solution to the problem.

I need to find a solution for this optimally using Java Collections. Any tips/hints are welcome.

Thanks in advance!

like image 242
bragboy Avatar asked Jul 03 '26 11:07

bragboy


1 Answers

It's called a Topological Sort. http://en.wikipedia.org/wiki/Topological_sorting

Given that, you should be able to complete your homework on your own.

like image 119
S.Lott Avatar answered Jul 04 '26 23:07

S.Lott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!