discrete math 2 shortest paths using matrix

Post on 31-Dec-2015

62 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Discrete Math 2 Shortest Paths Using Matrix. CIS112 February 14, 2007. Overview. Previously: In weighted graph . . Shortest path from #7 to all others Search matrix method Now: Problem 8.6.2 Implement Floyd’s Algorithm. Strategy. 3 nested loops i ≔ 1 to 6 j ≔ 1 to 6 - PowerPoint PPT Presentation

TRANSCRIPT

Discrete Math 2Discrete Math 2Shortest Paths Using MatrixShortest Paths Using Matrix

CIS112CIS112February 14, 2007February 14, 2007

2007 Kutztown University 2

OverviewOverview Previously:Previously:

In weighted graph . .In weighted graph . . Shortest path from #7 to all othersShortest path from #7 to all others Search matrix methodSearch matrix method

Now:Now: Problem 8.6.2Problem 8.6.2 Implement Floyd’s AlgorithmImplement Floyd’s Algorithm

2007 Kutztown University 3

StrategyStrategy 3 nested loops 3 nested loops

i i ≔≔ 1 to 6 1 to 6 j j ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6

Basic ruleBasic rule If d(j,i) + d(i,k) < d(j,k) . .If d(j,i) + d(i,k) < d(j,k) . . Then d(j,k) Then d(j,k) d(j,i) + d(i,k) d(j,i) + d(i,k)

2007 Kutztown University 4

Interpretation of MatrixInterpretation of Matrix Row designates Row designates from verticesfrom vertices Column designates Column designates to verticesto vertices SuppSuppose entry [2,3] is 7ose entry [2,3] is 7

From vertex #2 to vertex #3 . .From vertex #2 to vertex #3 . . cost of travel = 7cost of travel = 7

2007 Kutztown University 5

The Basic OperationThe Basic Operation

Seek – best cost route from Seek – best cost route from jj to to kk Initial entries = cost of direct route, Initial entries = cost of direct route,

i.e., cost of edge i.e., cost of edge jkjk No edge No edge cost = cost = ∞∞ Guarantees that any route found is Guarantees that any route found is

betterbetter

2007 Kutztown University 6

The Basic OperationThe Basic Operation

Old cost from Old cost from jj to to kk compared to . . compared to . . Cost of 2 step hopCost of 2 step hop

jj to to ii and and then then ii to to kk

If 2 step hop has better cost . .If 2 step hop has better cost . . Then it becomes the new Then it becomes the new jj to to kk cost cost

2007 Kutztown University 7

The Basic OperationThe Basic Operation

Initially old cost is edge costInitially old cost is edge cost Later . .Later . . Old cost is cost of best route found Old cost is cost of best route found

so far so far

2007 Kutztown University 8

DenotationsDenotations

Outer loop – the Outer loop – the i loopi loop Middle loop – the Middle loop – the j loopj loop Inner loop – the Inner loop – the k loopk loop

2007 Kutztown University 9

OperationOperation Loops work in tandemLoops work in tandem 2 outer loops {2 outer loops {ii & & jj loops} loops} 2 inner loops {2 inner loops {jj & & kk loops} loops} Proceed Proceed

» row by row &row by row &

» column by columncolumn by column

Update travel cost . . Update travel cost . . from vertex from vertex jj to vertex to vertex kk

2007 Kutztown University 10

Matrix for Weighted GraphMatrix for Weighted Graph

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 ∞∞ ∞∞ 55 22 ∞∞

CC 33 ∞∞ ∞∞ ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 11

First Outer LoopFirst Outer Loop

i = 1i = 1 j = j = ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 12

First Inner LoopFirst Inner Loop

i = 1i = 1 j = 1j = 1 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 13

First Middle LoopFirst Middle Loop

d(1,1) = d(1,1) = ∞∞ d(1,1) + d(1,k) d(1,1) + d(1,k) ≮≮ d(1,k), d(1,k), ∀∀kk Therefore, no changeTherefore, no change

2007 Kutztown University 14

Second Middle LoopSecond Middle Loop

i = 1i = 1 j = 2j = 2 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 15

Second Middle LoopSecond Middle Loop

k = 1k = 1 d(1,1) = d(1,1) = ∞∞ d(2,1) + d(1,1) = d(2,1) + d(1,1) = ∞∞ d(2,1) + d(1,1) = d(2,1) + d(1,1) = ∞∞ ≮≮ d(2,1) d(2,1) Therefore, no changeTherefore, no change

2007 Kutztown University 16

i=1; j=2; k = 2 i=1; j=2; k = 2 (2,1) + (1,2) < (2,2) (2,1) + (1,2) < (2,2) ≡ 4 < ∞≡ 4 < ∞

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 ∞∞ 55 22 ∞∞

CC 33 ∞∞ ∞∞ ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 17

i=1; j=2; k = 3 i=1; j=2; k = 3 (2,1) + (1, 3) < (2,3) (2,1) + (1, 3) < (2,3) ≡ ≡ 5 < 5 < ∞∞

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 ∞∞ ∞∞ ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 18

i=1; j=2; k i=1; j=2; k ≔≔ 4,5,6 4,5,6(2,1) + (1,k) < (2,k) (2,1) + (1,k) < (2,k) ≡ ∞ ≮ ≡ ∞ ≮ xx

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 ∞∞ ∞∞ ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 19

Third Middle LoopThird Middle Loop

i = 1i = 1 j = 3j = 3 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 20

Third Middle LoopThird Middle Loop

k = 1k = 1 d(1,1) = d(1,1) = ∞∞ d(3,1) + d(1,1) = d(3,1) + d(1,1) = ∞∞ d(3,1) + d(1,1) = d(3,1) + d(1,1) = ∞∞ ≮≮ d(3,1) d(3,1) Therefore, no changeTherefore, no change

2007 Kutztown University 21

i=1; j=3; k = 2 i=1; j=3; k = 2 (3,1) + (1, 2) < (3,2) (3,1) + (1, 2) < (3,2) ≡ ≡ 5 < 5 < ∞∞

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 ∞∞ ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 22

i=1; j=3; k = 3 i=1; j=3; k = 3 (3,1) + (1, 3) < (3,3) (3,1) + (1, 3) < (3,3) ≡ ≡ 6 < 6 < ∞∞

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 23

i=1; j=3; k i=1; j=3; k ≔≔ 4,5,6 4,5,6 (3,1) + (1, k) < (3,k) (3,1) + (1, k) < (3,k) ≡ ≡ ∞ ≮ ∞ ≮ xx

VtxVtx AA BB CC DD EE ZZ

AA ∞∞ 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 24

44thth, 5, 5thth & 6 & 6thth Middle Loops Middle Loops

i = 1i = 1 j j ≔≔ 4 to 6 4 to 6 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 25

44thth, 5, 5thth & 6 & 6thth Middle Loops Middle Loops

j j ≔≔ 4 to 6 4 to 6 d(j,1) = d(j,1) = ∞∞ d(j,1) + d(i,k) = d(j,1) + d(i,k) = ∞∞ d(j,1) + d(i,1) = d(j,1) + d(i,1) = ∞∞ ≮≮ d(j,k) d(j,k) Therefore, no changeTherefore, no change

2007 Kutztown University 26

Second Outer LoopSecond Outer Loop

i = 2i = 2 j = j = ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 27

i=2; j=1; k = 1 i=2; j=1; k = 1 (1,2) + (2, 1) < (1,1) (1,2) + (2, 1) < (1,1) ≡ ≡ 4 < 4 < ∞∞

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 28

i=2; j=1; k = 2 i=2; j=1; k = 2 (1,2) + (2, 2) < (1,2) (1,2) + (2, 2) < (1,2) ≡ ≡ 6 6 ≮≮ 2 2

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 29

i=2; j=1; k = 3 i=2; j=1; k = 3 (1,2) + (2, 3) < (1,3) (1,2) + (2, 3) < (1,3) ≡ ≡ 7 7 ≮≮ 3 3

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 ∞∞ ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 30

i=2; j=1; k = 4 i=2; j=1; k = 4 (1,2) + (2, 4) < (1,4) (1,2) + (2, 4) < (1,4) ≡ ≡ 7 7 < ∞< ∞

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 ∞∞ ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 31

i=2; j=1; k = 5 i=2; j=1; k = 5 (1,2) + (2, 5) < (1,5) (1,2) + (2, 5) < (1,5) ≡ ≡ 4 4 < ∞< ∞

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 32

i=2; j=1; k = 6 i=2; j=1; k = 6 (1,2) + (2, 6) < (1,6) (1,2) + (2, 6) < (1,6) ≡ ≡ ∞ ≮ ∞∞ ≮ ∞

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

2007 Kutztown University 33

Second Middle LoopSecond Middle Loop

i = 2i = 2 j = 2j = 2 k k ≔≔ 1 to 6 1 to 6

2007 Kutztown University 34

Second Middle Loop k Second Middle Loop k ≔≔ 1 to3 1 to3 ii jj kk 22 22 11

(2,2) + (2,1) < (2,1) ?(2,2) + (2,1) < (2,1) ? 4 + 2 4 + 2 ≮≮ 2 2

22 22 22 (2,2) + (2,2) < (2,2) ?(2,2) + (2,2) < (2,2) ? 4 + 4 4 + 4 ≮≮ 4 4

22 22 33 (2,2) + (2,3) < (2,3) ?(2,2) + (2,3) < (2,3) ? 4 + 5 4 + 5 ≮≮ 5 5

2007 Kutztown University 35

Second Middle Loop k Second Middle Loop k ≔≔ 4 to6 4 to6 ii jj kk 22 22 44

(2,2) + (2,4) < (2,4) ?(2,2) + (2,4) < (2,4) ? 4 + 5 4 + 5 ≮≮ 5 5

22 22 55 (2,2) + (2,5) < (2,5) ?(2,2) + (2,5) < (2,5) ? 4 + 2 4 + 2 ≮≮ 2 2

22 22 66 (2,2) + (2,6) < (2,6) ?(2,2) + (2,6) < (2,6) ? 4 + 4 + ∞∞ ≮≮ ∞∞

2007 Kutztown University 36

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 ∞∞ 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

Matrix After 2Matrix After 2ndnd Middle Loop Middle Loop {no change}{no change}

2007 Kutztown University 37

Third Middle Loop k Third Middle Loop k ≔≔ 1 to3 1 to3 ii jj kk 22 33 11

(3,2) + (2,1) < (3,1) ?(3,2) + (2,1) < (3,1) ? 5 + 2 5 + 2 ≮≮ 3 3

22 33 22 (3,2) + (2,2) < (3,2) ?(3,2) + (2,2) < (3,2) ? 5 + 4 5 + 4 ≮≮ 5 5

22 33 33 (3,2) + (2,3) < (3,3) ?(3,2) + (2,3) < (3,3) ? 5 + 5 5 + 5 ≮≮ 6 6

2007 Kutztown University 38

Third Middle Loop k Third Middle Loop k ≔≔ 4 to6 4 to6 ii jj kk 22 33 44

(3,2) + (2,4) < (3,4) ?(3,2) + (2,4) < (3,4) ? 5 + 5 < 5 + 5 < ∞∞

22 33 55 (3,2) + (2,5) < (3,5) ?(3,2) + (2,5) < (3,5) ? 5 + 2 5 + 2 ≮≮ 5 5

22 33 66 (3,2) + (2,6) < (3,6) ?(3,2) + (2,6) < (3,6) ? 5 + 5 + ∞∞ ≮≮ ∞∞

2007 Kutztown University 39

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 1010 55 ∞∞

DD ∞∞ 55 ∞∞ ∞∞ 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

Matrix After 3Matrix After 3rdrd Middle Loop Middle Loop {1 cell changed}{1 cell changed}

2007 Kutztown University 40

Fourth Middle Loop k Fourth Middle Loop k ≔≔ 1 to3 1 to3 ii jj kk 22 44 11

(4,2) + (2,1) < (4,1) ?(4,2) + (2,1) < (4,1) ? 5 + 2 < 5 + 2 < ∞∞

22 44 22 (4,2) + (2,2) < (4,2) ?(4,2) + (2,2) < (4,2) ? 5 + 4 5 + 4 ≮≮ 5 5

22 44 33 (4,2) + (2,3) < (4,3) ?(4,2) + (2,3) < (4,3) ? 5 + 5 < 5 + 5 < ∞∞

2007 Kutztown University 41

Fourth Middle Loop k Fourth Middle Loop k ≔≔ 4 to6 4 to6 ii jj kk 22 44 44

(4,2) + (2,4) < (4,4) ?(4,2) + (2,4) < (4,4) ? 5 + 5 < 5 + 5 < ∞∞

22 44 55 (4,2) + (2,5) < (4,5) ?(4,2) + (2,5) < (4,5) ? 5 + 2 5 + 2 ≮ ≮ 11

22 44 66 (4,2) + (2,6) < (4,6) ?(4,2) + (2,6) < (4,6) ? 5 + 5 + ∞∞ ≮≮ 2 2

2007 Kutztown University 42

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 1010 55 ∞∞

DD 77 55 1010 1010 11 22

EE ∞∞ 22 55 11 ∞∞ 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

Matrix After 4Matrix After 4thth Middle Loop Middle Loop {3 cells changed}{3 cells changed}

2007 Kutztown University 43

Fifth Middle Loop k Fifth Middle Loop k ≔≔ 1 to3 1 to3 ii jj kk 22 55 11

(5,2) + (2,1) < (5,1) ?(5,2) + (2,1) < (5,1) ? 2 + 2 < 2 + 2 < ∞∞

22 55 22 (5,2) + (2,2) < (5,2) ?(5,2) + (2,2) < (5,2) ? 2 + 4 2 + 4 ≮≮ 2 2

22 55 33 (5,2) + (2,3) < (5,3) ?(5,2) + (2,3) < (5,3) ? 2 + 5 2 + 5 ≮≮ 5 5

2007 Kutztown University 44

Fifth Middle Loop k Fifth Middle Loop k ≔≔ 4 to6 4 to6 ii jj kk 22 55 44

(5,2) + (2,4) < (5,4) ?(5,2) + (2,4) < (5,4) ? 2 + 5 2 + 5 ≮≮ 1 1

22 55 55 (5,2) + (2,5) < (5,5) ?(5,2) + (2,5) < (5,5) ? 2 + 2 < 2 + 2 < ∞∞ 22 55 66 (5,2) + (2,6) < (5,6) ?(5,2) + (2,6) < (5,6) ? 2 + 2 + ∞∞ ≮≮ 4 4

2007 Kutztown University 45

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 1010 55 ∞∞

DD 77 55 1010 1010 11 22

EE 44 22 55 11 44 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

Matrix After 5Matrix After 5thth Middle Loop Middle Loop {2 cells changed}{2 cells changed}

2007 Kutztown University 46

Sixth Middle Loop k Sixth Middle Loop k ≔≔ 1 to3 1 to3 ii jj kk 22 66 11

(6,2) + (2,1) < (6,1) ?(6,2) + (2,1) < (6,1) ? ∞∞ + 2 + 2 ≮ ∞≮ ∞ 22 66 22 (6,2) + (2,2) < (6,2) ?(6,2) + (2,2) < (6,2) ? ∞∞ + 4 + 4 ≮ ∞≮ ∞

22 66 33 (6,2) + (2,3) < (6,3) ?(6,2) + (2,3) < (6,3) ? ∞∞ + 5 + 5 ≮ ∞≮ ∞

2007 Kutztown University 47

Sixth Middle Loop k Sixth Middle Loop k ≔≔ 4 to6 4 to6 ii jj kk 22 66 44

(6,2) + (2,4) < (6,4) ?(6,2) + (2,4) < (6,4) ? ∞∞ + 5 + 5 ≮ ∞≮ ∞

22 66 55 (6,2) + (2,5) < (6,5) ?(6,2) + (2,5) < (6,5) ? ∞∞ + 2 + 2 ≮ ∞≮ ∞ 22 66 66 (6,2) + (2,6) < (6,6) ?(6,2) + (2,6) < (6,6) ? ∞∞ + + ∞∞ ≮ ∞≮ ∞

2007 Kutztown University 48

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 1010 55 ∞∞

DD 77 55 1010 1010 11 22

EE 44 22 55 11 44 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

Matrix After 6Matrix After 6thth Middle Loop Middle Loop {0 cells changed}{0 cells changed}

2007 Kutztown University 49

Third Outer LoopThird Outer Loop

i = 3i = 3 j = j = ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6 Step by step details given hereStep by step details given here

2007 Kutztown University 50

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 ∞∞

BB 22 44 55 55 22 ∞∞

CC 33 55 66 1010 55 ∞∞

DD 77 55 1010 1010 11 22

EE 44 22 55 11 44 44

ZZ ∞∞ ∞∞ ∞∞ 22 44 ∞∞

Matrix After 3Matrix After 3rdrd Outer Loop Outer Loop {0 cells changed}{0 cells changed}

2007 Kutztown University 51

Fourth Outer LoopFourth Outer Loop

i = 4i = 4 j = j = ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6 Step by step details given hereStep by step details given here

2007 Kutztown University 52

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 77 44 99

BB 22 44 55 55 22 77

CC 33 55 66 1010 55 1212

DD 77 55 1010 1010 11 22

EE 44 22 55 11 22 33

ZZ 99 77 1212 22 33 44

Matrix After 4Matrix After 4thth Outer Loop Outer Loop {10 cells changed}{10 cells changed}

2007 Kutztown University 53

Fifth Outer LoopFifth Outer Loop

i = 5i = 5 j = j = ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6 Step by step details given hereStep by step details given here

2007 Kutztown University 54

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 55 44 77

BB 22 44 55 33 22 55

CC 33 55 66 66 55 88

DD 55 33 66 22 11 22

EE 44 22 55 11 22 33

ZZ 77 55 88 22 33 44

Matrix After 5Matrix After 5thth Outer Loop Outer Loop {12 cells changed}{12 cells changed}

2007 Kutztown University 55

Sixth Outer LoopSixth Outer Loop

i = 6i = 6 j = j = ≔≔ 1 to 6 1 to 6 k k ≔≔ 1 to 6 1 to 6 Step by step details given hereStep by step details given here

2007 Kutztown University 56

VtxVtx AA BB CC DD EE ZZ

AA 44 22 33 55 44 77

BB 22 44 55 33 22 55

CC 33 55 66 66 55 88

DD 55 33 66 22 11 22

EE 44 22 55 11 22 33

ZZ 77 55 88 22 33 44

Matrix After 6Matrix After 6thth Outer Loop Outer Loop {0 cells changed}{0 cells changed}

2007 Kutztown University 57

VtxVtx AA BB CC DD EE ZZ

AA -- 22 33 55 44 77

BB 22 -- 55 33 22 55

CC 33 55 -- 66 55 88

DD 55 33 66 -- 11 22

EE 44 22 55 11 -- 33

ZZ 77 55 88 22 33 --

Final MatrixFinal MatrixDiagonal values are removedDiagonal values are removed

2007 Kutztown University 58

Path Costs from APath Costs from A Given by row entriesGiven by row entries A A B :: 2 B :: 2 A A C :: 3 C :: 3 A A D :: 5 D :: 5 A A E :: 4 E :: 4 A A Z :: 7 Z :: 7

2007 Kutztown University 59

Path Costs from BPath Costs from B Given by row entriesGiven by row entries B B A :: 2 A :: 2 B B C :: 5 C :: 5 B B D :: 3 D :: 3 B B E :: 2 E :: 2 B B Z :: 5 Z :: 5

2007 Kutztown University 60

Path Costs from CPath Costs from C Given by row entriesGiven by row entries C C A :: 3 A :: 3 C C B :: 5 B :: 5 C C D :: 6 D :: 6 C C E :: 5 E :: 5 C C Z :: 8 Z :: 8

2007 Kutztown University 61

Path Costs from DPath Costs from D Given by row entriesGiven by row entries D D A :: 5 A :: 5 D D B :: 3 B :: 3 D D C :: 6 C :: 6 D D E :: 1 E :: 1 D D Z :: 2 Z :: 2

2007 Kutztown University 62

Path Costs from EPath Costs from E Given by row entriesGiven by row entries E E A :: 4 A :: 4 E E B :: 2 B :: 2 E E C :: 5 C :: 5 E E D :: 1 D :: 1 E E Z :: 3 Z :: 3

2007 Kutztown University 63

Path Costs from ZPath Costs from Z Given by row entriesGiven by row entries Z Z A :: 7 A :: 7 Z Z B :: 5 B :: 5 Z Z C :: 8 C :: 8 Z Z D :: 2 D :: 2 Z Z E :: 3 E :: 3

2007 Kutztown University 64

Shortest PathsShortest Paths

Floyd’s Algorithm gives the path costsFloyd’s Algorithm gives the path costs Finding the actual paths reuqires Finding the actual paths reuqires

additional workadditional work We will need two matricesWe will need two matrices

One to hold travel costsOne to hold travel costs Other to hold actual pathsOther to hold actual paths

top related