0% found this document useful (0 votes)
7 views

DAA Unit 2 Notes

The document discusses advanced data structures including Red-Black Trees, Binomial Heaps, and Fibonacci Heaps, detailing their properties, operations, and algorithms. It also covers the brute force approach to problem-solving, highlighting its advantages and disadvantages, and presents examples like the Closest-Pair and Convex-Hull problems. Each data structure is explained with its respective operations and complexities, emphasizing their applications in computational geometry.

Uploaded by

itsprinnnce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DAA Unit 2 Notes

The document discusses advanced data structures including Red-Black Trees, Binomial Heaps, and Fibonacci Heaps, detailing their properties, operations, and algorithms. It also covers the brute force approach to problem-solving, highlighting its advantages and disadvantages, and presents examples like the Closest-Pair and Convex-Hull problems. Each data structure is explained with its respective operations and complexities, emphasizing their applications in computational geometry.

Uploaded by

itsprinnnce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Advanced Data Structures:

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."

• A red-black tree is a Binary tree where a particular node has color as


an extra attribute, either red or black. By check the node colors on
any simple path from the root to a leaf, red-black trees secure that no
such path is higher than twice as long as any other so that the tree is
generally balanced.
Properties of Red Black Tree:
1. Root property: The root is black.
2. External property: Every leaf (Leaf is a NULL child of a node)
is black in Red-Black tree.
3. Internal property: The children of a red node are black. Hence
possible parent of red node is a black node.
4. Depth property: All the leaves have the same black depth.
5. Path property: Every simple path from root to descendant leaf
node contains same number of black nodes.
The result of all these above-mentioned properties is that the Red-
Black tree is roughly balanced.
Properties of Red Black Tree:
Rules That Every Red-Black Tree Follows:
1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a
red parent or red child).
4. Every path from a node (including root) to any of its
descendants NULL nodes has the same number of black nodes.
5. Every leaf (e.i. NULL node) must be colored BLACK.
Operations on Red Black Tree:

FOLLOW THE CLASS NOTES


.
Deletion
Operations on
Red Black Tree:
BINOMIAL HEAPS
BINOMIAL HEAPS
• A binomial heap is a collection of binomial trees, so this section starts
by defining binomial trees and proving some key properties.
• We then define binomial heaps and show how they can be
represented
BINOMIAL HEAPS
BINOMIAL TREES
• The binomial tree Bk is an ordered tree defined recursively.
• The binomial tree B0 consists of a single node.
• The binomial tree Bk consists of two binomial trees Bk-1 that are linked
together: the root of one is the leftmost child of the root of the other.
BINOMIAL
TREES
BINOMIAL HEAPS
BINOMIAL TREES
• For the binomial tree Bk,
1. There are 2k nodes,
2. The height of the tree is k,
3. There are exactly nodes at depth i for i = 0, 1, . . . , k, and
4. The root has degree k, which is greater than that of any other node;
moreover if the children of the root are numbered from left to right by k -
1, k - 2, . . . , 0, child i is the root of a subtree Bi.
BINOMIAL HEAPS
A binomial heap H is a set of binomial trees that satisfies the following
binomial-heap properties.
1. Each binomial tree in H is heap-ordered: the key of a node is greater than
or equal to the key of its parent.
2. There is at most one binomial tree in H whose root has a given degree.
BINOMIAL HEAP
MEMORY
REPRESENTATION
OPERATIONS ON BINOMIAL HEAPS
MAKE-HEAP() creates and returns a new heap containing no elements.
INSERT(H, x) inserts node x, whose key field has already been filled in, into heap H.
MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.
EXTRACT-MIN(H) deletes the node from heap H whose key is minimum, returning a
pointer to the node.
UNION(Hl, H2) creates and returns a new heap that contains all the nodes of heaps
H1 and H2. Heaps H1 and H2 are "destroyed" by this operation.
In addition, the data structures in these chapters also support the following two
operations.
DECREASE-KEY(H, x, k) assigns to node x within heap H the new key value k, which is
assumed to be no greater than its current key value.
DELETE(H, x) deletes node x from heap H.
OPERATIONS ON BINOMIAL HEAPS
Creating a new binomial heap
To make an empty binomial heap, the MAKE-BINOMIAL-HEAP procedure
simply allocates and returns an object H, where head[H] = NIL. The running
time is (1).
OPERATIONS ON BINOMIAL HEAPS
Finding the minimum key
• The procedure BINOMIAL-
HEAP-MINIMUM returns a
pointer to the node with the
minimum key in an n-node
binomial heap H.
• This implementation assumes
that there are no keys with
value .
OPERATIONS ON
BINOMIAL HEAPS
Union of two binomial heaps
• The following procedure unites
binomial heaps H1 and H2,
returning the resulting heap. It
destroys the representations of
H1 and H2 in the process.
• The procedure BINOMIAL-HEAP-
MERGE that merges the root lists
of H1 and H2 into a single linked
list that is sorted by degree into
monotonically increasing order.
OPERATIONS ON BINOMIAL HEAPS
Union of two binomial heaps
• Case 1, shown in Figure (a), occurs when
degree[x] ≠ degree[next-x],

• 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

• For simplicity, we consider the two-dimensional case of the closest-pair


problem. We assume that the points in question are specified in a
standard fashion by their (x, y) Cartesian coordinates and that the
distance between two points pi(xi, yi) and pj (xj , yj ) is the standard
Euclidean distance

• The brute-force approach to solving this problem leads to the following


obvious algorithm: compute the distance between each pair of distinct
points and find a pair with the smallest distance. Of course, we do not
want to compute the distance between the same pair of points twice.
To avoid doing so, we consider only the pairs of points (pi, pj ) for
which i < j .
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.

You might also like