Академический Документы
Профессиональный Документы
Культура Документы
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To
represent weighted edges using adjacency matrices and priority queues (28.2). To model weighted graphs using the WeightedGraph class that extends the AbstractGraph class (28.3). To design and implement the algorithm for finding a minimum spanning tree (28.4). To define the MST class that extends the Tree class (28.4). To design and implement the algorithm for finding singlesource shortest paths (28.5). To define the ShortestPathTree class that extends the Tree class (28.5). To solve the weighted nine tail problem using the shortest path algorithm (28.6).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
0 1 2 3 4
1 2 null 7 3 null
2 null 7 null 4 5
3 8 3 4 null 6
1 2 8 0
7 3 4 6 3
2 5
4
7
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
1 2 8 0
7 3 4 6 3
2 5
4
8
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
interface
Graph<V>
AbstractGraph<V>
WeightedGraph<V>
-queues: java.util.PriorityQueue<WeightedEdge>[] +WeightedGraph(edges: int[][], vertices: V[]) +WeightedGraph(edges: List<WeightedEdge>, vertices: List<V>) +WeightedGraph(edges: int[][], numberOfVertices: int) +WeightedGraph(edges: List<WeightedEdge>, numberOfVertices: int) +printWeightedEdges(): void +getMinimumSpanningTree(): MST +getMinimumSpanningTree(index: int): MST +getShortestPath(index: int): ShortestPathTree +getWeightedEdges(): java.util.PriorityQueue<WeightedEdge>[] queues[i] is a priority queue that contains all the edges adjacent to vertex i. Constructs a weighted graph with the specified edges and the number of vertices in arrays. Constructs a weighted graph with the specified edges and the number of vertices. Constructs a weighted graph with the specified edges in an array and the number of vertices. Constructs a weighted graph with the specified edges in a list and the number of vertices. Displays all edges and weights. Returns a minimum spanning tree starting from vertex 0. Returns a minimum spanning tree starting from vertex v. Returns all single-source shortest paths. Returns all weighted edges for each vertex in a priority queue.
TestWeightedGraph
Graph AbstractGraph
TestWeightedGraph
WeightedGraph
9
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
6 7
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
10
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
V-T
v u
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
12
10 1 6 0 5 5 12 8 7 7 6 7 4 5 10 8 2 8 3 0 5 5 6 8 7 1 7
10 2 5 6 7 4 12 10 8 8 3 0 5 5 6 8 7 1 7
10 2 5 6 7 4 12 10 8 8 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
13
WeightedGraph.MST
-totalWeight: int +MST(root: int, parent: int[], totalWeight: int) +getTotalWeight(): int Total weight of the tree. Constructs a MST with the specified root, parent array, and total weight for the tree. Returns the totalWeight of the tree.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
14
Time Complexity
For each vertex, the program constructs a priority queue for its adjacent edges. It takes O(log|V|) time to insert an edge to a priority queue and the same time to remove an edge from the priority queue. So the overall time complexity for the program is O(|E|log|V|) , where |E| denotes the number of edges and |V| denotes the number of vertices.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15
Test MST
Seattle 2097 1 1331 807 1003 7 Denver 1267 3 San Francisco 381 2 1015 Kansas City 1663 864 496 5 781 810 Dallas 239 6 661 10 Atlanta 599 4 888 533 1260 787 Chicago 8 New York 983 9 214 Boston
Los Angeles
1435
Houston
1187
11 Miami
TestMinimumSpanningTree
TestMinimumSpanningTree
16
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Shortest Path
27.1 introduced the problem of finding the shortest distance between two cities for the graph in Figure 13.1. The answer to this problem is to find a shortest path between two vertices in the graph.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
17
18
V-T
T v s u
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
19
SP Algorithm Example
1 5 8 1 0 2 10 9 6 7 4 5 5 8 parent 5 0 -1 1 2 3 4 5 6 3 8 4 2 0 costs 0 1 2 3 4 5 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
20
SP Algorithm Example
1 5 8 1 0 2 10 9 6 7 4 5 5 8 parent 5 0 -1 1 1 2 3 4 5 6 3 8 4 2 0 costs 0 1 5 2 3 4 5 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
21
SP Algorithm Example
1 5 8 1 0 2 10 9 6 7 4 5 5 8 parent 5 2 0 -1 1 1 2 3 4 5 6 3 8 4 2
costs 6 0 0 5 1 2 3 4 5 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
22
SP Algorithm Example
1 5 8 2 1 0 2 10 9 6 7 4 5 5 8 parent 5 2 0 -1 1 1 2 3 4 5 0 6 3 costs 8 4 6 0 0 1 5 2 3 4 5 8 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
23
SP Algorithm Example
1 5 8 2 1 0 2 10 9 6 7 4 5 5 8 parent 5 2 0 -1 1 1 2 1 3 4 5 0 6 3 costs 8 4 6 0 0 1 5 2 10 3 4 5 8 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
24
SP Algorithm Example
1 5 8 2 1 0 2 10 9 6 7 4 5 5 8 parent 5 2 0 -1 1 1 2 1 3 5 4 0 5 0 6 3 costs 8 4 6 0 0 1 5 2 10 15 10 3 4 5 8 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
25
SP Algorithm Implementation
AbstractGraph.Tree
WeightedGraph.ShortestPathTree
-costs: int[] costs[v] stores the cost for the path from the source to v.
+ShortestPathTree(source: int, parent: int[], Constructs a shortest path tree with the specified source, parent array, and costs array. searchOrder: List<Integer>, costs: int[]) Returns the cost for the path from the source to vertex v. +getCost(v: int): int +printAllPaths(): void Displays all paths from the soruce.
TestShortestPath
TestShortestPath
26
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
SP Algorithm Example
Seattle 11 2097 983 1331 807 1003 Denver 10 San Francisco 381 8 1435 496 5 Dallas 239 6 781 810 661 1267 4 599 1 1015 Kansas City 1663 864 7 Atlanta 888 533 1260 787 Chicago 214 2 New York 3 Boston
Los Angeles
Houston
1187 9 Miami
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
27
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
28
WeightedNineTailModel
H H T T H H
H T H
T T H T H H
H T H
29
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807