import java.util.*;
public class Exercise29_18 {
public static void main(String[] args) {
String[] vertices = { "Seattle", "San Francisco", "Los Angeles", "Denver",
"Kansas City", "Chicago", "Boston", "New York", "Atlanta", "Miami",
"Dallas", "Houston" };
int[][] edges = { { 0, 1, 807 }, { 0, 3, 1331 }, { 0, 5, 2097 },
{ 1, 0, 807 }, { 1, 2, 381 }, { 1, 3, 1267 }, { 2, 1, 381 },
{ 2, 3, 1015 }, { 2, 4, 1663 }, { 2, 10, 1435 }, { 3, 0, 1331 },
{ 3, 1, 1267 }, { 3, 2, 1015 }, { 3, 4, 599 }, { 3, 5, 1003 },
{ 4, 2, 1663 }, { 4, 3, 599 }, { 4, 5, 533 }, { 4, 7, 1260 },
{ 4, 8, 864 }, { 4, 10, 496 }, { 5, 0, 2097 }, { 5, 3, 1003 },
{ 5, 4, 533 }, { 5, 6, 983 }, { 5, 7, 787 }, { 6, 5, 983 },
{ 6, 7, 214 }, { 7, 4, 1260 }, { 7, 5, 787 }, { 7, 6, 214 },
{ 7, 8, 888 }, { 8, 4, 864 }, { 8, 7, 888 }, { 8, 9, 661 },
{ 8, 10, 781 }, { 8, 11, 810 }, { 9, 8, 661 }, { 9, 11, 1187 },
{ 10, 2, 1435 }, { 10, 4, 496 }, { 10, 8, 781 }, { 10, 11, 239 },
{ 11, 8, 810 }, { 11, 9, 1187 }, { 11, 10, 239 } };
WeightedGraph<String> graph1 = new WeightedGraph<>(edges, vertices);
WeightedGraph<String>.ShortestPathTree tree1 = graph1
.getShortestPath(graph1.getIndex("Chicago"));
tree1.printAllPaths();
// Display shortest paths from Houston to Chicago
System.out.print("Shortest path from Houston to Chicago: ");
java.util.List<String> path = tree1.getPath(graph1.getIndex("Houston"));
for (String s : path) {
System.out.print(s + " ");
}
edges = new int[][] { { 0, 1, 2 }, { 0, 3, 8 }, { 1, 0, 2 }, { 1, 2, 7 },
{ 1, 3, 3 }, { 2, 1, 7 }, { 2, 3, 4 }, { 2, 4, 5 }, { 3, 0, 8 },
{ 3, 1, 3 }, { 3, 2, 4 }, { 3, 4, 6 }, { 4, 2, 5 }, { 4, 3, 6 } };
WeightedGraph<Integer> graph2 = new WeightedGraph<Integer>(edges, 5);
WeightedGraph<Integer>.ShortestPathTree tree2 = graph2.getShortestPath(3);
System.out.println("\n");
tree2.printAllPaths();
}
public static interface Graph<V> {
/** Return the number of vertices in the graph */
public int getSize();
/** Return the vertices in the graph */
public java.util.List<V> getVertices();
/** Return the object for the specified vertex index */
public V getVertex(int index);
/** Return the index for the specified vertex object */
public int getIndex(V v);
/** Return the neighbors of vertex with the specified index */
public java.util.List<Integer> getNeighbors(int index);
/** Return the degree for a specified vertex */
public int getDegree(int v);
/** Print the edges */
public void printEdges();
/** Clear graph */
public void clear();
/** Add a vertex to the graph */
public boolean addVertex(V vertex);
/** Add an edge to the graph */
public boolean addEdge(int u, int v);
/** Obtain a depth-first search tree */
public AbstractGraph<V>.Tree dfs(int v);
/** Obtain a breadth-first search tree */
public AbstractGraph<V>.Tree bfs(int v);
}
public static abstract class AbstractGraph<V> implements Graph<V> {
protected List<V> vertices = new ArrayList<V>(); // Store vertices
protected List<List<Integer>> neighbors
= new ArrayList<List<Integer>>(); // Adjacency lists
/** Construct an empty graph */
protected AbstractGraph() {
}
/** Construct a graph from edges and vertices stored in arrays */
protected AbstractGraph(int[][] edges, V[] vertices) {
for (int i = 0; i < vertices.length; i++)
addVertex(vertices[i]);
createAdjacencyLists(edges, vertices.length);
}
/** Construct a graph from edges and vertices stored in List */
protected AbstractGraph(List<Edge> edges, List<V> vertices) {
for (int i = 0; i < vertices.size(); i++)
addVertex(vertices.get(i));
createAdjacencyLists(edges, vertices.size());
}
/** Construct a graph for integer vertices 0, 1, 2 and edge list */
protected AbstractGraph(List<Edge> edges, int numberOfVertices) {
for (int i = 0; i < numberOfVertices; i++)
addVertex((V)(new Integer(i))); // vertices is {0, 1, ...}
createAdjacencyLists(edges, numberOfVertices);
}
/** Construct a graph from integer vertices 0, 1, and edge array */
protected AbstractGraph(int[][] edges, int numberOfVertices) {
for (int i = 0; i < numberOfVertices; i++)
addVertex((V)(new Integer(i))); // vertices is {0, 1, ...}
createAdjacencyLists(edges, numberOfVertices);
}
/** Create adjacency lists for each vertex */
private void createAdjacencyLists(
int[][] edges, int numberOfVertices) {
for (int i = 0; i < edges.length; i++) {
int u = edges[i][0];
int v = edges[i][1];
addEdge(u, v);
}
}
/** Create adjacency lists for each vertex */
private void createAdjacencyLists(
List<Edge> edges, int numberOfVertices) {
for (Edge edge: edges) {
addEdge(edge.u, edge.v);
}
}
@Override /** Return the number of vertices in the graph */
public int getSize() {
return vertices.size();
}
@Override /** Return the vertices in the graph */
public List<V> getVertices() {
return vertices;
}
@Override /** Return the object for the specified vertex */
public V getVertex(int index) {
return vertices.get(index);
}
@Override /** Return the index for the specified vertex object */
public int getIndex(V v) {
return vertices.indexOf(v);
}
@Override /** Return the neighbors of the specified vertex */
public List<Integer> getNeighbors(int index) {
return neighbors.get(index);
}
@Override /** Return the degree for a specified vertex */
public int getDegree(int v) {
return neighbors.get(v).size();
}
@Override /** Print the edges */
public void printEdges() {
for (int u = 0; u < neighbors.size(); u++) {
System.out.print(getVertex(u) + " (" + u + "): ");
for (int j = 0; j < neighbors.get(u).size(); j++) {
System.out.print("(" + u + ", " +
neighbors.get(u).get(j) + ") ");
}
System.out.println();
}
}
@Override /** Clear graph */
public void clear() {
vertices.clear();
neighbors.clear();
}
@Override /** Add a vertex to the graph */
public boolean addVertex(V vertex) {
if (!vertices.contains(vertex)) {
vertices.add(vertex);
neighbors.add(new ArrayList<Integer>());
return true;
}
else {
return false;
}
}
@Override /** Add an edge to the graph */
public boolean addEdge(int u, int v) {
if (u < 0 || u > getSize() - 1)
throw new IllegalArgumentException("No such index: " + u);
if (v < 0 || v > getSize() - 1)
throw new IllegalArgumentException("No such index: " + v);
if (!neighbors.get(u).contains(v)) {
neighbors.get(u).add(v);
return true;
}
else {
return false;
}
}
/** Edge inner class inside the AbstractGraph class */
public static class Edge {
public int u; // Starting vertex of the edge
public int v; // Ending vertex of the edge
/** Construct an edge for (u, v) */
public Edge(int u, int v) {
this.u = u;
this.v = v;
}
}
@Override /** Obtain a DFS tree starting from vertex v */
/** To be discussed in Section 30.6 */
public Tree dfs(int v) {
List<Integer> searchOrder = new ArrayList<Integer>();
int[] parent = new int[vertices
没有合适的资源?快使用搜索试试~ 我知道了~
Java语言程序设计与数据结构(基础篇)第15章课后习题代码chapter15.rar
共62个文件
java:62个
需积分: 50 15 下载量 125 浏览量
2019-07-04
20:24:43
上传
评论 5
收藏 76KB RAR 举报
温馨提示
Java语言程序设计与数据结构(基础篇)第15章课后习题代码
资源推荐
资源详情
资源评论
收起资源包目录
chapter15.rar (62个子文件)
chapter15
Exercise15_15.java 2KB
Exercise15_09.java 2KB
Exercise15_14.java 2KB
Exercise15_29.java 3KB
Exercise15_23.java 2KB
Exercise15_08.java 1KB
Exercise15_18.java 1KB
Exercise15_34.java 4KB
Exercise15_13.java 1KB
Exercise15_33.java 5KB
Exercise15_11Extra.java 2KB
Exercise15_11.java 1KB
Exercise15_04Extra.java 3KB
Exercise15_21.java 5KB
Exercise15_26.java 2KB
Exercise15_30.java 2KB
Exercise15_05Extra.java 8KB
Exercise15_12.java 1KB
Exercise15_27.java 2KB
Exercise15_09Extra.java 7KB
Exercise15_01Extra.java 2KB
chapter29
Exercise29_16.java 7KB
Exercise29_15.java 8KB
Exercise29_03.java 867B
Exercise29_04(1).java 3KB
Exercise29_13.java 6KB
Exercise29_18.java 24KB
Exercise29_04.java 3KB
Exercise29_20.java 21KB
Exercise29_10.java 2KB
Exercise29_05.java 2KB
Exercise29_14.java 7KB
Exercise29_09.java 2KB
Exercise29_11.java 2KB
Exercise29_17.java 13KB
Exercise29_12.java 4KB
Exercise15_08Extra.java 2KB
Exercise15_10.java 1KB
Exercise15_07Extra.java 6KB
Exercise15_32.java 7KB
Exercise15_06.java 1KB
Exercise15_01(1).java 2KB
Exercise15_31.java 3KB
Exercise15_19.java 2KB
Exercise15_04.java 3KB
Exercise15_25.java 3KB
Exercise15_28.java 4KB
Exercise15_02Extra.java 3KB
Exercise15_20.java 4KB
Exercise15_10Extra.java 5KB
Exercise15_07.java 1KB
Exercise15_16.java 3KB
Exercise15_35.java 4KB
Exercise15_36.java 2KB
Exercise15_17.java 3KB
Exercise15_03.java 2KB
Exercise15_22.java 2KB
Exercise15_06Extra.java 3KB
Exercise15_05.java 3KB
Exercise15_03Extra.java 1KB
Exercise15_02.java 2KB
Exercise15_24.java 2KB
共 62 条
- 1
资源评论
南哲风
- 粉丝: 46
- 资源: 45
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功