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

Preview:

Citation preview

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

Chapter 28 Weighted Graphs and Applications

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

Weighted Graph Animationwww.cs.armstrong.edu/liang/animation/ShortestPathAnimation.html

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

Weighted Graph Animationwww.cs.armstrong.edu/liang/animation/WeightedGraphLearningTool.html

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

ObjectivesTo 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 single-

source 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).

5Liang, 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

6Liang, 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}};

4 2

7

8 3

6

5

0

1 2

3 4

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

Representing Weighted Edges: Edge Array

4 2

7

8 3

6

5

0

1 2

3 4

0

1

2

3 4

0 1 2 3 4

2

3

4

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

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}

};

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

Priority Adjacency Lists

4 2

7

8 3

6

5

0

1 2

3 4

queues[0]

queues[1]

queues[2]

queues[3]

queues[4]

WeightedEdge(0, 1, 2) WeightedEdge(0, 3, 8)

WeightedEdge(1, 0, 2) WeightedEdge(1, 3, 3) WeightedEdge(1, 2, 7)

WeightedEdge(2, 3, 4) WeightedEdge(2, 4, 5) WeightedEdge(2, 1, 7)

WeightedEdge(3, 1, 3) WeightedEdge(3, 2, 4) WeightedEdge(3, 0, 8) WeightedEdge(3, 4, 6)

WeightedEdge(4, 2, 5) WeightedEdge(4, 3, 6)

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

GraphGraph

TestWeightedGraphTestWeightedGraph

AbstractGraphAbstractGraph WeightedGraphWeightedGraph

TestWeightedGraphTestWeightedGraph

«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.

10Liang, 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.

5 6

10

5

8

7

7

12

7

10

8

8

5

5

10

7 7 8

5

5

6

7

7 8

5

5

6

7 7 8

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

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;

}

}

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

Minimum Spanning Tree Algorithm

u

v

T

V - T Vertices already in the spanning tree

Vertices not currently in the spanning tree

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

Minimum Spanning Tree Algorithm Example

6

5 4

3

2

0

5 6

10

5

8

7

7

12

7

10

8

8

1

6

5 4

3

2

0

5 6

10

5

8

7

7

12

7

10

8

8

1

6

5 4

3

2

0

5 6

10

5

8

7

7

12

7

10

8

8

1

6

5 4

3

2

0

5 6

10

5

8

7

7

12

7

10

8

8

1

6

5 4

3

2

0

5 6

10

5

8

7

7

12

7

10

8

8

1

6

5 4

3

2

0

5 6

10

5

8

7

7

12

7

10

8

8

1

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

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.

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

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.

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

Test MST

TestMinimumSpanningTreeTestMinimumSpanningTreeTestMinimumSpanningTreeTestMinimumSpanningTree

Seattle

San Francisco

Los Angeles

Denver

Chicago

Kansas City

Houston

Boston

New York

Atlanta

Miami

661

888

1187

810

Dallas

1331

2097

1003 807

381

1015

1267

1663

1435

239

496

781

864

1260

983

787

214

533

599

1

2

3 4

6

7

8

9

10

11

5

17Liang, 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.

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

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;

}

}

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

Single Source Shortest Path Algorithm

u

v

T

V - T

s

T contains vertices whose shortest path to s have been found

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

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

SP Algorithm Example

5 5

10

1

8

9

2

4

7

8

8

5

1

2

3

4

5

6

0

0 1 2 3 4 5 6

costs 0

0 1 2 3 4 5 6

parent -1

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

SP Algorithm Example

5 5

10

1

8

9

2

4

7

8

8

5

1

2

3

4

5

6

0

0 1 2 3 4 5 6

costs 0 5

0 1 2 3 4 5 6

parent -1 1

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

SP Algorithm Example

5 5

10

1

8

9

2

4

7

8

8

5

1

2

3

4

5

6

0

0 1 2 3 4 5 6

costs 6 0 5

0 1 2 3 4 5 6

parent 2 -1 1

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

SP Algorithm Example

5 5

10

1

8

9

2

4

7

8

8

5

1

2

3

4

5

6

0

0 1 2 3 4 5 6

costs 6 0 5 8

0 1 2 3 4 5 6

parent 2 -1 1 0

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

SP Algorithm Example

5 5

10

1

8

9

2

4

7

8

8

5

1

2

3

4

5

6

0

0 1 2 3 4 5 6

costs 6 0 5 10 8

0 1 2 3 4 5 6

parent 2 -1 1 1 0

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

SP Algorithm Example

5 5

10

1

8

9

2

4

7

8

8

5

1

2

3

4

5

6

0

0 1 2 3 4 5 6

costs 6 0 5 10 15 10 8

0 1 2 3 4 5 6

parent 2 -1 1 1 5 0 0

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

SP Algorithm Implementation

TestShortestPathTestShortestPathTestShortestPathTestShortestPath

AbstractGraph.Tree

WeightedGraph.ShortestPathTree

-costs: int[]

+ShortestPathTree(source: int, parent: int[], searchOrder: List<Integer>, costs: int[])

+getCost(v: int): int

+printAllPaths(): void

costs[v] stores the cost for the path from the source to v.

Constructs a shortest path tree with the specified source, parent array, and costs array.

Returns the cost for the path from the source to vertex v.

Displays all paths from the soruce.

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

SP Algorithm Example

Seattle

San Francisco

Los Angeles

Denver

Chicago

Kansas City

Houston

Boston

New York

Atlanta

Miami

661

888

1187

810

Dallas

1331

2097

1003 807

381

1015

1267

1663

1435

239

496

781

864

1260

983

787

214

533

599 1

2

3

4

5

6

7

8

9

10

11

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

Shortest Path Animation

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

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

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.

H T

T

T

H

H

H

H

H

T H

T

T

T

H

H

H

H

WeightedNineTailModelWeightedNineTailModel

Recommended