Dsa Final Paper
Dsa Final Paper
Reg No:
DATA STRUCTURE AND ALGORITHM (THEORY)
FINAL EXAMINATION
QUESTION NO: 01
CODE:
import java.util.Scanner;
class Node{
int data;
Node next;
this.data = data;
this.next = null;
class NodeBST {
int key;
key = data;
NodeBST root;
LinkListInsertionAndBSTCreation(){
root = null;
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
else {
if (head.data == data){
head = newNode;
head.next = temp;
if (head == null) {
if (position != 0) {
return;
head = newNode;
tail = newNode;
if (head.data == data){
newNode.next = this.head;
this.head = newNode;
return;
int i = 1;
if (current.data == data){
previous = current;
current = current.next;
if (current == null) {
break;
i++;
newNode.next = current;
previous.next = newNode;
else{
tail.next = newNode;
tail = newNode;
System.out.println("List is empty");
return;
while(current != null) {
current = current.next;
System.out.println();
int listLength = 0;
if(head == null) {
return 0;
while(current != null) {
current = current.next;
listLength++;
return listLength;
}
// insert a node in BST
//tree is empty
if (root == null) {
return root;
// return pointer
return root;
void inorder() {
inorder_Recursive(root);
}
// recursively traverse the BST
if (root != null) {
inorder_Recursive(root.left);
inorder_Recursive(root.right);
while(current != null) {
bstree.insert(current.data);
current = current.next;
bstree.inorder();
try {
sList.addAtStart(7);
sList.addAtStart(2);
sList.addAtStart(6);
sList.addAtStart(8);
sList.insertNth(4, -1);
sList.insertNth(10, -1);
sList.insertNth(1, -1);
sList.insertNth(9, -1);
sList.insertNth(3, -1);
catch (IllegalAccessException e) {
/*while (true) {
try{
int e = sc.nextInt();
int p = sc.nextInt();
sList.insertNth(e, p);
if (!goAgain.equals("n") || !goAgain.equals("N")) {
break;
catch (IllegalAccessException e) {
} */
sList.display();
// System.out.println(listlength);
sList.createTree();
OUTPUT:
QUESTION NO: 02
a)Division method with m=97
Hash address
For KeyboardKey5+8+8+2=23
For cableKey6+7+1+3=17
For monitorKey1+8+2+5=16
digits in the keys are added to generate code
digits in the keys are reversed and are added to generate code
Program in java
class hashCode
//uses mod97
System.out.println("For KeyboardKey"+KeyboardKey%97);
System.out.println("For cableKey"+cableKey%97);
System.out.println("For webcamKey"+webcamKey%97);
System.out.println("For monitorKey"+monitorKey%97);
System.out.println();
System.out.println("For KeyboardKey"+gethash(KeyboardKey));
System.out.println("For cableKey"+gethash(cableKey));
System.out.println("For webcamKey"+gethash(webcamKey));
System.out.println("For monitorKey"+gethash(monitorKey));
System.out.println();
System.out.println("For KeyboardKey"+gethashFoldingMethod(KeyboardKey));
System.out.println("For cableKey"+gethashFoldingMethod(cableKey));
System.out.println("For webcamKey"+gethashFoldingMethod(webcamKey));
System.out.println("For monitorKey"+gethashFoldingMethod(monitorKey));
System.out.println();
System.out.println("Hash address with Folding method with reversing is ");
System.out.println("For KeyboardKey"+gethashFoldingMethodR(KeyboardKey));
System.out.println("For cableKey"+gethashFoldingMethodR(cableKey));
System.out.println("For webcamKey"+gethashFoldingMethodR(webcamKey));
System.out.println("For monitorKey"+gethashFoldingMethodR(monitorKey));
System.out.println();
int sum=0;
int r=0;
r=i%10;
sum=sum+r;
return sum;
int sum=0;
int r=0;
r=i%10;
sum=sum+r;
int rev=0;
r=i%10;
rev=(rev*10)+r;
return rev;
}
}
OUTPUT:
QUESTION NO: 03(a)
Answers.
A priority queue can have any implementation, like a array that you search linearly when you
pop. All it means is that when you pop you get the value with either the minimum or the
maximum depending. A classic heap as it is typically referred to is usually a min heap.
An implementation that has good time complexity (O(log n) on push and pop) and no memory
overhead.
QUESTION NO: 03(b)
If vertex v is leaf node of MST, then vertex v will be connected with remaining portion of MST
by least weight edge e which is incident on v to minimize the total weight of MST. So minimum
weight edge e incident on v will be taken for MST.
If vertex v is not leaf node of MST then it must be connecting two connected components of
MST. In this case also, edge e will be connected with one of the connected component of MST to
minimize the total weight of MST and another edge will connect other connected component of
MST.
So in either case, the minimum weight edge which is incident on any vertex v must be the part of
MST.
Since we know that for every vertex having minimum weight edge incident on it must be the part
of MST. So since there are |V| vertices in the graph, we can select |V|/2 edges such that each of
these edges is minimum weight edge incident on atleast one vertex out of 2 vertices that this
edge connect. Hence we could select |V|/2 edges and then contract these edges to reduce the size
of graph to |V|/2 vertices and edge size will also reduce.
Since contracting one edge of graph requires transformation of graph which could take O(|V|+|
E|) time as per operations mentioned in question. So contacting |V|/2 edges will require O(|V|(|V|
+|E|)) time complexity.
Since this algorithm perform the same task on new graph with |V|/2 vertices, so the time
complexity recurrence will be
T(|V|) = T(|V|/2) + O(|V|2+|V||E|),
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v)
is not the same as (v, u) in case of a directed graph(di-graph). The pair of the form (u, v)
indicates that there is an edge from vertex u to vertex v. The edges may contain
weight/value/cost.
Graphs are used to represent many real-life applications: Graphs are used to represent networks.
The networks may include paths in a city or telephone network or circuit network. Graphs are
also used in social networks like linkedIn, Facebook. For example, in Facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like person
id, name, gender, and locale. See this for more applications of graph.
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.