首页 > 代码库 > 图与搜索
图与搜索
主要知识点:
克隆图
拓扑排序
DFS BFS
BFS两个使用场景:图的遍历 简单图求最短路径
BFS in Graph 和BFS in Tree的主要区别就是有无环
Clone Graph --not bug free
方法一:递归
1 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 2 // write your code here 3 if (node == null) { 4 return node; 5 } 6 HashMap<UndirectedGraphNode, UndirectedGraphNode> hashMap = new HashMap<>(); 7 return help(node, hashMap); 8 } 9 10 public UndirectedGraphNode help(UndirectedGraphNode node, HashMap<UndirectedGraphNode, UndirectedGraphNode> hashMap) { 11 UndirectedGraphNode root = new UndirectedGraphNode(node.label); 12 hashMap.put(node, root); 13 for (UndirectedGraphNode neighbor : node.neighbors) { 14 if (hashMap.containsKey(neighbor)) { 15 root.neighbors.add(hashMap.get(neighbor)); 16 } else { 17 root.neighbors.add(help(neighbor, hashMap)); 18 } 19 } 20 return root; 21 }
方法二:非递归,分两步:找节点和mapping neighbors
1 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 2 // write your code here 3 if (node == null) { 4 return node; 5 } 6 //getNodeAndMapping 7 List<UndirectedGraphNode> nodes = new ArrayList<UndirectedGraphNode>(); 8 HashMap<UndirectedGraphNode, UndirectedGraphNode> mapping = new HashMap<>(); 9 Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); 10 queue.offer(node); 11 nodes.add(node); 12 mapping.put(node, new UndirectedGraphNode(node.label)); 13 while (!queue.isEmpty()) { 14 UndirectedGraphNode cur = queue.poll(); 15 for (UndirectedGraphNode neighbor : cur.neighbors) { 16 if (mapping.containsKey(neighbor)) { 17 continue; 18 } 19 nodes.add(neighbor); 20 mapping.put(neighbor, new UndirectedGraphNode(neighbor.label)); 21 queue.offer(neighbor); 22 } 23 } 24 25 //clone neighbor 26 for (UndirectedGraphNode old : nodes) { 27 UndirectedGraphNode newNode = mapping.get(old); 28 for (UndirectedGraphNode neighbor : old.neighbors) { 29 newNode.neighbors.add(mapping.get(neighbor)); 30 } 31 } 32 return mapping.get(node); 33 }
Topological Sorting
1 public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) { 2 // write your code here 3 Map<DirectedGraphNode, Integer> indegreeMap = new HashMap<DirectedGraphNode, Integer>(); 4 getInDegree(graph, indegreeMap); 5 Queue<DirectedGraphNode> queue = new LinkedList<DirectedGraphNode>(); 6 for (DirectedGraphNode node : graph) { 7 if (!indegreeMap.containsKey(node)) { 8 queue.offer(node); 9 } 10 } 11 ArrayList<DirectedGraphNode> result = new ArrayList<>(); 12 while (!queue.isEmpty()) { 13 DirectedGraphNode cur = queue.poll(); 14 result.add(cur); 15 for (DirectedGraphNode neighbor : cur.neighbors) { 16 int indegree = indegreeMap.get(neighbor); 17 indegreeMap.put(neighbor, indegree - 1); 18 if (indegree == 1) { 19 queue.offer(neighbor); 20 } 21 } 22 } 23 return result; 24 } 25 26 public void getInDegree(ArrayList<DirectedGraphNode> graph, 27 Map<DirectedGraphNode, Integer> indegreeMap) { 28 for (DirectedGraphNode node : graph) { 29 for (DirectedGraphNode neighbor : node.neighbors) { 30 if (!indegreeMap.containsKey(neighbor)) { 31 indegreeMap.put(neighbor, 1); 32 } else { 33 indegreeMap.put(neighbor, indegreeMap.get(neighbor) + 1); 34 } 35 } 36 } 37 }
Permutations
Given a list of numbers, return all possible permutations.You can assume that there is no duplicate numbers in the list.
1 public List<List<Integer>> permute(int[] nums) { 2 // write your code here 3 List<List<Integer>> results = new ArrayList<>(); 4 if (nums == null || nums.length == 0) { 5 results.add(new ArrayList<Integer>()); 6 return results; 7 } 8 boolean[] used = new boolean[nums.length]; 9 BFS(nums, results, new ArrayList<Integer>(), used); 10 return results; 11 } 12 13 public void BFS(int[] nums, List<List<Integer>> results, List<Integer> cur, boolean[] used) { 14 if (cur.size() == nums.length) { 15 results.add(new ArrayList<>(cur)); 16 return ; 17 } 18 for (int i = 0; i < nums.length; i++) { 19 if (used[i]) { 20 continue; 21 } 22 used[i] = true; 23 cur.add(nums[i]); 24 BFS(nums, results, cur, used); 25 cur.remove(cur.size() - 1); 26 used[i] = false; 27 } 28 }
Permutations II
注意不要忘了排序。
Given a list of numbers with duplicate number in it. Find all unique permutations.
1 public List<List<Integer>> permuteUnique(int[] nums) { 2 // write your code here 3 List<List<Integer>> results = new ArrayList<>(); 4 if (nums == null || nums.length == 0) { 5 results.add(new ArrayList<Integer>()); 6 return results; 7 } 8 Arrays.sort(nums);//Attention! 9 boolean[] used = new boolean[nums.length]; 10 BFS(nums, results, new ArrayList<Integer>(), used); 11 return results; 12 } 13 14 public void BFS(int[] nums, List<List<Integer>> results, List<Integer> cur, boolean[] used) { 15 if (cur.size() == nums.length) { 16 results.add(new ArrayList<>(cur)); 17 return ; 18 } 19 for (int i = 0; i < nums.length; i++) { 20 if (used[i]) { 21 continue; 22 } 23 if (i > 0 && !used[i - 1] && nums[i] == nums[i - 1]) { 24 continue; 25 } 26 used[i] = true; 27 cur.add(nums[i]); 28 BFS(nums, results, cur, used); 29 cur.remove(cur.size() - 1); 30 used[i] = false; 31 } 32 }
图与搜索
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。