1.26k likes | 1.38k Vues
Mid-term Answers & Tree traversal. 10/15/2014. How to Measure Efficiency?. Critical resources: Time, memory, programmer effort, user effort Factors affecting running time: For most algorithms, running time depends on “ size ” of the input .
E N D
Mid-term Answers & Tree traversal 10/15/2014
How to Measure Efficiency? • Critical resources: • Time, memory, programmer effort, user effort • Factors affecting running time: • For most algorithms, running time depends on “size” of the input. • Running timeis expressed as T(n)for some function T on input size n. 4
How do we analyze an algorithm? • Need to define objective measures. (1)Compare execution times? Not good: times are specific to a particular machine. (2)Count the number of statements? Not good: number of statements varies with programming language and style.
How do we analyze an algorithm? (cont.) (3) Express running time T as a function of problem size n (i.e., T=f(n)) Asymptotic Algorithm Analysis • Given two algorithms having running times f(n) and g(n),find which functions grows faster? • Compare “rates of growth”of f(n) and g(n). • Such an analysis is independent of machine time, programming style, etc.
Understanding Rate of Growth (cont’d) • The low order terms of a function are relatively insignificant for largen n4 + 100n2 + 10n + 50 Approximation: n4 • Highest order termdetermines rate of growth!
Names of Orders of Magnitude O(1) bounded (by a constant) time O(log2N) logarithmic time O(N) linear time O(N*log2N) N*log2N time O(N2) quadratic time O(N3)cubic time O(2N ) exponential time
Visualizing Orders of Growth • On a graph, as you go to the right, a faster growing function eventually becomes larger...
Complexity • Let us assume two algorithms A and B that solve the same class of problems. • The time complexity of A is 5,000n, T = f(n) = 5000*n • the one for B is 2nfor an input with n elements, T= g(n) = 2n • For n = 10, • A requires 5*104steps, • but B only 1024, • so B seems to be superior to A. • For n = 1000, • A requires 5*106steps, • while B requires 1.07*10301steps.
Running Time Examples (cont.’d) if (i<j) for ( i=0; i<N; i++ ) X = X+i; else X=0; O(N) O(1) Running time of the entire if-else statement? Max (O(N), O(1)) = O(N)
Running Time Examples i = 0; while (i<N) { X=X+Y; // O(1) result = mystery(X); // O(N) i++; // O(1) } • The body of the while loop: O(N) • Loop is executed: N times Running time of the entire iteration? N x O(N) = O(N2)
17 Main Index Contents C++ Arrays An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory. intarr[] = {1,2,3,4,5}; cout << arr[4];
Allow for dynamic resizing Have a way to store the size internally Allow for assignment of one object to another 18 Main Index Contents Vectors • A container is a class that stores a collection of data • It has operations that allow a programmer to insert, • erase, and update elements in the collection
C++ interview questions on “Vector” What do vectors represent?a) Static arraysb) Dynamic arraysc) Stackd) Queue Answer: bExplanation: Vectors are sequence containers representing arrays that can change in size. More questions here
Inserting into a List Container 22 Main Index Contents The List Container
25 Main Index Contents Map Containers A map is a storage structure that implements a key-value relationship.
1 2 3 4 5 1 2 3 4 5
Stacks • A stack is a sequence of items that are accessible at only one end of the sequence. Last in, first out, (LIFO)
Pushing/Popping a Stack • Because a pop removes the item that last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.
Queues A queue inserts new elements at the back and removes elements from the front of the sequence. Pop Push Queue: First in, first out, (FIFO)
33 Main Index Contents The Queue A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front.
Example Question F B G A D I C E H Pre-order? In-order? Post-order? Level-order ?
Computing the Leaf Count Pre-order scan
Computing the Depth of a Tree Post-order scan
Deleting Tree Nodes Post-order scan
Outline of In-Order Traversal • Three principle steps: • Traverse Left • Do work (Current) • Traverse Right • Work can be anything • Separate work from traversal
Traverse the tree “In order”: • Visit the tree’s left sub-tree • Visit the current and do work • Visit right sub-tree
In-Order Traversal Procedure procedure In_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform in-order traversal, call // Do_Something for each node // Preconditions: cur points to a binary tree // Postcondition: Do_Something on each tree // node in “in-order” order • if( cur <> NIL ) then • In_Order( cur^.left_child ) • Do_Something( cur^.data ) • In_Order( cur^.right_child ) • endif endprocedure // In_Order
22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R
22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R
22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L