



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Analysis Of Algorithms; Subject: Computer Science; University: University of Texas - San Antonio; Term: Unknown 1989;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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
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
decreases
the instance sizeby^
1 (or some other constant),e.g.,
9
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 3
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 4
5
InsertionSort
[0..n
// Sorts a given array by insertion sort// Input: An array
of orderable elements
// Output: Array
[0..n
in ascending order
for
i^ ←
to^
n^ −
do
v^ ←
[i] j^ ←
i^ −
while
j^
0 and
[j]^
v
do
A[j
[j]
j^ ←
j^ −
j^ + 1]
v
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 5
Chapter 5: Slide – 6
Insertion sort ensures
[i^ −
^
Insertion sort looks for correct position for
[i].
^
Insertion sort shifts values at and above correct position. ^
Worst Case: The number of comparisons is^ n−
1 ∑^ i=
i^ =
n(
n^ −
(^2) n)
^
Best Case:
n
n)^
comparisons if array is already sorted.
^
Average Case
(^2) n
comparisons.
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 7
8
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
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 Θ(
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 13
14
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:
. 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 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
n^
·^ m
by this method:
n^ ·
m
m^
if^ n
(n/
m^
if^ n
is even
(⌊n/
2 m
m^
if^ n
is odd
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 16
In the Josephus Problem, every other number is eliminated until one is left, forexample:^1
^
Let
(n) =
the last number for
n.
n) =
if^ n
(n/
if^ n
is even
(⌊n/
if
n^
is odd
^
Computing
n)^
as above is
Θ(log
n 2
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 17
The Selection Problem is finding the
kth smallest element out of
n^
elements.
^
One solution is to sort the elements, and then return
[k^
1]. This is
n^ log
n)
^
Better is to modify quicksort to only sort the partition with
k^ −
1]. This is
n)^
on average.
^
Suppose partitioning always splits in half, then the recurrence is: C(
n) =
(n/
n), which is
n)^
by the Master Theorem.
^
Even if
[k^
is always in a “big” partition, say
10 , then
n) =
(9n/
n)
is also
n).
CS 3343 Analysis of Algorithms
Chapter 5: Slide – 18