ds and algorithims

Upload: ayaan-sharma

Post on 14-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 DS and Algorithims

    1/17

    Data Structures&

    Algorithms

    Dr Ravi Prakash Gorthi

    Aug-Dec, 2010

  • 7/30/2019 DS and Algorithims

    2/17

    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

  • 7/30/2019 DS and Algorithims

    3/17

    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 thedescending-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.

  • 7/30/2019 DS and Algorithims

    4/17

    Sorting A Set of Records

    Region Code Sales-

    Person-

    Code

    Product-

    Code

    Qty-Sold Unit-Price Special-

    Discount

    Offered

    North-India N-001 P-1045 5000 99.00 1%

    North-India N-001 P-2022 900 49.00 0%

    North-India N-112 P-1025 15000 56.00 3%

    South-India S-005 P-1025 18000 56.00 3%

    South-India S-212 P-3033 300 230.00 2%

    West-India W-109 P-1025 25000 56.00 3%

    West-India W-119 P-1045 900 99.00 2%

    East-India E-112 P-1025 12000 56.00 3%

    East-India E-112 P-1045 300 99.00 1%

    Consider An Indian FMCG Company:

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

  • 7/30/2019 DS and Algorithims

    5/17

    Sorting A Set of RecordsConsider An Indian FMCG Company:

    Product-Code Total-

    Revenues

    P-1025 78L

    P-1045 74L

    P-2022 71L

    P-3082 68L

    Region-Code Total-

    Revenues

    West-India 450L

    North-India 437L

    South-India 412L

    East-India 373L

    Sales-Person-

    Code

    Total-

    RevenuesS-125 135L

    N-109 132L

    W-212 128L

    S-218 121L

  • 7/30/2019 DS and Algorithims

    6/17

    Sorting A Set of RecordsConsider An Indian FMCG Company:

    Product-Code Total-

    Revenues

    P-1025 78L

    P-1045 32L

    P-2022 31L

    P-3082 30L

    Region-Code Total-

    Revenues

    West-India 38L

    South-India 9L

    North-India 7.5L

    East-India 6L

    Sales-Person-

    Code

    Total-

    Revenues

    W-109 12.5L

    W-212 10l

    W-178 8l

    W-218 7.5L

  • 7/30/2019 DS and Algorithims

    7/17

    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 10mins.

    Efficient algorithms require very appropriate data-structures.

    Thus, there is a need to study a variety of data-structures

    and their usage in efficient algorithms.

  • 7/30/2019 DS and Algorithims

    8/17

    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 theright answer(s) in finite time (in other words, an algorithm isa provably correct method to solve the given problem)

    Finite in lengthTakes afinite number of valid input(s) of finite length, takesa finite time to solve the given problem for the given inputsand produces afinite number of correct output(s) of finite

    length (and produces an appropriate error message forinvalid input(s))

    Initially described in pseudo-English but can be programmedusing one of the formal computer programming languages

    on any computing platform

  • 7/30/2019 DS and Algorithims

    9/17

    Algorithms

    An example to illustrate

    Suppose the goal is to find the solution (values of x, y & z), ifany, of the following system of linear equations:

    The algorithm is as follows:

    Eliminatexfrom all equations below L1 And then, eliminate yfrom 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.

  • 7/30/2019 DS and Algorithims

    10/17

    Algorithms

    An example to illustrate

    In the example, xis eliminated from L2 by adding to L2.

    xis then eliminated from L3 by adding L1 to L3. The result is:

  • 7/30/2019 DS and Algorithims

    11/17

    AlgorithmsAn example to illustrate

    Now yis eliminated from L3 by adding 4L2 to L3:

    The result is:

    Then, z can be substituted into L2, which can then be solved toobtain

    Next, z and ycan be substituted into L1, which can be solved toobtain

  • 7/30/2019 DS and Algorithims

    12/17

    Algorithms

    Why to design and develop algorithms?

    1. A mechanism to solve a problem (e.g. Gauss EliminationMethod 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

  • 7/30/2019 DS and Algorithims

    13/17

    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 givenproblem which takes less time and consumes less memory-

    space; sometimes, there is a trade-off and one can choose

    the best-fit algorithm for a given situation

  • 7/30/2019 DS and Algorithims

    14/17

    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 particularalgorithm does NOT offer any definitive views on the

    complexity of an algorithm

    3. A theory & notation on the Order of Complexity wasinvented as a means to compare algorithms

  • 7/30/2019 DS and Algorithims

    15/17

    Algorithms

    Design and selection of Data Structures has a major

    impact on the Complexity of an algorithm

  • 7/30/2019 DS and Algorithims

    16/17

    Characteristics of a Good S/W Program

    1. Reliable

    2. Efficient / Scalable

    3. Readable / Maintainable / Extensible

    4. Portable

  • 7/30/2019 DS and Algorithims

    17/17

    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 integersas 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:

    [email protected]