Chapter 14 Weighted Graphs © John Urrutia 2014, All Rights Reserved1.

Post on 14-Dec-2015

215 views 1 download

Tags:

Transcript of Chapter 14 Weighted Graphs © John Urrutia 2014, All Rights Reserved1.

Chapter 14 Weighted Graphs

© John Urrutia 2014, All Rights Reserved 1

Weighted GraphsWeighted Graphs

Similar to directional GraphsAdded attribute called “weight” to the edges

Used to designate a preference between edgesHelps answer questions like

What is the minimum spanning tree for a weighted graph?

What is the shortest (or cheapest) distance from one vertex to another?

© John Urrutia 2014, All Rights Reserved 2

Minimum Spanning Treewith weighted graphs

Weight changes everythingEach vertex edge must be compared based on

their weightThe lightest weight wins

Given 6 cities where the weight between each city is the cost ($millions)of building an internet cableWhat is/are the least expensive route

© John Urrutia 2014, All Rights Reserved 3

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 4

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Repeat until no moreVertices to add

Pick a vertex

Add vertex to MSP

Identify the lightestEdge from this vertex

NewVertex in MSP

Identify the next lightest Edge

from this vertex

No

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 5

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Erizo Colina

Flor Bordo

Ajo Danza

5 6 7 6 4

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 6

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Flor Colina

Erizo Bordo

Ajo Danza

6 5 7 6 4

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 7

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Colina

Erizo Flor Bordo

Ajo Danza

5 6 7 6 4

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 8

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Bordo

Ajo Danza Erizo

Colina

Flor

6 4 7 5 6

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 9

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Ajo Danza

Bordo

Erizo Colina

Flor

4 6 7 5 6

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 10

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

Danza

Ajo Bordo

Erizo Colina

Flor

4 6 7 5 6

Minimum Spanning TreeThe Algorithm

Select a current vertex to start the treeFind all edges from the current vertex to

vertices not already in the tree. Place them into a priority queue.

Select the lowest priority edge from the queue add it and the destination vertex to the tree.

Repeat until no more vertices to add.

© John Urrutia 2014, All Rights Reserved 11

Priority Queue

Vertex List

Minimum Spanning Trees

© John Urrutia 2014, All Rights Reserved 12

Colina

Danza

Flor

Ajo

Bordo

Erizo

10 6

7

124

7

7

8

56

A

D4B6E7C5F6

Ajo Danza

Bordo

Erizo Colina

Flor

4 6 7 5 6

B6D4E12C8B7

B6

C10E7F7C5

Shortest Path ProblemWith the Minimum Spanning Tree we

answer the question:Can we get there from here

With the Shortest Path we answer the questions:What is the shortest way to get there from here

© John Urrutia 2014, All Rights Reserved 13

Dijkstra’s AlgorithmDeveloped in 1959 by Edsger DijkstraBased on an adjacency matrix for a directed

graphFinds the shortest path between one vertex

to anotherFinds the shortest paths between a vertex

and all other verticesSimilar to Warshall’s algorithm but updates a

path when it is shorter than a previous one

© John Urrutia 2014, All Rights Reserved 14

Dijkstra’s AlgorithmFor each unvisited vertex in the weighted

graphSelect the vertex with the smallest total

distanceCalculate the distance through this vertex to

each unvisited neighboring vertexUpdate the neighboring vertex distance if

smaller.Mark the source vertex as visited.

© John Urrutia 2014, All Rights Reserved 15

Shortest Path Array

© John Urrutia 2014, All Rights Reserved 16

Colina

Danza

Flor

Ajo

Bordo

Erizo

$10 $6

$7

$12$4

$7

$7

$8

$3$6

~

~

~

~

~

0

4

6

16

12

18

6 + 10 = 16!< 12 NoChg12 + 3 = 15< 16 Update

15

6 + 7 = 13!< 4 NoChg

All-pairs Shortest PathWeighted Adjacency Matrix

© John Urrutia 2014, All Rights Reserved 17

¯From / To® Ajo Bordo Colina Danza Erizo Flor

Ajo inf $6 inf $4 inf inf

Bordo inf inf $10 $7 inf inf

Colina inf inf inf inf $3 $6

Danza inf inf $8 inf $12 inf

Erizo inf $7 inf inf inf inf

Flor inf inf inf inf $7 inf

All-pairs Shortest PathAll-Pairs Table

© John Urrutia 2014, All Rights Reserved 18

¯From / To® Ajo Bordo Colina Danza Erizo Flor

Ajo inf $6 $12 $4 $16 $18

Bordo inf inf $10 $7 $13 $16

Colina inf $10 inf $17 $3 $6

Danza inf $18 $8 inf $12 $14

Erizo inf $7 $17 $14 inf $23

Flor inf $14 $24 $21 $7 inf

Floyd’s AlgorithmDijkstra’s algorithm for producing the All-

Pairs Shortest Path Table runs in O(N2)Floyds algorithm for the All-Pairs Shortest

Path Table is more efficient because it uses a variation on Warshall’s algorithm.

© John Urrutia 2014, All Rights Reserved 19

Floyd’s AlgorithmProcess each row and column

© John Urrutia 2014, All Rights Reserved 20

Adjacency Matrix

Ajo Bordo Colina Dansa Erizo Flor

Ajo 6 4

Bordo 10 7

Colina 3 6

Dansa 8 12

Erizo 7

Flor 7

Ajo goes to Bordo – Bordo goes to?

6 + 7 = 13!< 4 No Chg

SAN goes to LGA,MSP,SFO,STL,TPA – what goes to SAN Nothing?

Nothing goes to Ajo

TPA goes to STL – what goes to TPA

16

4 + 8 = 12< 16 Update

12

SummaryIn a weighted graph, edges have an associated

number called the weight, which might represent distances, costs, times, or other quantities.

The minimum spanning tree in a weighted graph minimizes the weights of the edges necessary to connect all the vertices. 

An algorithm using a priority queue can be used to find the minimum spanning tree of a weighted graph.

The minimum spanning tree of a weighted graph models real-world situations such as installing utility cables between cities.

© John Urrutia 2014, All Rights Reserved 21

SummaryThe shortest-path problem in a non-weighted graph

involves finding the minimum number of edges between two vertices.

Solving the shortest-path problem for weighted graphs yields the path with the minimum total edge weight.

The shortest-path problem for weighted graphs can be solved with Dijkstra’s algorithm.

The algorithms for large, sparse graphs generally run much faster if the adja- cency-list representation of the graph is used rather than the adjacency matrix.

© John Urrutia 2014, All Rights Reserved 22

SummaryThe all-pairs shortest-path problem is to find

the total weight of the edges between every pair of vertices in a graph. Floyd’s algorithm can be used to solve this problem.

Some graph algorithms take exponential time and are therefore not practical for graphs with more than a few vertices.

© John Urrutia 2014, All Rights Reserved 23

Directional Weighted Graph

© John Urrutia 2014, All Rights Reserved 24

Colina

Danza

Flor

Ajo

Bordo

Erizo

$10 $6

$7

$12$4

$7

$7

$8

$3$6