问题描述
大家好,我目前正在处理一个涉及图形和邻接列表的问题,但它不起作用。这仅用于自学,不适用于学校或家庭作业。 我得到了这张值表:
| Start name | End name | Example path found by a search |
|------------|----------|-----------------------------------|
| carrie | carrie | carrie (it's the same node) |
| carrie | humza | carrie -> humza |
| carrie | yervand | carrie -> jun -> silla -> yervand |
| ophelia | ursula | ophelia -> travis -> **FAIL** |
| travis | carrie | travis -> ophelia -> **FAIL** |
| ursula | ophelia | ursula -> travis -> ophelia |
| victor | humza | victor -> **FAIL** |
但我的代码无法正常工作是说我没有得到这个:
-
从嘉莉到嘉莉
-
从嘉莉到humza 我按照他们的建议先呼吸,我知道问题是什么
if (!adjacencyList[startName]) return null; const queue = [startName]; let visited = new Set(); let currentVertex; if (adjacencyList[startName] === adjacencyList[endName]) return adjacencyList[startName] while(queue.length) { currentVertex = queue.shift(); if (!visited.has(currentVertex)) { visited.add(currentVertex); adjacencyList[currentVertex].forEach(neighbor => queue.push(neighbor)) } } if (visited.has(endName)) { return visited; } else { return null } }
我知道问题是什么以及如何解决它。我以为通过返回集合我会返回路径但显然不是。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)