









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Question and Answers on Data Structures.
Typology: Exercises
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Introduction to graphs, Definition, Terminology, Directed, Undirected & Weighted graph, Representation of graphs, graph traversal- depth-first and breadth-first traversal of graphs, applications. Searching: sequential searching, binary searching, Hashing – linear hashing, hash functions, hash table searching; Sorting: Quick Sort, Exchange sort, Selection sort and Insertion sort.
1. Explain when a directed graph is said to be unilaterally connected? Ans:graph is said to be unilaterally connected if it contains a directed path from u to v OR directed path from v to u for every pair of vertices u, v. Hence, at least for any pair of vertices, one vertex should be reachable form the other. Such a path matrix would rather have upper triangle elements containing 1’s OR lower triangle elements containing 1’s. 2. What is the complexity of selection sort? Explain. Ans:Selection sort algorithm consists of two nested loops. By the two nested loops, it has O(n^2 ) time complexity. 3. A technique for direct search is : Hashing 4. The worst case occur in linear search algorithm when : Item is the last element in the array or is not there at all 5. Data structure used for DFS: Stack 6. What is weighted graph? A graph having a weight, or number, associated with each edge. Some algorithms require all weights to be nonnegative, integral, positive, etc. Also known as edge-weighted graph. 7. How to define data structure of a non-weighted graph? Adjacency Matrix Adjacency List 8. Define forest. Also give example of it. Forest is a collection of disjoint trees. In other words, we can also say that forest is a collection of an acyclic graph which is not connected. Here is a pictorial representation of a forest. 9. What is an undirected graph? An undirected graph is a set of nodes and a set of links between the nodes. Each node is called a vertex, each link is called an edge, and each edge connects two vertices. The order of the two connected vertices is unimportant. An undirected graph is a finite set of vertices together with a finite set of edges.
10. Define adjacency matrix. an adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph. In the special case of a finite simple graph, the adjacency matrix is a (0,1)-matrix with zeros on its diagonal. If the graph is undirected (i.e. all of its edges are bidirectional), the adjacency matrix is symmetric. 11. Write the hash function on integers will distribute keys most uniformly over 10 buckets numbered 0 to 9 for I ranging from 0 to 2020. (A) h(i) =i^2 mod 10 (B) h(i) =i^3 mod 10 (C) h(i) = (11 ∗ i^2 ) mod 10 (D) h(i) = (12 ∗ i) mod 10 Ans: Since mod 10 is used, the last digit matters. If you do cube all numbers from 0 to 9, you get following Number Cube Last Digit in Cube 0 0 0 1 1 1 2 8 8 3 27 7 4 64 4 5 125 5 6 216 6 7 343 3 8 512 2 9 729 9 Therefore all numbers from 0 to 2020 are equally divided in 10 buckets. If we make a table for square, we don’t get equal distribution. In the following table. 1, 4, 6 and 9 are repeated, so these buckets would have more entries and buckets 2, 3, 7 and 8 would be empty. Number Square Last Digit in Cube 0 0 0 1 1 1
return 0; }
13. How the depth first traversal algorithm works? Explain Depth-first search is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. So the basic idea is to start from the root or any arbitrary node and mark the node and move to the adjacent unmarked node and continue this loop until there is no unmarked adjacent node. Then backtrack and check for other unmarked nodes and traverse them. Finally, print the nodes in the path. Algorithm o Step 1: SET STATUS = 1 (ready state) for each node in G o Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state) o Step 3: Repeat Steps 4 and 5 until STACK is empty o Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state) o Step 5: Push on the stack all the neighbours of N that are in the ready state (whose STATUS = 1) and set their STATUS=2(waitingstate) [END OF LOOP] o Step 6: EXIT 14. When we call a graph is complete? Explain. a complete graph is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge. A complete digraph is a directed graph in which every pair of distinct vertices is connected by a pair of unique edges (one in each direction). 15. Define multigraph. a multigraph is a graph which is permitted to have multiple edges (also called parallel edges) , that is, edges that have the same end nodes. Thus two vertices may be connected by more than one edge. ... When multiple edges connect two nodes, these are different edges.
1. What is breadth first search? Explain with example Given an input graph G = (V, E) and a source vertex S, from where the searching starts. The breadth first search systematically traverse the edges of G to explore every vertex that is reachable from S. Then we examine all the vertices neighbor to source vertex S. Then we traverse all the neighbors of the neighbors of source vertex S and so on. A queue is used to keep track of the progress of traversing the neighbor nodes. ALGORITHM 1. Input the vertices of the graph and its edges G = (V, E) 2. Input the source vertex and assign it to the variable S. 3. Add or push the source vertex to the queue. 4. Repeat the steps 5 and 6 until the queue is empty ( i.e., front > rear) 5. Pop the front element of the queue and display it as visited. 6. Push the vertices, which is neighbor to just, popped element, if it is not in the queue and displayed ( i.e., not visited). 7. Exit. 2. What is hash function? Explain different hash functions with example? Ans: The two heuristic methods are hashing by division and hashing by multiplication which are as follows:
with a searched element till the element is not found. the array. Sorted data In a linear search, the elements don't need to be arranged in sorted order. The pre-condition for the binary search is that the elements must be arranged in a sorted order. Implementation The linear search can be implemented on any linear data structure such as an array, linked list, etc. The implementation of binary search is limited as it can be implemented only on those data structures that have two-way traversal. Approach It is based on the sequential approach. It is based on the divide and conquer approach. Size (^) It is preferrable for the small-sized data sets. It is preferrable for the large-size data sets. Efficiency It is less efficient in the case of large-size data sets. It is more efficient in the case of large-size data sets. C Program for sequential search
Ans: Following is an example of an undirected graph with 5 vertices. The following two are the most commonly used representations of a graph.
1. Adjacency Matrix 2. Adjacency List There are other representations also like, Incidence Matrix and Incidence List. The choice of graph representation is situation-specific. It totally depends on the type of operations to be performed and ease of use. Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w. The adjacency matrix for the above example graph is: Adjacency List: An array of lists is used. The size of the array is equal to the number of vertices. Let the array be an array[]. An entry array[i] represents the list of vertices adjacent to the i th vertex. This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs. Following is the adjacency list representation of the above graph.
int main(){ int i, count, number[25]; printf("How many elements are u going to enter?: "); scanf("%d",&count); printf("Enter %d elements: ", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1); printf("Order of Sorted elements: "); for(i=0;i<count;i++) printf(" %d",number[i]); return 0; }
2. How can we sort a list of numbers using insertion sort? Write an algorithm with a suitable example of insertion sort. Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part. Algorithm To sort an array of size n in ascending order: 1: Iterate from arr[1] to arr[n] over the array. 2: Compare the current element (key) to its predecessor. 3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element. 3. Discuss with division method and mid square method in hashing schemes Division Method TABLE is an array of database file where the employee details are stored. Choose a number m , which is larger than the number of keys k. i.e., m is greater than the total number of records in the TABLE. The number m is usually chosen to be prime number to minimize the collision. The hash function H is defined by H( k ) = k (mod m ) Where H( k ) is the hash address (or index of the array) and here k (mod m ) means the remainder when k is divided by m. For example: Let a company has 90 employees and 00, 01, 02, ...... 99 be the two digit 100 memory address (or index or hash address) to store the records. We have employee code as the key. Choose m in such a way that it is grater than 90. Suppose m = 93. Then for the following employee code (or key k ) : H( k ) = H(2103) = 2103 (mod93) = 57
Searching is a process of checking and finding an element from a list of elements. Following are the three important searching techniques :
Let A be a linear array of size n , A [1], A [2], A [3] ...... A [ n ], l1 and u 1 represent lower and upper bounds of the first sub-array and l2 and u 2 represent the lower and upper bound of the second sub-array. Aux is an auxiliary array of size n. Size is the sizes of merge files.
7. What is exchange sort? Write algorithm and sort the following array using exchange sort method. 84 69 76 86 94 91 The exchange sort compares each element of an array and swap those elements that are not in their proper position, just like a bubble sort does. The only difference between the two sorting algorithms is the manner in which they compare the elements.The exchange sort compares the first element with each element of the array, making a swap where is necessary. The Exchange Sort Algorithm: