Teaching Kids Programming – Algorithms to Find the Vertex of Zero Indegree in a Directed Acyclic Graph


Teaching Kids Programming: Videos on Data Structures and Algorithms

2923. Find Champion I

There are n teams numbered from 0 to n – 1 in a tournament. Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n – 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i. Team a will be the champion of the tournament if there is no team b that is stronger than team a. Return the team that will be the champion of the tournament.

Example 1:
Input: grid = [[0,1],[0,0]]
Output: 0
Explanation: There are two teams in this tournament.
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.

Example 2:
Input: grid = [[0,0,1],[1,0,1],[0,0,0]]
Output: 1
Explanation: There are three teams in this tournament.
grid[1][0] == 1 means that team 1 is stronger than team 0.
grid[1][2] == 1 means that team 1 is stronger than team 2.
So team 1 will be the champion.

Constraints:
n == grid.length
n == grid[i].length
2 <= n <= 100
grid[i][j] is either 0 or 1.
For all i grid[i][i] is 0.
For all i, j that i != j, grid[i][j] != grid[j][i].
The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.

Hint: The champion should be stronger than all the other teams.

Algorithms to Find the Vertex of Zero Indegree in a Directed Acyclic Graph

The strongest team (aka the Champion) isn’t weak to any other team. In terms of the graph (DAG = Directed Acyclic Graph), the vertex should not have any incoming edges. Then, we can iterate the Graph vertices/edges and then count the number of incoming degrees (indegrees) for each vertices. And we just need to find out the vertex which doesn’t have any incoming edges (zero indegrees). The degree of a vertex is the number of edges connecting to it. It can be further categorized into outgoing and incoming degrees which correspond to the outgoing and incoming edges respectively.

Please note that the given graph only has a vertex which has zero indegrees, and also there will be no cycles – A is stronger than B, B is stronger than C, so C is NOT stronger than A.

Iterating over the Graph Adjacency Matrix (Grid) is O(N^2) where N is the number of the vertices.

1
2
3
4
5
6
7
8
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int: 
        ind = defaultdict(int)
        n = len(grid)
        for i in range(n):
            for j in range(i):
                ind[j] += grid[i][j]
        return next((i for i in range(n) if ind[i] == 0), -1)
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int: 
        ind = defaultdict(int)
        n = len(grid)
        for i in range(n):
            for j in range(i):
                ind[j] += grid[i][j]
        return next((i for i in range(n) if ind[i] == 0), -1)

Another algorithm is to find out the row which has n-1 ones, as it indicates that that team is stronger than n-1 teams, which makes it the strongest team.

We can use the filter to get the row with n-1 ones via the sum function.

1
2
3
4
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int: 
        n = len(grid)
        return next(filter(lambda i: sum(grid[i]) == n - 1, range(len(grid))), -1)
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int: 
        n = len(grid)
        return next(filter(lambda i: sum(grid[i]) == n - 1, range(len(grid))), -1)

Another implementation is to use the max function, that returns the row which has the most ones.

1
2
3
4
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int: 
        n = len(grid)
        return max(range(n), key=lambda i: sum(grid[i]))
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int: 
        n = len(grid)
        return max(range(n), key=lambda i: sum(grid[i]))

See this related puzzle: Teaching Kids Programming – Algorithms to Find the Unique Vertex of Zero Indegree in a Directed Acyclic Graph

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
821 words
Last Post: Boost System Performance By: Switching to Best Performance Power Mode at Power & Battery on Windows OS
Next Post: Differences Between Web2 and Web3 Domains

The Permanent URL is: Teaching Kids Programming – Algorithms to Find the Vertex of Zero Indegree in a Directed Acyclic Graph

Leave a Reply