Dsa Assignment Ojas - Nagta 102184009
Dsa Assignment Ojas - Nagta 102184009
Submitted By:
Ojas Nagta
102184009
3EE4
Submitted To:
Dr. Mukesh Dalal
A DEEMED TO BE UNIVERSITY)
PATIALA, PUNJAB-147004
Solution:
We must determine the least height (h) that may hold n vertices in order to determine the minimal size of array X
necessary to store any binary tree on n vertices.
2^h - 1 >= n
2^h >= n + 1
h >= log2(n + 1)
Since h must be an integer, the lowest integer bigger than or equal to log2(n + 1) is the minimum height h necessary
to fit n vertices.
The minimal array X size needed to store any binary tree with n vertices is therefore given by 2(log2(n + 1)) - 1, which
may be written as 2(log2(n + 1) - 1.
The smallest size of array X will be 2(log2(n + 1) + 1) - 1 for array indexing to begin at 1.
Therefore, without utilizing the ceil function, the smallest size of array X needed to hold any binary tree with n
vertices is 2(log2(n + 1) + 1) - 1.
What do you understand by the connected components of a graph. Comment on the connected components of the
described graph above
Solution:
In graph theory, a subgraph in which every pair of vertices is connected by a route is referred to as a connected
component of an undirected graph. In other words, there is a path connecting every pair of vertices in the maximal
set of vertices, and no more vertices can be added while maintaining this feature.
We can identify the related parts of the network based on the provided data on the discovery and completion
timings of the vertices in the depth-first search.
We can see from the timings supplied that vertex P's discovery and completion times are the earliest (d(P) = 5, f(P) =
12). Vertex P was therefore examined and visited before any other vertex in the graph.
Vertex Q was identified following P (d(Q) = 6) and was fully explored before P (f(Q) = 10). Q independently creates a
distinct linked component since it was identified and completed before any other vertex.
Vertex R was found following both P and Q (d(R) = 14), and it was also explored to completion following P and Q (f(R)
= 18). As a result, R is a linked component like P and Q.
In conclusion, the network has two linked components, one of the vertices P and Q and the other of the vertex R,
depending on the specified discovery and finish times
Solution:
a.
b.
1 2 3 4 5 6
1 0 1 0 0 1 1
2 1 0 1 1 0 1
3 0 1 0 1 0 1
4 0 1 1 0 1 1
5 1 0 0 1 0 1
6 1 1 1 1 1 0
1 •X
2 1 •X
3 6 •X
4 3 5 6 •X
5 1 •X
6 1 2 5 •X
d.
• Generate a random number within the linked list size range to determine the insertion position.
• Traverse the linked list until you reach the randomly determined position.
• Create a new node with the desired value.
• Adjust the pointers of the surrounding nodes to include the new node.
• Update the pointers of the new node to connect it to the neighbouring nodes.
#include <bits/stdc++.h>
struct Node {
int data;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
int main() {
insertRandom(&head, 1);
insertRandom(&head, 2);
insertRandom(&head, 3);
insertRandom(&head, 4);
temp = temp->next; }
return 0; }
Solution:
The given routine inserts a new node, N, into a doubly linked list after a middle node, M.
The commands used:
1. N → lptr = M; This line sets the left pointer of the new node, N, to point to the middle node, M.
This establishes the link from N to the previous node in the list.
2. N → rtptr = M → rptr; This line sets the right pointer of the new node, N, to point to the next node
after the middle node, M. This preserves the existing link from the middle node, M, to the next
node in the list.
3. N → lptr → rptr = N; This line sets the right pointer of the node preceding the new node, N, to
point to N. This updates the link from the previous node to the new node.
4. N → rptr → lptr = N; This line sets the left pointer of the node following the new node, N, to point
to N. This updates the link from the next node to the new node.
So, the routine inserts the new node, N, into the doubly linked list by adjusting the pointers of the
surrounding nodes. It connects N to the middle node, M, and preserves the existing links between M and
the surrounding nodes.
Exclude URL: NO
We must determine the least height (h) that may hold n vertices in order to determine the minimal size
2^h - 1 >= n
2^h >= n + 1
h >= log2(n + 1)
Since h must be an integer, the lowest integer bigger than or equal to log2(n + 1) is the minimum
The minimal array X size needed to store any binary tree with n vertices is therefore given by 2(log2(n
The smallest size of array X will be 2(log2(n + 1) + 1) - 1 for array indexing to begin at 1.
Therefore, without utilizing the ceil function, the smallest size of array X needed to hold any binary tree
In graph theory, a subgraph in which every pair of vertices is connected by a route is referred to as a
connected component of an undirected graph. In other words, there is a path connecting every pair of
vertices in the maximal set of vertices, and no more vertices can be added while maintaining this
We can identify the related parts of the network based on the provided data on the discovery and
We can see from the timings supplied that vertex P's discovery and completion times are the earliest
(d(P) = 5, f(P) = 12). Vertex P was therefore examined and visited before any other vertex in the graph.
Vertex Q was identified following P (d(Q) = 6) and was fully explored before P (f(Q) = 10). Q
independently creates a distinct linked component since it was identified and completed before any
other vertex.
Vertex R was found following both P and Q (d(R) = 14), and it was also explored to completion following
In conclusion, the network has two linked components, one of the vertices P and Q and the other of the
a.
130
222
312
413
521
623
123456
1010011
2101101
3010101
4011011
5100101
6111110
b.
c.
Generate a random number within the linked list size range to determine the insertion position.
Traverse the linked list until you reach the randomly determined position.
Adjust the pointers of the surrounding nodes to include the new node.
Update the pointers of the new node to connect it to the neighbouring nodes.
#include
#include
struct Node {
int data;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
int main() {
insertRandom(&head, 1);
insertRandom(&head, 2);
insertRandom(&head, 3);
insertRandom(&head, 4);
cout
MATCHED SOURCES: