R23-ADSNAA-UNIT-2
R23-ADSNAA-UNIT-2
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:
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.
Unit-2 1
Advanced Data Structures and Algorithm Analysis
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.
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:
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)
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
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
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.
Unit-2 12
Advanced Data Structures and Algorithm Analysis
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.
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.
Unit-2 16
Advanced Data Structures and Algorithm Analysis
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.
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;
}
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 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];
Unit-2 21
Advanced Data Structures and Algorithm Analysis
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.
Unit-2 22
Advanced Data Structures and Algorithm Analysis
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.
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.
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.
Unit-2 24