0

I have been working on an interesting problem from my computer science class (not homework). So the question is this: there is a tournament going on. If each player has 2 "stats", strength and stamina, then one player would beat the other if and only if his strength stat and stamina is greater than the other. How would I construct a graph to show this? So each player who dominates another draws an edge in between them.

Clearly there is a O(n^2) algorithm: just test pairwise each player and draw an edge if there is domination. But is there more clever way? For example if player A dominates B and B dominates C, then A dominates C and no testing needed. Thanks.

TheLeogend
  • 99
  • 4
  • If you're explicitly constructing the graph (e.g. making an adjacency list) an O(n^2) algorithm might be unavoidable because there can be up to O(n^2) edges, for example when you have players with stats (1,1), (2,2), ..., (n,n). – aeternalis1 Mar 28 '20 at 13:27

1 Answers1

0

The "dominates" relation defines a partial order, so this is closely related to the problem of sorting a partially-ordered set (poset). It's not the same problem as yours because you don't want to put the nodes in some sorted order - you want to find all edges. But they are related problems because both want to take advantage of the transitive property to test fewer pairs of nodes.

Poset sorting is faster the more edges there are, because more edges means more benefit from the transitive property, and fewer tests are needed. In the worst case, poset sorting takes Θ(n2) when there are Θ(n) nodes with no edges between them.

However, since your output needs to be a graph with all of the edges, it will also take Θ(n2) time to actually produce the output in the opposite case, when there are many edges. A graph with Θ(n2) edges cannot be output in less time than that.

So even with a clever algorithm, it's going to take quadratic time when the graph has few edges, and it's also going to take quadratic time when the graph has many edges. Given that, the brute-force algorithm is probably fine.

kaya3
  • 47,440
  • 4
  • 68
  • 97