Вы находитесь на странице: 1из 29

Chapter 28 Weighted Graphs and Applications

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Weighted Graph Animation


www.cs.armstrong.edu/liang/animation/ShortestPathAnimation.html

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Weighted Graph Animation


www.cs.armstrong.edu/liang/animation/WeightedGraphLearningTool.html

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

Representing Weighted Graphs


Representing Weighted Edges: Edge Array Weighted Adjacency Matrices Priority Adjacency Lists

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Representing Weighted Edges: Edge Array


int[][] edges = {{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} };
1 2 8 0 3 7 3 4 6 4 2 5

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Representing Weighted Edges: Edge Array


Integer[][] adjacencyMatrix = { {null, 2, null, 8, null }, {2, null, 7, 3, null }, {null, 7, null, 4, 5}, {8, 3, 4, null, 6}, {null, null, 5, 6, null} };

0 1 2 3 4

0 null 2 2 3 null 4 8 null

1 2 null 7 3 null

2 null 7 null 4 5

3 8 3 4 null 6

4 null null 5 6 null

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

Priority Adjacency Lists


queues[0] queues[1] queues[2] queues[3] queues[4] WeightedEdge(0, 1, 2) WeightedEdge(1, 0, 2) WeightedEdge(2, 3, 4) WeightedEdge(3, 1, 3) WeightedEdge(4, 2, 5) WeightedEdge(0, 3, 8) WeightedEdge(1, 3, 3) WeightedEdge(2, 4, 5) WeightedEdge(3, 2, 4) WeightedEdge(4, 3, 6) WeightedEdge(1, 2, 7) WeightedEdge(2, 1, 7) WeightedEdge(3, 4, 6) WeightedEdge(3, 0, 8)

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

Minimum Spanning Trees


A graph may have many spanning trees. Suppose that the edges are weighted. A minimum spanning tree is a spanning tree with the minimum total weights. For example, the trees in Figures 28.3(b), 28.3(c), 28.3(d) are spanning trees for the graph in Figure 28.3(a). The trees in Figures 28.3(c) and 28.3(d) are minimum spanning trees.
10 6 8 5 7 7 12 7 5 10 8 5 7 7 8 8 10 5

6 7

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

10

Minimum Spanning Tree Algorithm


minimumSpanningTree() { Let V denote the set of vertices in the graph; Let T be a set for the vertices in the spanning tree; Initially, add the starting vertex to T; while (size of T < n) { find u in T and v in V T with the smallest weight on the edge (u, v), as shown in Figure 28.4; add v to T; } }

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

11

Minimum Spanning Tree Algorithm

Vertices already in the spanning tree T

V-T

Vertices not currently in the spanning tree

v u

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

12

Minimum Spanning Tree Algorithm Example


10 1 6 0 5 5 12 8 7 7 6 7 4 5 10 8 2 8 3 0 5 5 12 6 8 7 1 7 6 7 4 5 10 8 10 2 8 3 0 5 5 12 6 8 7 1 7 6 7 4 5 10 8 10 2 8 3

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

Implementing MST Algorithm


AbstractGraph.Tree

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

Single Source Shortest Path Algorithm


shortestPath(s) { Let V denote the set of vertices in the graph; Let T be a set that contains the vertices whose path to s have been found; Initially T contains source vertex s; while (size of T < n) { find v in V T with the smallest costs[u] + w(u, v) value among all u in T; add v to T; } }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

18

Single Source Shortest Path Algorithm


T contains vertices whose shortest path to s have been found

V-T

V- T contains vertices whose shortest path to s have not been found

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

Shortest Path Animation


www.cs.armstrong.edu/liang/animation/ShortestPat hAnimation.html

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

28

The Weighted Nine Tail Problem


The nine tail problem is to find the minimum number of the moves that lead to all coins face down. Each move flips a head coin and its neighbors. The weighted nine tail problem assigns the number of the flips as a weight on each move. For example, you can move from the coins in Figure 28(a) to Figure 28(b) by flipping the three coins. So the weight for this move is 3.

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

Вам также может понравиться