DAA Unit 2 Notes
DAA Unit 2 Notes
Brute Force:
Exhaustive Search:
RED BLACK TREE
Red Black Tree
• A Red Black Tree is a category of the self-balancing binary search tree.
It was created in 1972 by Rudolf Bayer who termed them "symmetric
binary B-trees."
• Case 2, shown in Figure (b), occurs when x is the first of three roots of equal
degree, that is, when
degree[x] = degree[next-x] = degree[sibling[next-x]].
• Cases 3 and 4 occur when x is the first of two roots of equal degree, that is,
when
degree[x] = degree[next-x] ≠ degree[sibling[next-x]].
OPERATIONS ON BINOMIAL HEAPS
Union of
two
binomial
heaps
OPERATIONS ON BINOMIAL HEAPS
Union of
two
binomial
heaps
OPERATIONS ON BINOMIAL HEAPS
Inserting a node
The procedure The following
procedure inserts node x into
binomial heap H, assuming of
course that node x has already
been allocated and key[x] has
already been filled in.
BINOMIAL-HEAP-INSERT(H,x)
OPERATIONS ON BINOMIAL HEAPS
Extracting the node
with minimum key
The following
procedure extracts
the node with the
minimum key from
binomial heap H and
returns a pointer to
the extracted node.
BINOMIAL-HEAP-
EXTRACT-MIN(H)
OPERATIONS ON BINOMIAL HEAPS
Extracting
the node
with
minimum
key
OPERATIONS ON BINOMIAL HEAPS
Decreasing a key
The following
procedure extracts
the node with the
minimum key from
binomial heap H and
returns a pointer to
the extracted node.
BINOMIAL-HEAP-
EXTRACT-MIN(H)
OPERATIONS ON BINOMIAL HEAPS
Deleting a key
BINOMIAL-HEAP-DELETE(H,x)
1. BINOMIAL-HEAP-DECREASE-KEY(H,x,-∞)
2. BINOMIAL-HEAP-EXTRACT-MIN(H)
FIBONACCI HEAPS
FIBONACCI HEAPS
• Like a binomial heap, a Fibonacci heap is a collection of heap-ordered trees. The
trees in a Fibonacci heap are not constrained to be binomial trees.
FIBONACCI HEAPS
• A given Fibonacci heap H is accessed by a pointer min[H] to the root of the tree
containing a minimum key; this node is called the minimum node of the Fibonacci
heap. If a Fibonacci heap H his empty, then min[H] = NIL.
• The roots of all the trees in a Fibonacci heap are linked together using their left
and right pointers into a circular, doubly linked list called the root list of the
Fibonacci heap. The pointer min[H] thus points to the node in the root list whose
key is minimum. The order of the trees within a root list is arbitrary.
• We rely on one other attribute for a Fibonacci heap H: the number of nodes
currently in H is kept in n[H].
OPERATIONS ON FIBONACCI HEAPS
Inserting a node
The following
procedure inserts
node x into Fibonacci
heap H, assuming of
course that the node
has already been
allocated and that
key[x] has already
been filled in.
OPERATIONS ON FIBONACCI HEAPS
Finding the minimum node
The minimum node of a Fibonacci heap H is given by the pointer min[H], so
we can find the minimum node in O(1) actual time.
Because the potential of H does not change, the amortized cost of this
operation is equal to its O(1) actual cost.
OPERATIONS ON FIBONACCI HEAPS
Union two
Fibonacci heaps
The following
procedure unites
Fibonacci heaps H1
and H2, destroying
H1 and H2 in the
process.
OPERATIONS ON FIBONACCI HEAPS
Extracting
the
minimum
node
OPERATIONS ON
FIBONACCI HEAPS
Extracting
the
minimum
node
OPERATIONS
ON FIBONACCI
HEAPS
Extracting
the
minimum
node
OPERATIONS
ON FIBONACCI
HEAPS
Extracting
the
minimum
node
OPERATIONS ON FIBONACCI HEAPS Decreasing a key
OPERATIONS ON FIBONACCI HEAPS Decreasing a key
46->15
35->5
OPERATIONS ON FIBONACCI HEAPS
Deleting a key
It is easy to delete a node from an n-node Fibonacci heap in O(D(n))
amortized time, as is done by the following pseudocode. We assume that
there is no key value of - ∞ currently in the Fibonacci heap.
FIB-HEAP-DELETE(H, x)
1. FIB-HEAP-DECREASE-KEY (H,x,-∞)
2. FIB-HEAP-EXTRACT-MIN(H)
BRUTE FORCE
APPROACH
BRUTE FORCE APPROACH
• A brute force approach is an approach that finds all the possible
solutions to find a satisfactory solution to a given problem.
• The brute force algorithm tries out all the possibilities till a satisfactory
solution is not found.
Advantages of a brute-force algorithm
• This algorithm finds all the possible solutions, and it also guarantees
that it finds the correct solution to a problem.
• This type of algorithm is applicable to a wide range of domains.
• It is mainly used for solving simpler and small problems.
• It can be considered a comparison benchmark to solve a simple problem
and does not require any particular domain knowledge.
Disadvantages of a brute-force algorithm
• It is an inefficient algorithm as it requires solving each and every state.
• It is a very slow algorithm to find the correct solution as it solves each
state without considering whether the solution is feasible or not.
• The brute force algorithm is neither constructive nor creative as
compared to other algorithms.
Closest-Pair Problem using
brute-force algorithm
Closest-Pair Problem using brute-force algorithm
• The closest-pair problem calls for finding the two closest points in a set
of n points. It is the simplest of a variety of problems in computational
geometry that deals with proximity of points in the plane or higher-
dimensional spaces.
Closest-Pair Problem using brute-force algorithm
Algorithm BruteForceClosestPoints(P)
// P is list of points
dmin ← ∞
for i ← 1 to n-1 do
for j ← i+1 to n do
d ← sqrt((xi-xj)2 + (yi-yj)2)
if d < dmin then
dmin ← d; index1 ← i; index2 ← j
return index1, index2
Closest-Pair Problem using brute-force algorithm
COMPLEXITY
Convex-Hull Problem using
brute-force algorithm
Convex-Hull Problem
• A set of points (finite or infinite) in the plane is called convex if for any
two points p and q in the set, the entire line segment with the
endpoints at p and q belongs to the set.
Convex-Hull Problem
Facts
• A few elementary facts from analytical geometry are needed to
implement the above algorithm.
• First, the straight line through two points (x1, y1), (x2, y2) in the
coordinate plane can be defined by the equation ax + by = c, where
a = y2 − y1, b = x1 − x2, c = x1y2 −y1x2.
• Second, such a line divides the plane into two half-planes: for all the
points in one of them, ax + by > c, while for all the points in the other, ax
+ by < c. (For the points on the line itself, of course, ax + by = c.) Thus, to
check whether certain points lie on the same side of the line, we can
simply check whether the expression ax + by − c has the same sign for
each of these points.
Convex-Hull Problem
Time efficiency of this algorithm.
• Time efficiency of this algorithm is in O(n3): for each of n (n − 1)/2 pairs
of distinct points, we may need to find the sign of ax + by – c for each of
the other n − 2 points.