Title: Mid Exam Review
1Mid Exam Review
- ECE573 Data Structures and Algorithms
- Electrical and Computer Engineering Dept.
- Rutgers University
- http//www.cs.rutgers.edu/vchinni/dsa/
2Data Structures Algorithms
- The key to your professional reputation
- A much more dramatic effect can be made on the
performance of a program by changing to a better
algorithm than by - hacking
- converting to assembler
- Data structures enable efficient implementation
of algorithms
3Introduction
- What is Data?
- I guess some useful information
- What is Structured Data? Or Data Structures?
- Why? How? Relations, orderliness, easy to
process - Ex Tables, Trees, Stacks, Lists, Queues
- What is an algorithms? Why do we need them?
- To manipulate data (the structured one?)
- Performance? (Why not assembly level coding?)
Data Structures and Algorithms
4Data Collections ADT
- Most algorithms operate on data collections, so
define - Collection Abstract Data Type (ADT)
- Methods
- Constructor / Destructor
- Add / Delete
- Find
- Sort
- .
5What is a Good Program?
- Run correctly
- Run in accordance with the specifications
- Run efficiently
- In minimum time?
- Other constraints Memory use?
- How? Use appropriate data structures and
algorithms - Algorithm A precisely specified procedure for
solving a problem - Easy to read and understand
- Easy to debug
- Easy to modify (easy to maintain)
- Portability?
Focus
Software Engineering Building large SW
systems Reliable easy to maintain
6Analyzing an Algorithm
- Simple statement sequence
- s1 s2 . sk
- O(1) as long as k is constant
- Simple loops
- for(i0iltni) s
- where s is O(1)
- Time complexity is n O(1) or O(n)
- Nested loops
- for(i0iltni) for(j0jltnj) s
- Complexity is n O(n) or O(n2)
7Analyzing an Algorithm
- Loop index depends on outer loop index
- for(j0jltnj)
- for(k0kltjk) s
- Inner loop executed
- 1, 2, 3, ., n times
- Complexity O(n2)
n
In the big picture, it did not matter!
8Array Implementations
- Constructor
- Specify maximum size in Constructor
- Allocate array with sufficient space
- Add method
- Takes constant time
- Independent of current size of collection
- Find method
- Linear search
- Worst case time n x constant
- Average time n/2 x constant
(if the key is always found!)
è n x constant
(depending on probability of
finding the key) - Worst case analysis preferred - easier and more
relevant!
9Find method Binary search
- Sort the data into ascending (or descending)
order - Check the middle item
- If desired key is less, go left
- If match, finished!
- If desired key is greater, go right
-
- You can divide n in half log2n times
- Therefore binary search takes time c log2n
- In big Oh, this is O( log n )
n
10Example Binary Search vs. Sequential Search
- Find method
- Sequential Worst case time c1 n
- Binary search Worst case time c2 log2n
Small problems - were not interested!
Large problems - were interested in this gap!
Binary search More complex Higher constant factor
11Linked Lists
- Dynamic allocation gives flexibility
- Use as much or as little space as needed
- Addition - constant time
- Searching - proportional to n
- Deletion - proportional to n
- Variants
- LIFO (simplest)
- FIFO (add tail pointer)
- Doubly linked Scan in either direction
- Circularly linked Scan entire list from any point
12A Comparison
s size of the array/list n lest length
Same space requirements as linked list
13Special Matrices
- Diagonal Matrix
- Memory efficient representation dn
- Tridiagonal Matrix
- Main diagonal (Ij)
- Diagonal below main diagonal (Ij1)
- Diagonal above main diagonal (I j-1)
- Triangular Matrices
- Need one dimensional array of size n(n1)/2
- Need to have a mapping
- Symmetric Matrices
- Need only n(n1)/2 array Store either the lower
or upper triangle of the matrix
14Sparse Matrices
- mXn matrix is sparse if many of its elements are
zero (not dense) - Diagonal and Tridiagonal matrices have sufficient
structure in their nonzero regions ? simple
representation scheme - Now irregularity and unstructured nonzero region
- Array representation
0 0 0 2 0 0 1 0 0 6 0 0 7 0 0 3 0
0 0 9 0 8 0 0 0 4 5 0 0 0 0 0
15Linked Representation
col link
col link
row a.first
1
4
2
7
1
0
value
value
2
2
6
5
7
8
3
0
3
4
9
6
8
0
4
0
2
4
3
5
0
16Stacks
- Stacks
- Just a collection with LIFO semantics
- Add, Delete usually named push, pop
- Recursion
- Stack frame to store function context
- Permits recursive calls
- Basis for some simple, elegant and efficient
solutions! - but, sometimes too simple
- Fibonacci - elegant but not efficient
17Searching
- Searching
- O(logn) search time Binary search -
- but addition is expensive
- Trees
- Recursive Data Structure
- Sub-trees are themselves trees
- Searched recursively
- Search time proportional to height, O(logn)
18Performance Comparison
Arrays Simple, fast Inflexible O(1) O(n) inc
sort O(n) O(n) O(logn) binary search
Add Delete Find
Linked List Simple Flexible O(1) sort -gt no
adv O(1) - any O(n) - specific O(n) (no bin
search)
Trees Still Simple Flexible ? ? O(log n)
19Sorting Algorithms
- The Sorting Repertoire
- Insertion O(n2) Guaranteed
- Bubble O(n2) Guaranteed
- Heap O(n log n) Guaranteed
- Quick O(n log n) Most of the time!
O(n2) - Bin O(n) Keys in small
range O(nm) - Radix O(n) Bounded
keys/duplicates O(nlog n) - Bin and Radix sort
- Distinguished by ability to
- map the key to a small integer or
- calculate an address from the key
- ? O(n log n)
Pathological case
20O(n) Sorting
- Radix Sort
- Any suitable radix can be used
- Different bases can be used in each phase
- Memory management must be efficient to achieve
O(n)
21What is covered from text books?
- Cormen
- Chapters 1, 2, 3, 4.1, 6, 7, 8, 10.1-2, 12, 28.1
- Goodrich
- ?
22Exam Format
- Theory Questions Similar to HWs
- Identify bugs/errors in programming
- C constructs
- Algorithm development
- Performance complexity analysis
- O, W, Q notation
- Common data structures Arrays, Matrices, Linked
lists, stacks, queues (priority?), heap, tree - Searching, Sorting (insertion, bubble, quicksort,
bin sort, radix..) - Addition, deletion, tree traversal
23Exam Format
- True/False, Short Questions
- Lengthy Algorithm/analysis questions