Download presentation
Presentation is loading. Please wait.
Published byAnn Thomas Modified over 9 years ago
1
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review
2
Announcements Final Exam (comprehensive) Friday Dec 11 7:00PM-9:00PM WHL007 Course Evaluations http://www.udel.edu/course-evals
3
Topics Pointers/Memory Management Arrays Linked Lists Analysis/ Big O notation Stacks Queues Trees Sorting Heaps Graphs Hashing NP-completeness
4
NP-Completeness A class of problems having two properties: Any given solution to the problem can be verified quickly (in polynomial time) – the set of problems with this property is called NP (nondeterministic polynomial time). If the problem can be solved quickly (in polynomial time), then so can every problem in NP.
6
Pointers An address refers to a particular memory location. In other words, it points to a memory location. Pointer: A variable that contains the address of a variable. z 2342... 101 102 103 104 105... x y Location (address) name 104
7
Pointers How to initialize a pointer: – can initialize to NULL (i.e. not currently pointing to anything) – & operator: get address of a variable int *x; x?y? int y = 3; x?y3 x = &y; xy3
8
Pointers How to get the value that is pointed to? – * “ dereference operator”: get value pointed to * x returns 3 How to change the variable pointed to? – Use dereference * operator to left of = xy5 *x = 5 ; xy3
9
Memory Allocation Two ways: –On the stack –On the heap
10
Memory Allocation (Stack) int main(void) { int x(5); if (x > 3) { int y(6); cout << (x + y) << endl; }
11
Memory Allocation (Heap) int main(void) { int *x = new int(5); if (*x > 3) { int *y = new int(6); cout << (*x + *y) << endl; }
12
Memory De-allocation (Heap) int main(void) { int *x = new int(5); if (*x > 3) { int *y = new int(6); cout << (*x + *y) << endl; delete y; } delete x; }
13
Array Allocation (Heap) int main(void) { int *x = new int[5]; *x = 5; if (*x > 3) { int *y = new int(6); cout << (*x + *y) << endl; delete y; } delete [] x; }
14
Arrays void ArrayTest() { int scores[100]; // operate on the elements of the // scores array... scores[0] = 1; scores[1] = 2; scores[2] = 3; }
16
Abstract Data Types (ADTs) Combination of data and operations Encapsulates implementation details Provides an interface for usage
17
Collection add(x) remove(index) member(x) size() first()
18
Single Linked List
19
Creating a Link
20
Efficiency of Algorithms An operation for an Absract Data Type can be thought of as a “problem” An algorithm solves the “problem” –A series of steps –Each step has a cost Time Space –Efficiency is a measurement of this cost
21
Example 2.14/2.16
22
Big-O notation Describes the relationship between input size and execution time If we double the number of inputs, n, and the execution time is approximately doubled –Linear growth rate –Growth rate has an order of n –O(n)
23
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25
24
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } n 2 executions
25
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } n executions, 5 statements per = 5n
26
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25 1 execution, 25 statements = 25
27
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25 T(n) = total execution time as a function of n T(n) = n 2 + 5n + 25
28
Formal Big-O Definition T(n) = n 2 + 5n + 25 T(n) = O(f(n)) means that there exists a function, f(n), that for sufficiently large n and some constant c: cf(n) T(n)
29
n 2 + 25n + 25 vs. 3n 2
30
Common Big-O Runtimes Constant -- O(1) Logarithmic -- O(log n) Fractional -- O(sqrt n) Linear -- O(n) Log-Linear-- O(n log n) Quadratic -- O(n 2 ) Cubic -- O(n 3 ) Exponential -- O(2 n ) Factorial -- O(n!)
31
Various Runtimes
32
Calculating Big-O 1.T(n) = 6n 4 − 2n 3 + 5 2.T(n) = 9 log n + 5 (log n) 3 + 3n 2 + 2n 3 3.T(n) = 3n 2 + O(log n) + O(n) + O(n log n)
33
Calculating Big-O 4.Binary Search on an array: T(n) = T(n/2) + O(1) 5.T(1) = 2 T(n) = 2T(n - 1) + O(1) for n>1 6.T(1) = 1 T(n) = 2T(n/2) + O(1) for n>1
34
Stacks
35
Stack ADT peek() - returns the top element of the stack without removing it pop() - removes (and returns) the top element of the stack push(x) - pushes x onto the top of the stack LIFO structure - last in, first out
36
Queue
37
Queue ADT front() - returns the front element of the queue without removing it dequeue() - removes (and returns) the front element of the queue queue(x) - queues x at the end of the queue FIFO structure - first in, first out
38
Priority Queue ADT front() - returns the highest priority element of the queue without removing it dequeue() - removes (and returns) the highest priority element of the queue queue(x, int) - queues x at the given priority in the queue FIFO structure??
39
Trees Nonlinear data structure
40
Tree Terminology root, leaf parent, child, sibling subtree external, internal node ancestor, descendant depth, height
41
Binary Trees Each node has 0, 1, or 2 children
42
Traversal Exercise Find the: preorder inorder postorder level-order
43
Ordered Binary Tree (Example)
44
Inserting to an Ordered Binary Tree
45
Removing from an Ordered Binary Tree
46
Worst Case Scenario?
48
AVL Balanced Tree (automatic)
49
Balancing a Binary Tree
54
Sorting Bubble Sort: K+W 577-580 Selection Sort: K+W 572-576 Insertion Sort: K+W 581-585 (Self-Balancing) Binary Tree Sort: http://en.wikipedia.org/wiki/Binary tree sort Quick Sort: K+W 604-613 Merge Sort: K+W 592-598 MSD Radix Sort: http://en.wikipedia.org/wiki/Radix sort
55
Sorting Analysis NameWorst CaseNotes Bubble SortO(n 2 ) Selection SortO(n 2 ) Insertion SortO(n 2 ) Quick SortO(n 2 )O(n log n) average Binary Tree SortO(n log n) Merge SortO(n log n) Radix SortO(n k) k is number of bits required to represent data Counting SortO(n + r) r is the range of possible data values. Used in radix sort.
56
Binary Max Heap
57
Heap Insert (example) 2 1 3
58
Heap Delete (example) 12
59
Binary Heap Operation Running Times OperationBinary findMaxO(1) deleteMaxO(log n) insertO(log n) decreaseKeyO(log n) mergeO(n)
60
Binary Heap Implementations Array
61
Graph Representations How do we represent a graph? A B E D C
62
List Structures V = {A, B, C, D, E} E = {{A, B}, {A, D}, {C, E}, {D, E}} Incidence List –E = {{A, B}, {A, D}, {C, E}, {D, E}} Adjacency List –L = [A={B, D}, B={A}, C={E}, D={A, E}, E={C, D}] A B E D C
63
Matrix Structures V = {A, B, C, D, E} E = {{A, B}, {A, D}, {C, E}, {D, E}} Adjacency Matrix A B E D C ABCDE A11010 B11000 C00101 D10011 E00111
64
Minimum Spanning Tree Example
73
Hash Tables Goal: access item given its key (not its position) –we wish to avoid much searching Hash tables provide this capability –Constant time in the average case!O(1) –Linear time in the worst caseO(n) Searching an array: O(n) Searching BST: O(log n)
74
How could we resolve collisions? Goal is to still be able to insert, delete, and search based on key 0 1 101 Smith Hall Newark, DE n-1 Array table Hash function mod(10000) 302-831-2712 302-737-2712 ?
75
Performance of Hash Tables (2)
76
Performance of Hash Tables (3) Hash table: –Insert: average O(1) –Search: average O(1) Sorted array: –Insert: average O(n) –Search: average O(log n) Binary Search Tree: –Insert: average O(log n) –Search: average O(log n) But balanced trees can guarantee worst case O(log n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.