Ques13: What is AVL Tree? Give an example. Ans: An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees. Example: Ques14: What is Balance Factor in AVL trees? Give an example. Ans: The Balance Factor is a numerical value that represents the difference in heights between the left and right subtrees of a node. The balance factor helps maintain the balance property of AVL trees, ensuring that the tree remains balanced and maintains its logarithmic height. Balance factor = Height [left sub-tree] – Height [right sub-tree]
Ques15: Explain AVL Rotations with an example. Ans: 1. Left to Left Rotations When the pivot node is left heavy [balance factor is 1] and the new node is inserted in left subtree of the left child of pivot node then the rotation performed is left to left rotation Example:
2. Right to Right Rotation When the pivot node is right heavy and the new node is inserted in right subtree of the right child of pivot node then the rotation performed is right to right rotation. This is mirror image to left to left rotation. Example: 3. Left to Right Rotation When the pivot node is left heavy and the new node is inserted in right sub tree of the left child of pivot node then the rotation performed is left to right rotation. Example: 4. Right to Left Rotation When the pivot node is right heavy and the new node is inserted in left subtree of the right child of pivot node then the rotation is right to left rotation. Example:
Ques16: Create an AVL tree by inserting the following elements: 60,10,20,30,19,120,100,80,19 Ans: Ques17: What is Heap Tree? Ans: A heap tree is a binary tree with a specific order property: in a max heap, each node is greater than or equal to its children, and in a min heap, each node is less than or equal to its children. It’s commonly used for priority queues. Ques18: How heaps are represented in memory? Give the memory representation. Ans: Heaps are typically represented in memory using arrays. In a heap, the elements are stored in an array such that the relationship between parent and child nodes can be easily determined using index calculations.
For a binary heap, if a node is at index i, its left child is at index 2i + 1, and its right child is at index 2i + 2. Conversely, if a node is at index j, its parent is at index (j -1)/2. 0 1 2 3 4 5 6 7 8 90 85 80 75 70 60 55 50 40 Heap representation using Array Ques19: Build a max heap H from the given set of numbers: 45,36,54,27,63,72,61 and 18. Also draw the memory representation of the heap. Ans:
Ques20: Build a max heap H from the given set of numbers: 45,36,54,27,63,72,61 and 18. Then insert 15,29,90,55 and delete 27,36,61. Ans:
Ques21: Give an example for Trie data structure. Ans: Ques22: Define B-Tree. Ans: A B-Tree is a self- balancing m-way tree data structure that allows searches, accesses, insertions and deletions in a logarithmic time. Each node in a B-Tree of order m can have, at most, m children and m-1 keys. Ques23: Define Multi Way Search Trees? Ans: A multi way search tree is a generalized version of a binary search tree that allows for efficient data searching and sorting. This can have multiple children per node. This allows for faster search and sort operations, especially on large datasets.
Ques24: Write the procedure of inserting a new element into B- Tree. Ans:
Ques25: Write the procedure of deleting an element from B-Tree. Ans: Step 1: Search for the element to be deleted. Step 2: If the element is found, delete it from the node. Step 3: If the node has fewer than the minimum number of keys, rebalance the tree. Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy
UNIT 3 Chapter-9 Graphs
UNIT 3 Chapter-9 Graphs Question & Answer: - Ques1: Define the below graphs and give example. (a) Directed Graphs Ans: A directed graph is defined as a type of graph where the edges have a direction associated with them. Example of Directed Graphs (b) Undirected Graphs Ans: An undirected graph is a type of graph where the edges have no specified direction assigned to them. Example of Undirected Graphs
Ques2: Define subgraph with an example. Ans: A graph whose vertices and edges are subset of another graph is called subgraph. Example of subgraph Ques3: Write a note on following. (a) Adjacent and incident vertices Ans: Adjacent Vertices: Two vertices in a graph are said to be adjacent if there exists an edge that connects them. Incident Vertices: For an edge in a graph, the vertices connected by that edge are called incident vertices. In other words, the endpoints of an edge are incident vertices.
(b) Indegree and Outdegree Ans: Indegree: The indegree of a vertex in a directed graph is the count of incoming edges to that vertex. In other words, it is the number of edges pointing towards the vertex. Outdegree: The outdegree of a vertex in a directed graph is the count of outgoing edges from that vertex. It is the number of edges leaving the vertex. Ques4: Explain complete graph with a example Ans: A complete graph is an undirected graph in which every pair of distinct vertices is connected by a unique edge. In other words, every vertex in a complete graph is adjacent to all other vertices. A complete graph is denoted by the symbol K_n, where n is the number of vertices in the graph. Example of complete graph
Ques5: Explain connected and strongly connected graph. Ans: Connected graph: A connected graph is a graph in which there is a path (a sequence of edges) between every pair of vertices. Strongly connected graph: A strongly connected graph is a concept associated with directed graphs. In a strongly connected graph, there is a directed path from every vertex to every other vertex in the graph. Ques6: Define Weighted graph. Give an example. Ans: A weighted graph is a type of graph in which each edge (connection between vertices) is assigned a numerical value called a "weight." These weights typically represent some measure of distance, cost, time, or any other relevant metric associated with the connection between the vertices. Example of weighted graph
Ques7: Explain the following terms: (a) Path A path refers to a sequence of connected vertices in a graph, where each connection is established by an edge. It represents a route or a way to move from one vertex to another within the graph structure. (b) Simple path Simple path is a sequence of distinct vertices connected by edges in such a way that no vertex is visited more than once, except for the first and last vertices in the path. (c) Cycle Cycle is a path that forms a loop, allowing for traversal through a series of distinct vertices and edges, ultimately returning to the starting point. (d) Simple cycle Simple cycle is a cycle without any self-intersections or revisits to the same vertex or edge, except for the initial and final vertices. Ques8: How graph can be represented using adjacency matrix? Ans: A graph can be represented using an adjacency matrix, which is a 2D array (matrix) where each cell at position i , j represents whether there is an edge between i and j. The size of the matrix is determined
by the number of vertices in the graph. If the graph is undirected, the matrix is symmetric because the relationship between vertex i and j is the same as the relationship between j and i. If the graph is directed, the matrix may not be symmetric. Ques9: How graph can be represented using adjacency list? Ans: An adjacency list represents a graph as an array of linked lists. The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex. Linked list representation of the graph Ques10: Explain the different ways of representing graphs. Ans: Different ways of representing graphs are: 1. Adjacency List: In an adjacency list, each vertex is associated with a list of its neighboring vertices. It is a more memory-efficient representation for sparse graphs. Space complexity is O(V + E), where V is the number of vertices and E is the number of edges. 2. Edge List:
An edge list represents a graph as a list of edges, where each edge is a pair of vertices. Simple and easy to understand, but not efficient for certain operations. Space complexity is O(E), where E is the number of edges. 3. Incidence Matrix: An incidence matrix is used for directed graphs and bipartite graphs. Rows represent vertices, columns represent edges, and entries indicate whether a vertex is incident on an edge. Space complexity is O(V * E), where V is the number of vertices and E is the number of edges. 4. Compact Sparse Matrix: This is a hybrid representation that combines features of adjacency matrix and adjacency list. Suitable for graphs with a moderate number of vertices and edges. Can be more memory-efficient than an adjacency matrix for certain cases.
Ques11: What is Depth first search? Explain with an example. Ans: Depth- first search [DFS] is a recursive algorithm that searches tree or graph data structure. The algorithm starts at the root node and explores as far as possible along each branch. If it can’t go any further, it backtracks until it finds an explored path. Example: Ques12: What is Breadth- first search? Explain with an example. Ans: Breadth-first search [BFS] is an algorithm that searches a tree or graph data structure for a node that meets a given property. It starts at the root of the tree or graph and explores all nodes at the current depth level before moving on to nodes at the next depth level. Example:
Ques13: Write an algorithm for depth-first search. Ans: //Let G(V,E) is a set of vertices and edges. //Let v be the starting vertex. // Visited [i] = false initially for all vertices //For any node I, visited [i] = true if I has already been visited, else it contains false. //stack is represented as s. { For (all V in G) Visited [v] = false; Add source vertex ‘v’ to stack ‘s’ While (stack is not empty) { pop top element N from stack s; if (visited [N] = = false) then { Visited[N] = true; } For all vertices W adjacent from N do {
Push w into stack s; } } } Ques14: Write an algorithm for breath-first search. Ans: // let G(V, E) is a set of vertices and edges. // let v be the starting vertex. // visited [i] = false initially for i = 1 to n //For any node i, visited [i] = true if I has already been visited, else it contains false // queue is represented as q. { For(all v in G) Visited [v] = true; While(q is not empty) { Delete from element N from q; For all vertices w adjacent from N do { If(visited [w] = = false) then
{ Add w to q; Visited[w] = true; } } } } far as possible before backtracking. Ques15: What is topological sorting? Give an example. Ans: Topological sorting is a linear ordering of the vertices of a Directed Acyclic Graph [DAG] such that for every directed edge [u,v] vertex ‘u’ comes before vertex ‘v’ in the ordering. In simple terms, it arranges the vertices in a way that respects the partial order defined by the directed edges, ensuring that if there is a directed edge from vertex u to vertex v, then u comes before v in the ordering. Example:
Ques16: Consider a graph shown below. Starting at vertex ‘5’ traverse the graph by depth first search. P.T.O
Ans:
Ques17: Consider the graph shown below. Starting at vertex ‘1’, traverse the graph by DFS and BFS. Draw the BFS and DFS spanning trees. Ans: +
Ques18: Find the topological sort for the below a directed acyclic graph. Ans: t
Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy
Unit-4 Chapter-10 Searching & Sorting
UNIT-4 CHAPTER-10 Searching and Sorting Question & Answers: - Ques1: What is Searching technique? Ans: Searching techniques refer to methods and strategies used to locate specific information or items within a given context. Ques2: Explain Linear search method. Ans: It is the simplest of all searching techniques which does not expect specific ordering of the data. Linear searching is nothing but searching an element/record in a linear way. This can be in arrays or in linked lists or in some other list. We need to start the search from the beginning by comparing the ‘key’ in each case and by scanning all the elements one by one until the end of array or linked list. If search is successful/retrieval, the entire record of that particular ‘key’ or the index of that record or the pointer to that record is returned. If the search is unsuccessful, it gives a failure notification (‘-1’ is returned) Ques3: Explain Binary search method. Ans: In Binary search, if the array is of large size, all the entries are first arranged in either ascending or descending order and then made in two halves. The required entry is first compared with middle element. If they are equal, the search has been completed successfully.
If the middle element is greater than the item being searched for, the search process is repeated for the first half of the array; otherwise the process is repeated for the second half/last half. Note, that each time a comparison is made, the number of elements to be searched is cut into half. That is why, this method is very useful for large arrays. Ques4: Write an algorithm to implement Linear search. Ans: Ques5: Write a C-program to demonstrate linear search. Ans:
Ques6: Write a C-Program to implement binary search. Ans:
Ques7: Write a C-Program to implement Binary search using recursion. Ans: Ques8: Compare Linear and Binary search techniques. Ans: 1.Search Method: Linear Search: Also known as sequential search, it involves checking each element in the list one by one until the target element is found or the end of the list is reached. Binary Search: Requires a sorted list and works by repeatedly dividing the search interval in half. It compares the target element with the middle element of the sorted list and narrows down the search range. 2.Efficiency: Linear Search: Typically has a time complexity of O(n), where n is the number of elements in the list. It performs a constant-time operation for each element in the worst case. Binary Search: Has a time complexity of O(log n) in the average and
worst cases, where n is the number of elements. It efficiently reduces the search space by half at each step. 3.Sorted vs. Unsorted Lists: Linear Search: Can be used on both sorted and unsorted lists. Binary Search: Requires a sorted list as it relies on dividing the search space in half, which is only possible in a sorted order. 4.Number of Comparisons: Linear Search: In the worst case, the number of comparisons is proportional to the number of elements in the list. Binary Search: The number of comparisons is logarithmic, making it significantly more efficient than linear search for large data sets. 5.Memory Usage: Linear Search: Requires minimal additional memory beyond storing the list itself. Binary Search: Involves recursive calls or iterative steps, which may require additional memory for function calls or loop variables. Ques9: What is Sorting. Ans: Sorting refers to the operation of arranging the given data items either in ascending order (numerically or lexicographically) or descending order. Let A be a list of a n elements A1,A2……..An in memory. For ascending order the elements would be A1<A2<A3……<An For descending order the elements would be A1>A2>A3……>An. Ques10: Write C-Program to sort ‘n’ numbers using a) Bubble sort b) Selection Sort c) Insertion Sort d) Shell Sort e) Quick Sort f) Merge Sort
Ans: Bubble Sort:
Selection Sort: Insertion Sort:
Shell Sort:
Quick Sort:
Merge Sort:
Ques11: Write a C program to sort an array using of integers using selection sort method. Show the steps to sort the elements. 25,57,48,37,12,92,86,33. Ans:
Ques12: Explain Insertion Sort. Ans: The insertion sorts inserts each element in appropriate position. This is same as playing cards, in which we insert a card in proper position. If an array A contains n elements and we place each element of an array at proper place in previously sorted element list. The first pass starts with comparison of 1st element with 0th element. i.e., A[1] with A[0]. In the second pass 2nd element is compared with 0 th and 1st element, i.e., A[2] with A[0] and A[1]. In general, in every pass an element is compared with all elements before it. Ques13: What are the factors to be considered during selection of a sorting techniques? Ans: the following factors are to be considered during the selection of sorting technique: - • Execution Time: Execution time of program means how much time is required for execution of program. • Space: How much space is required for variables. • Coding time: How much time is required to develop a sorting technique for the given problem. Ques14: Explain Divide and Conquer technique with an example Ans: The “divide and conquer” technique involves solving particular problem by dividing it into one (or) more sub problems of smaller size, recursively solving each sub problem and then “merging” the solutions to the sub-problem to produce a solution to the original problem. The divide and conquer paradigm involve three steps at each level of recursion. 1. Divide: Divide the problem into a number of sub problems.
2. Conquer: Conquer the sub problems by solving them recursively. If the sub problem sizes are small enough, then solve the sub problem in a straightforward manner. 3. Combine: Combine the solutions to the sub problems in to the solution for the original problem. Ques15: Write a program to implement Merger Sort using Linked List. Ans:
Ques16: Why Linked list implementation is suitable for merge sort ? Ans: Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quick sort) perform poorly, and others(such as heap sort) completely impossible. Merge sort is a divide and conquer algorithm in which need of random access to elements is less. Hence making Linked list more suitable for merge sort. Ques17: Explain the efficiencies of Linear and Binary search. Ans: Linear Search • Suitable for small lists or unordered lists. • It is easy to implement and doesn't require the data to be sorted. • However, its time complexity is proportional to the size of the list, which makes it less efficient for large datasets. Binary Search • Highly efficient for large, sorted datasets.
• The time complexity is logarithmic, which means the search space is divided in half with each comparison. • Binary search outperforms linear search for large datasets but requires the data to be sorted, and additional sorting steps may be needed. Ques18: Explain the efficiencies of Quick sort and Merge sort. Ans: Quick Sort • Quick sort is generally faster than many other sorting algorithms in practice. • It has good cache performance due to its partitioning nature. • The worst-case time complexity is less likely to occur in practice if a good pivot selection strategy is used. Merge Sort • Merge sort has a consistent and optimal time complexity of O(n log n) in all cases, making it a reliable choice. • It is a stable sort, meaning that the relative order of equal elements is preserved. • Merge sort performs well for linked lists as well as arrays. Ques19: Write a C function for recursive Binary search Ans: