Week 15 – Friday.  What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
Homework #3 Due Thursday, April 17 Problems: –Chapter 11: 11.6, –Chapter 12: 12.1, 12.2, 12.3, 12.4, 12.5, 12.7.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
CS4432: Database Systems II
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Multi-way Trees. M-way trees So far we have discussed binary trees only. In this lecture, we go over another type of tree called m- way trees or trees.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
COSC 2007 Data Structures II Chapter 15 External Methods.
12.1 Chapter 12: Indexing and Hashing Spring 2009 Sections , , Problems , 12.7, 12.8, 12.13, 12.15,
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Starting at Binary Trees
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Internal and External Sorting External Searching
Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
Week 14 - Wednesday.  What did we talk about last time?  Heapsort  Timsort  Counting sort  Radix sort.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Multiway Search Trees Data may not fit into main memory
B+-Trees.
Week 15 – Friday CS221.
B+-Trees.
B+-Trees.
B+ Tree.
Week 11 - Friday CS221.
Week 12 - Wednesday CS221.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
i206: Lecture 14: Heaps, Graphs intro.
B- Trees D. Frey with apologies to Tom Anastasio
CMSC 202 Trees.
Heaps By JJ Shepherd.
B-Trees.
Week 13 - Wednesday CS221.
Presentation transcript:

Week 15 – Friday

 What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees

 Given an N x N chess board, where N ≥ 4 it is possible to place N queens on the board so that none of them are able to attack each other in a given move  Write a method that, given a value of N, will return the total number of ways that the N queens can be placed

Graphic Violence

 Edges  Nodes  Types  Undirected  Directed  Multigraphs  Weighted  Colored  Triangle inequality

 Depth First Search  Cycle detection  Connectivity  Breadth First Search

 Start with two sets, S and V:  S has the starting node in it  V has everything else 1. Set the distance to all nodes in V to ∞ 2. Find the node u in V with the smallest d(u) 3. For every neighbor v of u in V a)If d(v) > d(u) + d(u,v) b)Set d(v) = d(u) + d(u,v) 4. Move u from V to S 5. If V is not empty, go back to Step 2

 Start with two sets, S and V:  S has the starting node in it  V has everything else 1. Find the node u in V that is closest to any node in S 2. Put the edge to u into the MST 3. Move u from V to S 4. If V is not empty, go back to Step 1

 Have no odd cycles  Are easy to do maximum matching on:  Find a maximal matching  Add augmenting paths until you can’t anymore

 An Euler path visits all edges exactly once  An Euler tour is an Euler path that starts and ends on the same node  If a graph only has an Euler path, exactly 2 nodes have odd degree  If a graph has an Euler tour, all nodes have even degree  Otherwise, the graph has no Euler tour or path

 A tour that visits every node exactly once is called a Hamiltonian tour  Finding the shortest Hamiltonian tour is called the Traveling Salesman Problem  Both problems are NP-complete (well, actually NP-hard)  NP-complete problems are believed to have no polynomial time algorithm

 For a tree in secondary storage  Each read of a block from disk storage is slow  We want to get a whole node at once  Each node will give us information about lots of child nodes  We don’t have to make many decisions to get to the node we want

 A B-tree of order m has the following properties: 1. The root has at least two subtrees unless it is a leaf 2. Each nonroot and each nonleaf node holds k keys and k + 1 pointers to subtrees where m/2 ≤ k ≤ m 3. Each leaf node holds k keys where m/2 ≤ k ≤ m 4. All leaves are on the same level

 Go down the leaf where the value should go  If the node is full  Break it into two half full nodes  Put the median value in the parent  If the parent is full, break it in half, etc.  Otherwise, insert it where it goes  Deletes are the opposite process  When a node goes below half full, merge it with its neighbor

 B*-tree  Shares values between two neighboring leaves until they are both full  Then, splits two nodes into three  Maintains better space utilization  B + -tree  Keeps (copies of) all keys in the leaves  Has a linked list that joins all leaves together for fast sequential access

 We do n rounds  For round i, assume that the elements 0 through i – 1 are sorted  Take element i and move it up the list of already sorted elements until you find the spot where it fits  O(n 2 ) in the worst case  O(n) in the best case  Adaptive and the fastest way to sort 10 numbers or fewer

 Take a list of numbers, and divide it in half, then, recursively:  Merge sort each half  After each half has been sorted, merge them together in order  O(n log n) best and worst case time  Not in-place

 Make the array have the heap property: 1. Let i be the index of the parent of the last two nodes 2. Bubble the value at index i down if needed 3. Decrement i 4. If i is not less than zero, go to Step 2 1. Let pos be the index of the last element in the array 2. Swap index 0 with index pos 3. Bubble down index 0 4. Decrement pos 5. If pos is greater than zero, go to Step 2  O(n log n) best and worst case time  In-place

1. Pick a pivot 2. Partition the array into a left half smaller than the pivot and a right half bigger than the pivot 3. Recursively, quicksort the left part (items smaller than the pivot) 4. Recursively quicksort the right part (items larger than the pivot  O(n 2 ) worst case time but O(n log n) best case and average case  In-place

 Make an array with enough elements to hold every possible value in your range of values  If you need 1 – 100, make an array with length 100  Sweep through your original list of numbers, when you see a particular value, increment the corresponding index in the value array  To get your final sorted list, sweep through your value array and, for every entry with value k > 0, print its index k times  Runs in O(n + |Values|) time

 We can “generalize” counting sort somewhat  Instead of looking at the value as a whole, we can look at individual digits (or even individual characters)  For decimal numbers, we would only need 10 buckets (0 – 9)  First, we bucket everything based on the least significant digits, then the second least, etc.  Runs in O(nk) time, where k is the number of digits we have to examine

 Timsort is a hybrid sort, combining elements of merge sort and insertion sort  Features  Worst case and average case running time: O(n log n)  Best case running time: O(n)  Stable  Adaptive  Not in-place  Find "runs" of data that are either non-decreasing or strictly decreasing  These runs are already sorted (or only need a reversal)  If runs are not as long as a minimum run length determined by the algorithm, the next few values are added in and sorted  Merge the sorted runs together

 A maximum heap is a complete binary tree where  The left and right children of the root have key values less than the root  The left and right subtrees are also maximum heaps

 Easy!  We look at the location of the new node in binary  Ignore the first 1, then each 0 is for going left, each 1 is for going right  This approach guarantees we get the first open spot in the complete tree

 Location: 6  In binary: 110  Right then left

 Oh no!

 Heaps only have:  Add  Remove Largest  Get Largest  Which cost:  Add:O(log n)  Remove Largest:O(log n)  Get Largest:O(1)  Heaps are a perfect data structure for a priority queue

 We can implement a heap with a (dynamic) array  The left child of element i is at 2i + 1  The right child of element i is at 2i

 We can use a (non-binary) tree to record strings implicitly where each link corresponds to the next letter in the string  Let’s store:  10  102  103   305   09

public class Trie { private static class TrieNode { public char letter; public boolean terminal; public TrieNode[] children; } private TrieNode root; public Trie() { root = new TrieNode(); root.letter = ' '; root.terminal = false; root.children = null; }

 There is no next time!

 Project 4 is due tonight!  Final Exam  7:30am - 10:30am, Monday, 12/08/2014 (CS221 A)  11:00am - 2:00pm, Monday, 12/08/2014 (CS221 B)