Algorithms - Assignment - UNIT 4
Algorithms - Assignment - UNIT 4
ASSIGNMENT UNIT- 4
COURSE: B. TECH
BRANCH- ECE
SUBMITTED
BY:
ARTI KAK
SEMESTER III
Objective types Questions
Answer: Non-linear
Answer: Non-linear.
Q3: BST contains all smaller values at . (Left side/ Right side)
Answer: Preorder.
Answer: Siblings : Children of the same parent node are called siblings.
D, E are Siblings.
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.
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.
Graphs data structures have a variety of applications. Some of the most popular applications
are:
• 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.
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.
• 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 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.
36
5 78
78
6 40
4
5 34 90
6 12
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:
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.
• 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 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.
{15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70}
Step 1
Step 2
• Visit all adjacent vertices of D which are not visited (there is no vertex).
• Delete D from the Queue.
Step 4
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.
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.
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.
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.
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.
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.