c++ - Winner of a tournament in O(N) and rank of the players in O(NLogN) -
in tennis tournament of n players every player plays every other player. following condition hold- if player p1 has won match p2 , player p2 has won p3, player p1 has defeated p3. find winner of tournament in o(n) time , o(1) space. find rank of players in o(nlogn) time. my solution : input boolean matrix element matrix[i][j] indicates whether player wins player j.
bool win[][]= { {0, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 0, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 1}, {0, 0, 1, 1, 1, 0, 0} };
so winner found like,
int winner = 0; (int = 1; < player_count; ++i) { if (win[i][winner]) winner = i; } return winner;
for getting rank of players, guess topological sorting one. if player 1 wins player 2, edge added lke p1-> p2. if player 1 winner here, have edges other players. topological sorting winner source vertex, give rank of player. solution correct ? there other efficient solution ? great, in advance.
the condition
if player p1 has won match p2 , player p2 has won p3
is defining total ordering, i.e. if define p1 < p2
"p2
defeated p1
", have transitive ordering relation <
, can used regular less-than
relation in either sorting or finding maximum. implementation can define predicate bool lessthan(int p1, int p2)
p1
, p2
relation in matrix in o(1)
. , use predicate "maximum" search, linear (o(n)
), or sorting (ranking), o(n log n)
.
Comments
Post a Comment