These are the 3 LeetCode problems pertinent to DAG, graph via bfs. The problem “Course Schedule II” pertains to “Topological sorting”, which is more like a way of traversing nodes from my perspective. Problem “Minimum Height Tree” relates to the concept of graph as a superset of trees. Also, how in-degrees of graph nodes are expressed as heights of tree nodes is ausgezeichnet as well.
207. Course Schedule
There are a total of n courses you have to take, labeled from 0
to n-1
.
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]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Example 1:1
2
3
4Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:1
2
3
4
5Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
Many hints:
- 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.
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
Solution
Actually, I did it via bfs which looks much easier to me.
1 | class Solution { |
210. Course Schedule II
Similar. Problem link.
Solution
Topological sorting: to me, it’s more like a traversal than a sorting.
1 | class Solution { |
310. Minimum Height Trees
For an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.
Format
The graph contains n
nodes which are labeled from 0
to n - 1
. You will be given the number n and a list of undirected edges
(each edge is a pair of labels).
You can assume that no duplicate edges will appear in edges
. Since all edges are undirected, [0, 1]
is the same as [1, 0]
and thus will not appear together in edges
.
Example 1:1
2
3
4
5
6
7
8
9Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]
0
|
1
/ \
2 3
Output: [1]
Example 2:1
2
3
4
5
6
7
8
9
10
11Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
0 1 2
\ | /
3
|
4
|
5
Output: [3, 4]
Note:
According to the definition of tree on Wikipedia): “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
Solution:
My thoughts:
- When building an undirected graph, it’s actually treated as a bi-directional graph. 0->2. When it’s a DG, it’s 1. Interesting.
- use
unordered_set
, notvector
, becauseerase
invector
can only erase by parameterindex
, not element.
1 | class Solution { |