Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Analysis of Algorithms - Discrete Mathematics - Lecture Slides, Slides of Discrete Mathematics

During the study of discrete mathematics, I found this course very informative and applicable.The main points in these lecture slides are:Analysis of Algorithms, Precise Instructions, Properties of Algorithm, Input Values, Output Values, Time Complexity, Space Complexity, Asymptotic Time Complexity, Pseudocode, Informal Statement, Algorithm Complexity

Typology: Slides

2012/2013

Uploaded on 04/27/2013

ashwini
ashwini 🇮🇳

4.5

(18)

177 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Discrete Mathematics
CS 2610
September 23, 2008
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Analysis of Algorithms - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

Discrete Mathematics

CS 2610

September 23, 2008

2

Algorithms

An Algorithm is a finite set of precise instructions

for performing a computation or for solving a

problem.

Properties of an algorithm:

  • input: input values are from a specified set
  • output: output values are from a specified set
  • definiteness: each step is precisely defined
  • correctness: correct output produced
  • finiteness: takes a finite number of steps
  • effectiveness: each step is finite & exact
  • generality: applicable to various input sizes

4

Pseudocode

procedure procName(argument: type)

variable := expression

informal statement

begin

statements

end

{ comment }

5

Pseudocode

if condition then statement

[ else statement2]

for variable := initial value to final value

statement

while condition statement

return expression

procName(arg1,..,argn)

7

Example: Max Algorithm

procedure max( a 1 , a 2 , …, an: integers)

v := a 1 1

for i := 2 to n n-1 x

if ai > v then 1

v := ai? (max 1)

return v 1

How many times is each step executed?

Worst-Case: the input sequence is a strictly

increasing sequence

8

Searching Algorithms

Searching Algorithms Problem:

Find an element x in a list a 1 ,…, an (not necessarily

ordered)

Linear Search Strategy:

Examine the sequence one element after another

until all the elements have been examined or the

current element being examined is the element x.

10

Example: Linear Search

Average Case:

 x is the first element

 1 loop comparison

 x is the second element

 2 loop comparisons, 1 iteration of the loop

 x is the third element

 3 loop comparisons, 2 iterations of the loop

 x is nth element

 n loop comparisons, n-1 iterations of the loop

11

Binary Search

Problem: Locate an element x in a sequence of

sorted elements in non-decreasing order.

Strategy: On each step, look at the middle

element of the remaining list to eliminate half of

it, and quickly zero in on the desired element.

13

Binary Search

Suppose n=2k

The loop is executed k times, where k=log 2 (n)

14

Linear Search vs. Binary Search

Linear Search Time Complexity:

 T(n) is O(n)

Binary Search Time Complexity:

 T(n) is O(log2n)

16

Bubble Sort

Smallest elements “float” up to the top (front) of

the list, like bubbles in a container of liquid

Largest elements sink to the bottom (end).

See the animation at:

http://math.hws.edu/TMCM/java/xSortLab

17

Example: Bubble Sort

procedure bubblesort( a 1 , a 2 , …, an : distinct integers) for i = 1 to n- for j = 1 to n-i if ( aj > aj+1 ) then swap aj and aj+

Worst-Case : The sequence is sorted in decreasing order

At step i •The loop condition of the inner loop is executed n – i + 1 times. •The body of the loop is executed n – i times

19

Insertion Sort

procedure insertSort( a 1 , a 2 , …, an : distinct integers) for j=2 to n begin i=j - 1 while i > 0 and ai > ai+ swap ai and ai+ i=i- end

Worst-Case: The sequence is in decreasing order At step j, the while loop condition is executed j times the body of the loop is executed j-1 times