1 / 14

Homework 6

Shortest Paths!. Homework 6. You have seen BFS and DFS tree searches. These are nice, but if we add the concept of “distance”, we can get some very useful algorithms. The Basic Idea. In life, there are many situations where you want to find the shortest path between two nodes:.

fjoshua
Download Presentation

Homework 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shortest Paths! Homework 6

  2. You have seen BFS and DFS tree searches. These are nice, but if we add the concept of “distance”, we can get some very useful algorithms. The Basic Idea

  3. In life, there are many situations where you want to find the shortest path between two nodes: Why Should I Care?

  4. Planning the Fastest Subway Ride...

  5. Finding the Winning Series of Moves in a Game...

  6. BFS versus Shortest Path

  7. Implement a “shortest path” search on a tree. Every node will contain a character and a numeric value representing its distance from its parent. Your Assignment

  8. public class TreeNode { //... double distance; //... } First, you should modify the binary tree class so that it can store distances. This can be done simply by adding an extra field in BinaryNode:

  9. Make sure BinaryNode implements Comparable, using distance values to compare nodes. TreeNode should be a generic class. More steps:

  10. findClosest(Node root, Object target) { make new min-heap and insert root with distance 0. while min-heap is not empty { q <- get min element while q not null, and q does not hold target: place q's children into heap, using distance from root as the sort key. } } Next, modify BFSusing the following pseudo-code:

  11. Dijkstra was one of the “giants” who created modern CS. This is a simplified version of Dijkstra's Algorithm

  12. Input will be of the form: a 0 ( b 3 c 7 ( d 1 e 3 ) ) You will read it from right to left, building a binary tree with rightmost nodes in parentheses being the children of the node to their left. For now, you can ignore the left parens. Reading input:

  13. After reading the input and building the tree, your program should search for the node containing the string “*” that has the shortest distance from the root. So, given the following input: a 0 ( b 4 ( * 100 b 6 ) w 9 (x 3 y 5 ( * 2 z 3 ) ) ) Your output should look like: Found "*" at distance 16. Read Input and Build A Tree

  14. Ambitious students have the option of making their tree support arbitrary numbers of children, rather than simply be a binary tree. This would be done by not ignoring the left parentheses when scanning the input text. Example non-binary input: a 0 (b 3 c 8 ( f 3 g 2 ) d 3 ( e 3 ) ) Optional Bonus Feature

More Related