Understanding Critical Sections and Algorithmic Complexity in Computer Science, Study notes of Computer Science

An overview of critical sections in algorithms and discusses the importance of analyzing their complexity. It covers the goal of algorithmic complexity analysis, the approach to finding critical sections, and examples of critical sections in various algorithms. The document also introduces recursive algorithms and the concept of asymptotic complexity categories.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-57evhmsro6
koofers-user-57evhmsro6 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CMSC 132:
Object-Oriented Programming II
Algorithmic Complexity II
Department of Computer Science
University of Maryland, College Park
2
Overview
Critical sections
Comparing complexity
Types of complexity analysis
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Critical Sections and Algorithmic Complexity in Computer Science and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Algorithmic Complexity II

Department of Computer Science

University of Maryland, College Park

2

Overview

Critical sections

Comparing complexity

Types of complexity analysis

3

Analyzing Algorithms

Goal

Find asymptotic complexity of algorithm

Approach

Ignore less frequently executed parts of algorithm Find critical section of algorithm Determine how many times critical section is executed as function of problem size

4

Critical Section of Algorithm

Heart of algorithm

Dominates overall execution time

Characteristics

Operation central to functioning of program Contained inside deeply nested loops Executed as often as any other part of algorithm

Sources

Loops Recursion

7

Critical Section Example 2

Code (for input size n)

1. A

  1. for (int i = 0; i < n; i++)
  2. B
  3. for (int j = 0; j < n; j++)
  4. C
  5. D

Code execution

A โ‡’

B โ‡’

Time โ‡’

C โ‡’

D โ‡’

8

Critical Section Example 2

Code (for input size n)

1. A

  1. for (int i = 0; i < n; i++)
  2. B
  3. for (int j = 0; j < n; j++)
  4. C
  5. D

Code execution

A โ‡’ once B โ‡’ n times

Time โ‡’ 1 + n + n 2 + 1 = O(n^2 )

critical

section

C โ‡’ n^2 times D โ‡’ once

9

Critical Section Example 3

Code (for input size n)

1. A

  1. for (int i = 0; i < n; i++)
  2. for (int j = i+1; j < n; j++)
  3. B

Code execution

A โ‡’

B โ‡’

Time โ‡’

10

Critical Section Example 3

Code (for input size n)

1. A

  1. for (int i = 0; i < n; i++)
  2. for (int j = i+1; j < n; j++)
  3. B

Code execution

A โ‡’ once B โ‡’ ยฝ n (n-1) times

Time โ‡’ 1 + ยฝ n^2 = O(n 2 )

critical

section

13

Critical Section Example 5

Code (for input size n)

  1. for (int i = 0; i < n; i++)
  2. for (int j = 0; j < n; j++)
  3. A
  4. for (int i = 0; i < n; i++)
  5. for (int j = 0; j < n; j++)
  6. B

Code execution

A โ‡’

B โ‡’

Time โ‡’

14

Critical Section Example 5

Code (for input size n)

  1. for (int i = 0; i < n; i++)
  2. for (int j = 0; j < n; j++)
  3. A
  4. for (int i = 0; i < n; i++)
  5. for (int j = 0; j < n; j++)
  6. B

Code execution

A โ‡’ n^2 times B โ‡’ n^2 times

Time โ‡’ n^2 + n 2 = O(n 2 )

critical

sections

15

Critical Section Example 6

Code (for input size n)

  1. i = 1
  2. while (i < n)
  3. A
  4. i = 2 ร— i
  5. B

Code execution

A โ‡’

B โ‡’

Time โ‡’

16

Critical Section Example 6

Code (for input size n)

  1. i = 1
  2. while (i < n)
  3. A
  4. i = 2 ร— i
  5. B

Code execution

A โ‡’ log(n) times B โ‡’ 1 times

Time โ‡’ log(n) + 1 = O( log(n) )

critical

section

19

Recursive Algorithms

Definition

An algorithm that calls itself

Components of a recursive algorithm

  1. Base cases

Computation with no recursion

  1. Recursive cases

Recursive calls Combining recursive results

20

Recursive Algorithm Example

Code (for input size n)

  1. DoWork (int n)
  2. if (n == 1)
  3. A
  4. else
  5. DoWork(n/2)
  6. DoWork(n/2)

recursive

cases

base

case

21

Asymptotic Complexity Categories

Complexity Name Example

O(1) Constant Array access O(log(n)) Logarithmic Binary search O(n) Linear Largest element O(n log(n)) N log N Optimal sort O(n^2 ) Quadratic 2D Matrix addition O(n^3 ) Cubic 2D Matrix multiply O(nk) Polynomial Linear programming O(kn^ ) Exponential Integer programming

From smallest to largest

For size n, constant k > 1

22

Comparing Complexity

Compare two algorithms

f(n), g(n)

Determine which increases at faster rate

As problem size n increases

Can compare ratio

If โˆž, f() is larger

If 0, g() is larger If constant, then same complexity

lim nโ†’ โˆž

f(n)

g(n)

25

2D Matrix Multiplication Example

Problem

C = A * B

Lower bound

ฮฉ(n^2 ) Required to examine 2D matrix

Upper bounds

O(n^3 ) Basic algorithm O(n2.807^ ) Strassenโ€™s algorithm (1969) O(n2.376^ ) Coppersmith & Winograd (1987)

Improvements still possible (open problem)

Since upper & lower bounds do not match

ร— (^) =

26

Additional Complexity Categories

Name Description

NP Nondeterministic polynomial time (NP) PSPACE Polynomial space EXPSPACE Exponential space Decidable Can be solved by finite algorithm Undecidable Not solvable by finite algorithm

Mostly of academic interest only

Quadratic algorithms usually too slow for large data Use fast heuristics to provide non-optimal solutions

27

NP Time Algorithm

Polynomial solution possible

If make correct guesses on how to proceed

Required for many fundamental problems

Boolean satisfiability Traveling salesman problem (TLP) Bin packing

Key to solving many optimization problems

Most efficient trip routes Most efficient schedule for employees Most efficient usage of resources

28

NP Time Algorithm

Properties of NP

Can be solved with exponential time Not proven to require exponential time Currently solve using heuristics

NP-complete problems

Representative of all NP problems Solution can be used to solve any NP problem Examples Boolean satisfiability Traveling salesman