You are on page 1of 10

SUPREME KNOWLEDGE FOUNDATI0N GROUP

OF INSTITUTIONS

NAME : ABHIJIT PAL

UNIVERSITY ROLL NO : 25300121050


UNIVERSITY REG. NO : 212530100110052
STREAM : CSE YEAR : 2nd SEMESTER : 3rd
SUBJECT NAME : Data Structure and Algorithm
SUBJECT CODE : PCC-CS301
Adjacency list:
1: 2,3
2: 3,5
3: 4
4: 2,5,6
5: 7
6: 5,7
7: 4,8,9,10
8: 9,10
9: 1
10: 9

ANSWER:
Let assume starting node is 10

Step-1: push 10 in stack and status of 10

become 2Stack : 10
Status 1: 1 2 3 4 5 6 7 8 9 Status 2: 10 Status3:

Step-2: pop the top element of the stack 10,print it status of 10 is right now 3 , push all
the neighbour of 10 in the stack that are ready state i.e 9 .status of 9 is now 2
 Print :10
 Stack :9
Status 1: 1 2 3 4 5 6 7 8 Status 2: 9 Status3: 10
Step-3: pop the top element of the stack 9 , print it status of 9 is right now 3, push all the
neighbour of 9 in the stack that are ready state i.e 1. Status of 1 is now 2
 Print :9
 Stack :1

Status 1: 2 3 4 5 6 7 8 Status 2: 1 Status3: 10 9


Step-4: pop the top element of the stack 1 , print it status of 1 is right now 3, push all the
neighbour of 1 in the stack that are ready state i.e 2,3. Status of 2,3 is now 2
 Print :1
 Stack :2,3
Status 1: 4 5 6 7 8 Status 2: 2 3 Status3: 10 9 1

Step-5: pop the top element of the stack 3 , print it status of 3 is right now 3, push all the
neighbour of 3 in the stack that are ready state i.e 4. Status of 4 is now 2
 Print :3
 Stack :4
Status 1: 5 6 7 8 Status 2: 2 4 Status3: 10 9 1 3

Step-6: pop the top element of the stack 4 , print it status of 2 is right now 3, push all the
neighbour of 2 in the stack that are ready state i.e 5,6. Status of 5,6 is now 2
 Print :4
 Stack :5,6
Status 1: 7 8 Status 2: 2 5 6 Status3: 10 9 1 3 4

Step-7 : pop the top element of the stack 6 , print it status of 6 is right now 3, push all the
neighbour of 6 in the stack that are ready state i.e 7. Status of 7 is now 2
 Print :6
 Stack :7
Status 1: 8 Status 2: 2 5 7 Status3: 10 9 1 3 4 6
Step-8: pop the top element of the stack 7 , print it status of 7 is right now 3, push all the
neighbour of 7 in the stack that are ready state i.e 8. Status of 8 is now 2
 Print :7
 Stack :8
Status 1: Status 2: 2 5 8 Status3: 10 9 1 3 4 6 7
Step-9: pop the all element form the top in the stack , and print all the element and its status is
right now 3.

Status 1: Status 2: Status3: 10 9 1 3 4 6 7 8 5 2


Hence , the stack now become empty and all the node of the graph have traversed and the

printing sequence is that 10,9,1,3,4,6,7,8,5,2

Adjacency list:
1: 2,3
2: 3,5
3: 4
4: 2,5,6
5: 7
6: 5,7
7: 4,8,9,10
8: 9,10
9: 1
10: 9
ANSWER:
Let assume starting node is 10
Step-1 : push 10 in queue and status of 10 become 2
Queue :10
Status 1: 1 2 3 4 5 6 7 8 9 Status 2: 10 Status3:

Step-2: delete an element form queue 10 and print it , status of 10 is right now 3 , push all the neighbour of 10 in
the queue that are ready state i.e 9 .status of 9 is now 2

 Print :10
 Queue :9
Status 1: 1 2 3 4 5 6 7 8 Status 2: 9 Status3: 10
Step-3: delete an element form queue 9 and print it , status of 10 is right now 3 , push all the neighbour of 9 in the
queue that are ready state i.e 1 .status of 1 is now 2

 Print :9
 Queue :1
Status 1: 2 3 4 5 6 7 8 Status 2: 1 Status3: 10 9
Step-4: delete an element form queue 1 and print it , status of 1 is right now 3 , push all the neighbour of 1 in the
queue that are ready state i.e 2,3 .status of 2,3 is now 2
 Print :1
 Queue :2,3
Status 1: 4 5 6 7 8 Status 2: 2 3 Status3: 10 9 1
Step-5: delete an element form queue 2 and print it , status of 1 is right now 3 , push all the neighbour of 2 in the
queue that are ready state i.e 5 .status of 5 is now 2

 Print :2
 Queue :3,5
Status 1: 4 6 7 8 Status 2: 3 5 Status3: 10 9 1 2
Step-6: delete an element form queue 3 and print it , status of 3 is right now 3 , push all the neighbour of 3 in the
queue that are ready state i.e 4 .status of 4 is now 2

 Print :3
 Queue :5,4
Status 1: 6 7 8 Status 2: 5 4 Status3: 10 9 1 2 3

Step-7: delete an element form queue 5 and print it , status of 5 is right now 3 , push all the neighbour of 5 in the
queue that are ready state i.e 7 .status of 7 is now 2

 Print :5
 Queue :4,7
Status 1: 6 8 Status 2: 4 7 Status3: 10 9 1 2 3 5
Step-8: delete an element form queue 4 and print it , status of 4 is right now 3 , push all the neighbour of 4 in the
queue that are ready state i.e 6 .status of 6 is now 2

 Print :4
 Queue :7,6
Status 1: 8 Status 2: 7 6 Status3: 10 9 1 2 3 5 4

Step-9: delete an element form queue 7 and print it , status of 7 is right now 3 , push all the neighbour of 7 in the
queue that are ready state i.e 8 .status of 8 is now 2

 Print :7
 Queue :6,8
Status 1: Status 2: 6 8 Status3: 10 9 1 2 3 5 4 7

Step-10:delete all element the element in the stack , and print all the element and its status is right now 3.

Print :6,8
Status 1: Status 2: Status3: 10 9 1 2 3 5 4 7 6 8

Hence now the queue is empty is and all the node of the graph have traversed and the printing sequence is

10,9,1,2,3,5,4,7,6,8
In this graph we can see here total vertices is 10

So the adjacency matix will be

So the matrix is:

After adding the 11 the graph is :

after adding the node 11 the adjacency matix will be 11*11


Adjacency list:
1:2,3
2:3,5
3:4
4:2,5,6
5:7
6:5,7
7:4,8,9,10
8:9,10
9:1
10:9

After add 11 node and delete 4->2 and


5->7 edge the graph is :

Adjacency list:
1:2,3
2:3,5
3:4
4:5,6
6:5,7,11
7:4,8,9,10
8:9,10
9:1
10:9,11
11:7

You might also like