BFS: From [North Gate] to [Canteen]: North Gate Square Canteen
public interface Algorithm {
/**
* 执行算法
*/
void perform(Graph g, String sourceVertex);
/**
* 得到路径
*/
Map<String, String> getPath();
}
/**
* (无向)图
*/
public class Graph {
// 图的起点
private String firstVertax;
// 邻接表
private Map<String, List<String>> adj = new HashMap<>();
// 遍历算法
private Algorithm algorithm;
public Graph(Algorithm algorithm) {
this.algorithm = algorithm;
}
/**
* 执行算法
*/
public void done() {
algorithm.perform(this, firstVertax);
}
/**
* 得到从起点到{@code vertex}点的最短路径
* @param vertex
* @return
*/
public Stack<String> findPathTo(String vertex) {
Stack<String> stack = new Stack<>();
stack.add(vertex);
Map<String, String> path = algorithm.getPath();
for (String location = path.get(vertex) ; false == location.equals(firstVertax) ; location = path.get(location)) {
stack.push(location);
}
stack.push(firstVertax);
return stack;
}
/**
* 添加一条边
*/
public void addEdge(String fromVertex, String toVertex) {
if (firstVertax == null) {
firstVertax = fromVertex;
}
adj.get(fromVertex).add(toVertex);
adj.get(toVertex).add(fromVertex);
}
/**
* 添加一个顶点
*/
public void addVertex(String vertex) {
adj.put(vertex, new ArrayList<>());
}
public Map<String, List<String>> getAdj() {
return adj;
}
}
public Graph(Algorithm algorithm) {
this.algorithm = algorithm;
}
// 邻接表 private Map<String, List<String>> adj = new HashMap<>();
/**
* 封装BFS算法
*/
public class BroadFristSearchAlgorithm implements Algorithm {
// 保存已经访问过的地点
private List<String> visitedVertex;
// 保存最短路径
private Map<String, String> path;
@Override
public void perform(Graph g, String sourceVertex) {
if (null == visitedVertex) {
visitedVertex = new ArrayList<>();
}
if (null == path) {
path = new HashMap<>();
}
BFS(g, sourceVertex);
}
@Override
public Map<String, String> getPath() {
return path;
}
private void BFS(Graph g, String sourceVertex) {
Queue<String> queue = new LinkedList<>();
// 标记起点
visitedVertex.add(sourceVertex);
// 起点入列
queue.add(sourceVertex);
while (false == queue.isEmpty()) {
String ver = queue.poll();
List<String> toBeVisitedVertex = g.getAdj().get(ver);
for (String v : toBeVisitedVertex) {
if (false == visitedVertex.contains(v)) {
visitedVertex.add(v);
path.put(v, ver);
queue.add(v);
}
}
}
}
}
String[] vertex = {"North Gate", "South Gate", "Classroom", "Square", "Toilet", "Canteen"};
Edge[] edges = {
new Edge("North Gate", "Classroom"),
new Edge("North Gate", "Square"),
new Edge("Classroom", "Toilet"),
new Edge("Square", "Toilet"),
new Edge("Square", "Canteen"),
new Edge("Toilet", "South Gate"),
new Edge("Toilet", "South Gate"),
};
@Test
public void testBFS() {
Graph g = new Graph(new BroadFristSearchAlgorithm());
addVertex(g);
addEdge(g);
g.done();
Stack<String> result = g.findPathTo("Canteen");
System.out.println("BFS: From [North Gate] to [Canteen]:");
while (!result.isEmpty()) {
System.out.println(result.pop());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有