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.
This commit is contained in:
William Carroll 2020-03-20 16:49:49 +00:00
parent 117f0be7c2
commit ae9e83f5d7
2 changed files with 184 additions and 1 deletions

View file

@ -25,7 +25,7 @@
** DONE Binary Search Tree Checker
** DONE 2nd Largest Item in a Binary Search Tree
** DONE Graph Coloring
** TODO MeshMessage
** DONE MeshMessage
** TODO Find Repeat, Space Edition BEAST MODE
* Dynamic programming and recursion
** TODO Recursive String Permutations