查看: 738| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[二分/排序/搜索] 求一个在DFS里print path的代码

全局:
10小米
本帖最后由 滑铁卢鹅大爷 于 2022-3-10 11:57 编辑

题目是这样:provide了一个adjacency list的weighted graph,然后需要通过backtracking return maximumProduct和他的path。例子如下:g = {
    "A": {"B": 6, "D": 1},
    "B": {"A": 6, "C": 5, "D": 2, "E": 2},
    "D": {"A": 1, "B": 2, "E": 2},
    "E": {"B": 2, "C": 5, "D": 2},
    "C": {"B": 5, "E": 5}
}
Expected: method getMaximumPathProduct() 应该return 120,variable List<String> path = ["A", "B", "D", "E", "C"]
楼主已经通过backtracking得到了maximumProduct,但是卡在了如何记录path. 代码贴在下面了,请大神们对其做更改,需要能run的code~ 感谢!

PS: 想再问一下时间复杂度以及有没有pruning的可能
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;

  8. public class Test {
  9.     static final String START = "A";
  10.     static final String TARGET = "C";
  11.     List<String> path = new ArrayList<>();
  12.     public static void main(String[] args) {
  13.         Map<String, Map<String, Integer>> graph = getSimplerStaticData();
  14.         System.out.println(getMaximumPathProduct(graph, START, TARGET));
  15.     }

  16.     private static int getMaximumPathProduct(Map<String, Map<String, Integer>> graph, String start, String target) {
  17.         Set<String> seen = new HashSet<>();
  18.         seen.add(start);
  19.         return dfs(start, target, seen, graph, new LinkedList<>());
  20.     }

  21.     private static int dfs(String current, String target, Set<String> seen, Map<String, Map<String, Integer>> graph, List<String> subPath) {
  22.         if(target.equals(current)) {
  23.             return 1;
  24.         }

  25.         int res = 0;
  26.         Map<String, Integer> neighbors = graph.get(current);
  27.         for(String neighbor: neighbors.keySet()) {
  28.             if(!seen.contains(neighbor)) {
  29.                 seen.add(neighbor);
  30.                 int distance = neighbors.get(neighbor);
  31.                 res = Math.max(res, distance * dfs(neighbor, target, seen, graph, subPath));
  32.                 seen.remove(neighbor);
  33.             }
  34.         }

  35.         return res;
  36.     }

  37.     private static Map<String, Map<String, Integer>> getSimplerStaticData() {
  38.         Map<String, Map<String, Integer>> res = new HashMap<>();
  39.         Map<String, Integer> value1 = new HashMap<>();
  40.         value1.put("B", 6);
  41.         value1.put("D", 1);
  42.         res.put("A", value1);

  43.         Map<String, Integer> value2 = new HashMap<>();
  44.         value2.put("A", 6);
  45.         value2.put("D", 2);
  46.         value2.put("E", 2);
  47.         value2.put("C", 5);
  48.         res.put("B", value2);

  49.         Map<String, Integer> value3 = new HashMap<>();
  50.         value3.put("B", 5);
  51.         value3.put("E", 5);
  52.         res.put("C", value3);

  53.         Map<String, Integer> value4 = new HashMap<>();
  54.         value4.put("A", 1);
  55.         value4.put("B", 2);
  56.         value4.put("E", 2);
  57.         res.put("D", value4);

  58.         Map<String, Integer> value5 = new HashMap<>();
  59.         value5.put("B", 2);
  60.         value5.put("C", 5);
  61.         value5.put("D", 2);
  62.         res.put("E", value5);

  63.         return res;
  64.     }
  65. }
复制代码

评分

参与人数 1大米 +3 收起 理由
14417335 + 3 给你点个赞!

查看全部评分


上一篇:请问常看到的karat题库是什么 回复加米
下一篇:An interview question
🔗
swordfeng 2022-3-11 17:22:02 | 只看该作者
全局:
  1.     static int maxRes = 0;
  2.     private static int getMaximumPathProduct(Map<String, Map<String, Integer>> graph, String start, String target) {
  3.         Set<String> seen = new HashSet<>();
  4.         seen.add(start);
  5.         return dfs(start, target, seen, graph, new LinkedList<>(), 1);
  6.     }
  7.     private static int dfs(String current, String target, Set<String> seen, Map<String, Map<String, Integer>> graph, LinkedList<String> subPath, int curProd) {
  8.         subPath.add(current);
  9.         if(target.equals(current)) {
  10.             if (maxRes < curProd) {
  11.                 path = (List<String>) subPath.clone();
  12.                 maxRes = curProd;
  13.             }
  14.             subPath.removeLast();
  15.             return 1;
  16.         }
  17.         ......................
  18.                 res = Math.max(res, distance * dfs(neighbor, target, seen, graph, subPath, curProd * distance));
  19.         ...................
  20.         subPath.removeLast();
  21.         return res;
  22.     }
复制代码
回复

使用道具 举报

🔗
ljnyc 2022-3-12 00:22:38 | 只看该作者
全局:
這不是Ford–Fulkerson max flow問題?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表