Data structures algorithms in Java ECE 242 Exam





































- Slides: 37
Data structures & algorithms in Java ECE 242 – Exam Review By Daniel Gomez-Prado Sept 2012
Outline • Analysis of complexity o Q 1 Fall 2011 problems • • • Classes, objects and containers Array Stack Queue (Single) Linked and Double Linked List Iterators Linear and Binary search Merge sort Quick sort Q 2 – Q 6, Fall 2011 problems ECE 242 Review class 2
Analysis big-Oh Random access memory Memory Your program Import java. util. * class review { Static public main() { // this is a review // for ECE 242 exam } } • Assumptions: o Unlimited memory We have what we need: 1 Gb or 1, 000 Tb o All memory accesses takes 1 unit time ECE 242 Review class No hierarchical memory, Cache, L 1, L 2, hard drive 3
Analysis big-Oh Running time PROGRAM OPERATIONS STEPS int sum = 0; 1 assignment 1 for (i=0; i<128; i=++) i =1, 2, 3, 4 … 128 j = 128, 64, 32, 16, 8, 4, 2, 1 log 2128+1 1 addition, 1 assignment 2 for (j = 128; j>0; j=j/2) sum = sum + a[i][j]; 1+128*(log 2128+1)*2 n could be the size of the stack, queue, list or the dimension of a matrix, etc. In general we have an arbitrary number “n” instead of 128, in that case: 1+n*(log 2 n+1)*2 1+2 n+2 n*log 2 n ECE 242 Review class can we simplify the expression? YES!, By using big-Oh notation we can specify the asymptotic complexity of the algorithm 4
Analysis big-Oh Definition • Given functions f(n) and g(n): o f(n) is said to be O(g(n)) o if and only if • there are (exist) 2 positive constants, C>0 and N>0 o such that • f(n) ≤ Cg(n) ECE 242 Review class for every n>N 5
Analysis big-Oh Example of definition usage keywords • f(n) is given g(n) is given Relationship between C & n ECE 242 Review class O(n*log 2 n) is true for C=3 and n≥ 32 6
Analysis big-Oh Q 1 exam 1 Fall 2011 State the asymptotic complexity of: big-Oh i. Print out middle element of an array of size n arrays allow access to any position randomly recall Your program Memory Solution is: o All memory accesses takes 1 unit time ECE 242 Review class O(1) 7
Analysis big-Oh Q 1 exam 1 Fall 2011 ii. Print out the middle element of a linked list of size n recall a linked list head tail next object next n/2 memory locations object f(n) = n/2 Solution is: the asymptotic complexity is O(n) ECE 242 Review class 8
Analysis big-Oh Q 1 exam 1 Fall 2011 iii. Print out the odd elements of an array of size n f(n) = n/2 Solution: the asymptotic complexity is O(n) iv. Pop 10 elements from a stack that is implemented with an array. Assume that the stacks contains n elements and n > 10. When in doubt, ASK! is n = 11 or is n > 1000 ? f(n) = 10 Solution: the asymptotic complexity is O(1) ECE 242 Review class 9
Classes and objects • The goal of a “class” (in object-oriented language) o Encapsulate state and behavior • A class is a blueprint that has o o o a constructor to initialize its data members a destructor to tear down the object A coherent interface to interact with the object (public methods) Private methods unreachable from the outside The possibility to extend and inherit members from other classes • An object is an instant of a class • What are the benefits: o Through inheritance, extensions, packages allows to structure a program o Exposes behavior that could be reused o Alleviates the problem of understanding somebody else code ECE 242 Review class 10
ADT (Abstract Data Type) • ADTs are containers • ADTs are primarily concern in: o Aggregation of data o Access of data o Efficiency of memory is used o Efficiency of the container access ECE 242 Review class 11
Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 int size 1023 o Object. Y[] object_y_array = new Object. Y[512]; 1, 934, 218 Object. X 0 … ECE 242 Review class Object. Y size 512 Objects. Y fixed boundary Java takes care of the memory management for you. 511 0 Object. Z … 12
Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 is. Empty true 1023 is. Empty false peek pop push pop status index peek 0 ECE 242 Review class 0 push 0 13
… Stacks • Enforce a LIFO behavior (last in, first out) Object. Z o It is based on an array o It overrides the random access of an array by a LIFO access 0 1023 Recall what a class encapsulates o Status & o Behavior 1024 is. Empty false pop Does it mean we are always safe o index = -1, stack is empty, good o index = 1024, o refuse to push objects o overflow, runtime exception ECE 242 Review class peek index 0 -1 14
Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 is. Empty true 1023 is. Empty false status index 1 enqueue peek index 2 dequeue 0 ECE 242 Review class 0 enqueue 0 dequeue 15
Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 Recall what a class encapsulates o Status & o Behavior is. Empty false status index 1 peek Does it mean we are always safe o index 1 = index 2, stack is empty, good o index 1 or index 2 = 1024, o rewind to 0 o test condition o Increment using mod 1024 Review class o. ECE 242 What if index 2 > index 1 > index 2 dequeue 0 dequeue 16
Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 status index 2 > > index 2 dequeue index 1 peek ECE 242 Review class index 1 peek 0 0 dequeue 17
Is everything an Array? can we use something else? beginning • Recall an array o Contiguous memory o Fixed bound size o Random access Object. X Object. Y 0 N-1 0 … Object. Z … fixed boundary How do we know in this container? o the beginning, the end o which element is next • Let’s use another construct o Non contiguous memory o Unlimited size o Sequential access only end Object. Y Object. X Object. Z … … head edges next prev next head Node object ECE 242 Review class 18
Linked List • Use the prior construct (node and edge) head next … object push pop peek object next object ECE 242 Review class 19
Double linked List • Use the prior construct (node and edge) head prev next prev object ECE 242 Review class next … prev object next prev object 20
Quick Questions Can we do: • a linked list or double linked list from an array o Yes • a queue with nodes and edges o Why not. • a stack with nodes and edges o Sure ECE 242 Review class 21
Iterators encapsulate container traversals • we have two implementations of a stack 1023 pop peek push pop prev next peek 0 1 2 3 4 head index push ECE 242 Review class prev next prev 4 next prev 3 next prev 2 next prev 1 next 22
Iterators encapsulate container traversals • we have two implementations of a stack Traverse container behavior according to container rules 1023 peek 0 1 2 3 4 5 state increment by 1 decrement by 1 index push ECE 242 Review class update next update prev head prev next prev 5 next prev 4 next prev 3 next prev 2 next prev 1 next 23
Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u a x z b n m l j i b c u worst case all elements are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process a b b c i Looking for u ECE 242 Review class j l n m u x z worst case there are log(n)+1 element visited. O(log(n)) 24
So binary search is faster but the assumption is… • The container is already order, so o how do we sort a container o how do insert elements in a sorted container o How do we remove elements in a sorted container • What is more expensive (big-Oh) o A linear search o Order a container and then a binary search o Maintain a container sorted and then a binary search ECE 242 Review class 25
Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 DIVIDE Wait, what? 24 85 45 24 63 85 45 CONQUER 24 85 85 24 19 37 91 56 RECUR 63 45 45 63 take the min 242445 ECE 242 Review class 24 85 85 45 63 63 26
Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 2 f(n/2) log 2(n) 4 f(n/4) 85 24 63 45 85 24 O(x+y) ECE 242 Review class 19 37 91 56 63 45 24 85 x elements 45 63 y elements 27
Sorting a container Merge sort • O(nlogn) ECE 242 Review class 28
Sorting a container Merge sort • Drawback of merge sort algorithm o The merge is not in place take the min Additional memory 24 45 85 63 • The merge could be modified to be in place, but the overhead will slow down the running time. ECE 242 Review class 29
Sorting a container Quick sort • Use the divide and conquer approach o Divide the problem into 3 subsets (unless you have a base case) • A (random) pivot x • A subset with numbers lower than x • A subset with numbers greater than x o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 19 37 91 56 45 24 37 19 24 37 ECE 242 Review class 85 63 91 56 In place sorting, it does not require additional memory 30
Sorting a container Quick sort • Complexity o Quick sort (with random pivot) is O(nlogn) • Drawback o No replicated element allowed in the container * The pivot is randomly chosen, * if an element occurs twice, in different divisions * then the merging mechanism won’t work ECE 242 Review class 31
Arrays Q 2 exam 1 Fall 2011 Accept 2 integer Arrays: A and B. And find the number of common elements in both assuming no duplicates in each array. o Brute force O(nm) o Merge-sort modified A, n elements B, m elements C, n+m elements Instead of merging compare and increment count when equal O( (n+m)log(n+m) ) ECE 242 Review class 32
Stacks and Queues Q 3 exam 1 Fall 2011 a) Write a reverse. Queue method using only stacks and queues Queue FIFO ECE 242 Review class a b c d e f g h out Stack LIFO in h g f e d c b a in in O(n) out Queue FIFO 33
Stacks and Queues Q 3 exam 1 Fall 2011 b) Write a method cut. Queue that adds an element to the head of the queue using only stacks and queues O(n) in ECE 242 Review class out in N a b c d e f g h Queue FIFO in h g f e d c b a N out N a b c d e f g h in a b c d e f g h N Stack LIFO Queue FIFO out 34
List Q 5 exam 1 Fall 2011 a) Write a method is. Sorted. Asc to check if a single linked list is sorted in non-decreasing order head next … next while ( node. next ) { if (node. next. key < node. key) return false; node = node. next; } return true; ECE 242 Review class 35
List Q 5 exam 1 Fall 2011 b) Write a method put. In. Place to insert preserving the nondecreasing order of the single linked list head next ECE 242 Review class next … if (!is. Sorted. Asc()) return false; while ( node. next ) { if ( !(new_key > node. next. key) ) { SNode new_node = new SNode(new_key, node. next); node. next = new_node; return true; } node = node. next; } SNode new_node = new SNode(new_key, NIL); node. next = new_node; next 36
List Q 6 exam 1 Fall 2011 Write a method compress to remove duplicated elements in the single linked list head next … next while ( node. next ) { if ( node. key == node. next. key) ) { node. next = node. next; } else { node = node. next; } } ECE 242 Review class 37