Catalogue
Detect the Cycle
Find if a cycle exits in a graph
261. Graph Valid Tree
- Undirected Graph: Union Find, DFS, BFS
Solution1. Union Find
- graph is represented by edges!!! signal to use union find!!!
- tree is connected graph without cycle, but graph can be seperate clusters!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class Solution(object):
def validTree(self, n, edges):
root = range(n)
self.cnt = n
def find(x):
if root[x] == x:
return x
root[x] = find(root[x])
return root[x]
def union(x, y):
rootx = find(x)
rooty = find(y)
if rootx != rooty:
root[rootx] = rooty
self.cnt -= 1
return True
return False
for i, j in edges:
if not union(i, j):
return False
return self.cnt == 1
solution2. DFS
- for undirected graph, it’s better to use Union Find and BFS
- prev node !!!!
- 3 states for visited: 0-no visited,1-visiting,2-visited
1 | class Solution(object): |
Solution3. BFS
- prev node to avoid edge(a, b) and (b, a) cycle !!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class Solution(object):
def validTree(self, n, edges):
graph = collections.defaultdict(set)
for i, j in edges:
graph[i].add(j)
graph[j].add(i)
visited = [0] * n
queue = collections.deque([(0, -1)])
while queue:
node, prev = queue.popleft()
visited[node] = 1
for nei in graph[node]:
if nei == prev:
continue
if visited[nei]:
return False
queue.append((nei, node))
return sum(visited) == n
Directed graph
207. Course Schedule
- Directed graph: DFS, BFS
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
Solution1. DFS
visited三种状态,-1正在visit, 0未visit, 1已经visit
- Why 三种状态?or 两种状态?
- 两种状态,做DFS时间复杂度是O(n^2)
- 引入第三种状态,可以避免访问已经访问过不构成环的点,O(n)
1 | # Time : O(n) |
Solution2. BFS
- 有向图的BFS算法还是比较有意思的,基于indegree的!
- Indegree, 每次访问入度为0的节点,并删掉所在边,最后sum(indegree) == 0!
1 | class Solution(object): |
Graph => Tree
684. Redundant Connection
- union find
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class Solution(object):
def findRedundantConnection(self, edges):
n = len(edges)
root = range(n+1)
def find(x):
if root[x] == x:
return x
root[x] = find(root[x])
return root[x]
for src, dst in edges:
root1 = find(src)
root2 = find(dst)
if root1 != root2:
root[root1] = root2
else:
return [src, dst]
685. Redundant Connection II
1 | class Solution(object): |