I’m terrified of such kind of problems. so I write this because I really want to know how to solve such problems…
many tree or graph realted problems are not that hard, they are easily to understand, but due to many recusrions are involved in the solution. I might can’t even write a single line for such problems.
Graph:
technicially, all dfs-bfs can be categorized as graph related question.
but first, let’s focused on graph problem.
first thing first, let’s talk about my understanding of graph.
graph means the connected world, everything is graph acutally. and most of the time, we want to find sth we need in current graph. so we have dfs and bfs to origanizely tranverse whole graph to get the things we need, so the two powerful weapon of graph-dfs/bfs are essentially brute force.
apart from those explicitly graph related problem, we have many problems that’s seems have nothing related to gragh, but it is graph.
133 Graph clone:deep clone a graph, use bfs.
//we can use a node to express element of a graph, use HashMap<Node, Node> to display a full view of a graph
class Node {
public int val;
public List<Node> neighbors;
public Node() {}
public Node(int _val,List<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
//when we want to use a graph as a parameter for a function, we can simply use a node on this graph, like when we want to use a linkedlist as the parameter, we just use head node.
//let's look back to this clone problem, how to clone? means that we need to use the new Node(int val, List<Node> neighbors) to deep clone current node. so the general idea will be: traverse every node in graph, and build this whole new graph.
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
HashMap<Node, Node> visited = new HashMap(); //visited hashmap stores node and its deep clone node. at last, when everything is tranversed, we just need to visited.get(node) to get the head of the new deep cloned node
LinkedList<Node> queue = new LinkedList<Node>();
queue.add(node);
visited.put(node, new Node(node.val, new ArrayList<>()));
while (!queue.isEmpty()) {
Node n = queue.remove();
for (Node neighbor: n.neighbors) { //for every neighbor of n
if (!visited.containsKey(neighbor)) {
visited.put(neighbor, new Node(neighbor.val, new ArrayList<>()));
queue.add(neighbor);
}
visited.get(n).neighbors.add(visited.get(neighbor));
}
}
return visited.get(node);//get head node
}
this is a simple problem, but why do I so terrified of them so much? because of the not-familiar, because I never seen sth like HashMap<Node, Node> , or visited.put(neighbor, new Node(node.val, new ArrayList<>())). deep down there, you just not confident and tooooo lazy to think about things.
207 course schedule
210 course schedule2
previous two problems are prerequest courses, which is like topology sort. 207 ask if there exist a spanning tree. and 201 asks you to return a path if there exist one. detailed explaination is on another article.
261 Graph Valid Tree: check the given graph is a valid tree or not.
269 Alien Dictionary: this is some what an odd problem at first glance, but it is easy to understanding though
319 Minimum height trees: we have an undirected graph, we can choose any node as root. now, we wantt to choose the node as root which makes the tree with minimun height. and the tree desn;t have to be binary as long as it is a tree.
323 Number of Conencted Components in an undirected graph: the input of this problem is like LC319. and the problem is very easy, just to find the tatol number of components in give graph. this is a classic-classic union find problem.
332 Reconstruct Itinerary: reconstrcut a list of unordered plane tickets, starting from JFK. the palne taickets may contains invalid route, but it is guaranteed to have at least one valid. and we need to return the route with smallest lexicographic. this is a very simple dfs problem as well, just constrcut the graph using hasmap, and did postorder dfs.
399 Evaluate Division: this question seems pretty odd for being catagorized in graph. so this problem is basically given a set of equations, and given a set of queries, return the query results in array. this problem can be seen as the dfs problem, we need to find the distance between two given route. and the other way to solve this, is Union Find,
444 Sequence Reconstruction: Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. so the problem seems hard to understand at first, but it’s not that diffcult. so basically we are given an arrayList, and a list of original list, we need to see if the given list can formed as this given sequence uniquely. I have an article about it.
1059: All Paths from Source Lead to Destination: this is a pretty straoght forward graph problem. given int n, int[][] edges, int source, int destination, we need to determine whether or not all paths starting from same given source eventually end at same given destination. this is pretty stragith forward. first we constrcut the graph, and then do a dfs backtracking, we have to make sure every neghbor of current node lead to the destination, if any of them can’t lead to, then return false.
684 Redundant Connection: this problem is not a regualr one, but we can see it in graph right away. we are given an array of edges, and that represent a graph, now we are gonna to remove some edges to make the graph into a valid tree. and we have to remove all the removed edges. the given graph is an undirected. this problem can be solve in simple dfs or Union find.
685 Redundant Connection2: this time, we will be given a directed graph, and we need to find that deplicate connected vertices
743 Network delay time: we are given a directed weighted graph, and we know that the node from 1 to N in this graph. now we are given the starting node k, now we need to know the farthest node from starting node k. return the umber of distance. this can be done using dfs or bfs, and this problem is exactly dijkstra: shortest path problem.
765 Couple Holding hands: N pairs of couples siin 2N seat, and we need to know the minimum swap that makes every pair sit together. and we need to return the number of swaps.