COMP 171 Data Structures and Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 171 Data Structures and Algorithms

Description:

?(g(n)) = {f(n) : there exist positive constants c and n0 such that 0f(n)cg(n) for all nn0} ... In class exercise: Improve the algorithm so it can achieve: ?(n2) O(n) ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 19
Provided by: vincen94
Category:

less

Transcript and Presenter's Notes

Title: COMP 171 Data Structures and Algorithms


1
COMP 171Data Structures and Algorithms
  • Tutorial 2
  • Analysis of algorithms

2
?-notation
  • Big-Oh
  • f(n) ?(g(n))
  • ?(g(n)) f(n) there exist positive constants
    c and n0 such that 0?f(n)?cg(n) for all n?n0
  • Upper bound
  • Worst-case running time

3
?-notation
  • Little-Oh
  • f(n) ?(g(n))
  • ?(g(n)) f(n) for any positive constants c
    and n0 such that 0?f(n)ltcg(n) for all n?n0
  • Non-tight upper bound

4
O-notation
  • Big-Omega
  • f(n) O(g(n))
  • O(g(n)) f(n) there exist positive constants
    c and n0 such that 0 ?cg(n)?f(n) for all n?n0
  • Lower bound
  • Best-case running time

5
?-notation
  • Little-Omega
  • f(n) ?(g(n))
  • ?(g(n)) f(n) for any positive constants c
    and n0 such that 0 ?cg(n)ltf(n) for all n?n0
  • Non-tight lower bound

6
T-notation
  • Theta
  • f(n) T(g(n))
  • T(g(n)) f(n) there exist positive constants
    c1, c2 and n0 such that 0?c1g(n)?f(n)?c2g(n) for
    all n?n0
  • Tight bound

7
Summary
Notation Constants ?n?n0
?(g(n)) ?c, n0, both gt 0 0 ? f(n) ? cg(n)
O(g(n)) ?c, n0, both gt 0 0 ? f(n) lt cg(n)
?(g(n)) ?c, n0, both gt 0 0 ? cg(n) ? f(n)
?(g(n)) ?c, n0, both gt 0 0 ? cg(n) lt f(n)
T(g(n)) ?c1, c2, n0, all gt 0 0 ? c1g(n) ? f(n) ? c2g(n)
8
Transitivity
  • f(n)T(g(n)), g(n)T(h(n)) ?f(n)T(h(n))
  • f(n)?(g(n)), g(n)?(h(n)) ?f(n)?(h(n))
  • f(n)O(g(n)), g(n)O(h(n)) ?f(n)O(h(n))
  • f(n)?(g(n)), g(n)?(h(n)) ?f(n)?(h(n))
  • f(n)?(g(n)), g(n)?(h(n)) ?f(n)?(h(n))

9
Reflexivity, Symmetry Transpose Symmetry
  • f(n)T(f(n))
  • f(n)?(f(n))
  • f(n)O(f(n))
  • f(n)T(g(n)) if and only if g(n)T(f(n))
  • f(n)?(g(n)) if and only if g(n)O(f(n))
  • f(n)?(g(n)) if and only if g(n)?(f(n))

10
Selection Sort
  • Input Array A of Size n
  • Output A sorted array A
  • Algorithm Find the smallest element of A and
    exchanging it with the element in A1. Then find
    the second smallest element of A and exchange it
    with A2. Continue for the first n-1 elements in
    A.

11
  • e.g. 5, 2, 4, 7, 3
  • Input 5, 2, 4, 7, 3
  • 1st iteration 2, 5, 4, 7, 3
  • 2nd iteration 2, 3, 4, 7, 5
  • 3rd iteration 2, 3, 4, 7, 5
  • 4th iteration 2, 3, 4, 5, 7
  • Output 2, 3, 4, 5, 7

12
  • 1 Find the smallest(m) in the unsorted part
  • 2 Swap with h
  • 3 Put m into the sorted part
  • 4 Back to 1 until unsorted part is size 1

Sorted Part
Unsorted Part
m
h
m
h
m
13
  • for i ? range 1
  • min value
  • min_pos value
  • for j ? range 2
  • find min
  • end for j
  • swap(value, value)
  • end for i

14
for i ? 1 to n-1 min infinity min_pos
0 for j ? i to n if Aj lt min then min
Aj min_pos j end if end for
j swap(Ai, Amin_pos) end for i
15
for i ? 1 to n-1 min infinity min_pos
0 for j ? i to n if Aj lt min then min
Aj min_pos j end if end for
j swap(Ai, Amin_pos) end for i
O(1)
O(n)
O(n)
O(1)
O(1)
16
  • ?(n2)
  • O(n2)?
  • T(n2)?
  • In class exercise
  • Improve the algorithm so it can achieve
  • ?(n2)
  • O(n)
  • Given a sorted input sequence, which sorting
    algorithm(s) can achieve O(n)?

17
Binary Search
..
..
..
..
..
..
18
  • If tree height is k
  • The number of elements is 2k1-1n
  • The number of comparison is at most k1
  • k1 log22k1 log2 ( n1 )
  • ?(? n)
Write a Comment
User Comments (0)
About PowerShow.com