Sie sind auf Seite 1von 45

Algorithm & Data Structures

Fall 2011

Click to edit Master subtitle style

3/11/13

My Profile
Name:
Syed

Muhammad Raza

Education:
MSc

Wireless Communication
Sweden , 2007-2009

LTH

BS

Computer Information Sciences


Pakistan , 2002-2006

PIEAS

Contact

Information:

3/11/13

Rules
Dont Mess With Me & I Wont Mess With You

3/11/13

Course Overview
Fundamentals Review Sorting Stacks Queues, Linked Trees Hashing
3/11/13

of Algorithms

of Programming and Searching

Priority Queues and Circular Queues

List

Text Book

No Text Book & Every Text Book

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 Total = = 10 Marks 100 Marks
3/11/13

Lets see what you got!

3/11/13

Algorithm

3/11/13

Problem Solving
Problem solving is the process of transforming the description of a problem into the solution of that problem by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques, and tools.

3/11/13

Algorithms
An

Algorithm is a step by step solution to a

problem
Why
For

bother writing an algorithm


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

rethink the problem.


So

others can solve the problem, even if they

know very little about the principles behind how the solution was derived.
3/11/13

Examples of Algorithms
Washing

machine instructions for a ready-to-assemble piece of

Instructions

furniture
A

Classic: GCD - Greatest Common Divisor - The

Euclidean Algorithm

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.
3/11/13

Observations
There We

are a finite number of steps.

are capable of doing each of the instructions. we have followed all of the steps, the

When

washing machine will wash the clothes and then will stop.
Are Do

all of the clothes clean ? we want the washing machine to run until all

of the clothes are clean ?


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.

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.

3/11/13

Final Definition

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

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 used to solve any problem of a particular type.

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 value of R and return to step 2, otherwise the greatest common divisor is the value currently assigned to N.

3/11/13

Finding GCD of 24 & 9


MNR 24 9 6 963 630 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 ?

3/11/13 What intelligence is required to find the GCD using this algorithm ?

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"

3/11/13

Algorithm Representation
Syntax

and Semantics

Syntax refers to the representation itself. Semantics refers to the concept represented.

3/11/13

Algorithm Requirements

Requirements for an algorithm:


Input Output Unambiguous Generality Correctness Finite Efficiency

3/11/13

Algorithm Representation

Pseudo-code Flow chart

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-tounderstand manner for its readers, who may or may not be proficient in computer programming.

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.

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.

3/11/13

Control Structures

Sequence Selection Repetition

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

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

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

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

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.

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 */

3/11/13

Example 1
If student's grade is greater than or equal to 60 Print "passed" else Print "failed End_if

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

counter

while asterik counter is less than or equal to odd multiple of outer print * increment asterik counter by one end_while

3/11/13

Basic Symbols
Rounded box - use it to represent an event which occurs automatically. Such an event will trigger a subsequent action, for example `receive telephone call, an Rectangle or box - use it to represent or describe a new is controlled within the event which state of affairs. process. Typically this will be a step or action which is it to represent a decision Diamond - use taken. In most flowcharts this will be the most frequently used point in the process. Typically, the symbol. statement in the symbol will require a `yes' or use it response and a point at Circle - `no' to represent branch to differentthe parts of flowchart which flowchart the connects with accordingly. another process. The name or reference for the other process should appear within the symbol. 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

3/11/13

Guide Lines

Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required
Start or end of the program. Computational steps or processing function of a program Input or output operation, Decision making and branching Connector or joining of two parts of program Magnetic Tape Off-page connector Flow line Annotation Display

3/11/13

3/11/13

Example

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

3/11/13

Example

Draw a flowchart to find the sum of first 50 natural numbers.

3/11/13

Imagination is more important than knowledge Knowledge is Limited. Imagination encircles the world
Einstein

3/11/13

Das könnte Ihnen auch gefallen