The Role of Algorithms in Computing Chapter 1 1 CPTR 430 - - PowerPoint PPT Presentation

the role of algorithms in computing
SMART_READER_LITE
LIVE PREVIEW

The Role of Algorithms in Computing Chapter 1 1 CPTR 430 - - PowerPoint PPT Presentation

The Role of Algorithms in Computing Chapter 1 1 CPTR 430 Algorithms The Role of Algorithms in Computing What is an Algorithm? Any well-defined computational procedure that takes some value (or set of values)its input and produces some


slide-1
SLIDE 1

The Role of Algorithms in Computing

Chapter 1

CPTR 430 Algorithms The Role of Algorithms in Computing

1

slide-2
SLIDE 2

What is an Algorithm?

■ Any well-defined computational procedure that takes some value (or set

  • f values)—its input—and produces some value (or set of values)—its
  • utput

■ A sequence of computational steps that transforms its input into its

  • utput

■ A tool for solving a well-specified computational problem

CPTR 430 Algorithms The Role of Algorithms in Computing

2

slide-3
SLIDE 3

Example—Sorting

■ Input: a sequence of n numbers

  • a0

a1

✁ ✂ ✂ ✂ ✁

an

1

■ Output: a permutation (re-ordering)

  • a
✆ ✁

a

1

✁ ✂ ✂ ✂ ✁

a

n

1

such that

a

✆ ✝

a

1

✝ ✞ ✞ ✞ ✝

a

n

1 ■ Instance of problem:

Input:

  • 31

41

59

26

41

58

Output:

  • 26

31

41

41

58

59

CPTR 430 Algorithms The Role of Algorithms in Computing

3

slide-4
SLIDE 4

Algorithms for Sorting

■ Sorting is an important operation in computer science

Many programs sort as an intermediate step

■ Thus, a large number of good sorting algorithms have been developed ■ Which is best?

CPTR 430 Algorithms The Role of Algorithms in Computing

4

slide-5
SLIDE 5

Algorithm Correctness

■ An algorithm is correct if, for every input instance, it halts with the

correct output

■ A correct algorithm solves the given computational problem ■ An incorrect algorithm ❚ might not halt on some input instances, or ❚ might halt with incorrect output ■ Can an incorrect algorithm ever be useful?

CPTR 430 Algorithms The Role of Algorithms in Computing

5

slide-6
SLIDE 6

Expressing Algorithms

Algorithms can be expressed in

■ English (or any other natural language) ■ a computer program ■ a hardware design

Requirement: The specification must provide a precise description of the computational procedure to be followed

CPTR 430 Algorithms The Role of Algorithms in Computing

6

slide-7
SLIDE 7

Data Structures

■ A data structure is a way to store and organize data in order to facilitate

access and modifications

■ No single data structure works well for all purposes ❚ It is advantageous to be familiar with a wide variety of data structures

and be aware of their strengths and weaknesses

CPTR 430 Algorithms The Role of Algorithms in Computing

7

slide-8
SLIDE 8

Hard Problems

■ We are most concerned about efficient algorithms

An algorithm’s efficiency is often measured by its running time

■ Some problems have no known efficient solutions ❚ These are called NP-complete problems ❚ No one has proven that efficient algorithms for NP-complete

problems do not exist

❚ If an efficient solution can be found for one NP-complete problem,

then an efficient solution exists for all other NP-complete problems!

❚ NP-complete problems are very similar to problems that have an

efficient solution A small change to the problem statement can cause a big change to the efficiency of the best known algorithm

CPTR 430 Algorithms The Role of Algorithms in Computing

8

slide-9
SLIDE 9

Why study NP-Complete Problems?

■ They often arise in everyday situations ■ If you are asked to produce an efficient algorithm to to find an exact

solution, . . .

■ If you can show that the problem is equivalent to a known NP-complete

problem, then you can devote your efforts to developing an algorithm that gives a good approximation of the solution The traveling salesman problem (TSP) is one example of a common NP- complete problem

CPTR 430 Algorithms The Role of Algorithms in Computing

9

slide-10
SLIDE 10

Algorithm Efficiency

■ Computing resources—time and space—are bounded resources ■ Efficient algorithms use computing resources wisely ■ Algorithms to solve the same problem often differ drastically in their

efficiencies

■ Compare insertion sort and merge sort

CPTR 430 Algorithms The Role of Algorithms in Computing

10

slide-11
SLIDE 11

Insertion Sort vs. Merge Sort

■ Insertion sort takes time

  • c1 n2 to sort n elements

■ Merge sort takes time

  • c2 n
✁ ✂

n to sort n elements

■ c1 and c2 are constants that do not depend on n ■ Usually c1

c2

■ Comparing insertion sort vs. merge sort, note that n

☎ ✁ ✂

n

■ Given a large enough n, the

✁ ✂

n

n advantage of merge sort will

  • verwhelm the c1

c2 advantage of insertion sort

CPTR 430 Algorithms The Role of Algorithms in Computing

11

slide-12
SLIDE 12

Concrete Example

■ The hardware: ❚ 1 GHz Pentium III vs. 10 MHz 386 ❚ In raw processing speed the Pentium is 100 times faster than the 386 ■ The algorithms: ❚ Insertion sort vs. merge sort ❚ Insertion sort implemented in assembly language by a team of

algorithm/assembler experts, so c1

  • 2

❚ Merge sort implemented in C with a non-optimizing compiler by an

average programmer, so c2

  • 50

■ The task: sort 1,000,000 integers

CPTR 430 Algorithms The Role of Algorithms in Computing

12

slide-13
SLIDE 13

The Results

Pentium:

2

  • 106

2instructions

109instructions

second

  • 2000 seconds

386:

50

  • 106
✁ ✁ ✂

106 instructions 107instructions

second

  • 100 seconds

The merge sort is 20 times faster than the insertion sort! For 10,000,000 numbers: 2.3 days vs. 20 minutes!

CPTR 430 Algorithms The Role of Algorithms in Computing

13