You are on page 1of 11

Data Structures and Algorithms


Data Structures and Algorithms
About the course (objectives, outline, recommended reading) Problem solving Notions of Algorithmics (growth of functions, efficiency, programming model, example analysis) Lists, stacks, queues.

• •
• • • •

Marius Joldoş (lectures + lab. sessions),

• • • •

Room D3, 71-73 DorobanŃilor St. Tel. 0264-401276,

Paulina Mitrea (lab. sessions)

Course web site: local in the laboratory (but fully featured) Lectures: Friday 12-14, room P03, 26-28 G.BariŃiu St. Labs: room D3, 71-73 DorobanŃilor St. every week : 30411-2: Wednesday, 30413-4: Thursday, as scheduled
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 2

Schedule: as set in the timetable

DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos

To get familiar with various ways to characterize properties of algorithms and data structures. To learn to compare and algorithms and data structures. To get familiar with standard algorithms and data structures so that you can find appropriate algorithms and data structures when they are available. To learn to design your own algorithms and data structures when no appropriate can be found in the literature and libraries.
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 3

Organization & interaction
During lectures there will be 15 min, on assigned topics (one week before) presentations made by you

• • • • • • •

Followed by discussions on the topic Are part of the requirements for DSA Work in teams of two Split the task between team members Present your work in class Criticize (constructively) the work – learn how to evaluate work Deadlines MUST be met
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 4

Laboratory sessions (already mentioned)

DSA .U. Uses pseudocode. hash tables. extremely well written and documented by a Master of the field.1028 pages. in class – 30% • After lecture 6? (theory + problem) of written • Final. binary trees. Ullman. Outline (preliminary) Introduction: algorithmics and asymptotics. Also useful as a reference book. reprinted several times. though it misses a few topics. Lists. 1987.U. greedy algorithms. (2nd edition). Joldos Undirected graphs: minimum cost spanning trees (MST). MIT Press / McGraw Hill. backtracking.T. Rivest.Cluj-Napoca . traversals.M. John Wiley and Sons.Lecture 1. during summer session – 70% of written There may be bonuses ( <= 1 point) Cormen. all pairs shortest path.T. with introductory chapters in discrete math and offering specialized topics in later chapters. Written exams • The classic book on the topic. 1999. Joldos 5 DSA . Addison-Wesley.M. Leiserson. Joldos . The Art of Computer Programing. which makes the presentation compact. dynamic programming. Hopcroft. easy to follow and “timeless”. three volumes. B-trees.Cluj-Napoca . Uses “extended Pascal”. with an accessible introduction to complexity analysis. still most valuable introductory book.U. An excellent source for good object-oriented design principles. stacks and queues Trees. graph matching Algorithm analysis and design techniques: divide and conquer. Activity during the semester – 40% • Assigned programming tasks • Presentations. AVL trees. 1990 (2002).Evaluation Recommended books Aho. Unfortunately uses the language of the hypothetical machine MIX for describing the algorithms which makes understanding more difficult.Cluj-Napoca .U.Cluj-Napoca . unionfiind sets Directed graph ADTs: single source shortest path. (Stein): Introduction to Algorithms.T.T. MST algorithms.Lecture 1. local search algorithms Sorting Data Structures and algorithms for external storage. DSA . Data Structures and Algorithms. 8 DSA . Joldos 6 Recommended books Preiss. • Midterm.Lecture 1. Data Structures and Algorithms with objectOriented Design Patterns in C++. 660 pages.M. mappings. • A classical book. priority queues Advanced ADTs for sets: 23 trees. binary search trees Basic ADTs for sets: dictionary. 427 pages. traversals 7 • • An unusual algorithms and data structures book in that it stresses from the beginning the role of design patterns for the implementation of data structures as classes. etc.Lecture 1. Available online (see course web site) Knuth. • • Written for all levels.M. Addison Wesley.

an> • Output: A reordering of the input sequence <a´1.Problem Solving Representation of real world objects Issues when dealing with the representation of real world objects in a computer program • how real world objects are modeled as mathematical entities. DSA . a´n> so that a´i ≤ a´j whenever i < j Instance: The sequence <7.M. 10.T. a´2. Problem Instance: A particular collection of inputs for a given problem.U.Lecture 1. Once we have a suitable mathematical model. Joldos Basic Notions Model of Computation: An abstract sequential computer.g. • Finding such a model is already half the solution. Usually there are many algorithms for a given problem.Cluj-Napoca .Cluj-Napoca . Joldos 11 Example of computational problem: sorting Statement of problem: • Input: A sequence of n numbers <a1.Cluj-Napoca . …. Computational Problem: A specification in general terms of inputs and outputs and the desired input/output relationship. • how these entities are stored in a computer's memory (e. • the set of operations that we define over these mathematical entities. Joldos 12 .U. …. 9 DSA . Program: Particular implementation of some algorithm. called a Random Access Machine or RAM. Any branch of mathematics or science can be called into service to help model the problem domain. we can specify a solution in terms of that model. how they are aggregated as fields in records and how these records are arranged in memory.Lecture 1.M.T. Uniform cost model.M. 4.Lecture 1. perhaps as arrays or as linked structures). DSA . Joldos 10 First construct an exact model in terms of which we can express allowed solutions.Lecture 1. a2.T. and • the algorithms that are used to perform these operations. 5.U.M.Cluj-Napoca . Algorithm: A method of solving a problem which can be implemented on a computer.T.U. 5> Algorithms: • Selection sort • Insertion sort • Merge sort • (many others) DSA .

Joldos 15 T(n) is O(log n) if T(n) = c log2n Formally: • O( g(n) ) is the set of functions.M.M. Typically. Joldos 14 Growth of functions A General Portable Performance Metric Informally.U.M.Lecture 1.T. problems become computationally intensive as the input size grows. f.e.Cluj-Napoca . time to solve a problem of size.T.M.Cluj-Napoca .Lecture 1.U.Algorithm Design Algorithm Analysis of algorithms Determine the amount of some resource required by an algorithm (usually depending on the size of the input). Hence we are studying the asymptotic efficiency of algorithms. Joldos 13 DSA . n. We look at input sizes large enough to make only the order of the growth of the running time relevant for the analysis and comparison of algorithms.T.Lecture 1. we may write lim ≤ c n→∞ • g(n) and say g is an upper bound for f DSA . and n > N (i. The resource might be: • • • • • running time memory usage (space) number of accesses to secondary storage number of basic arithmetic operations network traffic Formally. DSA .Cluj-Napoca . sufficiently large N) f(n) Alternatively.Lecture 1. we define the running time of an algorithm on a particular input instance to be the number of computation steps performed by the algorithm on this instance. DSA . c > 0. Joldos 16 .Cluj-Napoca . such that f(n) < c g(n) for some constant.U.U.T.

Lecture 1. Joldos Example: a n2 + b n + c = Ω(n) provided a > 0 18 DSA .T. such that f(n) > c g(n) for some constant. c2 > 0. c.M.T.3 n ≤ c1 n2 for any n > n0 Example: Dividing by n2 yields: c2 ≤ 1 / 2 – 3 / n ≤ c1 This is satisfied for c2 = 1 / 14. DSA .M.M.M. and n > N (g(n)): class of functions f(n) that grow at least as fast as g(n) g(n) describes the best case behavior of an algorithm that is Ω(g) 17 O(g) is the set of functions that grow no faster than g.T.Cluj-Napoca .U. n0 ∈ N such that: c2 n2 ≤ n2 / 2 .3 n = Θ(n2) We have to determine c1 > 0.Cluj-Napoca . c1 = 1 / 2. Joldos 19 Basic Asymptotic Efficiency classes 1 log n n n log n n2 n3 2n n! constant logarithmic linear n log n quadratic cubic exponential factorial DSA .Big-oh Big-omega Ω(g) is the set of functions.U.U.Lecture 1. f. Joldos Big-theta Θ (g(n)): class of functions f(n) that grow at same rate as g(n) Example: n2 / 2 . n0 = 7.Cluj-Napoca .Lecture 1.T.U.Lecture 1. Joldos 20 .Cluj-Napoca . g(n) describes the worst case behavior of an algorithm that is O(g) Examples: n lg n + n = O(n2) lgk n = O(n) for all k ∈ N DSA .

M.U.Lecture 1.T.U.M.Lecture 1. Joldos 23 DSA . average. input size T(n) ≈ copC(n) running time execution time for basic operation Number of times basic operation is executed 22 DSA .A Simple Programming Model Prog = statement.M.Cluj-Napoca . do Prog”: n times Time(Prog') Prog = function call: Time(function) Prog = “while(expr).T.U.Cluj-Napoca .T. Prog': Time(statement) + Time (Prog') Prog = “var = expr”: 1 + Time(expr) Prog = “expr1 op expr2”: 1 + Time(expr1) + Time(expr2) for primitive operations Prog = “array[expr]'': 1 + Time(expr) Prog = “for i = 1 to n. Joldos 21 Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size • Basic operation: the operation that contributes most towards the running time of the algorithm. Joldos Time efficiency of nonrecursive algorithms Example (nonrecursive) Steps in mathematical analysis of nonrecursive algorithms: • Decide on parameter n indicating input size • Identify algorithm’s basic operation • Determine worst. do Prog' ”: Time(expr) + Time(Prog') times DSA .Cluj-Napoca .M.Lecture 1. and best case for input • • of size n Set up summation for C(n) reflecting algorithm’s loop structure Simplify summation using standard formulas Other issues regarding this algorithm implementation DSA .T.Lecture 1. Joldos 24 .U.Cluj-Napoca .

Lecture 1.U.Lecture 1.M. Joldos 28 . average.M.U.U.Lecture 1.Cluj-Napoca .Cluj-Napoca .Lecture 1.T. and best case for input of size n • Set up a recurrence relation and initial condition(s) for C(n) − the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls).T.Time efficiency of recursive algorithms Example (recursive) Steps in mathematical analysis of recursive algorithms: • Decide on parameter n indicating input size • Identify algorithm’s basic operation • Determine worst. • Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution DSA . Joldos 25 DSA . Joldos 27 DSA .M.M.Cluj-Napoca .T. Joldos 26 Example (recursive) Analysis Example (recursive) Analysis DSA .T.U.Cluj-Napoca .

Joldos 29 DSA . if stack is empty an error occurs Input: none.U.Cluj-Napoca .M.Lecture 1.Lecture 1. Joldos 31 DSA . Inserting an item is known as “pushing” onto the stack. without removing it.M.Cluj-Napoca . Joldos 30 The Stack Abstract Data Type Stacks .Relevance Stacks appear in computer programs • Key to call / return in functions & procedures • Stack frame allows recursive calls • Call: push stack frame • Return: pop stack frame Stack frame • Function arguments • Return address • Local variables DSA . linked lists The Stack Abstract Data Type A stack is an abstract data type (ADT) that supports two main operations: Stacks • A stack is a container of StackElements that are • • inserted and removed according to the last-in-firstout (LIFO) principle.Cluj-Napoca .U. “Popping” off the stack is synonymous with removing an item.M. Output: boolean isEmpty(): Return a boolean indicating if stack is empty. • • • • • push(x): Inserts StackElement x onto top of stack • • • • • Input: StackElement. Output: StackElement The following support operations should also be defined: size(): Returns the number of StackElements in stack Input: none.T. top(): return the top StackElement of the stack.Lecture 1. Output: none pop(): Removes the top StackElement of stack and returns it. StackElements can be inserted at any time.M.T.Cluj-Napoca . queues. Joldos 32 . if the stack is empty an error occurs.U.U. Output: integer Input: none.T. Input: none.T.Lecture 1. but only the last (the most-recently inserted) StackElement can be removed.Stacks. Output: StackElement DSA .

Joldos 35 DSA . Joldos 36 .Cluj-Napoca .M.M.M. an error occurs if the queue is empty • Input: none. Joldos 33 • • • • • enqueue(o): Insert QueueElement o at the rear of the queue • Input: QueueElement. Output: boolean front(): Return. Elements are inserted at the rear (enqueued) and removed from the front (dequeued) DSA .Lecture 1.M. Output: QueueElement • Input: none. Output: integer These support methods should also be defined: size(): Return the number of QueueElements in the queue isEmpty(): Return a boolean value that indicates whether the queue is empty • Input: none. Output: none dequeue(): Remove the QueueElement from the front of the queue and return it.T.Cluj-Napoca .U. but only the element which has been in the queue the longest may be removed. Output: QueueElement DSA . but do not remove.U.T.U. Elements may be inserted at any time.T. the front QueueElement in the queue.Lecture 1. deletion of element of key 18) DSA .Lecture 1.Cluj-Napoca . an error occurs if the queue is empty • Input: none.g.T.Lecture 1.Cluj-Napoca . Joldos 34 The Queue Abstract Data Type Operations given for a circular queue List example (doubly linked) An empty list contains no elements Insertions occur at the front of the list Deletions done by key (e.U.Queues The Queue Abstract Data Type The queue supports two fundamental operations: A queue differs from a stack in that its insertion and removal routines follows the first-in-first-out (FIFO) principle.

Output: ListElement prev(x).M. an error occurs if the list is empty Input: pointer to element to delete.Cluj-Napoca .Lecture 1.18.Lecture 1.T.Cluj-Napoca .T. next(x): ListElement preceeding/succeeding ListElement x on list DSA .T. Output: ListElement last(): Return. Joldos 40 .M. Output: integer Input: none. Joldos 38 The List ADT. but do not remove. Output: none Input: key ot search. an error occurs if the queue is empty Input: none.U. Operations List examples with sentinels An empty list always contains the sentinel (grey cell) Insertion is done at the front of the list List with elements of keys 6.Cluj-Napoca . Joldos 37 DSA . an error occurs if the queue is empty Input: none.T.U. but do not remove.U.U.The List Abstract Data Type The list supports three fundamental operations: The List ADT.M. Output: none delete(x): Remove the ListElement from the front of the list. Output: pointer to ListElement or nil if not found search(k): search for ListElement of key k on list These support methods should also be defined: size(): Return the number of ListElements in the lits • • • • Input: none. the last ListElement in the list.Lecture 1. Joldos 39 DSA . the first ListElement in the list.Lecture 1. and 11 inserted After insertion of 27 After deletion of 18 DSA .Cluj-Napoca . Output: boolean isEmpty(): Return a boolean value that indicates whether the list is empty first(): Return.M. Operations • • • • • • • • insert(x): Insert ListElement x at the front of the list • • • Input: ListElement.

2 Notes DSA . chapters: Algorithm Analysis. Stacks. chapters 1 & 2 CLR. 11. 1. 2.3 Preiss.U.T.1.T. Joldos 42 . Knuth. vol. chapters 2. Data Types and Abstraction.List ADT (w/ sentinels) Reading AHU.M.U. 2.1-11. Joldos 41 DSA .M. Queues and Dequeues-4ups. Asymptotic Notation. Foundational Data Structures.Cluj-Napoca .Cluj-Napoca . Ordered Lists and Sorted Lists.Lecture 1.Lecture 1.