Skip to main content

Uncategorized

A holding area for graph problems that don't yet belong to a named pattern section. As patterns emerge, these get promoted into their own sections.

1791. Find Center of Star Graph

Easy·

Solutions:
class Solution:
def findCenter(self, edges: List[List[int]]) -> int:
counter = collections.defaultdict(int)
for a, b in edges:
counter[a] += 1
counter[b] += 1
for node, degree in counter.items():
if degree == len(counter) - 1:
return node

997. Find the Town Judge

Easy·

Solutions:
class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
in_degree = [0 for i in range(n + 1)]
out_degree = [0 for i in range(n + 1)]
for a, b in trust:
out_degree[a] += 1
in_degree[b] += 1
for node in range(1, n + 1):
if in_degree[node] == n - 1 and out_degree[node] == 0:
return node
return -1