Lecture Notes on Decrease and Conquer | CS 3343, Study notes of Algorithms and Programming

Material Type: Notes; Class: Analysis Of Algorithms; Subject: Computer Science; University: University of Texas - San Antonio; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-daz-1
koofers-user-daz-1 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5: Decrease and Conquer
A journey of a thousand miles begins with a
single step. (Confucius)
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Decrease by a Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Decrease by a Constant Factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Insertion Sort 5
Insertion Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Illustration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Comments on Insertion Sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Depth-First Search and Breadth-First Search 8
Graph Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Graph Marking Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Depth-First Search Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Breadth-First Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
DFS Illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Comments on Graph Traversal Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 13
Miscellaneous Problems 14
Gray Code Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Fake Coin Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Multiplication a la Russe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The Josephus Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Selection Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1
Introduction
Decrease-and-conquer is an approach to solving a problem by:
Change an instance into one smaller instance of the problem.
Solve the smaller instance.
Convert the solution of the smaller instance into a solution for the larger
instance.
CS 3343 Analysis of Algorithms Chapter 5: Slide 2
Decrease by a Constant
Decrease-by-a-
constant decreases
the instance size
by 1(or some
other constant),
e.g., 210 = 2 29
CS 3343 Analysis of Algorithms Chapter 5: Slide 3
Decrease by a Constant Factor
Decrease-by-a-
constant-factor
decreases the
instance size by
half (or some
other fraction),
e.g., 210 = 2525.
CS 3343 Analysis of Algorithms Chapter 5: Slide 4
2
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Decrease and Conquer | CS 3343 and more Study notes Algorithms and Programming in PDF only on Docsity!

Chapter 5: Decrease and Conquer^ A journey of a thousand miles begins with asingle step. (Confucius)

Introduction................................................ 2Decrease by a Constant........................................ 3Decrease by a Constant Factor................................... 4 Insertion Sort

Insertion Sort Algorithm...................................... 5Illustration................................................ 6Comments on Insertion Sort.................................... 7 Depth-First Search and Breadth-First Search

Graph Traversal............................................ 8Graph Marking Algorithm..................................... 9Depth-First Search Algorithm.................................. 10Breadth-First Search Algorithm................................ 11DFS Illustration........................................... 12Comments on Graph Traversal Algorithms......................... 13 Miscellaneous Problems

Gray Code Problem........................................ 14The Fake Coin Problem...................................... 15Multiplication a la Russe..................................... 16The Josephus Problem...................................... 17The Selection Problem...................................... 18

Introduction Decrease-and-conquer

is an approach to solving a problem by:

^

Change an instance into one smaller instance of the problem. ^

Solve the smaller instance. ^

Convert the solution of the smaller instance into a solution for the largerinstance. CS 3343 Analysis of Algorithms

Chapter 5: Slide – 2

Decrease by a Constant Decrease-by-a-constant

decreases

the instance sizeby^

1 (or some other constant),e.g.,

∗^2

9

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 3

Decrease by a Constant Factor Decrease-by-a-constant-factor decreases theinstance size byhalf (or someother fraction),e.g.,

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 4

Insertion Sort

5

Insertion Sort Algorithm algorithm

InsertionSort

(A

[0..n

−^

1])

// Sorts a given array by insertion sort// Input: An array

A

of orderable elements

// Output: Array

A

[0..n

−^

1]^

in ascending order

for

i^ ←

to^

n^ −

do

v^ ←

A

[i] j^ ←

i^ −

while

j^

≥^

0 and

A

[j]^

v

do

A[j

+ 1]

A

[j]

j^ ←

j^ −

A[

j^ + 1]

v

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 5

Illustration CS 3343 Analysis of Algorithms

Chapter 5: Slide – 6

Comments on Insertion Sort ^

Insertion sort ensures

A

[0]

A[1]

A

[i^ −

1]

^

Insertion sort looks for correct position for

A

[i].

^

Insertion sort shifts values at and above correct position. ^

Worst Case: The number of comparisons is^ n−

1 ∑^ i=

i^ =

n(

n^ −

/^2

∈^

O(

(^2) n)

^

Best Case:

n

−^

n)^

comparisons if array is already sorted.

^

Average Case

(^2) n

/^4

comparisons.

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 7

Depth-First Search and Breadth-First Search

8

Graph Traversal ^

Graph traversal algorithms process all the vertices of a graph in a systematicfashion. ^

They are useful for many graph problems such as checking connectivity,checking acyclicity, connected components, finding articulation points, andtopological sorting. ^

First all the vertices are marked as unvisited. ^

Then an unvisited vertex is selected, marked as visited, and all unvisitedvertices reachable from that vertex are marked as visited. ^

Repeat above step until all vertices are visited. CS 3343 Analysis of Algorithms

Chapter 5: Slide – 8

Comments on Graph Traversal Algorithms ^

Each vertex is marked with 0 once and some positive integer once. ^

Each edge is processed once for directed graphs, twice for undirected graphs. –^

In DFS, edges from a vertex are processed after vertex is marked.

-^

In BFS, edges from a vertex are processed after vertex is dequeued. Eachvertex is enqueued once (right after it is marked). ^

All Cases: Each vertex and edge is processed once or twice, which implies Θ(

V^ +

E)

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 13

Miscellaneous Problems

14

Gray Code Problem ^

A Gray Code generates all possible bit strings of a given length by changingonly one bit each time. ^

One Gray Code for 3 bits is:

,^001

,^011

,^010

,^110

,^111

,^101

,^100

. This is

useful for generating all subsets of length 3. ^

An outline of the algorithm for length

n^

is:

-^

Generate Gray code for length

n^

−^

-^

Change

nth bit and output bit string.

-^

Generate Gray code for length

n^

−^

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 14

The Fake Coin Problem ^

The fake coin problem is to detect a single fake coin from a set of coins usinga balance scale. ^

Supposing the fake coin is lighter, we can repeatedly compare one half of a setto the other half, eliminating the heavier half. ^

This decreases the number of coins in half each iteration, so only

log

n 2

weighings are needed. ^

Consider variations of this problem, e.g., not known whether coin is lighter ofheavier, or 2 fake coins. CS 3343 Analysis of Algorithms

Chapter 5: Slide – 15

Multiplication a la Russe To multiply two pos. integers

n^

·^ m

by this method:

n^ ·

m

m^

if^ n

(n/

2)^

·^2

m^

if^ n

is even

(⌊n/

2 m

+^

m^

if^ n

is odd

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 16

The Josephus Problem ^

In the Josephus Problem, every other number is eliminated until one is left, forexample:^1

//^2

//^4

//^6

//^8

//^1

//^5

//^9

//^7

^

Let

J

(n) =

the last number for

n.

J(

n) =

if^ n

2 J

(n/

2)^

−^1

if^ n

is even

2 J

(⌊n/

if

n^

is odd

^

Computing

J(

n)^

as above is

Θ(log

n 2

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 17

The Selection Problem ^

The Selection Problem is finding the

kth smallest element out of

n^

elements.

^

One solution is to sort the elements, and then return

A

[k^

−^

1]. This is

n^ log

n)

^

Better is to modify quicksort to only sort the partition with

A[

k^ −

1]. This is

n)^

on average.

^

Suppose partitioning always splits in half, then the recurrence is: C(

n) =

C

(n/

n), which is

n)^

by the Master Theorem.

^

Even if

A

[k^

−^ 1]

is always in a “big” partition, say

10 , then

C(

n) =

C

(9n/

n)

is also

n).

CS 3343 Analysis of Algorithms

Chapter 5: Slide – 18