Graphs Leetcode Problems
Graphs Leetcode Problems
Example 2:
Input: n = 6, edges =
[[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0,
destination = 5
Output: false
Explanation: There is no path from vertex 0
to vertex 5.
Constraints:
1 <= n <= 2 * 105
0 <= edges.length <= 2 * 105
edges[i].length == 2
0 <= ui, vi <= n - 1
ui != vi
0 <= source, destination <= n - 1
There are no duplicate edges.
There are no self edges.
#BFS(Breadth First Search) - Queue
from collections import deque
class Solution:
def validPath(self, n, edges, source,
destination):
graph = {node: [] for node in range(n)}
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
visited = [False] * n
queue = deque([source])
visited[source] = True
while queue:
node = queue.popleft()
if node == destination:
return True
return False
#DFS(Depth First Search) - Stack
class Solution:
def validPath(self, n, edges, source, destination):
graph = {node: [] for node in range(0, n, +1)}
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
visited = [False] * n
stack = [source]
visited[source] = True
while stack:
node = stack.pop()
if node == destination:
return True
return False
Q2. Find Center of Star Graph
There is an undirected star graph consisting of n
nodes labeled from 1 to n. A star graph is a graph
where there is one center node and exactly n - 1
edges that connect the center node with every
other node.
You are given a 2D integer array edges where each
edges[i] = [ui, vi] indicates that there is an edge
between the nodes ui and vi. Return the center of
the given star graph.
Example 1:
Input: edges = [[1,2],[2,3],[4,2]]
Output: 2
Explanation: As shown in the figure above, node 2
is connected to every other node, so 2 is the
center.
Example 2:
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
Output: 1
Constraints:
3 <= n <= 105
edges.length == n - 1
edges[i].length == 2
1 <= ui, vi <= n
ui != vi
The given edges represent a valid star graph.
class Solution:
def findCenter(self, edges):
if edges[0][0] in edges[1]:
return edges[0][0]
else:
return edges[0][1]
Q3. Number of Provinces
There are n cities. Some of them are connected,
while some are not. If city a is connected directly
with city b, and city b is connected directly
with city c, then city a is connected indirectly
with city c.
Example 2:
Constraints:
1 <= n <= 200
n == isConnected.length
n == isConnected[i].length
isConnected[i][j] is 1 or 0.
isConnected[i][i] == 1
isConnected[i][j] == isConnected[j][i]
#BFS
from collections import deque
class Solution:
def bfs(self, city, isConnected, visited):
queue = deque([city])
while queue:
current = queue.popleft()
# Visit all connected cities
for neighbor in
range(len(isConnected)):
if isConnected[current][neighbor]
== 1 and not visited[neighbor]:
visited[neighbor] = True
queue.append(neighbor)
return provinces
Q4. Find the Town Judge
In a town, there are n people labeled from 1
to n. There is a rumor that one of these
people is secretly the town judge.
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Constraints:
1 <= n <= 1000
0 <= trust.length <= 104
trust[i].length == 2
All the pairs of trust are unique.
ai != bi
1 <= ai, bi <= n
class Solution:
def findJudge(self, n, trust):
if len(trust) == 0 and n == 1:
return 1
count = [0] * (n + 1)
for person in trust:
count[person[0]] -= 1
count[person[1]] += 1