Sunteți pe pagina 1din 17

Data Structures & Algorithms

Dr Ravi Prakash Gorthi Aug-Dec, 2010

Data Structures & Algorithms


Topics 1. Why Study DS & Algorithms? 2. Abstract Data Types 3. Arrays and Linked Lists 4. Stacks & Queues 5. Trees & Graphs 6. Sorting and Searching 7. Complexity of Algorithms

Why Study DS & Algorithms?


Let consider an example: Many business companies NEED daily / weekly data on sales or revenue sorted in different ways; Example: A Company needs daily data of:
1. Product-wise sales (first sort sales data according to the product-type, sum the sales per product and sort in the descending-order of sales) 2. Region-wise sales (similar to the above) 3. Sales-person-wise sales (similar to the above)

The sorting software module has to sort a few million records sometime between 2 to 4am in the night.

Sorting A Set of Records


Consider An Indian FMCG Company:
Region Code SalesPersonCode North-India North-India North-India South-India South-India West-India West-India East-India East-India N-001 N-001 N-112 S-005 S-212 W-109 W-119 E-112 E-112 ProductCode P-1045 P-2022 P-1025 P-1025 P-3033 P-1025 P-1045 P-1025 P-1045 Qty-Sold Unit-Price SpecialDiscount Offered 1% 0% 3% 3% 2% 3% 2% 3% 1%

5000 900 15000 18000 300 25000 900 12000 300

99.00 49.00 56.00 56.00 230.00 56.00 99.00 56.00 99.00

From the above records, you have region-wise, sales-person-wise, product-wise, total revenues!

Sorting A Set of Records


Consider An Indian FMCG Company:
Product-Code TotalRevenues P-1025 P-1045 P-2022 P-3082 78L 74L 71L 68L Sales-Person- TotalCode Revenues S-125 N-109 W-212 S-218 135L 132L 128L 121L

Region-Code West-India North-India South-India East-India

TotalRevenues 450L 437L 412L 373L

Sorting A Set of Records


Consider An Indian FMCG Company:
Product-Code TotalRevenues P-1025 78L P-1045 Region-Code32LTotalP-2022 31LRevenues West-India 30L38L P-3082 South-India 9L Sales-PersonTotalCode North-India 7.5L Revenues W-109 East-India W-212 W-178 W-218 6L 12.5L 10l 8l 7.5L

Why Study DS & Algorithms?


An inefficient sorting algorithm will take 10 hours to sort 1 million records on the latest supercomputer whereas an efficient sorting algorithm will take 10 mins. Efficient algorithms require very appropriate datastructures. Thus, there is a need to study a variety of data-structures and their usage in efficient algorithms.

Algorithms
What is an algorithm?
A step-by-step procedure, which is unambiguous and machine executable Solves a given problem correctly and guarantees to give the right answer(s) in finite time (in other words, an algorithm is a provably correct method to solve the given problem) Finite in length Takes a finite number of valid input(s) of finite length, takes a finite time to solve the given problem for the given inputs and produces a finite number of correct output(s) of finite length (and produces an appropriate error message for invalid input(s)) Initially described in pseudo-English but can be programmed using one of the formal computer programming languages on any computing platform

Algorithms
An example to illustrate
Suppose the goal is to find the solution (values of x, y & z), if any, of the following system of linear equations:

The algorithm is as follows:


Eliminate x from all equations below L1 And then, eliminate y from all equations below L2 This will give a value for z (if the equations are solvable) Then, using back-substitution, each unknown variable is solved for.

Algorithms
An example to illustrate
In the example, x is eliminated from L2 by adding x is then eliminated from L3 by adding L1 to L3. The result is:
to L2.

Algorithms
An example to illustrate
Now y is eliminated from L3 by adding 4L2 to L3: The result is:

Then, z can be substituted into L2, which can then be solved to obtain Next, z and y can be substituted into L1, which can be solved to obtain

Algorithms
Why to design and develop algorithms?
1. A mechanism to solve a problem (e.g. Gauss Elimination Method is a mechanism to solve Linear Simultaneous Equations) 2. A mechanism to automate the process of solving a given problem repeatedly on a computing platform on different sets of inputs

Algorithms
Why to analyze algorithms?
1. Any correct solution to a given problem is NOT the end-of research endeavor 2. The time and memory-space taken by an algorithm are of critical importance to both the computer scientists and IT industry professionals 3. If there are more than one algorithm to solve a given problem, these algorithms can be compared along the dimensions of time and memory-space complexities 4. It is at times possible to find a better algorithm to a given problem which takes less time and consumes less memoryspace; sometimes, there is a trade-off and one can choose the best-fit algorithm for a given situation

Algorithms
Why to analyze algorithms?
1. The complexity of an algorithm is typically analyzed along the dimensions of 1. Time taken 2. Memory-space taken to solve a typical average type instance / the worst-case instance / the best-case instance of the given problem 2. The time or memory-space taken by a digital computer to solve an instance of the problem (a given set of inputs define an instance of the problem) using a particular algorithm does NOT offer any definitive views on the complexity of an algorithm 3. A theory & notation on the Order of Complexity was invented as a means to compare algorithms

Algorithms
Design and selection of Data Structures has a major impact on the Complexity of an algorithm

Characteristics of a Good S/W Program


1. Reliable 2. Efficient / Scalable 3. Readable / Maintainable / Extensible 4. Portable

Assignments of Class # 1
1. Send a write-up on who invented calculus and why it was invented? 2. Write a Computer Program that reads three integers as input and determines whether these three integers constitute the sides of a triangle or not. 3. Who invented Quick-Sort algorithm? Send your C language based program to my email-id: ravipgorthi@gmail.com

S-ar putea să vă placă și