diff options
author | William Carroll <wpcarro@gmail.com> | 2020-03-20T16·49+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-03-20T16·49+0000 |
commit | ae9e83f5d7eb2bf6860ec88785b7216e98f53adf (patch) | |
tree | 7734bc7ffb1877bc05443501469077f6c9463880 /scratch/data_structures_and_algorithms/reverse-words.py | |
parent | 117f0be7c2dee368944263ebe482a397acc7ea55 (diff) |
Solve InterviewCake.com's mesh-message problem
Write a function that returns the shortest path between nodes A and B in an unweighted graph. I know two algorithms for finding the shortest path in a *weighted* graph: - Use a heap as a priority queue instead of the regular queue that you would use when doing a BFT. This is called Dijkstra's algorithm. You can also use Dijkstra's algorithm in an unweight graph by imaginging that all of the weights on the edges are the same value (e.g. 1). - Map the weighted graph into an unweighted graph by inserting N nodes between each node, X and Y, where N is equal to the weight of the edge between X and Y. After you map the weighted graph into an unweighted graph, perform a BFT from A to B. A BFT will always find the shortest path between nodes A and B in an unweighted graph. I had forgotten that a BFT in an unweighted graph will always return the shortest path between two nodes. I learned two things from InterviewCake.com's solution: 1. I remembered that a BFT in an unweighted graph will return the shortest path (if one exists). 2. I learned to use a dictionary to store the edge information and then back-tracking to reconstruct the shortest path.
Diffstat (limited to 'scratch/data_structures_and_algorithms/reverse-words.py')
0 files changed, 0 insertions, 0 deletions