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

Algorithms - Assignment - UNIT 4

The document discusses a student assignment submission for a data structures and algorithms course. It contains questions related to trees, graphs, binary search trees, depth-first search, breadth-first search and their related concepts. The document includes objective questions with answers, short questions with explanations and long questions requiring detailed responses involving examples and illustrations.

Uploaded by

Shirin Razdan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Algorithms - Assignment - UNIT 4

The document discusses a student assignment submission for a data structures and algorithms course. It contains questions related to trees, graphs, binary search trees, depth-first search, breadth-first search and their related concepts. The document includes objective questions with answers, short questions with explanations and long questions requiring detailed responses involving examples and illustrations.

Uploaded by

Shirin Razdan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

AMORAH VENKATESHWARA COLLEGE

ASSIGNMENT UNIT- 4

COURSE: B. TECH

SUBJECT: DATA STRUCTURE AND ALGORITHMS

STUDENT ID- SVU66850724

BRANCH- ECE

SUBMITTED
BY:
ARTI KAK
SEMESTER III
Objective types Questions

Q1: Tree is . (Linear/Non-linear)

Answer: Non-linear

Q2: Graph is . . (Linear/ Non-linear)

Answer: Non-linear.

Q3: BST contains all smaller values at . (Left side/ Right side)

Answer: Left side

Q4: Root, left, right is traversal type of . (Preorder/ Inorder)

Answer: Preorder.

Q5: Weighted graphs has . (Null values/ weight as values)

Answer: Weight as values


Very Short Question

Q1: Discuss about Siblings.

Answer: Siblings : Children of the same parent node are called siblings.

D, E are Siblings.

Q2: Write short note on depth of a tree.

Answer:

Depth of a tree

• In a tree, many edges from the root node to the particular node are called the depth
of the tree.
• In the tree, the total number of edges from the root node to the leaf node in the
longest path is known as "Depth of Tree".
• In the tree data structures, the depth of the root node is 0.

Q3:- What is binary tree?

Answer: The Binary tree means that the node can have maximum two children. Here binary
name itself suggests that ‘two’; therefore each node can have either 0, 1 or 2 children.

Example:

The above tree is a binary tree because each node contains the utmost two children. The logical
representation of the above tree is given below:
In the above tree, node 1 contains two pointers, i.e., left and a right pointer pointing to the left
and right node respectively. The node 2 contains both the nodes (left and right node);
therefore, it has two pointers (left and right). The nodes 3, 5 and 6 are the leaf nodes, so all
these nodes contain NULL pointer on both left and right parts.

Q4:- Write down all the applications of Graph.

Answer. Applications of Graph:

Graphs data structures have a variety of applications. Some of the most popular applications
are:

• Helps to define the flow of computation of software programs.


• Used in Google maps for building transportation systems. In Google maps, the
intersection of two or more roads represents the node while the road connecting two
nodes represents an edge. Google maps algorithm uses graphs to calculate the shortest
distance between two vertices.
• Used in social networks such as Facebook and Linkedin.

Some other applications of graphs include:

• Knowledge graphs
• Biological networks
• Neural networks
Q5:- Elaborate vertex and edges in a graph.

Answer:

Vertices: Vertices are the fundamental unit of graphs. Sometimes vertices are also known as
vertex or nodes. Every node/vertex can be labeled or unlabeled.

Edges : Edges are drawn or used to connect two nodes of the graph. It can be ordered paired of
nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no
rules. Sometimes edges are also known as arcs. Every Edges can be labeled or unlabeled.
Long Questions

Q1: What do you understand with BST? Create a BST where data are 36, 5, 6, 78, 5, 6, 34, 12,
4, 78, 90, 40.

Answer:

A binary search tree follows some order to arrange the elements. In a Binary search tree, the
value of left node must be smaller than the parent node, and the value of right node must be
greater than the parent node. This rule is applied recursively to the left and right sub-trees of
the root.

Understand the concept of Binary search tree with an example.

In the above figure, we can observe that the root node is 40, and all the nodes of the left Sub-
tree are smaller than the root node, and all the nodes of the right Sub-tree are greater than the
root node.

Similarly, we can see the left child of root node is greater than its left child and smaller than its
right child. So, it also satisfies the property of binary search tree. Therefore, we can say that the
tree in the above image is a binary search tree.

Create a BST where data are 36,5,6,78,5,6,34,12,4,78,90,40.

• First, we have to insert 36 into the tree as the root of the tree.
• Then, read the next element; if it is smaller than the root node, insert it as the root of
the left Sub-tree, and move to the next element.
• Otherwise, if the element is larger than the root node, then insert it as the root of the
right Sub-tree.

Now, let's see the process of creating the Binary search tree using the given data element. The
process of creating the BST is shown below –

Step 1 - Insert 36

ROOT

36

Step 2 - Insert 5.
As 5 is smaller than 36, so insert it as the root node of the left Sub-tree.
ROOT

36

Step 3 - Insert 6.
As 6 is lesser than 36, so insert it as the root node of the left Sub-tree.
ROOT

36

6
Step 4 - Insert 78.
78 is greater than 36 and 6, so it will be inserted as the right of the root 36.

Step 5 - Insert 5.
5 is smaller than 45 and 6, so it will be inserted as a left Sub-tree of 6.

Step 6 - Insert 6.
6 is smaller than 36 and greater than 5 sub tree, so it will be inserted as the right Sub-tree of 5.

Step 7 - Insert 34.


34 is smaller than 36 but greater than 6, so it will be inserted as the right Sub-tree of 6.

Step 8 - Insert 12.


12 is smaller than 36 and 34, so it will be inserted as the left Sub-tree of 34.

Step 9 - Insert 4.
4 is smaller than 45 and 5(left sub tree), so, it will be inserted as a left Sub-tree of 5.

Step 10 - Insert 78.


78 is greater than 36 and equal to sub tree 78, so it will be inserted as the right of the 78.

Step 11 - Insert 90.


90 is greater than 36 and sub tree 78, so it will be inserted as the right of the sub tree 78.

Step 12 - Insert 40.


40 is greater than 36 and smaller than sub tree 78, so it will be inserted as the left of the sub
tree 78.
ROOT

36

5 78

78
6 40
4

5 34 90

6 12

Now, the creation of binary search tree is completed.


Q2: Consider the following graph and perform DFS on it-.

Answer:
Q3: Consider the following tree and perform in-order traversal on it.

Answer: If we want to traverse the nodes in ascending order, then we use the in-order
traversal. Following are the steps required for the in-order traversal:

o Visit all the nodes in the left Sub-tree


o Visit the root node
o Visit all the nodes in the right Sub-tree
Example by Graph

The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above
tree using in order traversal.

• Here, 40 is the root node. We move to the left Sub-tree of 40, that is 30, and it also has
Sub-tree 25, so we again move to the left Sub-tree of 25 that is 15. Here, 15 has no Sub-
tree, so print 15 and move towards its parent node, 25.
• Now, print 25 and move to the right Sub-tree of 25.

• Now, print 28 and move to the root node of 25 that is 30.

• So, left Sub-tree of 30 is visited. Now, print 30 and move to the right child of 30.
• Now, print 35 and move to the root node of 30.

• Now, print root node 40 and move to its right Sub-tree.


• Now recursively traverse the right Sub-tree of 40 that is 50.
50 have Sub-tree so first traverse the left Sub-tree of 50 that is 45. 45 has no children,
so print 45 and move to its root node.

• Now print 50 and move to the right Sub-tree of 50 that is 60.

• Now recursively traverse the right Sub-tree of 50 that is 60. 60 have Sub-tree so first
traverse the left Sub-tree of 60 that is 55. 55 has no children, so print 55 and move to its
root node.
• Now print 60 and move to the right Sub-tree of 60 that is 70.

• Now print 70.

After the completion of in order traversal, the final output is -

{15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70}

Q4: Discuss about BFS using proper example.

Answer: BFS (Breadth First Search)


BFS traversal of a graph produces a spinning tree as final result. Spinning tree is graph without
loops. we use Queue data structure with maximum size of total numbers of vertices in the
graph to implement BFS traversal.

We use the following step to implement BFS traversal.

• Step 1- Define a Queue of size total number of vertices in the graph


• Step 2- Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
• Step 3- Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
• Step 4- When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
• Step 5- Repeat step 3 and 4 until queue becomes empty.
• Step 6- When Queue becomes empty, then produce final spinning tree by removing
unused edges from the graph.
Example:

Consider the following example graph to perform BFS traversal.

Step 1

• Select the vertex A as starting point (visit A)


• Insert A into the Queue.

Step 2

• Visit all adjacent vertex of A which are not visited (D,E,B).


• Insert newly visited vertices into the Queue and delete A from the Queue.
Step 3

• Visit all adjacent vertices of D which are not visited (there is no vertex).
• Delete D from the Queue.

Step 4

• Visit all adjacent vertices of E which are not visited ( C,F ).


• Inserted newly visited vertices into the Queue and delete E from the Queue.

Step 5
• Visit all adjacent vertices of B which are not visited (there is no vertex).
• Delete B from the Queue.

Step 6
• Visit all adjacent vertices of C which are not visited ( G).
• Inserted newly visited vertices into the Queue and delete C from the Queue.
Step 7
• Visit all adjacent vertices of F which are not visited (there is no vertex).
• Delete F from the Queue.

Step 8
• Visit all adjacent vertices of G which are not visited (there is no vertex).
• Delete G from the Queue.

• Queue becomes empty so stop BFS process.


• Final result of BFS is a Spinning tree is shown below :
Q5: What is Graph? What are different ways using which one can represent the Graph?

Answer: A Graph is a non-linear data structure consisting of vertices and edges. The vertices are
sometimes also known as nodes and the edges are lines or arcs that connect any two nodes in
the graph. More formally a graph is composed of a set of vertices (V) and a set of edges ( E).
The graph is denoted by G(E,V).

Components of Graph:

Vertices: Vertices are the fundamental unit of graphs. Sometimes vertices are also known as
vertex or nodes. Every node/vertex can be labeled or unlabeled.

Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered paired of
nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no
rules. Sometimes edges are also known as arcs. Every edge can be labeled or unlabeled.

There are two ways in which we represent graphs, these are;

1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix

Adjacency matrix representation makes use of a matrix (table) where the first row and first
column of the matrix denote the nodes (vertices) of the graph. The rest of the cells contains
either 0 or 1 (can contain an associated weight w if it is a weighted graph).
Each row X column intersection points to a cell and the value of the cell will help us in
determining that whether the vertex denoted by the row and the vertex denoted by the
column are connected or not. If the value of the cell for v1 X v2 is equal to 1, then we can
conclude that these two vertices v1 and v2 are connected by an edge, else they aren’t
connected at all.

Consider the given graph below:

The graph shown above is an undirected one and the adjacency matrix for the same looks are :

The above matrix is the adjacency matrix representation of the graph shown above. If we look
closely, we can see that the matrix is symmetric. Now let’s see how the adjacency matrix
changes for directed graph.

Consider the given graph below:


For the directed graph shown above the adjacency matrix will look something like this:

Adjacency List

In the adjacency list representation, we have an array of linked-list where the size of the array is
the number of the vertex(nodes) present in the graph. Each vertex has its own linked-list that
contains the nodes that it is connected to.

Consider the graph shown below:


The above graph is an undirected one and the Adjacency list for it looks like:

The first column contains all the vertices we have in the graph above and then each of these
vertices contains a linked list that in turn contains the nodes that each vertex is connected to.
For a directed graph the only change would be that the linked list will only contain the node on
which the incident edge is present.

Consider the graph shown below:


The above graph is a directed one and the Adjacency list for this looks like:

You might also like