Algorithms

Post on 18-Dec-2014

1324 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Event #3 for DevMix Team

Transcript

Program

By :- Eman El-ma’asarawi

2/24/2011 1

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

2/24/2011 2

2/24/2011 3

DS Algorithm+ Progra

m =

2/24/2011 4

list of instruction to achieve specific task.

Program

O/P instruction

Processing instruction

I/P instruction

2/24/2011 5

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

HW

OS

Compiler

Application (text editor)

Algorithm DS Methodologie

s

2/24/2011 7

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

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

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

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

2/24/2011 12

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

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

1-Array

Individual elements Array

n

12

0

2/24/2011 13

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

Exambel

1 2 34 56 2 78 0 10

0 1 2 3 4 5 6 7

2/24/2011 15

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

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

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

Which is fast array or linked list?Why?

2/24/2011 19

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

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

2/24/2011 22

Data rightleft

Data rightleftData righ

tleft

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

2/24/2011 23

Binary search tree op…

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

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

2/24/2011 25

Algorithmes

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

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

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

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

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

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

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

O(n) - the algorithm requires n multiplications.

Time complexity

2/24/2011 33

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

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

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

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

2

Time complexity

2/24/2011 37

2/24/2011 38

Quiz

2/24/2011 39

Towers of Hanoi

1 2 3

2/24/2011 40

1 2 3

2/24/2011 41

1 2 3

2/24/2011 42

1 2 3

2/24/2011 43

1 2 3

2/24/2011 44

1 2 3

2/24/2011 45

1 2 3

2/24/2011 46

1 2 3

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

2/24/2011 48

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

The End

2/24/2011 50

top related