algorithm & data structures lec1(bet)

Upload: xafran-khan

Post on 03-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    1/45

    Click to edit Master subtitle style

    3/11/13

    Algorithm & Data StructuresFall 2011

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    2/45

    3/11/13

    My Profile

    Name:Syed Muhammad Raza

    Education:

    MSc Wireless Communication

    LTH Sweden , 2007-2009

    BS Computer Information Sciences

    PIEAS Pakistan , 2002-2006

    Contact Information:

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    3/45

    3/11/13

    Rules

    Dont Mess With Me

    &

    I Wont Mess With You

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    4/45

    3/11/13

    Course Overview

    Fundamentals of AlgorithmsReview of Programming

    Sorting and Searching

    Stacks

    Queues, Priority Queues and Circular Queues

    Linked List

    Trees

    Hashing

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    5/45

    3/11/13

    Text Book

    No Text Book

    &Every Text Book

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    6/45

    3/11/13

    Marks Distribution

    Lab 25% of 100 Marks

    Theory 75% of 100 Marks

    Sessional 1 = 10 Marks

    Sessional 2 = 15 Marks

    Final Paper = 50 Marks

    Assignment = 15 Marks

    Quiz = 10 Marks

    Total = 100 Marks

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    7/45

    3/11/13

    Lets see what you got!

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    8/45

    3/11/13

    Algorithm

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    9/45

    3/11/13

    Problem Solving

    Problem solving is the process of transformingthe description of a problem into the solution of thatproblem by using our knowledge of the problem

    domain and by relying on our ability to select anduse appropriate problem-solving strategies,techniques, and tools.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    10/45

    3/11/13

    Algorithms

    An Algorithm is a step by step solution to aproblem

    Why bother writing an algorithm

    For your own use in the future - Don't have to

    rethink the problem.

    So others can solve the problem, even if theyknow very little about the principles behind how

    the solution was derived.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    11/45

    3/11/13

    Examples of Algorithms

    Washing machine instructions

    Instructions for a ready-to-assemble piece of

    furniture

    A Classic: GCD - Greatest Common Divisor - The

    Euclidean Algorithm

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    12/45

    3/11/13

    Washing Machine Instructions

    Separate clothes into white clothes and colored clothes.

    For white clothes:

    Set water temperature knob to HOT.

    Place white laundry in tub.

    For colored clothes:

    Set water temperature knob to COLD

    Place colored laundry in tub.

    Add 1 cup of powdered laundry detergent to the tub.

    Close lid and press the start button.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    13/45

    3/11/13

    Observations

    There are a finite number of steps.

    We are capable of doing each of the instructions.

    When we have followed all of the steps, the

    washing machine will wash the clothes and then

    will stop.

    Are all of the clothes clean ?

    Do we want the washing machine to run until all

    of the clothes are clean ?

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    14/45

    3/11/13

    Refinement of the Definition

    Our old definition:

    An algorithm is a step by step solution to a problem.

    Adding our observations:

    An algorithm is a finite set of executable instructions that

    directs a terminating activity.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    15/45

    3/11/13

    Instructions for a Ready-to-Assemble Furniture

    "Align the marks on side A with the grooves on

    Part F

    Why are these instructions typically so hard to

    follow ?

    Lack of proper tools - instructions are not executable

    Which side is A ? A & B look alike. Both line up with Part F

    - Ambiguous instructions.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    16/45

    3/11/13

    Final Definition

    An algorithm is a finite set of unambiguous,executable instructions that directs a terminatingactivity.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    17/45

    3/11/13

    History of Algorithms

    The study of algorithms began as a subject in mathematics.

    The search for algorithms was a significant activity of early

    mathematicians.

    Goal: To find a single set of instructions that could be usedto solve any problem of a particular type.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    18/45

    3/11/13

    GCD- Euclidean Algorithm

    Assign M and N the value of the larger and the value of the

    smaller of the two positive integer input values, respectively.

    Divide M by N and call the remainder R.

    If R is not 0, then assign M the value of N, assign N the valueof R and return to step 2, otherwise the greatest common

    divisor is the value currently assigned to N.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    19/45

    3/11/13

    Finding GCD of 24 & 9

    M N R

    24 9 6

    9 6 3

    6 3 0

    So 3 is the GCD of 24 and 9.

    Do we need to know the theory that Euclid used to come up with

    this algorithm in order to use it ?

    What intelligence is required to find the GCD using this algorithm ?

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    20/45

    3/11/13

    Idea Behind the Algorithms

    Once an algorithm behind a task has been

    discovered

    Don't need to understand the principles.

    Task is reduced to following the instructions.

    Intelligence is "encoded into the algorithm"

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    21/45

    3/11/13

    Algorithm Representation

    Syntax and Semantics

    Syntax refers to the representation itself.

    Semantics refers to the concept represented.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    22/45

    3/11/13

    Algorithm Requirements

    Requirements for an algorithm:

    Input

    Output

    Unambiguous

    Generality

    Correctness

    Finite

    Efficiency

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    23/45

    3/11/13

    Algorithm Representation

    Pseudo-code

    Flow chart

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    24/45

    3/11/13

    Pseudo-Code

    Pseudo-code is a semi-formal, English-like language with a

    limited vocabulary that can be used to design and describe

    algorithms.

    The main purpose of a pseudo-code is to define the

    procedural logic of an algorithm in a simple, easy-to-

    understand manner for its readers, who may or may not be

    proficient in computer programming.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    25/45

    3/11/13

    Pseudo-Code

    Used in designing algorithms.

    Used in communicating to users.

    Used in implementing algorithms as programs.

    Used in debugging logic errors in programs.

    Used in documenting programs for future maintenance and

    expansion purposes.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    26/45

    3/11/13

    Pseudo-Code

    Must have a limited vocabulary.

    Must be easy to learn.

    Must produce simple, English-like narrative notation.

    Must be capable of describing all algorithms, regardless of

    their complexity.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    27/45

    3/11/13

    Control Structures

    Sequence

    Selection

    Repetition

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    28/45

    3/11/13

    Sequence

    Series of steps or statements that are executed in the order

    they are written.

    Example:

    Read taxable income

    Read filing status

    Compute income tax

    Print income tax

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    29/45

    3/11/13

    Selection

    Defines one or two courses of action depending on the

    evaluation of a condition.

    A condition is an expression that is either true or false.

    Example

    if condition (is true)

    then-part

    else

    else-part

    end_if

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    30/45

    3/11/13

    Nested Selection

    if status is equal to 1

    print Single

    else

    if status is equal to 2

    print Married filing jointly

    else

    if status is equal to 3

    print Married filing separately

    end_if

    end_if

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    31/45

    3/11/13

    Repetition

    Specifies a block of one or more statements that are

    repeatedly executed until a condition is satisfied.

    Example:

    while condition

    loop-body

    end_while

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    32/45

    3/11/13

    Conventions

    Each pseudo-code statement consists of keywords that

    describe operations and some appropriate, English-like

    description of operands.

    Each statement should be written on a separate line.

    Continuation lines should be indented

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    33/45

    3/11/13

    Conventions II

    Sequence statements should begin with unambiguous words

    (compute, set, initialize).

    Selection statements then-part and else-part should be

    indented.

    Selection statements end with the keyword end_if.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    34/45

    3/11/13

    Convention III

    Repetition statements end with end_while.

    Loop-bodies are indented.

    All words in a pseudo-code statement must be chosen to be

    unambiguous, and as easy as possible to understand by

    non-programmers.

    Enclose comments between /* and */

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    35/45

    3/11/13

    Example 1

    If student's grade is greater than or equal to 60

    Print "passed"

    else

    Print "failed

    End_if

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    36/45

    3/11/13

    Example 2

    Set total to zero

    Set grade counter to one

    While grade counter is less than or equal to ten

    Input the next grade

    Add the grade into the total

    End_while

    Set the class average to the total divided by ten

    Print the class average.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    37/45

    3/11/13

    Example 3

    initialize passes to zero

    initialize failures to zero

    initialize student to one

    while student counter is less than or equal to ten

    input the next exam result

    if the student passed

    add one to passes

    else

    add one to failures

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    38/45

    3/11/13

    Set asterik counter to one

    Set outer while counter to one

    While out counter is less than or equal to 4

    set spaces counter to 1

    while spaces counter is less than 4

    print

    increment space by one

    end_while

    set asterik counter to one

    while asterik counter is less than or equal to odd multiple of outercounter

    print *

    increment asterik counter by one

    end_while

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    39/45

    3/11/13

    Basic Symbols

    Rounded box - use it to represent anevent which occurs automatically. Suchan event will trigger a subsequent action,for example `receive telephone call, ordescribe a new state of affairs.Rectangle or box - use it to represent anevent which is controlled within the

    process. Typically this will be a step oraction which is taken. In most flowchartsthis will be the most frequently usedsymbol.

    Diamond - use it to represent a decisionpoint in the process. Typically, thestatement in the symbol will require a

    `yes' or `no' response and branch todifferent parts of the flowchartaccordingly.

    Circle - use it to represent a point atwhich the flowchart connects withanother process. The name or referencefor the other process should appear

    within the symbol.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    40/45

    3/11/13

    Flowchart

    A flowchart is a diagrammatic representation that illustrates

    the sequence of operations to be performed to get the

    solution of a problem

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    41/45

    3/11/13

    Guide Lines

    Flowcharts are usually drawn using some standard symbols;

    however, some special symbols can also be developed when

    requiredStart or end of the program. Computational steps orprocessing function of a program

    Input or output operation, Decision making andbranching

    Connector or joining of two parts of program

    Magnetic TapeOff-page connector

    Flow line

    Annotation

    Display

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    42/45

    3/11/13

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    43/45

    3/11/13

    Example

    Draw a flowchart to find the largest of three numbers A,B,

    and C.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    44/45

    3/11/13

    Example

    Draw a flowchart to find the sum of first 50 natural

    numbers.

  • 7/29/2019 Algorithm & Data Structures Lec1(BET)

    45/45

    Imagination is more important than knowledge

    Knowledge is Limited.

    Imagination encircles the world

    Einstein