The document discusses data structures and algorithms. It aims to teach how to design good algorithms and evaluate performance, and how to implement basic algorithms and data structures. Some key goals are solving common algorithmic problems like searching, sorting, and graph exploration. It provides references for further reading on algorithms and data structures. It defines algorithms, programs, and the relationship between them. It provides examples of algorithms using iteration and recursion, and discusses basic data types, abstract data types, and implementing abstract data types using object-oriented classes.
DSA- Learn Algorithms, Data Structures & Their Analysis
1. Data Structures and Algorithms
Snehal Masne
www.TechProceed.com
www.iSnehal.com
2. 2
learn ...
... how to design “good” algorithms and data
structures
... how to evaluate the performance of algorithms
... how to program basic algorithms and data
structures
Goals
3. 3
learn ...
... how to solve the most common algorithmic
problems, such as
searching a data collection
sorting data
exploring a graph/network
text processing
Goals
4. 4
Michael T. Goodrich, Roberto Tamassia:
Algorithm Design: Foundations, Analysis,
and Internet Examples.
John Wiley & Sons, Inc., 2001.
Mark Allen Weiss:
Data Structures and Algorithm Analysis
in Java.
2nd edition, Addision Wesley, 2007.
Literature
5. 5
Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest, Clifford Stein:
Introduction to Algorithms.
3rd edition, The MIT Press, 2009.
Donald E. Knuth:
The Art of Computer Programming.
Vol. 1: Fundamental Algorithms.
Vol. 3: Sorting and Searching.
Addision Wesley, 1997/98.
Literature
6. 6
• an algorithm is a step-by-step procedure for
solving a problem in a finite amount of time and
space
• an algorithm is “good” if it is correct and solves the
problem in lowest time/space known for that
problem
Algorithms
Input Algorithm Output
7. 7
• a program is a description of an algorithm in a
programming language
• a program repesents exactly one algorithm
• an algorithm represents a multitude of programs
• writing a good program requires designing a good
algorithm
No algorithm, no program!
Programs
8. 8
Problem – Algorithm - Program
Problem
Algorithm 1 Algorithm 2
Program 21
Program 22
. . .
. . .
. . .
. . .
. . .
9. 9
[ 1] int mod(int a, int b) {
[ 2] /* Requirements (input): a: a>=0; b: b>0 */
[ 3] /* Guarantee (output): r: a=t*b + r and 0<r<=b */
[ 4] /* (1) Declaration */
[ 5] int r;
[ 6] /* (2) Initialization */
[ 7] r=a;
[ 8] /* (3) Iteration start */
[ 9] while (r>=b) {
[10] /* (4) Problem reduction */
[11] r=r-b;
[12] /* (5) Iteration end */
[13] }
[14] /* (6) Output formation */
[15] return(r);
[16] }
Iteration
10. 10
[ 1] int mod(int a, int b) {
[ 2] /* Requirements (input): a: a>=0; b: b>0 */
[ 3] /* Guarantee (output): r: a=t*b + r and 0<r<=b */
[ 4] /* (1) Declaration */
[ 5] int r;
[ 6] /* (2) Initialization */
[ 7] r=a;
[ 8] /* (3) Trivial case */
[ 9] if (r<b) return(r);
[10] /* (4) Problem reduction */
[11] r=r-b;
[12] /* (5) Recursion step */
[13] return(mod(r,b));
[14] }
Recursion
11. 11
• elementary types (in Java):
–logical type: boolean
–integer type: int, short, byte, long
–floating point type: float, double
–character type: char
• composed data types (concrete data structures):
–arrays, e.g., int[], char[]
–records/structs (not available in Java)
–lists
Data Types
12. 12
• an abstract data type (ADT) is an abstraction of a
concrete data structure
• an ADT specifies:
–data stored (auxiliary data, inclusive)
–operations on the data
–error conditions associated with operations
• an ADT abstracts from the implementation of the
data and operations
Abstract Data Types
13. 13
• a class is an ADT implementation in an OOPL
• an instance of an ADT (or a class) has an
operational life span, during which a state (an
element of the ADT„s data set) is always being
assigned to the object
• methods of an ADT are programs manipulating the
state of an object
• a state of an object can only be manipulated by
ADT methods (encapsulation)
Objects
14. 14
• ADT modeling a simple stock trading system
• the data stored are buy/sell orders
• the operations supported are:
–Order buy(Stock st, Shares sh, Price p)
–Order sell(Stock st, Shares sh, Price p)
–void cancel(Order)
• error conditions are:
–buy/sell a nonexistent stock
–cancel a nonexistent order
Example of an Abstract Data Type
Editor's Notes
Note: by the above definition, a “good” algorithm may still be unusable if it requires too much time/space to solve the problem (i.e. if the algorithm is intractable).