我正在尝试将此Python深度优先搜索转换为Java。这是我的Python代码:
def dfs(graph, current_vertex, target_value, visited=None):
if visited is None: #for when not a recursive call
visited = [] #empty list
visited.append(current_vertex) #adds current vertex to visited
if current_vertex == target_value: #for when current_vertex is target value (ie target reached)
return visited
# Add your recursive case here:
for neighbor in graph[current_vertex]: #checks each neighbor of curr, Remember that the graphs hold key-value pairs for each vertex and its set of connected vertices.
if neighbor not in visited: #if neighbor has not been added to visited
path = dfs(graph, neighbor, target_value, visited) #recursive call with new vertex, a visited list(now a list of at least one vertex value), graph and TV remain same
if path: #if the path exists
return path #return the path
#set with keys and values
the_most_dangerous_graph = {
'lava': set(['sharks', 'piranhas']),
'sharks': set(['lava', 'bees', 'lasers']),
'piranhas': set(['lava', 'crocodiles']),
'bees': set(['sharks']),
'lasers': set(['sharks', 'crocodiles']),
'crocodiles': set(['piranhas', 'lasers'])
}
# Call dfs() below and print the result:
print(dfs(the_most_dangerous_graph, "crocodiles", "bees"))
我对算法有一个大致的了解:转到一个元素的子级,直到所有子级都遍历为止,开始弹出,直到到达一个没有访问过子级的顶点,访问该顶点,并按访问顺序保存所有访问的顶点。但是,我有一个想法,如何使用递归从Java开始。这是我得到的:
import java.util.*;
public class DepthFirstSearch {
private static Set<String> DFSHelper(HashMap<String,String[]> graph, String currentValue,
String targetValue, HashMap<String, String> visited, Stack<String> s) {
Iterator it = graph.values().iterator();
visited.put(currentValue, null);
s.push(currentValue);
System.out.println(s.peek());
System.out.println(currentValue);
//System.out.println(graph.get(currentValue));
if(!s.isEmpty()) {
String neighbor = it.next().toString();
if(!visited.containsKey(neighbor)) {
visited.put(neighbor,currentValue);
currentValue = neighbor;
return DFSHelper(graph,currentValue,targetValue,visited,s);
}
}
return visited.keySet();
}
public static Set<String> DFS(HashMap<String,String[]> graph, String currentValue,
String targetValue) {
HashMap<String,String> visited = new HashMap<>();
Stack<String> s = new Stack<String>();
return DFSHelper(graph,currentValue,targetValue,visited,s);
}
public static void main(String[] args) {
HashMap<String, String[]> myGraph = new HashMap<>();
myGraph.put(
"lava", new String[] {"sharks", "piranhas"}
);
myGraph.put(
"sharks", new String[] {"lava", "bees", "lasers"}
);
myGraph.put(
"piranhas", new String[] {"lava", "crocodiles"}
);
myGraph.put(
"bees", new String[] {"sharks"}
);
myGraph.put(
"lasers", new String[] {"sharks", "crocodiles"}
);
myGraph.put(
"crocodiles", new String[] {"piranhas", "lasers"}
);
System.out.println(DFS(myGraph, "crocodiles", "bees"));
System.out.println(DFS(myGraph, "crocodiles", "crocodiles"));
System.out.println(DFS(myGraph, "crocodiles", "zebras"));
}
}
截至目前,我只获得除打印语句以外的哈希码,而且不确定是否在正确的轨道上。
答案 0 :(得分:0)
由于以下原因,您的输出看起来很糟糕:not a directory
迭代器定义为:
it.next().toString();
,其中图形为:Iterator it = graph.values().iterator();
,因此HashMap<String,String[]> graph
返回it.next()
使用String[]
打印数组将产生与
toString
输出:
[Ljava.lang.String; @ 6a6824be
一种打印数组的更好方法是:String[] array = {"crocodiles", "zebras"};
System.out.println(array.toString());
输出:
[鳄鱼,斑马]
dfs的逻辑也有缺陷。要获得帮助,请发布一个新问题,并包括图表的可视化和预期的输出。