• No results found

Lecture 5.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lecture 5.pptx"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

Algorithms

Zaheer Ahmad (

[email protected]

)

(2)

Algorithms

Set of well define rules or recipe for solving some computation problems

You have bunch of number and you want to rearrange them may be in sorted order

(3)

Why Study Algorithms?

Important for all branches of computer science

Understanding the basics of algorithms and data structures are essentials in performing

serious work in computer science

(4)

Examples

Routing and communication networks based on shortest path algorithms

Computer graphics need geometric algorithms

Database industry relies on balanced search tree data structures

Computation biology uses dynamic programming algorithms to measure GNOME similarities

and the list goes on…

Search engines like google uses the page rank algorithms currently

(5)

Introduction: Integer

multiplications

1. We all did learn multiplication in our basic classes in which we did get 2 numbers as input and we calculate output

2. So we knew some sets of predefined steps to multiply which is actually an algorithm

Input

: two n-digit number x,y

(6)

The Grade-School Algorithm

In third grade what we called this algorithms

is Correct one.

No matter what integer you use in x and y if

you carry out this procedure/algorithm

All intermediate computation done properly

Then the algorithm terminate with the

product of x.y.

But you probably did not think about was the

amount of time needed to carry out this algorithm

22712

17034X

5678

X 1234

11356XX

5678XXX

(7)

Informal

Analysis of our Multiplication

Algorithm

1. Let’s begin with the first partial product the top row

2. How did we compute this number

1. Well we multiply 4 with each number 5678

2. So there were 4 basic operations

3. Plus we have to deal the carry outs for additions

1. Almost twice timed the number of digits at the first <=2n Operations

4. Still we need to add these rows

22712

17034X

5678

X 1234

11356XX

5678XXX

7006652

<= 2n Operations

n rows

2n(operations each) x n (rows)

(8)

Informal

Analysis of our Multiplication

Algorithm

1. So # of operations overall <= constant . n2

(9)

The Algorithm Designer’s Code

Perhaps the most important principle for the good algorithm designer is to refuse to be content.”

Aho, HopcroF, and Ullman, The Design and Analysis of Computer Algorithms, 1974

Can we do better?

(10)

Karatsuba Multiplication

Step1: compute a.c=672

Step2: computer b.d=2652

Step3: compute (a+b)(c+d)=134x46=6164

Step4: step3-step2-step1=2840

Step5:

6720000

2652

284000

7006652

X = 5 6 7 8

Y = 1 2 3 4

b

c a

(11)

Guiding Principal #1

Worst Case Analysis (Big Oh)

our running time bound holds for every input of n or our max time

Average case analysis

analyze the average running time of the algorithm

Bench marks

(12)

Worst case analysis (Big Oh

Notation)

We do not take assumptions

Particularly appropriate for general purpose algorithms about which we do not have enough knowledge

(13)

Guiding Principal #2

We won’t pay attention to the constants

Justification:

Way easier if we ignore constants

Constants depend on architecture/compiler/programmer own way

(14)

Guiding Principal #3

Asymptotic Analysis:

we focus on large values rather smaller ones mean large input size Only big problems are interesting like how big problems we can solve while computer are

becoming faster and faster

Like merg sort time is say 6nlog2n+n

(15)

Justification of principal #3

Insertion Sort

(16)

Justification of principal #3

Insertion Sort

(17)

What is a Fast Algorithm?

we will consider the three guiding principal for this purpose

(18)

Asymptotic Analysis (Motivation)

Basic vocabulary for the analysis and design of algorithms e.g. Big Oh notation

Sharp enough to make comparison between algorithms especially for large inputs

High Level Idea

(19)

Example: Loop

Problem: search an integer in array

 Given A an array of length n

And t an integer

For i=1 to n

If(a[i]==t) return true Else

rerutn false

What is the running time of this algorithm?

1. Big O(n) 3. Big O(nlogn)

(20)

Example: Two Loops

Problem: search an integer in both arrays

 Given A,B an array of length n

And t an integer

For i=1 to n

If(a[i]==t) return true

For i=1 to n

If(b[i]==t) return true Else rerut false

What is the running time of this algorithm?

(21)

Example: Nested Loops

Problem: search for duplicate values in arrays

 Given A,B arrays of length n

And t an integer

For i=1 to n

For j=1 to n

If(a[i]==b[j]) return true Else rerun false

What is the running time of this algorithm?

1. Big O(n) 3. Big O(nlogn)

(22)

Example: Nested Loops (II)

Problem: search for duplicate values in single array

 Given A arrays of length n

And t an integer

For i=1 to n

For j=i+1 to n

If(a[i]==a[j]) return true Else rerun false

What is the running time of this algorithm?

(23)

Lower bound (Big Omega)

T(n)= Ω(f(n))

If and only if constants c and no

such that

(24)
https://sites.google.com/site//datastructuresabc/

References

Related documents

The performance of an algorithm can be analyzed in terms of its worst case or average case behavior. Worst case analysis is a pessimis- tic approach, since it has

We do an asymptotic worst-case analysis noting the running time in Big-(O, Ω, Θ) notation and use it to compare algorithms..?. How do we describe

WORKSHOP ON AIR ACCIDENT AND INCIDENT INVESTIGATION AND PREVENTION EUROMED AVIATION PROJECT – BRUSSELS -13

Ideas renovadoras que, como veremos, se pudieron poner en marcha durante las etapas de gestión munici- pal del propio Rincón de Arellano (1958-1969) sobre todo las referidas a

You can return to the Panel Navigator™ at anytime and from within any module by clicking on the Panel Navigator text button on the Menu Bar or you can return to the last selected

As students Credit system steadily, tuition fee loans and grants in the form of flexible and diverse, increasingly rich community activities, physical health and mental

An ideal WCET tool would have all of its aspects in the top quadrants (making it suitably fast for interactivity), so we are faced with two choices: 1) Devote the effort needed

Based on the expression of the tachykinin precursor in the low oxygen and high carbon dioxide sensing neurons, it would be interesting to test if avoidance behavior