Sei sulla pagina 1di 6

CS6103 SS Lab

Problem Set 1c : Data Structures

09-Aug-2016

Submission date : on or before 13-08-16 Saturday 11:55 pm


Submit as a single .tar file.
The name of this file must be PS1C_<FIRSTNAME>_<ROLLNO>.tar (eg :
PS1C_LAXMAN_M160xxxCS.tar).
Do not add any other files except your source code.
The source codes must be named as
PS1C_<FIRSTNAME>_<ROLLNO>_<QUESTIONNUMBER>.<extension> (eg: PS1C_LAXMAN_M160xxxCS_1.c).
If there is a part a and part b for a particular question, then, name the source
files for each part seperately as in PS1C_LAXMAN_M160xxxCS_1a.c
If you do not conform to the above naming conventions, your submission might
not be recognized by some automated tools, and hence will lead to a score of 0
for the submission.
So, make sure that you follow the proper naming conventions.

1. Implement stack using linked list and perform the following operations
1.
2.
3.
4.

Push()
Pop() - print the deleted element. If the stack is empty then print Empty
Peek() - print the top element. If the stack is empty then print Empty
reverseContent() - Reverse the elements in stack

Input Format Explanation


First line is Number of test cases (Q). Next Q lines contains a number that indicate the stack operation
(1,2,3,4) and next number is the argument for the operation if any.
Sample Input
19
1 100
1 200
1 300
2
3
1 400
1 500
1 600
4
2
2
1 700
1 800
2
2
2

CS6103 SS Lab

Problem Set 1c : Data Structures

Page 2 of 6

2
2
2
Sample Output
300
200
100
200
800
700
400
500
600
Empty
2. Implement Binary Search Tree as a program that supports the following operations.
1. insert() Insert a new node
2. delete() Delete a specific node
3. successor() Retrieve the inorder successor of a specific node
4. predecessor() Retrieve the inorder successor of a specific node
5. findMax() Retrieve the largest value in the tree
6. findMin() Retrieve the smallest value in the tree
7. inOrder traversal
8. levelOrder traversal
9. preOrder traversal
10. postOrder traversal
11. leastCommonAncestor() Given two nodes find the ancestor whose value is least
12. getTopView() (All nodes which are visible when you are looking from root of the tree) For example consider the below BST, getTopView() should return 1,3,8,10,14(Order of nodes are not important)

Cont.

CS6103 SS Lab

Problem Set 1c : Data Structures

Page 3 of 6

Input Format The first line contains T, denoting the number of test cases. The remaining T lines
start with a number which indicate the tree action to be performed . Second and third indicates the
attribute for the operation (if any)
Sample Input
22
1 12
1 13
1 15
1 14
12
2 13
1 10
19
1 11
18
1 20
3 15
4 11
6
2 20
5
7
8
9
10
11 8 11
12
Sample Output
12 13 15
20
10
8
15
8 9 10 11 12 14 15
12 10 15 9 11 14 8
12 10 9 8 11 15 14
8 9 11 10 14 15 12
10
12 10 15 9 8
3. Create a program to perform the following graph algorithms
Each subpart should be submitted as seperate source file following the naming conventions mentioned
above
a. Breadth First Search
Input Format
The first line contains T, denoting the number of test cases. First line of each test case has two integers
(N,M), where N denotes the number of nodes (labelled from 1 ...N) in the graph and M denotes the
number of edges in the graph. The next M lines each consist of two space separated integers x y, where

Cont.

CS6103 SS Lab

Problem Set 1c : Data Structures

Page 4 of 6

x and y denote the two nodes between which the edge exists. The last line of a testcase has an integer
S, denoting the starting position.
Output Format
For each of test cases T, print a single line consisting of nodes in BFS traversal from the starting node S
Sample Input
2
42
12
13
1
31
23
2
Sample Output
123
23
b. Depth First Search
Input Format
The first line contains T, denoting the number of test cases. First line of each test case has two integers
(N,M), where N denotes the number of nodes (labelled from 1 ...N) in the graph and M denotes the
number of edges in the graph. The next M lines each consist of two space separated integers x y, where
x and y denote the two nodes between which the edge exists. The last line of a testcase has an integer
S, denoting the starting position.
Output Format
For each of test cases T, print a single line of nodes denoting BFS traversal from the starting node S
Sample Input
2
42
12
13
1
31
23
2
Sample Output
123
23
c. Prims Minimum Spanning Tree algorithm
Input Format
The first line contains T, denoting the number of test cases. First line of each test case has two integers
(N,M), where N denotes the number of nodes (labelled from 1 ...N) in the graph and M denotes the
number of edges in the graph. The next M lines each consist of two space separated integers x y z,
where x and y denote the two nodes between which the undirected edge exists and z denotes weight of
the edge. The last line of a testcase has an integer S, denoting the starting position.

Cont.

CS6103 SS Lab

Problem Set 1c : Data Structures

Page 5 of 6

Output Format
Print a single integer denoting the total weight (sum of weights of all edges in the MST)
Sample Input
1
46
125
133
416
247
324
345
1
Sample output
12
d. Kruskals Minimum Spanning Tree algorithm
Input Format
The first line contains T, denoting the number of test cases. First line of each test case has two integers
(N,M), where N denotes the number of nodes (labelled from 1 ...N) in the graph and M denotes the
number of edges in the graph. The next M lines each consist of three space separated integers x y r,
where x and y denote the two nodes between which the undirected edge exists, r denotes the weight of
edge between x and y.
Output Format
Print a single integer denoting the total weight (sum of weights of all edges in the MST)
Sample Input
1
46
125
133
416
247
324
345
Sample Output
12
e. Floyd Warshalls all pair shortest path algorithm
Input Format
The first line contains T, denoting the number of test cases. First line of each test case starts with
number of vertices (N). Which is followed by an adjacancy matrix of the Graph
Output Format
Cost of the shortest path between each vertices in a Matrix form
Sample Input
1

Cont.

CS6103 SS Lab

Problem Set 1c : Data Structures

Page 6 of 6

5
0 10 0 5 0
10 0 5 5 10
05000
5 5 0 0 20
0 10 0 20 0
Sample Output
0 10 15 5 20
10 0 5 5 10
15 5 0 10 15
5 5 10 0 15
20 10 15 15 0
f. Dijkstras single source shortest path algorithm
Input Format
The first line contains T, denoting the number of test cases. First line of each test case has two integers
(N,M), where N denotes the number of nodes in the graph and M denotes the number of edges in the
graph. The next M lines each consist of three space-separated integers x y r , where x and y denote
the two nodes between which the undirected edge exists, r denotes the length of edge between these
corresponding nodes. The last line has an integer S, denoting the starting position If there are edges
between the same pair of nodes with different weights, they are to be considered as is, like multiple edges
Output Format
For each of the test cases T, print a single line consisting N-1 space separated integers denoting the
shortest distance of N-1 nodes other than S from starting position S in increasing order of their labels.
For unreachable nodes, print -1
Sample Input
1
44
1 2 24
1 4 20
313
4 3 12
1
Sample Output
24 3 15

The End.

Potrebbero piacerti anche