使用BFS的最短路径
public static LinkedList<String> findShortestPath(String start, String end) {
LinkedList<String> bfsList = new LinkedList<String>();
Queue<Actor> queue = new LinkedList<Actor>();
Map<String, Actor> prev = new HashMap<String, Actor>();
Actor current = graph.getActorsByName().get(start);
queue.add(current);
current.setVisited(true);
while(!queue.isEmpty()) {
current = queue.remove();;
if(current.getName().equals(end)) {
break;
} else {
for(int i = 0; i < current.getFriends().size(); i++) {
if(current.getFriends().get(i).getVisited() == false) {
queue.add(graph.getActorsByName().get(current.getFriends().get(i).getName()));
graph.getActorsByName().get(current.getFriends().get(i).getName()).setVisited(true);
prev.put(current.getFriends().get(i).getName(), current);
}
}
}
}
if(!current.getName().equals(end)) {
System.out.println("\nThere is no path between " + start + " and " + end);
}
for(Map.Entry<String, Actor> entry : prev.entrySet()) {
String key = entry.getKey();
bfsList.add(key);
}
return bfsList;
}
以上是我用来尝试找到图中两点之间的最短路径的代码。它并没有给我两点之间的正确路径,我无法弄清楚原因。
答案 0 :(得分:0)
我已经设置了一个试验台,我希望演员有一个名字和一些朋友。那些直接的朋友也有互惠的关系。
在这个测试中,我正在寻找詹姆斯和#34;之间的最短路径。和&#34;玛丽&#34;。
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.junit.Test;
public class ShortestPathTest {
Map<String, ShortestPath.Actor> graph = new HashMap<>();
private ShortestPath.Actor newActor(String name, String... friends) {
ShortestPath.Actor actor = graph.computeIfAbsent(name, k -> new ShortestPath.Actor(name));
for(String friendsName : friends) {
ShortestPath.Actor friend = newActor(friendsName);
actor.addFriend(friend);
friend.addFriend(actor);
}
return actor;
}
@Test
public void findShortestPath() {
newActor("james", "harry", "luke", "john");
newActor("harry", "luke", "mary");
LinkedList<String> shortestPath = ShortestPath.findShortestPath(graph, "james", "mary");
assertThat(shortestPath, equalTo(Arrays.asList("james", "harry", "mary")));
}
}
我收到的结果与我的期望不符,并且出于某种原因&#34; mary&#34;在输出的中间:
java.lang.AssertionError:
Expected: <[james, harry, mary]>
but: was <[luke, harry, mary, john]>
比较algorithm you have referenced,我怀疑问题出现在(引用算法的)部分中:
for(Node node = finish; node != null; node = prev.get(node)) {
directions.add(node);
}
directions.reverse();
在您的实现中,您已初始化prev = new HashMap<String, Actor>()
,然后您正在添加每个被访问节点(而不仅仅是节点以指示最短路径)。您需要使用prev
作为链接列表...例如:
for (Actor node = graph.get(end); node != null; node = prev.get(node.getName())) {
bfsList.add(node.getName());
}
Collections.reverse(bfsList);
仅对原始发布应用了少量修改:
graph
作为参数代码列表
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class ShortestPath {
public static List<String> findShortestPath(Map<String, Actor> graph, String start,
String end) {
LinkedList<String> bfsList = new LinkedList<>();
Queue<Actor> queue = new LinkedList<>();
Map<String, Actor> prev = new HashMap<>();
Actor current = graph.get(start);
queue.add(current);
current.setVisited(true);
while (!queue.isEmpty()) {
current = queue.remove();
if (current.getName().equals(end)) {
break;
} else {
LinkedList<Actor> currentFriends = current.getFriends();
for (Actor currentFriend : currentFriends) {
if (!currentFriend.getVisited()) {
queue.add(currentFriend);
currentFriend.setVisited(true);
prev.put(currentFriend.getName(), current);
}
}
}
}
if (!current.getName().equals(end)) {
System.out.println("\nThere is no path between " + start + " and " + end);
return Collections.emptyList();
}
for (Actor node = graph.get(end); node != null; node = prev.get(node.getName())) {
bfsList.add(node.getName());
}
Collections.reverse(bfsList);
return bfsList;
}
static class Actor {
private final String name;
private final LinkedList<Actor> friends = new LinkedList<>();
private boolean visited;
Actor(String name) {
this.name = name;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
// Would normally be `isVisited`
public boolean getVisited() {
return visited;
}
public String getName() {
return name;
}
public LinkedList<Actor> getFriends() {
return friends;
}
public void addFriend(Actor actor) {
this.friends.add(actor);
}
}
}
构建测试图并断言在各个节点之间找到正确的路径
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
public class ShortestPathTest {
Map<String, ShortestPath.Actor> graph = new HashMap<>();
@Before
public void setup() {
newActor("james", "harry", "luke", "john");
newActor("harry", "luke", "mary");
newActor("luke", "john", "hepzibah");
newActor("john", "kate");
newActor("mary", "hepzibah", "mia");
newActor("hepzibah", "richard");
newActor("kate", "martin", "mia");
newActor("mia", "susan");
newActor("richard", "rebecca");
newActor("rebecca", "hannah");
newActor("michelle");
}
private ShortestPath.Actor newActor(String name, String... friends) {
ShortestPath.Actor actor = graph.computeIfAbsent(name, k -> new ShortestPath.Actor(name));
for (String friendsName : friends) {
ShortestPath.Actor friend = newActor(friendsName);
actor.addFriend(friend);
friend.addFriend(actor);
}
return actor;
}
@Test
public void findShortestPath() {
List<String> shortestPath = ShortestPath.findShortestPath(graph, "james", "mary");
assertThat(shortestPath, equalTo(Arrays.asList("james", "harry", "mary")));
}
@Test
public void findLongerShortestPath() {
List<String> shortestPath = ShortestPath.findShortestPath(graph, "james", "mia");
assertThat(shortestPath, equalTo(Arrays.asList("james", "harry", "mary", "mia")));
}
@Test
public void findAnotherShortestPath() {
List<String> shortestPath = ShortestPath.findShortestPath(graph, "harry", "hannah");
assertThat(shortestPath, equalTo(Arrays.asList("harry", "luke", "hepzibah", "richard", "rebecca", "hannah")));
}
@Test
public void findNoPath() {
List<String> shortestPath = ShortestPath.findShortestPath(graph, "james", "michelle");
assertThat(shortestPath, equalTo(Collections.emptyList()));
}
}