SUJITH
SUJITH

Reputation: 141

Sorting n objects when binary relationships between any two numbers are given

Problem Statement: Sort n objects a1,a2...an when binary relationships between any two number are given.

Say for eg 5 objects a1,a2,a3,a4 & a5

a1 < a5
a4 < a2
a3 < a5
a2 < a1
a1 < a3

So the order would be a4 a2 a1 a3 a5 Any algorith to do this

Upvotes: 0

Views: 99

Answers (1)

IVlad
IVlad

Reputation: 43477

An easy way is to keep a matrix relationships[x, y] = true if x < y and use that matrix as the comparison function in your favorite sorting algorithm.

Topological sorting will probably be more efficient however.

Upvotes: 1

Related Questions