Time Complexity - 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:Time Complexity, Complexity of Algorithms, Execution Time, Space Complexity, Worst Case Analysis, Division of Integers, Number of Comparisons, Binary Search, Average Case Complexity, Complexity of Bubble Sort

Typology: Slides

2012/2013

Uploaded on 04/27/2013

atasi
atasi 🇮🇳

4.6

(32)

134 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE115/ENGR160 Discrete Mathematics
03/10/11
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Time Complexity - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

CSE115/ENGR160 Discrete Mathematics

3.3 Complexity of algorithms

• Algorithm

  • Produce correct answer
  • Efficient

• Efficiency

  • Execution time (time complexity)
  • Memory (space complexity)

• Space complexity is related to data structure

Example

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

max := a 1

for i:= 2 to n

if max < ai then max := ai

{ max is the largest element}

• There are 2(n-1)+1=2n-1 comparisons, the

time complexity is 𝛳(n) measured in terms of

the number of comparisons

Example

  • procedure linear search ( x:integer, a 1 , a 2 , …, a (^) n : distinct integers) i := 1 while (i≤ n and x≠ai ) i:=i+ if i < n then location := n else location:= { location is the index of the term equal to x, or is 0 if x is not found}
  • At most 2 comparisons per iteration, 2n+1 for the while loop and 1 more for if statement. At most 2n+2 comparisons are required (^5)

Time complexity of binary search

  • For simplicity, assume n=2k,k=log 2 n
  • At each iteration, 2 comparisons are used
  • For example, 2 comparisons are used when the list has 2k- elements, 2 comparisons are used when the list has 2k-2, …, 2 comparisons are used when the list has 2^1 elements
  • 1 comparison is ued when the list has 1 element, and 1 more comparison is used to determine this term is x
  • Hence, at most 2k+2=2log 2 n +2 comparisons are required
  • If n is not a power of 2, the list can be expanded to 2k+1, and it requires at most 2 log n+2 comparisons
  • The time complexity is at most 𝛳(log n)

Average case complexity

  • Usually more complicated than worst-case analysis
  • For linear search, assume x is in the list
  • If x is at 1st^ term, 3 comparisons are needed (1 to determine the end of list, 1 to compare x and 1st^ term, one outside the loop)
  • If x is the 2nd^ term, 2 more comparisons are needed, so 5 comparisons are needed
  • In general, if x is the i-th term, 2 comparisons are used at each of the i-th step of the loop, and 1 outside the loop, so 2i+ comparisons are used
  • On average , (3+5+7+…+2n+1)/n=(2(1+2+3+…n)+n)/n=n+2, which is 𝛳(n)

Complexity of bubble sort

procedure bubble sort ( a 1 , a 2 , …, a (^) n : real numbers with n≥2)

for i:=1 to n-

for j:=1 to n-i if a (^) j >a (^) j+1 then interchange a (^) j and a (^) j+ { a 1 , a 2 , …, a (^) n is in increasing order }

  • When the i-th pass begins, the i-1 largest elements are guaranteed to be in the correct positions
  • During this pass, n-i comparisons are used,
  • Thus from 2nd^ to (n-1)-th steps,

(n-1)+(n-2)+…+2+1=(n-1)n/2 comparisons are used

  • Time complexity is always 𝛳(n^2 )

Insertion sort

procedure insertion sort ( a 1 , a 2 , …, an : real numbers with n≥2) i :=1 (left endpoint of search interval) j := 1 (right end point of search interval) for j:=2 to n begin i:= while a (^) j >a (^) i i:=i+ m:=aj for k:=0 to j-i- aj-k:= a (^) j-k- a (^) i := m end {a 1 , a 2 , …, a (^) n are sorted}

Understanding complexity

Tractable

• A problem that is solvable by an algorithm

with a polynomial worst-case complexity is

called tractable

• Often the degree and coefficients are small

• Intractable problems may have low average-

case time complexity, or can be solved with

approximate solutions

NP-complete problems

• NP : problems for which a solution can be

checked in polynomial time

• NP (nondeterministic polynomial time)

• NP-complete problems : if any of these

problems can be solved by a polynomial

worst-case time algorithm, then all problems

in the class NP can be solved by polynomial

worst cast time algorithms

NP-complete problems

  • The satisfiability problem is an NP-complete problem
  • We can quickly verify that an assignment of truth

values to the variables of a compound proposition

makes it true

  • But no polynomial time algorithm has been

discovered

  • It is generally accepted, though not proven, that no

NP-complete problem can be solved in polynomial

time

17

E =( x 1 ∨¬ x 2 ∨¬ x 3 )∧( x 1 ∨ x 2 ∨ x 4 )