Data Structures and Algorithms: Objectives
Data Structures and Algorithms: Objectives
Instructors:
Room D3, 71-73 Dorobanilor St. Tel. 0264-401276 (lab.) [email protected], [email protected]
Objectives
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
Evaluation
Activity during the semester 40%
Written exams
of written
Recommended books
Aho, Hopcroft, Ullman. Data Structures and Algorithms, Addison-Wesley, 427 pages, 1987.
Recommended books
Preiss. Data Structures and Algorithms with objectOriented Design Patterns in C++, John Wiley and Sons, 660 pages, 1999.
The classic book on the topic, still most valuable introductory book, though it misses a few topics. Uses extended Pascal.
Cormen, Leiserson, Rivest, (Stein): Introduction to Algorithms. MIT Press / McGraw Hill, (2nd edition),1028 pages, 1990 (2002).
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. An excellent source for good object-oriented design principles, with an accessible introduction to complexity analysis. Available online (see course web site)
Written for all levels, with introductory chapters in discrete math and offering specialized topics in later chapters. Uses pseudocode, which makes the presentation compact, easy to follow and timeless. Also useful as a reference book.
Knuth. The Art of Computer Programing, Addison Wesley, three volumes, reprinted several times. A classical book, extremely well written and documented by a Master of the field. Unfortunately uses the language of the hypothetical machine MIX for describing the algorithms which makes understanding more difficult.
5 DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 6
Outline (preliminary)
Introduction: algorithmics and asymptotics. Lists, stacks and queues Trees, binary trees, binary search trees Basic ADTs for sets: dictionary, hash tables, mappings, priority queues Advanced ADTs for sets: 23 trees, AVL trees, unionfiind sets Directed graph ADTs: single source shortest path, all pairs shortest path, traversals Undirected graphs: minimum cost spanning trees (MST), MST algorithms, traversals, graph matching Algorithm analysis and design techniques: divide and conquer, dynamic programming, greedy algorithms, backtracking, local search algorithms Sorting Data Structures and algorithms for external storage. B-trees.
7
Problem Solving
First construct an exact model in terms of which we can express allowed solutions.
Once we have a suitable mathematical model, we can specify a solution in terms of that model.
Basic Notions
Model of Computation: An abstract sequential computer, called a Random Access Machine or RAM. Uniform cost model. Computational Problem: A specification in general terms of inputs and outputs and the desired input/output relationship. Problem Instance: A particular collection of inputs for a given problem. Algorithm: A method of solving a problem which can be implemented on a computer. Usually there are many algorithms for a given problem. Program: Particular implementation of some algorithm.
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 10
Statement of problem:
Input: A sequence of n numbers <a1, a2, , an> Output: A reordering of the input sequence <a1,
a2, , an> so that ai aj whenever i < j
Analysis of algorithms
Determine the amount of some resource required by an algorithm (usually depending on the size of the input). The resource might be:
Growth of functions
running time memory usage (space) number of accesses to secondary storage number of basic arithmetic operations network traffic
Formally, 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 - Lecture 1- T.U.Cluj-Napoca - M. Joldos 13
Typically, problems become computationally intensive as the input size grows. 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. Hence we are studying the asymptotic efficiency of algorithms.
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 14
Big-oh
O(g)
Examples: n lg n + n = O(n2) lgk n = O(n) for all k N
15 DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 16
g(n)
and say
Big-omega
(g) is the set of
functions, f, such that f(n) > c g(n) for some constant, c, 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)
Big-theta
(g(n)): class of functions f(n) that grow at same rate as g(n) Example: n2 / 2 - 3 n = (n2) We have to determine c1 > 0, c2 > 0, n0 N such that: c2 n2 n2 / 2 - 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, c1 = 1 / 2, n0 = 7.
17 DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 18
Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size
Decide on parameter n indicating input size Identify algorithms basic operation Determine worst, average, and best case for input
T(n) copC(n)
running time execution time for basic operation Number of times basic operation is executed
21
of size n Set up summation for C(n) reflecting algorithms loop structure Simplify summation using standard formulas
22
Example (nonrecursive)
Steps in mathematical analysis of recursive algorithms: Decide on parameter n indicating input size Identify algorithms basic operation Determine worst, average, 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). Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution
23
24
Example (recursive)
25
26
Stacks
27
28
pop(): Removes the top StackElement of stack and returns it; if stack is empty an error occurs
Input: none; Output: StackElement
isEmpty(): Return a boolean indicating if stack is empty. top(): return the top StackElement of the stack, without removing it; if the stack is empty an error occurs.
Input: none; Output: StackElement
29
30
Stacks - Relevance
Queues
Key to call / return in functions & procedures Stack frame allows recursive calls Call: push stack frame Return: pop stack frame
Stack frame
A queue differs from a stack in that its insertion and removal routines follows the first-in-first-out (FIFO) principle. Elements may be inserted at any time, but only the element which has been in the queue the longest may be removed. Elements are inserted at the rear (enqueued) and removed from the front (dequeued)
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 32
dequeue(): Remove the QueueElement from the front of the queue and return it; an error occurs if the queue is empty
front(): Return, but do not remove, the front QueueElement in the queue; an error occurs if the queue is empty
33
34
delete(x): Remove the ListElement from the front of the list; an error occurs if the list is empty
isEmpty(): Return a boolean value that indicates whether the list is empty
first(): Return, but do not remove, the first ListElement in the list; an error occurs if the queue is empty
last(): Return, but do not remove, the last ListElement in the list; an error occurs if the queue is empty
Input: none; Output: ListElement
35
37
38
39
40
Reading
AHU, chapters 1 & 2 CLR, chapters 2, 11.1-11.3 Preiss, chapters: Algorithm Analysis. Asymptotic Notation. Foundational Data Structures. Data Types and Abstraction. Stacks, Queues and Dequeues-4ups. Ordered Lists and Sorted Lists. Knuth, vol. 1, 2.1, 2.2 Notes
DSA - Lecture 1- T.U.Cluj-Napoca - M. Joldos 41