Top Banner
Program By :- Eman El- ma’asarawi 2/24/2011 1
50
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Algorithms

Program

By :- Eman El-ma’asarawi

2/24/2011 1

Page 2: Algorithms

What is Program? How to make program? How to think about the program? Steps for making a good project.

2/24/2011 2

Page 3: Algorithms

2/24/2011 3

Page 4: Algorithms

DS Algorithm+ Progra

m =

2/24/2011 4

Page 5: Algorithms

list of instruction to achieve specific task.

Program

O/P instruction

Processing instruction

I/P instruction

2/24/2011 5

Page 6: Algorithms

H/W S/W

Os Compiler Text editor

Idea ware Algorithm DS Programming Methodologies

Structure Design Oop

S/W Concepts Information hiding Data encapsulation Data abstraction

Programming Toolboxes

2/24/2011 6

Page 7: Algorithms

HW

OS

Compiler

Application (text editor)

Algorithm DS Methodologie

s

2/24/2011 7

Page 8: Algorithms

Data :- factual information Structure :- Arrangement or relationship of

elements as particles

Data structure :-A means of storing a collection of data

DS (Data Structure)

2/24/2011 8

Page 9: Algorithms

A data structure can be viewed as consisting of a set of algorithms for performing operations on the data it stores.

DS cont…

2/24/2011 9

Page 10: Algorithms

Variable:-data storage location that has a

value that can change during program execution.

Constant:-fixed value that can’t change.

2/24/2011 10

Page 11: Algorithms

Applications of Data Structure

2/24/2011 11

Linear

DS

Non-Linear

Sequential

Linked

arra

y queu

e

linke

dlis

tsstac

k

Link

edst

acks

Link

edqu

eues

Tree

Gra

phs

Page 12: Algorithms

2/24/2011 12

1-Array2-Linked List3-Stack4-Queue5-Tree

Page 13: Algorithms

Is a data structure where all elements are stored in contiguous memory

1-Array

Individual elements Array

n

12

0

2/24/2011 13

Page 14: Algorithms

Random access Fixed size Less efficient used on data stored in array

Array properties

Array size

Element size

1-in bytes2-the number of element

Data type of array

2/24/2011 14

Page 15: Algorithms

Exambel

1 2 34 56 2 78 0 10

0 1 2 3 4 5 6 7

2/24/2011 15

Page 16: Algorithms

2-linked list

List head /Dummy Node

Data

Null

Is a data structure in which the first element is (head) stored on its own

2/24/2011 16

Page 17: Algorithms

The size not fixed grow/shrink The extra space is needed to store the

pointer of each element More time Implement linked list use array but not

efficient Complex to code and manage

Linked list properties

2/24/2011 17

Page 18: Algorithms

Appending Nodeadd Node to the end of the list.

Traversingcheck the linked list.

Inserting Nodeadd Node in the middle of a list.

Deleting Node from memory Modified links

Destroy

Operation on linked list

2/24/2011 18

Page 19: Algorithms

Which is fast array or linked list?Why?

2/24/2011 19

Page 20: Algorithms

Is a data structure consisting of data nodes connected to each other with pointers.

Each node connected to 2 or more other nodes.

2/24/2011 20

5-Trees

Page 21: Algorithms

The order of the tree:-is the max number of nodes to which any

single node connected. binary tree:-

is the simplest tree is of order 2.each child have two pointers.

Root node:-the topmost node in the tree.

leaf node:-node with no children.

2/24/2011 21

Tree properties

Page 22: Algorithms

2/24/2011 22

Data rightleft

Data rightleftData righ

tleft

Page 23: Algorithms

Create binary tree Inserting Node Traversing the tree recursion Searching the tree Deleting the tree

2/24/2011 23

Binary search tree op…

Page 24: Algorithms

Logical sequence of steps describe complete solution to solve problem in finite amount of time

May involve alternation, iteration or recursion

More than one algorithm possible for the same task

Algorithms

2/24/2011 24

Page 25: Algorithms

Sorting Searching-Insertion sort -linear search-Merge sort -Binary search-Heap sort-Quick sort

2/24/2011 25

Algorithmes

Page 26: Algorithms

What resources are required to accomplish the task

How one algorithm compares with other algorithms

How much time or space is required Measured in terms of common basic

operations

Efficiency of Algorithms

2/24/2011 26

Page 27: Algorithms

Worst-case: (usually) T(n) = maximum time of algorithm on

any input of size n. Average-case: (sometimes) T(n) = expected time of algorithm over

all inputs of size n. Need assumption of statistical distribution of

inputs. Best-case: (bogus) Cheat with a slow algorithm that works

fast on some input.

2/24/2011 27

Kinds of analyses

Page 28: Algorithms

Running time: On a particular input, it is the number

of primitive operations (steps) executed. One line may take a different amount of

time than another, but each execution of line i takes

the same amount of time c . Running time of the algorithm is

Ʃ (cost of statement)*(number of times statement is

executed)

2/24/2011 28

Page 29: Algorithms

How efficiency varies with the size of the task

E.g. if the size of the task increases linearly, do the efficiency measures increase linearly, quadratically, exponentially,...?

Expressed in terms of standard functions of n

Complexity

2/24/2011 29

Page 30: Algorithms

notations

2/24/2011 30

Big o

Big ƟBig Ω

F(n)<=g(n) F(n)>=g(n) F(n)=g(n)C1g(n)<=f(n)<=c2g(n)

Data shapeEx - insertion sort

best case

Data methodEx – insertion sort

worst case

Page 31: Algorithms

PseudoCode conventions

2/24/2011 31

Indentation indicates block structure. While, for, repeat , if , then, and else.

indicates comment. indicates assignment. Variables are local by default. Array elements accessed as in A[i].

Page 32: Algorithms

Calculate x^n, where n is an integer greater than 0.

Algorithm

1. Set r = 1 2. Set i = 1 3. Repeat 3.1 If i=n then terminate loop 3.2 Multiply r by x 3.3 Add 1 to i 4. Exit with result r

ExampleSimple Power Algorithm

2/24/2011 32

Page 33: Algorithms

O(n) - the algorithm requires n multiplications.

Time complexity

2/24/2011 33

Page 34: Algorithms

Formulate a Concise Specification of the Problem

Design a high-level strategy for a solution Refine steps 1 and 2 if necessary Complete the details of the design Implement in the chosen language

How to Approach Recursive Algorithms

2/24/2011 34

Page 35: Algorithms

Calculate x^n, where n is an integer greater than 0.

Recursive Algorithm

1. If n=0 then 1.1 set r = 1 2. Else 2.1 set p = n/2 (dropping any remainder) 2.2 set r = (xp)2 2.3 if n is odd then 2.3.1 multiply r by x 3. Exit with result r

Recursive Power Algorithm

2/24/2011 35

Page 36: Algorithms

static int power (int x, int n) int r; if (n == 0) r = 1; else r = power(x,n/2); r = r*r; if (n%2 != 0) r *= x; return r;

2/24/2011 36

implementation

Page 37: Algorithms

O(log n) - the algorithm requires approximately log n multiplications.

2

Time complexity

2/24/2011 37

Page 38: Algorithms

2/24/2011 38

Page 39: Algorithms

Quiz

2/24/2011 39

Towers of Hanoi

1 2 3

Page 40: Algorithms

2/24/2011 40

1 2 3

Page 41: Algorithms

2/24/2011 41

1 2 3

Page 42: Algorithms

2/24/2011 42

1 2 3

Page 43: Algorithms

2/24/2011 43

1 2 3

Page 44: Algorithms

2/24/2011 44

1 2 3

Page 45: Algorithms

2/24/2011 45

1 2 3

Page 46: Algorithms

2/24/2011 46

1 2 3

Page 47: Algorithms

2/24/2011 47

Solution' ≡ shortest path Recursive Solution:

1. Identify biggest discrepancy (=disk N)

2. If moveable to goal peg Then move Else

3. Subgoal: set-up (N−1)-disk tower on non-goal peg.

4. Go to 1. ...

Page 48: Algorithms

2/24/2011 48

Page 49: Algorithms

2/24/2011 49

SDLC (s/w Development life cycle)

Problem analysis

Requ

irem

ent d

efini

tions

Desig

nIm

plem

enta

tion

Testing

Delivery

Usi

ng p

rogr

am

Maintenance

Page 50: Algorithms

The End

2/24/2011 50