310 likes | 436 Views
Discrete Math 2 Shortest Path Using Matrix. CIS112 February 10, 2007. Overview. Previously: In weighted graph . . Shortest path from one vertex to another Search tree method Now: Same problem Matrix method. Strategy. Represent weighted graph as matrix Create search matrix . .
E N D
Discrete Math 2Shortest Path Using Matrix CIS112 February 10, 2007
Overview • Previously: • In weighted graph . . • Shortest path from one vertex to another • Search tree method • Now: • Same problem • Matrix method Kutztown University
Strategy • Represent weighted graph as matrix • Create search matrix . . • with entries matching • nodes expansion • node production Kutztown University
Matrix for Weighted Graph Kutztown University
Step #0 • Create a search matrix • Layout same as weighted graph matrix • Entries will hold path information • Vertices along path • Total cost of path • Path info built up step by step Kutztown University
Search Matrix Kutztown University
Step #1 • Enter information for first path segment • Initial entry goes in row #7 . . • Since #7 is starting vertex Kutztown University
Search Matrix – Step #1 Kutztown University
Comment • Notice similarity to search tree • Choose lowest cost entry • Which is in column #3 • Corresponds to expanding lowest cost node • Next entry goes in row #3 Kutztown University
Search Matrix – Step #2 Kutztown University
Comment • Entries represent path cost • 15+8=23 • 15+19=34 • Path elements stored implicitly • Look in row #7 (starting point) • Find lowest cost entry (column #3) • In corresponding row • Find lowest cost entry (column #1) • Path is: 7 1 • Again, note similarity to search tree Kutztown University
Step #3 • Among all leaves . . • Find lowest cost entry [7,10] • “Expand” “node” #10 • I.e., compute path cost + edge cost • Enter into matrix Kutztown University
Search Matrix – Step #3 Kutztown University
Comment • No entry is made for starting point – #7 • There are two entries in column #8 • Correspondence to search tree • Two nodes for #8 • So we mark higher one as deleted Kutztown University
Search Matrix – Step #3b Kutztown University
Continuing . . • Keep choosing “nodes” • Keep “expanding” them • Until there are no more to “expand” Kutztown University
Search Matrix – Step #4 Kutztown University
Search Matrix – Step #4b Kutztown University
Comment • Reached #12, but not yet finished • Other less costly path possible • But not through #4 & #9 • Why not? • So mark #4 & #9 as dead ends Kutztown University
Search Matrix – Step #4c Kutztown University
Step #5 • Now expand #1 . . • Marking dead ends, if they occur Kutztown University
Search Matrix – Step #5 Kutztown University
Comment • All entries in row #1 are dead ends • So the column is also a dead end • Then the same happens . . • with row #3 • and column #3 Kutztown University
Search Matrix – Step #5b Kutztown University
Step #6 • And expand #8 . . • Marking dead ends, if they occur Kutztown University
Search Matrix – Step #6 Kutztown University
Comment • We are finished . . • since there are no more nodes to expand • We already have the path cost = 26 • How do we get the path? Kutztown University
To Get the Path • Start at row 7 • Column 10 has an entry . . • giving us 7 10 • Go to row 10 • Column has 11 an entry . . • giving us 7 10 11 • Go to row 11 • Column has 12 an entry . . • giving us 7 10 11 12 Kutztown University
Shortest Path from 7 to 12 • 7 10 11 12 :: 26 Kutztown University
Final Comments • We see how the 1-1 search tree can be implemented as a matrix • We look next at how the 1-many can also Kutztown University