0% found this document useful (0 votes)
17 views11 pages

Dsa Assignment Ojas - Nagta 102184009

Uploaded by

Ojas Nagta
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)
17 views11 pages

Dsa Assignment Ojas - Nagta 102184009

Uploaded by

Ojas Nagta
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/ 11

Assignment

DATA STRUCTURE AND ALGORITHMS


(UCS540)

Submitted By:

Ojas Nagta

102184009
3EE4

Submitted To:
Dr. Mukesh Dalal

Electrical and Instrumentation Engineering Department


Thapar Institute of Engineering and Technology (TIET)

A DEEMED TO BE UNIVERSITY)
PATIALA, PUNJAB-147004

Ojas Nagta 102184009


Problem #7: A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1 instead of 0. the
root is stored at X[1]. For a node stored at X[i], the left child, if any, is stored in X[2i] and the right child, if any, in
X[2i+1]. Compute the minimum size of X to be able to store any binary tree on n vertices.

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.

A binary tree of height h can have up to 2h - 1 node at most.

This equation can be used to get the smallest height h:

2^h - 1 >= n

To each side, add 1:

2^h >= n + 1

Using the base-2 logarithm of both sides:

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.

Ojas Nagta 102184009


Problem #20: Consider the depth-first-search of an undirected graph with 3 vertices P, Q, and R. Let discovery
time d(u) represent the time instant when the vertex u is first visited, and finish time f(u) represent the time instant
when the vertex u is last visited.

Given that d(P) = 5 units f(P) = 12 units

d(Q) = 6 units f(Q) = 10 units

d(R) = 14 units f(R) = 18 units

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

Ojas Nagta 102184009


Problem #25: For the digraph shown below (Figure 7) obtain
a the in-degree and out-degree of each vertex

b its adjacency-matrix representation

c its adjacency -list representation

d its strongly connected components

Solution:
a.

Vertex In-degree Out-degree


1 3 0
2 2 2
3 1 2
4 1 3
5 2 1
6 2 3

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

Ojas Nagta 102184009


c.

1 •X
2 1 •X

3 6 •X
4 3 5 6 •X

5 1 •X
6 1 2 5 •X

d.

Vertices 2,3,4 and 6 are strongly connected components of the graph.

Ojas Nagta 102184009


Problem #31: Explain and write the pseudocode to insert a node in a random location of the Linked List?
Solution:
To insert a node at a random location in a linked list:

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

Pseudocode to implement the insertion of a node at a random location:


#include <iostream>

#include <bits/stdc++.h>

using namespace std;

struct Node {

int data;

struct Node* next; };

void insertRandom(Node** head_ref, int new_data) {

Node* new_node = new Node;

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

int main() {

Node* head = NULL;

insertRandom(&head, 1);

insertRandom(&head, 2);

insertRandom(&head, 3);

insertRandom(&head, 4);

cout << "Linked List after insertion: ";

Node* temp = head;

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next; }

return 0; }

Ojas Nagta 102184009


Problem #34: What does the following routine do on a doubly linked list? M is some middle node address and N
is a newly inserted node address.
N → lptr = M ;
N → rtptr = M → rptr ;
N → lptr → rptr = N ;
N → rptr → lptr = N ;

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.

Ojas Nagta 102184009


Date June 02, 2023

Exclude URL: NO

Unique Content Word Count

Plagiarized Content Records Found

CONTENT CHECKED FOR PLAGIARISM:

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.

A binary tree of height h can have up to 2h - 1 node at most.

This equation can be used to get the smallest height h:

2^h - 1 >= n

To each side, add 1:

2^h >= n + 1

Using the base-2 logarithm of both sides:

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.

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

Ojas Nagta 102184009


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

a.

Vertex In-degree Out-degree

130

222

312

413

521

623

123456

1010011

2101101

3010101

4011011

5100101

6111110

b.

c.

Ojas Nagta 102184009


d.

Vertices 2,3,4 and 6 are strongly connected components of the graph.

To insert a node at a random location in a linked list:

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.

Pseudocode to implement the insertion of a node at a random location:

#include

#include

using namespace std;

struct Node {

int data;

struct Node* next; };

void insertRandom(Node** head_ref, int new_data) {

Node* new_node = new Node;

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

int main() {

Node* head = NULL;

insertRandom(&head, 1);

insertRandom(&head, 2);

insertRandom(&head, 3);

insertRandom(&head, 4);

cout

MATCHED SOURCES:

Ojas Nagta 102184009


Report Generated on June 02, 2023 by check-
plagiarism.com

Ojas Nagta 102184009

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy