1 / 65

Data Structures and algorithms (IS ZC361) An overview of topics for the mid-semester exam

Data Structures and algorithms (IS ZC361) An overview of topics for the mid-semester exam. S.P.Vimal BITS-Pilani. Source :This presentation is composed from the presentation materials provided by the authors (GOODRICH and TAMASSIA) of text book -1 specified in the handout. Overview topics.

Download Presentation

Data Structures and algorithms (IS ZC361) An overview of topics for the mid-semester exam

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures and algorithms (IS ZC361)An overview of topics for the mid-semester exam S.P.Vimal BITS-Pilani Source:This presentation is composed from the presentation materials provided by the authors (GOODRICH and TAMASSIA) of text book -1 specified in the handout Data Structures and Algorithms

  2. Overview topics • Algorithm analysis • Linear Data structures • Trees • Sorting Algorithms Data Structures and Algorithms

  3. Introduction • Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. • An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Input Algorithm Output Data Structures and Algorithms

  4. Algorithm Descriptions • Nature languages: Chinese, English, etc. • Pseudo-code: codes very close to computer languages, e.g., C programming language. • Programs: C programs, C++ programs, Java programs. Goal: • Allow a well-trained programmer to be able to implement. • Allow an expert to be able to analyze the running time. Data Structures and Algorithms

  5. Algorithm: An example Algorithmsorting(X, n) Inputarray X of n integers Outputarray X sorted in a non-decreasing order fori0ton 1do for ji+1tondo if(X[i]>X[j])then { s=X[i]; X[i]=X[j]; X[j]=s; } returnX Data Structures and Algorithms

  6. What do we analyze in an algorithm??? • Estimate the running time • Estimate the memory space required. >> Depends on the input size. Data Structures and Algorithms

  7. Running time of an algorithm • Most algorithms transform input objects into output objects. • The running time of an algorithm typically grows with the input size. Data Structures and Algorithms

  8. Counting Primitive Operations • By inspecting the pseudo code, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size AlgorithmarrayMax(A, n) # operations currentMaxA[0] 2 fori1ton 1do 2+n ifA[i]  currentMaxthen 2(n 1) currentMaxA[i] 2(n 1) { increment counter i } 2(n 1) returncurrentMax 1 Total 7n 1 Data Structures and Algorithms

  9. Growth Rate of Running Time • Changing the hardware/ software environment • Affects T(n) by a constant factor, but • Does not alter the growth rate of T(n) • The linear growth rate of the running time T(n) is an intrinsic property of algorithm arrayMax • Growth rates of functions: • Linear  n • Quadratic  n2 • Cubic  n3 • In a log-log chart, the slope of the line corresponds to the growth rate of the function Data Structures and Algorithms

  10. Growth rates Data Structures and Algorithms

  11. Big-Oh notation • To simplify the running time estimation, for a function f(n), we ignore the constants and lower order terms. Example: 10n3+4n2-4n+5 is O(n3) Formally, Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constantsc and n0 such that f(n)cg(n) for n n0 Data Structures and Algorithms

  12. Example: 2n + 10 is O(n) • 2n + 10cn • (c  2) n  10 • n  10/(c  2) • Pick c = 3 and n0 = 10 Data Structures and Algorithms

  13. Big-Oh notation - examples • Example: the function n2is not O(n) • n2cn • n c • The above inequality cannot be satisfied since c must be a constant • n2 is O(n2). Data Structures and Algorithms

  14. Big-Oh notation - examples • 7n-2 is O(n) • need c > 0 and n0  1 such that 7n-2  c•n for n  n0 • this is true for c = 7 and n0 = 1 • 3n3 + 20n2 + 5 is O(n3) • need c > 0 and n0  1 such that 3n3 + 20n2 + 5  c•n3 for n  n0 • this is true for c = 4 and n0 = 21 • 3 log n + 5 is O(log n) • need c > 0 and n0  1 such that 3 log n + 5  c•log n for n  n0 • this is true for c = 8 and n0 = 2 Data Structures and Algorithms

  15. The big-Oh notation gives an upper bound on the growth rate of a function • The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than the growth rate of g(n) • We can use the big-Oh notation to rank functions according to their growth rate Data Structures and Algorithms

  16. Algorithm analysis ( ) • Linear Data structures • Trees • Sorting Algorithms Data Structures and Algorithms

  17. ADT (Abstract Data Type) • An abstract data type (ADT) is an abstraction of a data structure • An ADT specifies: • Data stored • Operations on the data • Error conditions associated with operations Data Structures and Algorithms

  18. ADT (Abstract Data Type) • Example: ADT modeling a students record • The data stored are • Student name, id No., as1, as2,as3, exam • The operations supported are • int averageAs(as1,as2,as3) • Int finalMark(as1, as2,as3, exam) ) • Error conditions: • Calculate the final mark for absent student Data Structures and Algorithms

  19. The Stack ADT • The Stack ADT stores arbitrary objects • Insertions and deletions follow the last-in first-out scheme • Main stack operations: • push (object): inserts an element • object pop(): removes and returns the last inserted element Data Structures and Algorithms

  20. Auxiliary stack operations: • object top(): returns the last inserted element without removing it • integer size(): returns the number of elements stored • boolean isEmpty(): indicates whether no elements are stored Data Structures and Algorithms

  21. Array-based Stack • A simple way of implementing the Stack ADT uses an array • We add elements from left to right • A variable t keeps track of the index of the top element (size is t+1) Algorithmpop(): ifisEmpty()then throw EmptyStackException else tt 1 returnS[t +1] Algorithmpush(o) ift=S.length 1then throw FullStackException else tt +1 S[t] o Data Structures and Algorithms

  22. The Queue ADT • The Queue ADT stores arbitrary objects • Insertions and deletions follow the first-in first-out scheme • Insertions are at the rear of the queue and removals are at the front of the queue • Main queue operations: • enqueue(object): inserts an element at the end of the queue • object dequeue(): removes and returns the element at the front of the queue Data Structures and Algorithms

  23. Auxiliary queue operations: • object front(): returns the element at the front without removing it • integer size(): returns the number of elements stored • boolean isEmpty(): indicates whether no elements are stored • Exceptions • Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Data Structures and Algorithms

  24. Singly Linked List • A singly linked list is a concrete data structure consisting of a sequence of nodes • Each node stores • element • link to the next node next node elem  A B C D Data Structures and Algorithms

  25. Queue with a Singly Linked List • We can implement a queue with a singly linked list • The front element is stored at the first node • The rear element is stored at the last node • The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f  elements Data Structures and Algorithms

  26. The List ADT models a sequence of positions storing arbitrary objects It allows for insertion and removal in the “middle” Query methods: isFirst(p), isLast(p) Accessor methods: first(), last() before(p), after(p) Update methods: replaceElement(p, o), swapElements(p, q) insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o) remove(p) List ADT Data Structures and Algorithms

  27. Doubly Linked List • A doubly linked list provides a natural implementation of the List ADT • Nodes implement Position and store: • element • link to the previous node • link to the next node • Special trailer and header nodes prev next elem node trailer nodes/positions header elements Data Structures and Algorithms

  28. Algorithm analysis ( ) • Linear Data structures ( ) • Trees • Sorting Algorithms Data Structures and Algorithms

  29. Computers”R”Us Sales Manufacturing R&D US International Laptops Desktops Europe Asia Canada Trees (§2.3) • In computer science, a tree is an abstract model of a hierarchical structure • A tree consists of nodes with a parent-child relation • Applications: • Organization charts • File systems • Programming environments Data Structures and Algorithms

  30. Tree ADT (§2.3.1) • Query methods: • boolean isInternal(p) • boolean isExternal(p) • boolean isRoot(p) • Update methods: • swapElements(p, q) • object replaceElement(p, o) • Additional update methods may be defined by data structures implementing the Tree ADT • We use positions to abstract nodes • Generic methods: • integer size() • boolean isEmpty() • objectIterator elements() • positionIterator positions() • Accessor methods: • position root() • position parent(p) • positionIterator children(p) Data Structures and Algorithms

  31. Preorder Traversal (§2.3.2) • A traversal visits the nodes of a tree in a systematic manner • In a preorder traversal, a node is visited before its descendants • Application: print a structured document AlgorithmpreOrder(v) visit(v) foreachchild w of v preorder (w) 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.3 BankRobbery 2.1 StockFraud 2.2 PonziScheme 1.1 Greed 1.2 Avidity Data Structures and Algorithms

  32. Post order Traversal • In a postorder traversal, a node is visited after its descendants • Application: compute space used by files in a directory and its subdirectories AlgorithmpostOrder(v) foreachchild w of v postOrder (w) visit(v) 9 cs16/ 8 3 7 todo.txt1K homeworks/ programs/ 4 5 6 1 2 Robot.java20K h1c.doc3K h1nc.doc2K DDR.java10K Stocks.java25K Data Structures and Algorithms

  33. Binary Trees • A binary tree is a tree with the following properties: • Each internal node has two children • The children of a node are an ordered pair • We call the children of an internal node left child and right child • Alternative recursive definition: a binary tree is either • a tree consisting of a single node, or • a tree whose root has an ordered pair of children, each of which is a binary tree • Applications: • arithmetic expressions • decision processes • searching A C B D E F G I H Data Structures and Algorithms

  34. +   2 - 3 b a 1 Arithmetic Expression Tree • Binary tree associated with an arithmetic expression • internal nodes: operators • external nodes: operands • Example: arithmetic expression tree for the expression (2  (a - 1) + (3  b)) Data Structures and Algorithms

  35. Properties of Binary Trees • Notation n number of nodes e number of external nodes i number of internal nodes h height • Properties: • e = i +1 • n =2e -1 • h  i • h  (n -1)/2 • e 2h • h log2e • h log2 (n +1)-1 Data Structures and Algorithms

  36. Inorder Traversal • In an inorder traversal a node is visited after its left subtree and before its right subtree • Application: draw a binary tree • x(v) = inorder rank of v • y(v) = depth of v AlgorithminOrder(v) ifisInternal (v) inOrder (leftChild (v)) visit(v) ifisInternal (v) inOrder (rightChild (v)) 6 2 8 1 4 7 9 3 5 Data Structures and Algorithms

  37. +   2 - 3 b a 1 Printing Arithmetic Expressions • Specialization of an inorder traversal • print operand or operator when visiting node • print “(“ before traversing left subtree • print “)“ after traversing right subtree AlgorithmprintExpression(v) ifisInternal (v)print(“(’’) inOrder (leftChild (v)) print(v.element ()) ifisInternal (v) inOrder (rightChild (v)) print (“)’’) ((2  (a - 1)) + (3  b)) Data Structures and Algorithms

  38. Linked Data Structure for Representing Trees • A node is represented by an object storing • Element • Parent node • Sequence of children nodes • Node objects implement the Position ADT B   A D F B A D F   C E C E Data Structures and Algorithms

  39. D C A B E Linked Data Structure for Binary Trees • A node is represented by an object storing • Element • Parent node • Left child node • Right child node • Node objects implement the Position ADT    B A D     C E Data Structures and Algorithms

  40. A … B D C E F J G H Array-Based Representation of Binary Trees • nodes are stored in an array 1 2 3 • let rank(node) be defined as follows: • rank(root) = 1 • if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) • if node is the right child of parent(node), rank(node) = 2*rank(parent(node))+1 4 5 6 7 10 11 Data Structures and Algorithms

  41. A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 2 9 1 4 8 Binary Search Tree Data Structures and Algorithms

  42. Search AlgorithmfindElement(k, v) ifT.isExternal (v) returnNO_SUCH_KEY if k<key(v) returnfindElement(k, T.leftChild(v)) else if k=key(v) returnelement(v) else{ k>key(v) } returnfindElement(k, T.rightChild(v)) • To search for a key k, we trace a downward path starting at the root • The next node visited depends on the outcome of the comparison of k with the key of the current node • If we reach a leaf, the key is not found and we return NO_SUCH_KEY • Example: findElement(4) 6 < 2 9 > = 8 1 4 Data Structures and Algorithms

  43. Insertion 6 < • To perform operation insertItem(k, o), we search for key k • Assume k is not already in the tree, and let let w be the leaf reached by the search • We insert k at node w and expand w into an internal node • Example: insert 5 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5 Data Structures and Algorithms

  44. Deletion • To perform operation removeElement(k), we search for key k • Assume key k is in the tree, and let let v be the node storing k • If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w) • Example: remove 4 6 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8 Data Structures and Algorithms

  45. Deletion (cont.) 1 • We consider the case where the key k to be removed is stored at a node v whose children are both internal • we find the internal node w that follows v in an inorder traversal • we copy key(w) into node v • we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z) • Example: remove 3 v 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9 Data Structures and Algorithms

  46. Performance • Consider a dictionary with n items implemented by means of a binary search tree of height h • the space used is O(n) • methods findElement , insertItem and removeElement take O(h) time • The height h is O(n) in the worst case and O(log n) in the best case Data Structures and Algorithms

  47. Algorithm analysis ( ) • Linear Data structures ( ) • Trees ( ) • Sorting Algorithms Data Structures and Algorithms

  48. Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S1and S2 Recur: solve the subproblems associated with S1and S2 Conquer: combine the solutions for S1and S2 into a solution for S The base case for the recursion are subproblems of size 0 or 1 Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm Like heap-sort It uses a comparator It has O(n log n) running time Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) Divide-and-Conquer Data Structures and Algorithms

  49. Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1and S2 of about n/2 elements each Recur: recursively sort S1and S2 Conquer: merge S1and S2 into a unique sorted sequence Merge-Sort AlgorithmmergeSort(S, C) Inputsequence S with n elements, comparator C Outputsequence S sorted • according to C ifS.size() > 1 (S1, S2)partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) Smerge(S1, S2) Data Structures and Algorithms

  50. Merging Two Sorted Sequences Algorithmmerge(A, B) Inputsequences A and B withn/2 elements each Outputsorted sequence of A  B S empty sequence whileA.isEmpty() B.isEmpty() ifA.first().element()<B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) whileA.isEmpty() S.insertLast(A.remove(A.first())) whileB.isEmpty() S.insertLast(B.remove(B.first())) return S • The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B • Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time Data Structures and Algorithms

More Related