Data Structures and Algorithms

Data Structures and Algorithms

<p> Cho, Joonmyun 18-4-5</p><p>Data Structures and Algorithms</p><p>ALGORITHMS AND EFFICIENCY</p><p>ALGORITMS EFFICIENCY DATA STRUCTURES WITH C++</p><p>STACKS QUEUES TREES</p><p>1/6 Cho, Joonmyun 18-4-5</p><p>Algorithms and Efficiency</p><p>Algoritms</p><p>Efficiency</p><p>In order for you to make an intelligent choice of which algorithm to use in a given situation, you need to have some idea of how the efficiencies of algorithms are calculated. There are three main measures of efficiency that can be applied to most algorithms: the best, worst, average case</p><p>The main point of working out the efficiency of an algorithm is to know how it behaves for large amounts of data; in particular, how fast the amount of work increases as the amount of data increases. linear, logarithmic, … Once you get used to the idea of classifying algorithms by the general way they depend on the amount of data, you will find a shorthand notation for efficiency to be quite handy; linear: O(n), which is read “order n” logarithmic: O(log n) (where n is a quantity that measures the amount of data being processed by the algorithm; n was equal to L when we were discussing list length above, for example)</p><p>Common orders of algorithms 1 Constant: O(1) 2 Logarithmic: O(log n) 3 Linear: O(n) 4 Log-linear: O(n log n) 5 Quadratic: O(n^2)</p><p>There are two types of operations involved in sorting a list of data; comparison and assignment. Since these two operations require different resources in terns of computing, we need to evaluate them separately. Assignment are almost more time-consuming than comparisons. In general, algorithms that minimize the number of assignments are to be preferred.</p><p>We find that there are L(L-1)(L-1)….. = L! ways of ordering the entire list. Now suppose that, with each swap, the number of final ordering of the list that needed to be considered was reduced by a factor 2. If the total number of possible ordering is a power of 2 (say, L! = 2^n), then the number of steps we need to completely sort the list is n  n = ln(L!) ~= L ln L where n = needed steps  efficiency ~= O(L ln L)  maximum efficiency</p><p>2/6 Cho, Joonmyun 18-4-5</p><p>Data Structures with C++</p><p>Stacks</p><p>The Main Features of a Stack  Access is allowed at one end (the top of the stack). Data are added and removed at this end  The action of adding a data element to a stack is called pushing  The action of removing a data element from a stack is called popping  Stacks handle data in a last-in-first-out, or LIFO fashion; the last item pushed onto the stack is always the first item popped off the stack</p><p>Implement push algorithm: 1 if stack is not full then 2 add 1 to the stack pointer 3 store item at stack pointer location pop algorithm: 1 if stack is not empty then 2 read item at stack pointer location I 3 subtract 1 from the stack pointer</p><p>C++ Code class charstack { protected: int StackSize; char* Element; int StackPointer;</p><p> public: BOOL Empty() const; BOOL Full() const; charstack(int StackSize = 10); virtual ~charstack(); virtual BOOL Pop(char& TopElem); virtual BOOL Push(const char& NewElem); }</p><p>3/6 Cho, Joonmyun 18-4-5</p><p>Queues</p><p>Features From the point of view of data structures, a queue is similar to a stack, in that data are stored in a linear fashion, and access to the data is allowed only of the ends of the queue The actions allowed on a queue:  creating an empty queue  testing if a queue is empty  adding data to the tail of the queue  removing data from the head of the queue</p><p>Implement Algorithms for dealing with queues in a circular array</p><p>Creating an empty queue: set head = tail = 0</p><p>Testing if a queue is empty: is head == tail ?</p><p>Testing if a queue is full: is (tail + 1) % QSize == head ?</p><p>Adding an item to a queue: if queue is not full, add item at location tail and set tail = ( tail + 1) % QSize</p><p>Removing an item from a queue: if queue is not empty, remove item from location head and set head = (head + 1) % QSize</p><p>C++ Code class intqueue { protected: int QSize; int* Element; int Head, Tail;</p><p> public: BOOL Empty() const; BOOL Full() const; intqueue(int queuesize = 10); virtual ~intqueue();</p><p>4/6 Cho, Joonmyun 18-4-5</p><p> virtual BOOL Remove(int& TopElem); virtual BOOL Add(const int& NewElem); }</p><p>5/6 Cho, Joonmyun 18-4-5</p><p>Trees</p><p>Features A tree is a datastructure consisting of data nodes connected to each other with pointers. Each node in a tree may be connected to two or more other nodes. Basic types of operation:  inserting a new node  deleting a node  listing or visiting the nodes of the tree</p><p>Implement Inserting the integers into a binary tree: if the current pointer is 0. create a new node, store the data, and return the address of the new node. otherwise, compare the integer to the data stored at the current node if the new integer is less than the integer at current node, insert the new integer into the left child of the current node</p><p>Inorder traversal: if the current pointer is not 0, then traverse the left child of the current pointer visit(or print) the data at the current pointer traverse the right child of the current pointer</p><p>C++ Code Because many of the binary tree functions are recursive, we often need to pass pointers to individual binary tree nodes to and from functions. So we are to encapsulate the data node class as a nested public class within the binary tree class</p><p>6/6</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us