Homework 6 Shortest Paths!. The Basic Idea You have seen BFS and DFS tree searches. These are nice, but if we add the concept of “distance”, we can get.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

Data Structures: A Pseudocode Approach with C
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Leftist Heaps Text Read Weiss, §23.1 (Skew Heaps) Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Binary Tree A data structure whose nodes contain two pointer fields. One or both pointers can have the value NULL. Each node in a binary tree can have.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Binary Search Trees Chapter 7 Objectives
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Lecture: Priority Queue. Questions Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Implementing stacks Prof. Ramin Zabih
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Binary Search Trees Chapter 7 Objectives
Heap Chapter 9 Objectives Define and implement heap structures
Data Structures and Design in Java © Rick Mercer
Red-Black Tree Neil Tang 02/04/2010
CSCE 3110 Data Structures & Algorithm Analysis
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Priority Queues and Heaps
Splay Trees Binary search trees.
Binary Search Tree (BST)
Trees.
Programming Abstractions
Lecture 22 Binary Search Trees Chapter 10 of textbook
Hashing Exercises.
Chapter 16 Tree Implementations
Splay Trees Binary search trees.
Chapter 20: Binary Trees.
Chapter 8 – Binary Search Tree
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Rick Mercer, Allison Obourn, Marty Stepp
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 21: Binary Trees.
Binary Heaps Text Binary Heap Building a Binary Heap
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
Dijkstra’s Algorithm We are given a directed weighted graph
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Chapter 16 Tree Implementations
CS Data Structure: Heaps.
Chapter 6: Transform and Conquer
Binary Search Trees.
Advanced Implementation of Tables
Data Structures Lecture 29 Sohail Aslam.
CSE 12 – Basic Data Structures
Amortized Analysis and Heaps Intro
Implementation of Dijkstra’s Algorithm
Binary Search Trees reading: 17.3 – 17.4
Heaps By JJ Shepherd.
Important Problem Types and Fundamental Data Structures
CS 6310 Advanced Data Structure Wei-Shian Wang
Heaps & Multi-way Search Trees
General Tree Concepts Binary Trees
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Homework 6 Shortest Paths!

The Basic Idea 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.

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

Planning the Fastest Subway Ride...

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

BFS versus Shortest Path

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

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: public class TreeNode { //... double distance; //... }

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

Next, modify BFS using the following pseudo-code: 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. }

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

Reading input: 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.

Read Input and Build A Tree 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.

Optional Bonus Feature 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 ) )