Discrete Math 2 Shortest Path Using Matrix
-
Upload
vanna-wilkinson -
Category
Documents
-
view
32 -
download
3
description
Transcript of Discrete Math 2 Shortest Path Using Matrix
Discrete Math 2Discrete Math 2Shortest Path Using MatrixShortest Path Using Matrix
CIS112CIS112February 10, 2007February 10, 2007
2007 Kutztown University 2
OverviewOverview
Previously:Previously: In weighted graph . .In weighted graph . . Shortest path from one vertex to anotherShortest path from one vertex to another Search tree methodSearch tree method
Now:Now: Same problemSame problem Matrix methodMatrix method
2007 Kutztown University 3
StrategyStrategy
Represent weighted graph as matrixRepresent weighted graph as matrix Create search matrix . .Create search matrix . . with entries matching with entries matching
nodes expansionnodes expansion node productionnode production
2007 Kutztown University 4
Matrix for Weighted GraphMatrix for Weighted Graphvtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11 2323 88 55
22 2323 1717 1616
33 88 1919 1515
44 55 1919 1111
55 1717 1515 2424 2020
66 1616 1515 1414 2121
77 1515 2525 1515
88 1111 2424 2525 99 2323
99 2020 1414 2222
1010 1515 99 66
1111 2323 2222 66 55
1212 2121 55
2007 Kutztown University 5
Step #0Step #0
Create a search matrixCreate a search matrix Layout same as weighted graph matrixLayout same as weighted graph matrix Entries will hold path informationEntries will hold path information
Vertices along pathVertices along pathTotal cost of pathTotal cost of path
Path info built up step by stepPath info built up step by step
2007 Kutztown University 6
Search MatrixSearch Matrixvtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33
44
55
66
77
88
99
1010
1111
1212
2007 Kutztown University 7
Step #1Step #1
Enter information for first path Enter information for first path segmentsegment
Initial entry goes in row #7 . .Initial entry goes in row #7 . . Since #7 is starting vertexSince #7 is starting vertex
2007 Kutztown University 8
Search Matrix – Step #1Search Matrix – Step #1vtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33
44
55
66
77 1515 2525 1515
88
99
1010
1111
1212
2007 Kutztown University 9
CommentComment
Notice similarity to search treeNotice similarity to search tree Choose lowest cost entryChoose lowest cost entry
Which is in column #3Which is in column #3 Corresponds to expanding lowest cost Corresponds to expanding lowest cost
nodenode Next entry goes in row #3Next entry goes in row #3
2007 Kutztown University 10
Search Matrix – Step #2Search Matrix – Step #2vtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33 2323 3434
44
55
66
77 1515 2525 1515
88
99
1010
1111
1212
2007 Kutztown University 11
CommentComment Entries represent path costEntries represent path cost
15+8=2315+8=23 15+19=3415+19=34
Path elements stored implicitlyPath elements stored implicitly Look in row #7 (starting point)Look in row #7 (starting point) Find lowest cost entry (column #3)Find lowest cost entry (column #3) In corresponding rowIn corresponding row
» Find lowest cost entry (column #1)Find lowest cost entry (column #1)
» Path is: 7 Path is: 7 1 1
Again, note similarity to search treeAgain, note similarity to search tree
2007 Kutztown University 12
Step #3Step #3 Among all leaves . .Among all leaves . . Find lowest cost entry [7,10]Find lowest cost entry [7,10] ““Expand” “node” #10Expand” “node” #10 I.e., compute path cost + edge costI.e., compute path cost + edge cost Enter into matrixEnter into matrix
2007 Kutztown University 13
Search Matrix – Step #3Search Matrix – Step #3vtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33 2323 3434
44
55
66
77 1515 2525 1515
88
99
1010 2424 2121
1111
1212
2007 Kutztown University 14
CommentComment No entry is made for starting point – #7No entry is made for starting point – #7 There are two entries in column #8There are two entries in column #8
Correspondence to search treeCorrespondence to search tree Two nodes for #8Two nodes for #8
So we mark higher one as deletedSo we mark higher one as deleted
2007 Kutztown University 15
Search Matrix – Step #3bSearch Matrix – Step #3bvtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33 2323 3434
44
55
66
77 1515 2525 1515
88
99
1010 2424 2121
1111
1212
2007 Kutztown University 16
Continuing . .Continuing . .
Keep choosing “nodes”Keep choosing “nodes” Keep “expanding” themKeep “expanding” them Until there are no more to “expand”Until there are no more to “expand”
2007 Kutztown University 17
Search Matrix – Step #4Search Matrix – Step #4vtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33 2323 3434
44
55
66
77 1515 2525 1515
88
99
1010 2424 2121
1111 4444 4343 2626
1212
2007 Kutztown University 18
Search Matrix – Step #4bSearch Matrix – Step #4bvtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33 2323 3434
44
55
66
77 1515 2525 1515
88
99
1010 2424 2121
1111 4444 4343 2626
1212
2007 Kutztown University 19
CommentComment Reached #12, but not yet finishedReached #12, but not yet finished Other less costly path possibleOther less costly path possible But not through #4 & #9But not through #4 & #9
Why not?Why not? So mark #4 & #9 as dead endsSo mark #4 & #9 as dead ends
2007 Kutztown University 20
Search Matrix – Step #4cSearch Matrix – Step #4cvtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11
22
33 2323 34x
44
55
66
77 1515 2525 1515
88
99
1010 2424 2121
1111 4444 43x 2626
1212
2007 Kutztown University 21
Step #5Step #5
Now expand #1 . .Now expand #1 . . Marking dead ends, if they occurMarking dead ends, if they occur
2007 Kutztown University 22
Search Matrix – Step #5Search Matrix – Step #5vtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11 46x 28x
22
33 2323 34x
44
55
66
77 1515 2525 1515
88
99
1010 2424 2121
1111 4444 43x 2626
1212
2007 Kutztown University 23
CommentComment All entries in row #1 are dead endsAll entries in row #1 are dead ends So the column is also a dead endSo the column is also a dead end Then the same happens . . Then the same happens . .
with row #3with row #3and column #3and column #3
2007 Kutztown University 24
Search Matrix – Step #5bSearch Matrix – Step #5bvtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11 46x 28x
22
33 23x 34x
44
55
66
77 15x 2525 1515
88
99
1010 2424 2121
1111 4444 43x 2626
1212
2007 Kutztown University 25
Step #6Step #6
And expand #8 . .And expand #8 . . Marking dead ends, if they occurMarking dead ends, if they occur
2007 Kutztown University 26
Search Matrix – Step #6Search Matrix – Step #6vtxvtx 11 22 33 44 55 66 77 88 99 1010 1111 1212
11 46x 28x
22
33 23x 34x
44
55
66
77 15x 2525 1515
88 3535 48x 4747
99
1010 24x 2121
1111 4444 43x 2626
1212
2007 Kutztown University 27
CommentComment We are finished . .We are finished . . since there are no more nodes to since there are no more nodes to
expandexpand We already have the path cost = 26We already have the path cost = 26 How do we get the path?How do we get the path?
2007 Kutztown University 28
To Get the PathTo Get the Path Start at row 7Start at row 7 Column 10 has an entry . .Column 10 has an entry . .
giving us 7 giving us 7 10 10 Go to row 10Go to row 10 Column has 11 an entry . .Column has 11 an entry . .
giving us 7 giving us 7 10 10 11 11 Go to row 11Go to row 11 Column has 12 an entry . .Column has 12 an entry . .
giving us 7 giving us 7 10 10 11 11 12 12
2007 Kutztown University 29
Shortest Path from 7 to 12Shortest Path from 7 to 12
7 7 10 10 11 11 12 12 :::: 26 26
2007 Kutztown University 30
Final CommentsFinal Comments
We see how the 1-1 search tree can be We see how the 1-1 search tree can be implemented as a matriximplemented as a matrix
We look next at how the 1-many can We look next at how the 1-many can alsoalso