670 likes | 844 Views
CSC 213 – Large Scale Programming. Lecture 41: Semester Review. Final Exam. Tues., May 10 from 10:15 – 12:15 in OM 200 Plan on exam taking full 2 hours If major problem , come talk to me ASAP Exam covers material from entire semester Open-book & open-note so bring what you’ve got
E N D
CSC 213 – Large Scale Programming Lecture 41:Semester Review
Final Exam • Tues., May 10 from 10:15 – 12:15 in OM 200 • Plan on exam taking full 2 hours • If major problem, come talk to me ASAP • Exam covers material from entire semester • Open-book & open-note so bring what you’ve got • My handouts, solutions, & computers are not allowed • Cannot collaborate with a neighbor on the exam • Problems will be in a similar style to 2 midterms • Lab mastery: 2:45 – 3:45 on Thurs., May 12 in OM119
Critical Property of Test All goodtests FAIL
Loop Testing: Simple Loops • Loop executed at most n times, try inputs that: • Skip loop entirely • Make 1 pass through the loop • Make 2 passes through the loop • Make m passes through the loop, where (m < n) • If possible, n-1,n,&(n+1)passesthrough the loop
Indexed File Format • Split information into two (or more) files • Data file uses fixed-size records to store data • Index files contain search terms & location record starts • Fixed-size records usually used in data file • Each record will use exactly that much space • Extra space wasted if the value is smaller • But limits data size, cannot get more space • Makes it far easier to reuse space & rebuild index
Entry ADT • Needs 2 pieces: what we have & what we want • First part is the key: data used in search • Item we want is value; the second part of an Entry • Implementations must define 2 methods • key()& value()return appropriate item • Usually includes setValue()but NOTsetKey()
What is a Map? • At simplest level, Map is collection of Entrys • key-value pairs serve as the basic data in a Map • size() & isEmpty()work at level of Entry • Searchable data stored using Maps • put() adds an Entry so key is mapped to the value • get() retrieves valueassociated with key from Map • remove()deletes entire Entry • At most one value per key using a Map
Dictionary ADT • Dictionary ADT very similar to Map • Hold searchable data in each of these ADTs • Both data structures are collections of Entrys • Convert key to valueusing either concept • Dictionary can have multiple values to one key • 1 value for key is still legal option
Dictionary ADT • Dictionary ADT very similar to Map • Hold searchable data in each of these ADTs • Both data structures are collections of Entrys • Convert key to valueusing either concept • Dictionary can have multiple values to one key • 1 value for key is still legal option “pantsless”
Dictionary ADT • Dictionary ADT very similar to Map • Hold searchable data in each of these ADTs • Both data structures are collections of Entrys • Convert key to valueusing either concept • Dictionary can have multiple values to one key • 1 value for key is still legal option “pantsless” • Also many Entryswith same key but different value “cool” “cool”
Using Hash Properly • Normally, table holds one Entry per index • Need to be smarter when keys collide • Efficiency mattersimportantcritical • If we do not care, use List-based approach • Several common schemes used to provide speed • Each form of probing has strengths & weaknesses • Must consider bad hash effects before using • If this O(n) time unacceptable, use other leafy plant
Using Hash Properly • Normally, table holds one Entry per index • Need to be smarter when keys collide • Efficiency mattersimportantcritical • If we do not care, use List-based approach • Several common schemes used to provide speed • Each form of probing has strengths & weaknesses • Must consider bad hash effects before using • If this O(n) time unacceptable, use other leafy plant
Binary Search Trees • Implements a BinaryTree for searching • Map or Dictionary will be ADT exposed • Data organized to make usage efficient (maybe) • Strict ordering maintained in tree • Nodes to theleft are smaller • Larger keys in right child of node • Equal values not specified • No problem, just be consistent 6 2 9 10 1 4 6
BST Performance • Search, insert, & remove take O(h) time • h is height of tree • Height’s best case is complete tree at O(log n) • O(n) height for linked list is BST’s worst case
AVL Tree Definition • Fancy type of BST • O(logn) time provided • For this, needs more info 6 2 9 1 4 8 5
AVL Tree Definition • Fancy type of BST • O(logn) time provided • For this, needs more info 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
AVL Tree Definition • Fancy type of BST • O(logn) time provided • For this, needs more info • Keep tree balanced by… • Checking heights of kids • Only let differ by 0 or 1 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
AVL Tree Definition • Fancy type of BST • O(logn) time provided • For this, needs more info • Keep tree balanced by… • Checking heights of kids • Only let differ by 0 or 1 • Fix larger differences by • Shifts nodes in the BST • For balance maintainence Trinode Restructuring 4 6 3 2 2 9 1 2 1 1 4 8 1 5 Node heights are shown in blue
Building a SplayTree • Another approach which builds upon BST • Not anAVLTree, however, but a new BST subclass
Concept Behind SplayTree • Splay trees do NOT maintain balance • Recently used nodes clustered near top of BST • Most recently accessed nodes take O(1) time • Other nodes may need O(n) time to access • Usually very efficient, but provides no guarantees
Red-Black Tree • Root Property:Root node painted black • External Property: Leaves are painted black • Internal Property: Red nodes’ children are black • Depth Property: Leaves have identical blackdepth • Number of blackancestorsfor the node 9 4 15 21 2 6 12 7
Merge Sort Execution Tree • Show steps used to sort all of the data 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 11 3 6 8 72 2 7 944 9 38 3 8 611 6 77 22 99 44 33 88 66 11
Quick Sort • Divide: Partition by pivot • L has values <= p • G uses values >= p • Recur: Sort L and G • Conquer: Merge L, p, G p p G L p
Quick Sort v. Merge Sort Quick Sort Merge Sort • Divide data around pivot • Want pivot to be near middle • All comparisons occur here • Conquer with recursion • Does not need extra space • Merge usually done already • Data already sorted! • Divide data in blindly half • Always gets even split • No comparisons performed! • Conquer with recursion • Needs* to use other arrays • Merge combines solutions • Compares from (sorted) halves
Bucket & Radix Sort • Sort data written as tuple of enumerable data • Consumption of wine overall, in liters • Annual per capita consumption of liters • Sort one place in tupleusing bucket sort • Uses 1 bucket per value that could be enumerated • When there are ties, preserve relative ordering • Repeat stable sorts to perform radix sort • Must preserve relative ordering, like bucket sort • From least to most important sort each tuple place
Radix-Sort In Action • List of 4-bit integers sorted using Radix-sort 1001 0010 1001 1001 0001 0010 1110 1101 0001 0010 1101 1001 0001 0010 1001 0001 1101 0010 1101 1101 0001 1110 1110 1110 1110
Lower Bound on Sorting • Smallest number of comparisonsis tree’s height • Decision tree sorting n elements has n! leaves • At least log(n!) height needed for this many leaves • As we saw, this simplifies to at most O(n log n) height • O(n log n) time needed to compare data! • Practical lower bound, but cheating can do better • Need enumerable tuples- cannot always cheat • “If you believe radix hypothesis” it takes O(n) time
Graph Applications • Electronic circuits • Transportation networks • Databases • Packing suitcases • Finding terrorists • Scheduling college’s exams • Assigning classes to rooms • Garbage collection • Coloring countries on a map • Playing minesweeper
Edge List Structure u • Simplest Graph • Space efficient • No change to use withdirected or undirected a c b d v w z
Edge List Structure u • Simplest Graph • Space efficient • No change to use withdirected or undirected • Fields • Sequenceof vertices a c b d v w z vertices z w v u
Edge List Structure u • Simplest Graph • Space efficient • No change to use withdirected or undirected • Fields • Sequenceof vertices • Sequenceof edges a c b d v w z vertices z w v u edges c a b d
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex v b a u u w w
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base v b a u u w w vertices w u v a b edges
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base • Extends Vertex v b a u u w w vertices w u v a b edges
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base • Extends Vertex • Add Position reference to speed removal v b a u u w w vertices w u v a b edges
Adjacency Matrix Structure v b a • Edge-List structurestill used as base u w vertices 0 1 2 w v u a b edges
Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix u w vertices 0 1 2 w v u a b edges
Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix • Adjacency matrix in Graph class u w vertices 0 1 2 w v u a b edges
Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix • Adjacency matrix in Graph class • nullif not adjacent u w vertices 0 1 2 w v u a b edges
Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix • Adjacency matrix in Graph class • nullif not adjacent -or- • Edgeincidentto both vertices u w vertices 0 1 2 w v u a b edges
Adjacency Matrix Structure v b a • Undirected edgesstored in both array locations u w vertices 0 1 2 w v u a b edges
Adjacency Matrix Structure v b a • Undirected edgesstored in both array locations • Directed edgesonly in array from source to target u w vertices 0 1 2 w v u a b edges