Test 3 Review Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.

Slides:



Advertisements
Similar presentations
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Advanced Data Structures
CS 171: Introduction to Computer Science II
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
BST Data Structure A BST node contains: A BST contains
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CS 206 Introduction to Computer Science II 04 / 29 / 2009 Instructor: Michael Eckmann.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Fundamentals of Python: From First Programs Through Data Structures
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Important Problem Types and Fundamental Data Structures
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
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 ),
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Review and Prepare for Test 3 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
Heap Sorting and Building
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
 Saturday, April 20, 8:30-11:00am in B9201  Similar in style to written midterm exam  May include (a little) coding on paper  About 1.5 times as long.
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Trees Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Binary Trees Priority Queues, and Heaps CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Final Exam Review CS 3358.
CSE373: Data Structures & Algorithms Priority Queues
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Trees Chapter 15.
Top 50 Data Structures Interview Questions
Week 11 - Friday CS221.
Cse 373 April 26th – Exam Review.
i206: Lecture 13: Recursion, continued Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
More on Merge Sort CS 244 This presentation is not given in class
CSE 326: Data Structures: Midterm Review
Final Exam Review COP4530.
Review and Prepare for Test 2
Important Problem Types and Fundamental Data Structures
Data Structures and Algorithms CS 244
Presentation transcript:

Test 3 Review Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Test Layout Same as previous 5 to 10 true/false 5 to 10 multiple choice 10 to 20 short answer or fill in the blank some coding/algorithm design lots of problem solving using data structures and algorithms 100 points maximum but more than 100 points possible currently looking like 105, maybe 110 select your own “bonus”

Test Topics Technically test 3 is not cumulative HOWEVER Everything builds

Unit 1 Background Stuff C++ UML Big-Oh Misc Other

Unit 2 Background Stuff Know how each of the following works, common functions, relation of implementation and Big Oh Arrays (static and dynamic) Linked Lists (singly linked and doubly linked) Stacks Queues Deques Misc Other

Example Summary: Stacks Stacks can be implemented using different data structures Array Fixed- Size Array Expandable (doubling strategy) Singly Linked List Pop() O(1) Push(o)O(1)O(1) Average CaseO(1) Top()O(1) Size(), isEmpty() O(1) Seems all the same… what’s the real difference between them if not in run-times? Fixed Arrays maximum number of items can be pushed Expandable/Dynamic Arrays push is O(1) only via amortized time analysis, so sometimes it takes longer, but on average is O(1) Singly Linked list no maximum, but have to manage memory

Example Summary: Queues Queues can be implemented using different data structures Seems all the same… what’s the real difference between them if not in run-times? Queue Ops Array Fixed- Size Array Expandable (doubling strategy) List Singly- Linked dequeue() O(1) enqueue(o)O(1)O(1) Average CaseO(1) front()O(1) size(), isEmpty() O(1) Fixed Arrays maximum number of items can be enqueued Expandable/Dynamic Arrays enqueue is O(1) only via amortized time analysis, so sometimes it takes longer, but on average is O(1) Singly Linked list no maximum, but have to manage memory

Example Summary: Deque Deques can be implemented using different data structures Array Fixed- Size Array Expandable (doubling strategy) List Singly- Linked List Doubly- Linked removeFirst() O(1) removeLast()O(1) O(n)O(1) insertFirst(o), InsertLast(o) O(1)O(1) Average CaseO(1) first(), lastO(1) size(), isEmpty() O(1) ALMOST all the same… what’s the real difference between them if not in run-times? Fixed Arrays maximum number of items can be inserted Expandable/Dynamic Arrays insert is O(1) only via amortized time analysis, so sometimes it takes longer, but on average is O(1) Singly and Doubly Linked list no maximum, but have to manage memory

Book’s Vector Summary This is not necessarily same as std::vector, It is to show the possible consequences of using a vector (dynamic array) and possible another reason for amortized analysis Array Fixed-Size or Dynamic RemoveAtRank(r), InsertAtRank(r,o) O(n) Average Case elemAtRank(r), ReplaceAtRank(r,o) O(1) Size(), isEmpty()O(1) NOTE: Vectors generalize arrays

Generalizing Things We have Vectors and Lists We can make other things from them Stacks, Queues, Deques Can we generalize/merge vectors and lists into ONE data type? So stacks, queues, and stuff made out of ONE data type and the details fall into the implementation of that data type

Sequences Sequence ADT is the union of vectors and lists Elements are accessed by rank OR position

Sequences Sequence ADT is the union of vectors and lists Elements are accessed by rank OR position OperationArrayList size, isEmpty 11 atRank, rankOf, elemAtRank 1n first, last, before, after 11 replaceElement, swapElements 11 replaceAtRank 1n insertAtRank, removeAtRank nn insertFirst, insertLast 11 insertAfter, insertBefore n1 remove n1 Generic Methods

Sequences Sequence ADT is the union of vectors and lists Elements are accessed by rank OR position OperationArrayList size, isEmpty 11 atRank, rankOf, elemAtRank 1n first, last, before, after 11 replaceElement, swapElements 11 replaceAtRank 1n insertAtRank, removeAtRank nn insertFirst, insertLast 11 insertAfter, insertBefore n1 remove n1 Vector based methods

Sequences Sequence ADT is the union of vectors and lists Elements are accessed by rank OR position OperationArrayList size, isEmpty 11 atRank, rankOf, elemAtRank 1n first, last, before, after 11 replaceElement, swapElements 11 replaceAtRank 1n insertAtRank, removeAtRank nn insertFirst, insertLast 11 insertAfter, insertBefore n1 remove n1 List based methods

Sequences Sequence ADT is the union of vectors and lists Elements are accessed by rank OR position OperationArrayList size, isEmpty 11 atRank, rankOf, elemAtRank 1n first, last, before, after 11 replaceElement, swapElements 11 replaceAtRank 1n insertAtRank, removeAtRank nn insertFirst, insertLast 11 insertAfter, insertBefore n1 remove n1 Bridge methods

Other Data Types in the Background Standard Template Library stuff using std namespace strings, vectors, etc

Other ADT things Understand and be able to read (use if given) the ADTs of Iterator Position Locator Comparator etc

Early Sorting Algorithms AlgorithmTimeNotes Selection SortO(n 2 )Slow, in-place For small data sets Insertion SortO(n 2 )Slow, in-place For small data sets Bubble SortO(n 2 )Slow, in-place For small data sets Heap SortO(nlog n)Fast, in-place For large data sets Merge SortO(nlogn)Fast, sequential data access For huge data sets All three seem the same, BUT The Best Case for Insertion and Bubble Sort turns out to be O(n)… average and worst case is O(n^2) Yet the best case for selection sort remains O(n^2) Later classes may emphasize these types of differences in greater depth

AlgorithmTimeNotes Merge SortO(n lg n)Fast, sequential data access For huge data sets Then Came Merge Sort Visualizing Merge Sort used a Tree-Like structure 7 2  9 4   2  2 79  4   72  29  94  4 Its runtime is O(n lg n) Proving that relied heavily on the fact the number of times N can be divided by two is roughly lg(N) The algorithm itself is fast, uses sequential data access and works well on huge data sets

Then Quick Sort Given an array of n elements (e.g., integers): If array only contains one element, return Else pick one element to use as pivot. Partition elements into two sub-arrays: Elements less than or equal to pivot Elements greater than pivot Quicksort two sub-arrays Return results recursive calls

Quick Sort: Details There were scenarios quicksort did not do so well in See lecture DS19_aQuickSort for more information In sum though: AlgorithmTimeNotes Quick SortO(nlogn)Assumes key values are random and uniformly distributed

Next Were Trees General Trees and Binary Trees And Traversal methods

Tree Traversal Notes Pre-Order, In-Order, Post-Order All “visit” each node in a tree exactly once Pretend the tree is an overhead view of a wall Maybe the nodes are towers (doesn’t matter) We want to walk all the way around the wall structure We begin above the root node, facing towards it We then place our left hand against the node and walk all the way around the wall structure so that our left hand never leaves the wall    

Tree Traversal Notes Pre-Order, In-Order, Post-Order All “visit” each node in a tree exactly once Pretend the tree is an overhead view of a wall Maybe the nodes are towers (doesn’t matter) We want to walk all the way around the wall structure We begin above the root node, facing towards it We then place our left hand against the node and walk all the way around the wall structure so that our left hand never leaves the wall    

Tree Traversal Notes Pre-Order, In-Order, Post-Order All “visit” each node in a tree exactly once Pretend the tree is an overhead view of a wall Maybe the nodes are towers (doesn’t matter) We want to walk all the way around the wall structure We begin above the root node, facing towards it We then place our left hand against the node and walk all the way around the wall structure so that our left hand never leaves the wall    

Tree Traversal Notes Pre-Order, In-Order, Post-Order All “visit” each node in a tree exactly once Pretend the tree is an overhead view of a wall Maybe the nodes are towers (doesn’t matter) We want to walk all the way around the wall structure We begin above the root node, facing towards it We then place our left hand against the node and walk all the way around the wall structure so that our left hand never leaves the wall    

Generalizing We will now encounter each node 3 times Once on the ‘left’ side with respect to the paper Once on the ‘bottom’ side with respect to the paper Once on the right side with respect to the paper    L 

Generalizing Pre-Order, In-Order, Post-Order All “visit” each node in a tree exactly once We will now encounter each node 3 times Once on the ‘left’ side with respect to the paper Once on the ‘bottom’ side with respect to the paper Once on the right side with respect to the paper    L B 

Generalizing We will now encounter each node 3 times Once on the ‘left’ side with respect to the paper Once on the ‘bottom’ side with respect to the paper Once on the ‘right’ side with respect to the paper    L B R 

Euler Tour This is an Euler Tour Generic traversal of a binary tree Includes as special cases the pre-order, post-order and in-order traversals Walk around the tree and visit each node three times: on the left (pre-order) from below (in-order) on the right (post-order)    L B R 

Euler Tour Traversal Algorithm eulerTour(p) left-visit-action(p) if isInternal(p) eulerTour(p.left()) bottom-visit-action(p) if isInternal(p) eulerTour(p.right()) right-visit-action(p) End Algorithm    L B R  Notice leaf nodes all three visits happen one right after the other (effectively all at the same “time” )

Tree Traversal Applications Be able to apply tree traversal methods to solve various types of problems Most likely Binary Trees and their traversal will be important

End “in the background” In sum Remember the stuff that came before May have to use it

Unit 3 Began with trees, binary trees, tree traversals Next was Priority Queues (PQs) Two types Min and Max Heavy emphasis on PQ Sorting Insert Items Remove Items in priority order Good example of How the choice of data structure can influence the runtime of an algorithm

Priority Queue Sort Summary PQ-Sort consists of n insertions followed by n removeItem operations InsertRemovePQ-Sort Total Insertion Sort (ordered sequence) O(n)O(1)O(n 2 ) Selection Sort (unordered sequence) O(1)O(n)O(n 2 ) Heap Sort (binary heap, vector- based implementation) O(logn) O(n logn) Of course to do a Heap Sort we presented Heaps first…

Heaps Two major properties Structure = complete binary tree Order property = key value of each node is at least as small/large as the key of its children So also two types: Min and Max Look like Binary Trees Usually implemented using vectors Building top-down: O(n lg n) time Removing items Sorting “in-place” with a single array Reheap up and Reheap down support functions Building bottom-up faster than building top-down: O(n) time but sort still runs in O(n lg n) time i Parent  (i-1)/2  2i+1 Left Child 2i+2 Right Child

Application of Data Structures We side tracked a little to see an application of trees and priority queue sorting in action Huffman Encoding

Huffman’s Algorithm The idea: Create a “Huffman Tree” that will tell us a good binary representation for each character Left means 0 Right means 1 Example 'b‘ is 10 More frequent characters will be higher in the tree (have a shorter binary value). To build this tree, we must do a few steps first Count occurrences of each unique character in the file to compress Use a priority queue to order them from least to most frequent Make a tree and use it

Huffman Compression – Overview Step 1 Count characters (frequency of characters in the message) Step 2 Create a Priority Queue Step 3 Build a Huffman Tree Step 4 Traverse the Tree to find the Character to Binary Mapping Step 5 Use the mapping to encode the message

Huffman Decompression via Tree Apply the Prefix Property No encoding A is the prefix of another encoding B Never will have x  011 and y  the Algorithm Read each bit one at a time from the input If the bit is 0 go left in the tree Else if the bit is 1 go right If you reach a leaf node output the character at that leaf and go back to the tree root

Sorting Summary Sorting discussions (sort of) ended here and we moved onto searching Sorting Summary (some sorts may be omitted) check previous lectures, homework, book for others AlgorithmTimeNotes Selection SortO(n 2 )Slow, in-place For small data sets Insertion SortO(n 2 )Slow, in-place For small data sets Bubble SortO(n 2 )Slow, in-place For small data sets Heap SortO(n lg n)Fast, in-place For large data sets Merge SortO(n lg n)Fast, sequential data access For huge data sets Quick SortO(n lg n)Assumes key values are random and uniformly distributed

Searching Begins Binary Search Trees (BSTs) adding nodes deleting nodes searching tree for value Example find: F D H F G C A E B D F G E

Nicely Built Matters Height Balanced BSTs Basic idea of AVL Tree An AVL Tree, or height-balanced tree, is a binary search tree such that The heights of the left and right subtrees of the root differ by at most 1 The left and right subtrees of the root are AVL trees

AVL Trees Data structure of nodes includes balance factor Inserting Nodes Deleting Nodes Rotations used to maintain balance

Decision Trees Application of binary trees Relates to BSTs and AVL trees

More ADTs Maps and Dictionaries List of keys with data associated to the key The main difference from a map is that dictionaries allow multiple items with the same key Again ordered versus unordered sequence implementations Linear Search versus Binary Search Example: Direct Address Tables Problem: Needs too much memory if large number of possible keys

Hash Tables Improves Direct Address Table idea Uses a hash function, h: U-> {0, …, m-1} that maps keys to positions in the hash table Benefit: Reduces memory needed The size of the table is m, not the size of the universe of keys

Hash Functions Typically use mod functions with prime numbers Problem: Collisions

Resolving Collisions Chaining Open Addressing Linear Probing Quadratic Probing Double hashing

Last Major Topic: Graphs Graph Theory Definitions needed so we can discuss implementation Graphs are vertices connected with edges Trees are a type of graph (with no cycles) Graph Representation Adjacency Matrices Adjacency Lists Pulls together lots of stuff vectors, lists, UML, C++ classes,

Breadth First Traversal Example Algorithm to traverse a graph Typically the traversal of nodes is listed as they are discovered and marked as visited per the algorithm For this class can assume adjacent nodes of a given vertex are listed in alphabetical or numerically increasing order (unless specified otherwise) A, B, C… or 1, 2, 3 order If given a tree a Breadth First Traversal typically equates to reading the nodes in each level of the tree starting at level zero and going from left node to right node (typewriter style) May also want to look up Depth First Traversal for contrast DFS was not shown in class

The End of This Presentation End ANY QUESTIONS ??? Is this thing on?