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

R23-ADSNAA-UNIT-2

The document covers advanced data structures focusing on heaps and graphs, detailing their properties, types, and operations. It explains binary heaps, including min-heaps and max-heaps, as well as graph representations such as adjacency matrices and lists. Additionally, it discusses traversal algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS), along with concepts of connected components and articulation points in graphs.

Uploaded by

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

R23-ADSNAA-UNIT-2

The document covers advanced data structures focusing on heaps and graphs, detailing their properties, types, and operations. It explains binary heaps, including min-heaps and max-heaps, as well as graph representations such as adjacency matrices and lists. Additionally, it discusses traversal algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS), along with concepts of connected components and articulation points in graphs.

Uploaded by

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

Advanced Data Structures and Algorithm Analysis

UNIT-2

HEAP: A heap is a specialized tree-based data structure that satisfies the heap property. It is
primarily used to implement efficient priority queues.

Binary Heap: The most common type of heap is the binary heap, which is a complete binary
tree. This means that every level of the tree is fully filled, except possibly for the last level,
which is filled from left to right.

Min-Heap: In a min-heap, the key (or value) of the parent node is less than or equal to the keys
of its children. The minimum element is at the root of the tree.

Max-Heap: In a max-heap, the key of the parent node is greater than or equal to the keys of its
children. The maximum element is at the root of the tree.

Array Representation: Heaps can be efficiently stored in arrays without needing pointers to
child or parent nodes. In an array representation:

• The root element is at index 0.

• For any element at index i:


o Its left child is at index 2i+1.
o Its right child is at index 2i+2.
o Its parent is at index (i−1)/2

Basic Operations
Insertion: When inserting a new element into the heap, the element is initially added at the end
of the heap (the next available position in the tree). Then, it is "bubbled up" (or "heapified") by
comparing it with its parent and swapping them if necessary, until the heap property is restored.

Inserting elements into a max heap


Step 1: Insert the node in the first available level order position.
Step 2: Compare the newly inserted node with its parent. If the newly inserted node is
larger, swap it with its parent.
Step 3: Continue step 2 until the heap order property is restored.

Deletion (Extract-Max/Min): The root element (maximum in a max-heap, minimum in a min-


heap) is removed. The last element in the heap is moved to the root, and then "bubbled down"
(or "heapified") by comparing it with its children and swapping it with the larger (in max-heap)
or smaller (in min-heap) child until the heap property is restored.

Unit-2 1
Advanced Data Structures and Algorithm Analysis

Deleting from a max heap


Step 1: Find the maximum node.
Step 2: Replace the maximum node with the last leaf node in level order.
Step 3: Compare the replacement against its children. If one of the children is larger, swap the
replacement with the largest child.
Step 4: Repeat step 3 until the heap order property is restored.

Peek: This operation simply returns the root element without modifying the heap.

Heapify: The process in which the binary tree is reshaped into a Heap data structure is known
as Heapify.

Construct a max Heap with the following elements


10, 20, 25, 6, 12, 15, 4, 16

The first step is to create a binary tree with the given elements

Now we’ll take a subtree at the lowest level and start checking whether it follows the max-heap
property or not:

Unit-2 2
Advanced Data Structures and Algorithm Analysis

As we can see, the subtree doesn’t follow the max-heap property. Here, the parent node should
contain a greater value than its children node. So in order to make sure that the tree follows the
max-heap property, we swap the key values between the children node and the parent node.

Let’s continue and examine all the subtrees from the lowest level to the top level:

This subtree follows the max-heap property, and we don’t need to change anything here. Next,
we look at the right side branches:

Again the subtree follows the max-heap property. Let’s continue this process:

Unit-2 3
Advanced Data Structures and Algorithm Analysis

Here again, we can see that the key value of the root node is not the largest among all the nodes
in the tree. Hence, we swapped the key values of the root node with the key value of its right
children node to match with the max-heap property.

Now, after the swap, we need to check the right subtree from the root node in order to see
whether it follows the max-heap property or not:

Finally, we’ve to check the whole tree in order to see if it satisfies the max-heapify property,
and then we’ll get our final max-heap tree:

Construction of a Heap: A Heap can be constructed in any of the two ways.

Method 1: A heap can be constructed by inserting elements one by one and heapifying after
each insertion.

Method 2: Construct a complete binary tree with the given elements and then apply heapify
property.

Priority Queue: A priority queue is an abstract data type where each element in the priority
queue has a priority associated with it. Elements with higher priority are dequeued before
elements with lower priority. If two elements have the same priority, they are usually dequeued
according to their order in the queue. Priority queues are generally implemented either using
max heap or using min heaps.

Unit-2 4
Advanced Data Structures and Algorithm Analysis

GRAPHS: A graph G is a collection of two sets V & E. Where V is a finite non empty of
vertices and E is a finite non empty set of edges.
Or
A graph G is a defined as a set of objects called nodes and edges. G = (V, E). A graph
G consists of two things:
✓ A set V of elements called nodes (or points or vertices).
✓ A set E of edges such that each edge e in E is identified with a unique pair (u, v) of
nodes in V, denoted by e = (u, v)

G = { { V1, V2, V3, V4}, { E1, E2, E3, E4, E5} }

TYPES OF GRAPHS
There are basically two types of graphs:
✓ Directed graphs
✓ Undirected graph

In a directed graph or digraph, the directions are shown on the edges. The edges
between the vertices are ordered. In the below graph, the edge is in between the vertices V1
and V2. The vertex V1 is called head and the vertex V2 is called tail. The edge is the set of
(V1, V2) and not (V2, V1).

Unit-2 5
Advanced Data Structures and Algorithm Analysis

In undirected graph, there are no directions shown on the edges. The edges are not
ordered. In the below graph, the edge E1 is the set of (V1, V2) and (V2, V1).

If there is more than one edge between the vertices then it is called multi – graph which
is shown below.

A planar graph is a kind of graph that can be drawn in a plane and which contains no
crossing edges. It is shown below.

PROPERTIES OF GRAPHS
Complete graph – if an undirected graph of n vertices contains n(n- 1) / 2 number of edges
then it is called complete graph.
Sub graph – a sub graph G’ of a graph G is a graph such that the set of vertices and set of
edges of G’ are power subset of the set of edges of G.
Connected graph – an undirected graph is said to be a connected graph if for every pair of
distinct vertices Vi and Vj in V(G) there is a graph from Vi to Vj in G
Weighted graph – a weighted graph is a graph that contains weights along with its edges.
Path – a path is denoted using sequence of vertices and there exists an edge from one vertex
to next vertex.
Cycle – a closed walk through the graph with repeated vertices so that the starting and ending
vertex is same.
Indegree – the number of edges that are incident to that vertex is indegree.
Outdegree – the number of edges that are exiting out from the vertex is outdegree.

Unit-2 6
Advanced Data Structures and Algorithm Analysis

Degree – the number of edges associated with the vertex is degree of a vertex.
Self loop – self loop is the edge that connects the same vertex to itself.
Strongly connected graph - A Directed graph is called a strongly connected graph if for any
two nodes I and J, there is a directed path from I to J and also from J to I.
Weakly connected graph - A Directed graph is called a weakly connected graph if for any
two nodes I and J, there is a directed path from I to J or from J to I.
Source node - A node where the indegree is 0 but has a positive value for outdegree is called
a source node. That is there are only outgoing arcs to the node and no incoming arcs to the
node.
Sink node - A node where the outdegree is 0 and has a positive value for indegree is called the
sink node. That is there is only incoming arcs to the node and no outgoing arcs the node.
Forest – it is a set of disjoint trees. If we remove the root node of a given tree then it becomes
forest.

GRAPH REPRESENTION: There are different ways of representing digraphs. They are:
✓ Adjacency matrix.
✓ Adjacency List.

Adjacency matrix
In this representation, the adjacency matrix of a graph G is a two dimensional n x n
matrix, say A = (ai,j), where

The matrix is symmetric in case of undirected graph, while it may be asymmetric if the
graph is directed. This matrix is also called as Boolean matrix or bit matrix. The below figure
shows the adjacency matrix representation of the graph G. The adjacency matrix is also useful
to store multigraph as well as weighted graph.

The adjacency matrix for a weighted graph is called as cost adjacency matrix. The
below figure shows the cost adjacency matrix representation of the graph G.

Unit-2 7
Advanced Data Structures and Algorithm Analysis

Adjacency List
As we have problem with array, to overcome it we have selected the flexible data
structure called linked lists. The type in which a graph is created with the linked list is called
adjacency list.

We know that the graph is set of vertices and edges we will maintain two structures for
vertices and edges respectively. The graph contains four vertices V1, V2, V3, V4 so to maintain
them we use linked list of head nodes and adjacent nodes.

The down pointer helps us to go to each node in the graph whereas next pointer is for
going to adjacent node of each of the head node.

Unit-2 8
Advanced Data Structures and Algorithm Analysis

BREADTH FIRST SEARCH


To traverse a graph by BFS, first a vertex V1 in the graph will be visited then all the
adjacent vertices of it will be traversed. Suppose if V1 has the adjacent vertices (V1, V2, …..Vn)
then they will be printed first and the process continues for all vertices. The data structures
used for keeping all vertices and their adjacent vertices is Queue. We also use array for visited
vertices. The nodes that are visited are marked as 1. In BFS, traversing is done in breadth wise
fashion.

Algorithm
1. Create a graph, depending on the type of graph either it may be directed or undirected set
the value of flag to 1 or 0.
2. Read the vertex from which you want to traverse the graph say Vi
3. Initialize the visited array to 1 at the index of Vi
4. Insert the visited vertex Vi in the queue
5. Visit the vertex which is at the front of the queue, delete it from the queue and place the
adjacent vertices in the queue.
6. Repeat the step5 till queue is not empty.

Unit-2 9
Advanced Data Structures and Algorithm Analysis

Step 1: Initialize the queue.

Unit-2 10
Advanced Data Structures and Algorithm Analysis

Step 2: Start with the vertex from visiting S (starting node), and mark it as visited.

Step 3: We then see an unvisited adjacent node from S. In this example, we have three nodes
but alphabetically we choose A, mark it as visited and enqueue it.

Step 4: Next, the unvisited adjacent node from S is B. We mark it as visited and enqueue it.

Unit-2 11
Advanced Data Structures and Algorithm Analysis

Step 5: Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it.

Step 6: Now, S is left with no unvisited adjacent nodes. So, we dequeue S and find A.

Step 7: Next, the unvisited adjacent node from A is D. We mark it as visited and enqueue it.

Step 8: Now, A is left with no unvisited adjacent nodes. So, we dequeue A.

Unit-2 12
Advanced Data Structures and Algorithm Analysis

Step 9: Now, B is dequeued.

Step 10: Now, C is dequeued.

Step 11: Now, D is dequeued.

DEPTH FIRST SEARCH (DFS)


Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses
a stack to remember to get the next
vertex to start a search, when a dead end
occurs in any iteration.

Unit-2 13
Advanced Data Structures and Algorithm Analysis

Algorithm
1. Create a graph, depending on the type of graph either it may be directed or undirected.
2. Read the vertex from which you want to traverse the graph say Vi
3. Insert the visited vertex Vi and push all the adjacent vertices into the Stack
4. If no adjacent vertices found ten pop the top vertex from the stack.
5. Repeat the step4 till stack is empty.

Algorithm DFS(v)
{
//Given a graph G = (V, E) with n vertices and an array visited[ ] initially set to zero, //
this algorithm visits all vertices reachable from v. G and visited[ ] are global.

for each vertex w adjacent to v do


{
if (visited[w] == 0 )
DFS(w);
}
}

Step 1: Initialize the stack.

Step 2: Mark S as visited and put it onto the stack. See any unvisited adjacent node from S. We
have three nodes and we can pick any of them. For this example, we shall take the node in an
alphabetical order.

Unit-2 14
Advanced Data Structures and Algorithm Analysis

Step 3: Mark A as visited and put it onto the stack. See any unvisited adjacent node from A.
Both S and D are adjacent to A but we are concerned for unvisited nodes only.

Step 4: Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes,
which are adjacent to D and both are unvisited. However, we shall again choose in an
alphabetical order.

Step 5: We choose B, mark it as visited and put onto the stack. Here B does not have any
unvisited adjacent node. So, we pop B from the stack.

Unit-2 15
Advanced Data Structures and Algorithm Analysis

Step 6: We check the stack top for return to the previous node and check if it has any unvisited
nodes. Here, we find D to be on the top of the stack.

Step 7: Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited
and put it onto the stack.

Step 8: So now we pop one by one node from stack until it is empty.

Connected Components: A connected component of an undirected graph is a subgraph in


which each pair of nodes are connected with each other via a path. In connected components,
all the nodes are always reachable from each other.

Example 1: The given undirected graph has one connected component

Unit-2 16
Advanced Data Structures and Algorithm Analysis

Example 2: The given undirected graph has three connected components

Strongly Connected Components: In a directed graph, a Strongly Connected Component is


a subset of vertices where every vertex in the subset is reachable from every other vertex in the
same subset by traversing the directed edges.

Example: The below graph has two strongly connected components {1,2,3,4} and {5,6,7}
since there is path from each vertex to every other vertex in the same strongly connected
component.

Biconnected Components: A biconnected component (or block) in a graph is a maximal subgraph such
that the subgraph remains connected even if any single vertex is removed. In other words, it’s a connected
subgraph with no articulation points.

Unit-2 17
Advanced Data Structures and Algorithm Analysis

Articulation Point: An articulation point (or cut vertex) is defined as a vertex which, when
removed along with associated edges, makes the graph disconnected.
Articulation points represent vulnerabilities in a connected network – single points
whose failure would split the network into 2 or more components. They are useful for designing
reliable networks.

In the above graph vertex 3 and 4 are Articulation Points since the removal of vertex 3
(or 4) along with its associated edges makes the graph disconnected.

Divide and Conquer: The divide-and-conquer strategy can be explained as follows:


1. Take a problem with n inputs.
2. Split the inputs into k distinct subsets, 1 < k ≤ n, yielding k subproblems.
3. Solve each subproblem to get the respective subsolutions.
4. Apply a method to combine subsolutions into a solution of the whole.

Unit-2 18
Advanced Data Structures and Algorithm Analysis

General Method:

Algorithm DAndC(P)
{
// P is a problem of size n
if Small(P) then
return S(P);
else
{
divide P into smaller instances P1, P2, ..., Pk, K ≥ 1;
Apply DAndC to each subproblem;
return Combine(DAndC(P1), ..., DAndC(Pk));
}
}
The time required for the above algorithm is computed as follows:

Where n problem size, g(n) time required to solve the problem when n is small n1, n2, …, nk
sizes of k subproblems and f(n) time required to divide P into subproblems and combine the
sub solutions into the whole
The complexity of many divide-and-conquer algorithms is given by the recurrences of the
form:

where a and b are known constants. We assume that T(1) is known and n is a power of b
(i.e., n = bk).
We can solve the recurrence relation by using substitution method.
QUICK SORT: In Quick sort, the division into 2 sub arrays is made so that the sorted sub
arrays do not need to be merged later.
This is accomplished by rearranging the elements in a[1:n] such that a[i]<=a[j] for all i
between 1 & m and all j between (m+1) & n for some m, 1<=m<=n. Thus the elements in
a[1:m] & a[m+1:n] can be independently sorted. No merge is needed. This rearranging is
referred to as partitioning.
In order to achieve this, we choose one of the element as pivot element. Generally, we
choose first element as pivot element. Now we take two pointers called left pointer and right
pointer. The left pointer moves from left to right and stops when it encounters an element
greater than pivot. Similarly, the right pointer moves from right to left and stops when it
encounters an element smaller than pivot.

Unit-2 19
Advanced Data Structures and Algorithm Analysis

Now we check the positions of left and right pointers. If they do not cross each other,
swap the left and right pointer elements and repeat the process. If they cross each other, swap
right pointer elements with pivot element. Once the right and pivot elements are swapped, we
say that the pivot element is sorted.

Algorithm Interchange(a, i, j)
//Exchange a[i] with a[j]
{
p=a[i];
a[i]=a[j];
a[j]=p;
}

Algorithm: Partition the array a[m : p-1] about a[m]


Algorithm Partition(a, m, p)
{
v=a[m];
i=m;
j=p;
repeat
{
repeat
i=i+1;
until(a[i]>=v);

repeat
j=j-1;
until(a[j]<=v);

if (i<j) then
interchange(a, i. j );

}until(i>=j);
a[m]=a[j];
a[j]=v;
retun j;
}

Unit-2 20
Advanced Data Structures and Algorithm Analysis

Algorithm: Sorting by Partitioning


Algorithm Quicksort(p,q)
//Sort the elements a[p],….a[q] which resides in the global array //a[1:n] into ascending
//order; a[n+1] is considered to be defined and must be >= all the elements in a[1:n]
{
if(p<q) then // If there are more than one element
{
// divide p into 2 sub problems
j = partition(a, p, q+1);
//‟ j ‟ is the position of the partitioning element.
//solve the sub problems.
quicksort(p, j-1);
quicksort(j+1, q);
//There is no need for combining solution.
}
}

Merge Sort: Another application of Divide and conquer is merge sort.


 Given a sequence of n elements a[1],…,a[n] the general idea is to split into 2 sets
a[1],…..,a[n/2] and a[[n/2]+1],….a[n].
 Each set is individually sorted, and the resulting sorted sequences are merged to
produce a single sorted sequence of n elements.
 Thus, we have another ideal example of the divide-and-conquer strategy in which the
splitting is done into 2 equal-sized sets & the combining operation is the merging of
two sorted sets into one.

Algorithm MergeSort(low,high)
/*a[low:high] is a global array to be sorted. Small(P) is true if there is only one element to
sort. In this case the list is already sorted. */
{
if (low<high) then //if there are more than one element
{
//Divide P into subproblems find where to split the set
mid = [(low+high)/2];

//solve the subproblems.


mergesort (low,mid);
mergesort(mid+1,high);

//combine the solutions .


merge(low,mid,high);
}
}

Unit-2 21
Advanced Data Structures and Algorithm Analysis

Algorithm: Merging 2 sorted subarrays using auxiliary storage.


Algorithm merge(low, mid, high)
/*a[low:high] is a global array containing two sorted subsets in a[low:mid] and in
a[mid+1:high].The goal is to merge these 2 sets into a single set residing in a[low:high]. b[] is
an auxiliary global array. */
{
h=low;
i=low;
j=mid+1;
while ((h<=mid) and (j<=high)) do
{
if (a[h]<=a[j]) then
{
b[i]=a[h];
h = h+1;
}
else
{
b[i]= a[j];
j=j+1;
}
i=i+1;
}

Strassen’s Matrix Multiplication: Strassen's algorithm, developed by Volker Strassen in


1969, is a fast algorithm for matrix multiplication. It is an efficient divide-and-conquer method
that reduces the number of arithmetic operations required to multiply two matrices compared
to the conventional matrix multiplication algorithm.

The traditional matrix multiplication algorithm has a time complexity of O(n^3) for
multiplying two n x n matrices. However, Strassen's algorithm improves this to O(n^log2(7)),
which is approximately O(n^2.81). The algorithm achieves this improvement by recursively
breaking down the matrix multiplication into smaller subproblems and combining the results.

Algorithm working As below: Given two matrices A and B, of size n x n, we divide each
matrix into four equal-sized submatrices, each of size n/2 x n/2.

A = | A11 A12 | B = | B11 B12 |


| A21 A22 | | B21 B22 |

We then define seven intermediate matrices:

P1 = A11 * (B12 - B22)


P2 = (A11 + A12) * B22

Unit-2 22
Advanced Data Structures and Algorithm Analysis

P3 = (A21 + A22) * B11


P4 = A22 * (B21 - B11)
P5 = (A11 + A22) * (B11 + B22)
P6 = (A12 - A22) * (B21 + B22)
P7 = (A11 - A21) * (B11 + B12)

Next, we recursively compute seven products of these submatrices, i.e., P1, P2, P3, P4,
P5, P6, and P7. Finally, we combine the results to obtain the four submatrices of the resulting
matrix C of size n x n:

C11 = P5 + P4 - P2 + P6
C12 = P1 + P2
C21 = P3 + P4
C22 = P5 + P1 - P3 - P7

Concatenate the four submatrices C11, C12, C21, and C22 to obtain the final result matrix C.

The efficiency of Strassen's algorithm comes from the fact that it reduces the number
of recursive calls, which means fewer multiplication operations are needed overall.

Convex Hull: Given a set of points, the convex hull is the smallest convex polygon that
contains all the points. Think of it as stretching a rubber band around the set of points; when
you let go, the rubber band forms the convex hull.

Formally, the convex hull of a set of points S is the smallest convex set C such that
every point in S is contained in C. In 2D, the convex hull can be visualized as the "shape"
formed by connecting the outermost points (vertices) in such a way that the shape is convex.

The steps that we’ll follow to solve the problem are:

1. First, we’ll sort the vector containing points in ascending order (according to their x-
coordinates).
2. Next, we’ll divide the points into two halves S1 and S2. The set of points S1 contains
the points to the upper side of the median, whereas the set S2 contains all the points that
are lower to the median.
3. In the two spaces S1 and S2, we will find out the farthest point.

Unit-2 23
Advanced Data Structures and Algorithm Analysis

4. We’ll find the convex hulls for the set S1 and S2 individually. Assuming the convex
hull for S1 is C1, and for S2, it is C2.
5. Now, we’ll merge C1 and C2 such that we get the overall convex hull C.

Consider the following example.

Step1: Find out the farthest two points in the given sample.

Step2: In the two spaces S1 and S2, we will find out the farthest point

Step3: Repeat the above process by applying Divide and Conquer Strategy.

Finally, the resultant output will be

Unit-2 24

You might also like