Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

UNIT IV

CHAPTER - 7
TREES
7.1 DEFINITION:
A tree is a finite set of one or more nodes such that,
i. There is a specially designated node called the root.
ii. The remaining nodes are collection of sub trees.

BINARY TREES:
A binary tree is a special form of a tree
A binary tree can also be defined as a finite set of nodes
Such that,
i.T is empty
ii.T contains a specially designated node called the root of T, and the remaining nodes of T form
two disjoint binary trees T1 and T2 which are called left sub tree and the right sub-tree.

Two special situations of binary tree are possible,


Full binary tree
Complete binary tree

FULL BINARY TREE:


A binary tree is a full binary tree if the contains the maximum possible number of nodes at all
levels.
COMPLETE BINARY TREE:
A binary tree is to be a complete binary tree if all its levels, except possibly the last levels, have
the maximum number of possible nodes and all the nodes in the last level appear as far left as possible.

7.2 BASIC TERMINOLOGIES


 Node
 Parent
 Child
 Link
 Root
 Leaf
 Level
 Height
 Degree
 Sibling
 Path

NODE:
A node of a tree stores the actual data and link to the other node.

PARENT:
The parent of a node is the immediate predecessor of a node.
Here, X is the parent of Y and Z
CHILD:
If the immediate predecessor of a node is the parent of a node then all immediate successors of
a node are known as child. Here, Y and Z are the two child of X.
The child which is on the left side is called the left child and that on the right side is called the
right child.
LINK:
This is a pointer to a tree; there may be more than two links on the node.
ROOT:
This is a specially designated node which has no parent. A is a root node. (Fig (a))

Fig (a)
LEAF:
The node which is at the end does not have any child is called leaf node. In above fig (a) H, I,
K, L and M are the leaf nodes.
A leaf node is also called terminal node.
LEVEL:
Level is the rank in the hierarchy. The root node has level 0.
If a node is at level L, then its child is at level L + 1 and the parent is at level L - 1.
HEIGHT:
The maximum number of nodes that is possible in a path starting from the root node to a leaf
node is called the height of a tree.
The longest path is A-C-F-J-M and hence the height of this tree is 5. (Fig (a))
DEGREE:
The maximum number of the children that is possible for a node is known as the degree of a
node. For example, the degree of each node of the tree is 2. (Fig (a)
SIBLINGS:
The nodes which have the same parent are called sibling.
For example, J & K are siblings.
PATH:
Sequence of consecutive edges is called a path the path from root node A to M is A-C-F-J-M
and the length of this path is 5. (Fig (a))
7.3 REPRESENTATION OF BINARY TREE
There are the two common methods used for representing this conceptual structure.
1. Sequential (linear) or Array Representation of a Binary tree.
2. Linked representation of a binary tree.
7.3.1 LINEAR REPRESENTATION OF BINARY TREE:
This type of representation is static, that a block of memory for an array is allocated before
storing the actual tree in it, and once the memory is allocated, the size of the tree is restricted.
The representation is as follows:
1. The binary tree root node is stored at location 1.
2. The remaining nodes left and right are stored in following way.
a. If the tree T has N nodes the left child is stored in A[2*K].
b. The right child is stored in A[2*K+1].

The array blocks 8, 9, 10, 11, 12 and 13 are blank because nodes A, B and C does not contain the
childs.
ADVANTAGES:
1. Any node can be accessed from any other node by calculating the index and this is efficient
from execution point of view.
2. Only data are stored without any pointer.
3. It is efficient and convenient representation.
DISADVANTAGES:
1. Other than the full binary tree, the majority of the array entries may be empty.
2. It allows only static representation; it is in no way possible to enhance the tree structures if the
array is limited.
3. Inserting a new node to the tree or deleting a node from it is inefficient with this
representation.
4. The size of the tree structure is unpredictable; this representation uses static allocation leads to
wastage of memory space.
7.3.2 LINKED REPRESENTATION OF BINARY TREE:
Linked representation of binary tree is similar to the way in which doubly linked list are
represent in memory that is maintained in memory by means of a linked list each node will have three
fields.

LC: Points to the left child of node.


Data: Contains data of the left node.
RC: Points to the right child of node.

ADVANTAGES:
1. The linked representation of binary tree is the efficient use of computer storage and computer
time.
2. The insertion and deletion operation may be performed more easily.
3. When the size of a tree structure is unpredictable, the linked allocation technique is suitable,
because allocates memory space dynamically.
7.4 OPERATION ON BINARY TREE
The major operations on a binary tree can be listed as follows:
1. Insertion
2. Deletion
3. Traversal
4. Merge
7.4.1 INSERTION:
With this operation, a new node can be inserted into any position in a binary tree.
G node can be inserted as a left child of a node having data content E.
The insertion procedure is a two-step process.
1. To search for the existence of a node in the given binary tree which an insertion is made.
2. To establish a link for the new node.
ALGORITHM INSERT BINARY TREE-SEQ
STEPS:
1. L=search_SEQ(1,KEY)
2. If(l=0)then
3. Print"search is unsuccessful:No insertion"
4. Exit
5. Endif
6. If (A[2*2]=NULL)or(A[2*l+1]=NULL)then
7. Read option to read as left (L)or right(R)
8. If (option=L)then
9. If A[2*l]=NULL then
10. A[2*l]=ITEM
11. Else
12. Print "Desired insertion is not possible"
13. Exit
14. Endif
15. Endif
16. If (option=R)then
17. If (A[2*l+1]=NULL)then
18. A[2*l+1]=ITEM
19. Else
20. Print "Desired operation is not possible"
21. Exit
22. Endif
23. Endif
24. Else
25. Print "ITEM cannot be inserted as a leaf node"
26. Endif
27. Stop
ALGORITHM SEARCH-SEQ:
STEPS:
1. i=INDEX
2. If (A[i]≠KEY)then
3. If(2*i<_size)then
4. Search_SEQ(2*i,KEY)
5. Else
6. If (2*i+1<_SIZE)then
7. Search_SEQ(2*i+1,KEY)
8. Else
9. Return(0)
10. Endif
11. Endif
12. Else
13. Return(i)
14. Endif
15. Stop
ALGORTHIM INSERTBINARY TREE-LINK
1. ptr=Search_LINK(ROOT,KEY)
2. If(ptr=NULL)then
3. Print "Search is unsuccessful:No insertion"
4. Exit
5. Endif
6. If (ptr→LC=NULL)or(ptr→RC=NULL)
7. Read option to insert as left (L) or right(R) child
8. If (option→NULL)then
9. If(ptr→LC=NULL)then
10. new=GetNode(NODE)
11. new→DATA=ITEM
12. new→LC=new→RC=NULL
13. ptr→LC=new
14. Else
15. Print "Insertion is not possible as left child"
16. Exit
17. Endif
18. Else
19. If (ptr→RC=NULL)
20. new=GetNode(NODE)
21. new→DATA=ITEM
22. new→LC=new→RC=NULL
23. ptr→RC=new
24. Else
25. Print "Insertion is not possible as right child"
26. Exit
27. Endif
28. Else
29. Print "The key node already has child"
30. Endif
31. Endif
32. Stop
ALGORITHM SEARCH_LINK
1. ptr=PTRO
2. If (ptr→DATA≠KEY)
3. If(ptr→LC≠NULL)
4. Search_LINK(ptr→LC)
5. Else
6. Return(0)
7. Endif
8. If (ptr→RC≠NULL)
9. Search_LINK(ptr→RC)
10. Else
11. Return(0)
7.4.2 DELETION:
This operation is used to delete any node from any non-empty binary tree.
A node containing data G can be deleted from a binary tree.
In order to delete a node in a binary tree, it is required to reach at the parent node of the node
to be deleted.
The line field of the parent node which stores the location of the node to be deleted is then set
by a NULL entry.
ALGORTHIM DELETE BINARY TREE-SEQ
STEPS:
1. flag=FALSE
2. l=search_SEQ(1,KEY)
3. If l=0 GOTO setup 10
4. If (A[2*lJ=NULL)or(A[2*l+1]=NULL)
5. flag=TRUE
6. A[l]=NULL
7. Else
8. Print "The node containing ITEM is not a leaf node"
9. Endif
10. If (flag=FALSE)
11. Print "Node does not exist:No deletion"
12. Endif
13. Stop
ALGORITHM DELETE BINARY TREE-LINK:
STEPS:
1. ptr=ROOT
2. If(ptr=NULL)then
3. Print "Tree is empty"
4. Exit
5. Endif
6. Parent=search parent(ROOT,ITEM)
7. If(Parent=NULL)then;
8. ptr1=parent→LC,ptr2=parentt→RC
9. If(ptr→DATA=ITEM)then
10. If(ptr→LC=NULL)and(ptr1→RC=NULL)
11. parent→LC=NULL
12. Else
13. Print "Node is not a leaf node: No deletion"
14. Endif
15. Else
16. If(ptr2→LC=NULL)and(ptr2→RC=NULL)then
17. Parent→RC=NULL
18. Else
19. Print" Node is not a leave node: No deletion"
20. Endif
21. Endif
22. Else
23. Print "Node with data ITEM does not exist: Deletion fails"
24. Endif
25. Stop
ALGORITHM: SEARCH PARENT
STEPS:
1. parent=ptr
2. If(ptrDATA≠ITEM)then
3. ptr1=ptr→LC,ptr2=PTR→RC
4. If(ptr1≠NULL)
5. Searchparent(ptr1)
6. Else
7. Parent=NULL
8. Endif
9. If(ptr2=NULL)then
10. Searchparent(ptr2)
11. Else
12. Parent=NULL
13. Endif
14. Else
15. Return(parent)
16. Endif
17. Stop
7.4.3 TRAVERSAL:
This operation is used to visit each node in the tree exactly once.
A full traversal on a binary tree gives a linear ordering of the data in the tree.
The traversal of a tree is performed in three different ways
i. Preorder
ii. Inorder
iii. Postorder
In each of these methods involves in visiting the root and traversal its left and right sub trees.
PREORDER TARVERSAL:
In this traversal, the root is visited first, and then the left sub tree, in the then the right sub-tree,
that is
 Visit the root node R
 Traverse the left sub-tree of R
 Traverse the right sub-tree of R
R Tl Tr

Example1:

Preorder Traversal: + - A B * C / D E
Example2:

Preorder Traversal: 18, 16, 11, 14, 81, 64, 26, 73, 143
ALGORITHM FOR PREORDER:
STEPS:
1. ptr=ROOT
2. If(ptr≠NULL)then
3. Visit(ptr)
4. Preorder(ptr→LC)
5. preorder(ptr→RC)
6. Endif
7. Stop
INORDER TRAVERSAL:
With this traversal before visiting the root node, the left sub=tree of the root node is visited,
then the root node and after the visit of the node the right sub-tree of the root node is visited i.e.,
 Traverse the left sub-tree of the root node R.
 Visit the root node R.
 Traverse the right sub-tree of the root node R.
Tl R Tr
Example1:

Inorder Traversal: A – B + C * D / E
Example2:

Inorder Traversal: 11, 14, 16, 18, 26, 64, 73, 81, 143
ALGORITHM FOR INORDER:
STEPS:
1. ptr=ROOT
2. If(ptr≠NULL)then
3. Inorder(ptr→LC)
4. Vist(ptr)
5. Inorder(ptr→RC)
6. Endif
7. Stop
POSTORDER TRAVERSAL:
In this case, the root node is visited in the end, that is first visit the left sub-tree, then the right
sub-tree and finally the root node.
 Traverse the left sub-tree of the root R.
 Traverse the right sub-tree of the root R.
 Visit the root node R.
Tl Tr R
Example1:

Postorder Traversal: A B – C E F / * +
Example2:

Postorder Traversal: 14, 11, 16, 26, 73, 64, 143, 81, 18
ALGORITHM FOR POSTORDER:
STEPS:
1. ptr=ROOT
2. If(ptr≠NULL)then
3. Postorder(ptr→LC)
4. postorder(ptr→RC)
5. Visit(ptr)
6. Endif
7. Stop
7.4.4 MERGING OF BINARY TREE:
This operation is applicable to trees which are represented using a linked structure.
There are two ways that this operation can be carried out.
Suppose T1 and T2 are two binary trees. T2 can be merged with T1 if all the nodes from T2 are
inserted into the binary tree T1 one by one.
There is another way, when the entire tree T2 (or) T1 can be included as a sub-tree of T1 (or)
T2.
Before performing merging, we have to test for compatibility if in both trees, the root node has
both the left and right sub trees, and then the merge will fail.
If T1 has left sub-tree (or) right sub-tree empty then T2 will be added as the left sub-tree of the
T1.
T(n1+n2)=T1(n1)+T2(n2)
Where T is the resultant tree after merging T2 and T1

ALGORITHM
STEPS:
1. If(ROOT1=NULL)then
2. ROOT=ROOT2
3. Exit
4. Else
5. If(ROOT2=NULL)then
6. ROOT=ROOT1
7. Exit
8. Else
9. If(ROOT1→LCHILD=NULL)then
10. Root1→LCHILD=ROOT2
11. ROOT=ROOT1
12. Else
13. If(ROOT→RCHILD=NULL)then
14. ROOT1→RCHILD=ROOT
15. ROOT=ROOT2
16. Else
17. If (Root2→LCHILD=NULL)then
18. ROOT2→LCHILD=ROOT1
19. ROOT=ROOT2
20. Else
21. If(ROOT2→RCHILD=NULL)then
22. ROOT2→RCHILD=ROOT1
23. ROOT=ROOT2
24. Else
25. Print “Trees are not compatible for merge operation”
26. Exit
27. Endif
28. Endif
29. Endif
30. Endif
31. Endif
32. Endif
33. Stop
7.5 TYPES OF BINARY TREE
1. Expression tree
2. Binary tree
3. Heap tree
4. Threaded binary tree
5. Huffman tree
6. Height balanced tree
7. Red black tree
8. Splay tree
9. Decision tree
7.5.1 EXPRESSION TREE:
An expression tree is a binary tree which stores an arithmetic expression.
The leaves of an expression tree are operands such as constants or variable names and all
interval nodes are the operators.

Example
ALGORITHM BUILD EXPRESSIONS TREE:
STEPS:
1. Symbol=E.Getsymbol()
2. While (symbol ≠ #)do
3. If(symbol=operand)then
4. New=Getnode(NODE)
5. new→DATA=symbol
6. PUSH(new)
7. Else
8. If(symbol=operator)then
9. Ptr1=pop()
10. Ptr2=pop()
11. New=GetNode(NODE)
12. new→DATA=symbol
13. new→LCHILD=ptr2
14. new→RCHILD=ptr1
15. PUSH(new)
16. Endif
17. Endif
18. Symbol=E.Getsymbol
19. Endwhile
20. Stop
7.5.2 BINARY SEARCH TREE
A binary tree T is termed binary search tree (or binary sorted tree) if each node N of T satisfies
the following properties.
The value at N is greater than every value in the left sub-tree of N and is less then every value
in the right sub-tree of N.
Example
i) A binary search tree with numeric data ii) A binary search tree with alphabetic data

OPERATIONS ON A BINARY SEARCH TREE:


 Searching data
 Inserting data
 Deleting data
 Traversing the tree(same as binary tree traversal)
SEARCHING A BINARY SEARCH TREE:
Searching data is a binary search tree is must faster than searching data in arrays are linked
list.

We start form the root node R, then if ITEM is less than the value in the root node R, we
proceed to its left child if the ITEM is greater than the value in the node R, and we proceed to its right
child. The process will be continued till the ITEM is not found or we reach a dead end that is, the leaf
node.
ALGORITHM FOR SEARCH-B.S.T
STEPS:
1. Ptr = ROOT, flag = FALSE
2. While (ptr ≠ NULL)and(flag = FALSE)do
3. Case: ITEM < ptr→DATA
4. Ptr = ptr→LCHILD
5. Case: ptr→DATA=ITEM
6. Flag=TRUE
7. Case: ITEM > ptr→DATA
8. Ptr = ptr→RCHILD
9. End case
10. End while
11. If(flag=TRUE)then
12. Print ”ITEM has found at the node”,ptr
13. Else
14. Print ”ITEM does not exist: search is unsuccessful”
15. End if
16. Stop
INSERTING A NODE INTO A BINARY SEARCH TREE
To insert a node with data, say ITEM not a tree, the tree is required to be searched starting
from the root node. If ITEM is found, do nothing, otherwise ITEM is to be inserted at the dead end.
ALGORITHM INSERT –BST
STEPS:
1. Ptr=ROOT,flag=FALSE
2. While (ptr≠NULL)and(flag=FALSE)do
3. Case:ITEM<ptr→DATA
4. Ptr1=ptr
5. Ptr=ptr→RCHILD
6. Case:ITEM>ptr→DATA
7. Ptr1=ptr
8. Ptr=ptr→RCHILD
9. Case:ptr→DATA=ITEM
10. Flag=TRUE
11. Print”ITEM already exists”
12. Exit
13. Endwhile‟
14. Endcase
15. If(pt=NULL)then
16. New=Getnode(NODE)
17. new→DATA=ITEM
18. new→LCHILD=NULL
19. new→RCHILD=NULL
20. If (ptr1→DATA<ITEM)then
21. Ptr1→LCHILD=new
22. Else
23. Ptr1→LCHILD=new
24. Endif
25. Endif
26. Stop
DELETING A NODE FROM THE BINARY SEARCH TREE:
Deletion of N can then be carried out under the various situations,
Case1: N is the leaf node
Case2: N has exactly one child
Case3: N has two Childs

After deletion of node 27 (case 1)

After deletion of node 45 (case 2)

After deletion of node 20 (case 3)

ALGORITHM DELETE-BST
STEPS:
1. Ptr=ROOT,flag=FALSE
2. While (ptr≠NULL)and(flag=FALSE)do
3. Case:ITEM<ptr→DATA
4. Parent=ptr
5. Ptr=ptr→LCHILD
6. Case:ITEM>ptr→DATA
7. parent=ptr
8. Ptr=ptr→RCHILD
9. Case:ptr→ATA=ITEM
10. Flag=TRUE
11. Endcase
12. Endwhile
13. I(flag=FALSE)then
14. Print ”ITEM does not exist:No deletion”
15. Exit
16. Endif
17. If (ptr→LCHILD=NULL)and(ptr→RCHILD=NULL)then
18. Case=1
19. Else
20. If (ptr→LCHILD≠NULL)and(ptr→RCHILD≠NULL)then
21. Case=3
22. Else
23. Case=2
24. Endif
25. Endif
26. If (case=1)then
27. If(parent→LCHILD=ptr)then
28. parent→LCHILD=NULL
29. else
30. parent→RCHILD=NULL
31. endif
32. returnNode(ptr)
33. endif
34. If(case=2)then
35. If(parent→LCHILD=ptr)then
36. If(ptr→LCHILD=NULL)then
37. Parent→LCHILD=ptr→RCHILD
38. Else
39. parent→LCHILD=ptr→LCHILD
40. endif
41. else
42. If(parent→RCHILD=ptr)then
43. If(ptr→LCHILD=NULL)then
44. parent→RCHILD=ptr→RCHILD
45. else
46. parent→RCHILD=ptr→LCHILD
47. endif
48. endif
49. endif
50. return Node(ptr)
51. endif
52. If(case=3)
53. Ptr1=succ(ptr)
54. Item1=ptr→DATA
55. Delete-BST(item1)
56. ptr→DATA=item1
57. Endif
58. Stop
7.5.3 HEAP TREES:
H is a complete binary tree. It will be termed heap tree if it satisfies the following properties:
 For each node N is H, the value at N is greater than or equal to the value of each of the
children of N.
 N has a value which is greater than are equal to the value of every successor of N. Such as
heap tree is called max heap.
Similarly, a min heap is possible, where any node N has a value less then are equal to the
value of any of the successors of N.

In a max heap, the root node contains the largest data where as in a main heap it contains the
smallest data.
REPRESENTATION OF A HEAP TREE:
A heap tree can be represented using a linked structure, but single array represented using a
linked structure but a single array representation as certain advantages for a heap tree over its linked
representation.
As a heap tree is a complete binary tree, there will be no wastage of array space between the
two non-null entries. If there are null entries, there are only at the tail of the array.
So, it is better to consider single array representation for a heap tree.
Single array representation of a min heap tree

OPERATIONS ON A HEAP TREE


 INSERTION
 DELETION
 MERGING
INSERTION INTO A HEAP TREE:
This operation is used to insert a node into an existing heap tree for example,
If we want to insert 19 to the existing heap tree it will insert as follows,

The principle of insertion is that first we have to adjoin the data in the complete binary tree.
Next, we have to compare it with the data in its parent.
ALGORITHM INSERT MAX HEAP:
STEPS:
1. If(N ≥ SIZE)then
2. Print “insertion is not possible”
3. Exit
4. Else
5. N=N+1
6. A[N]=ITEM
7. i=N
8. p=i div 2
9. while (p>0) and (A[p] < A[i])do
10. temp=A[i]
11. A[i]=A[p]
12. A[p]=temp
13. i=p
14. p = p div 2
15. End while
16. Endif
17. Stop
DELETION OF A NODE FROM A HEAP TREE:
Any node can be deleted from a heap tree, but deleting the root node has some special
importance.
Read the root node into a temporary storage a ITEM.

Replace the root node by the last node in the heap tree then reheap the tree as stated below.
 Let the newly modified root node be the current no compare its values with the values of its
two children.
 Let X be the child whose value is the largest. Interchange the value of X with the values of the
current node.
 Make X as the current node.
 Continue reheap if the current node is not a empty node.
For Example,

The root node is 99.The last node is 26,then it is in level 3,so 99 is replaced by 26 and this node
with data 26 is removed from the tree.
Next 26 at the root node is compared with its two children 45 and 63.As 63 is greater, so they
are interchanged. Now, 26 is compared with the children 57 and 42, as 57 is greater. So, they are
interchanged now 26 appears as the leaf node, hence the reheap is completed.
ALGORTHIM DELETE MAX HEAP:
STEPS:
1. If(N=0)then
2. Print “heap deletion is not possible”
3. Exit
4. Endif
5. ITEM=A[1]
6. A[1]=A[N]
7. N=N-1
8. Flag=FALSE,i=1
9. While(flag=FALSE)and(i<N)do
10. lchild=2*i,rchild=2*i+1
11. If (lchild≤N)then
12. X=A[lchild]
13. Else
14. X=-∞
15. Endif
16. If(rchild≤N)then
17. Y=A[rchild]
18. Else
19. y=-∞
20. endif
21. If(A[i]>x)and(A[i]>y)then
22. Flag=TRUE
23. Else
24. If(x>y)and(A[i]<x)
25. Swap(A[i],A[lchild])
26. I=lchild
27. Else
28. If(y>x)and(A[i]<y)
29. Swap(A[i],A[rchild])
30. I=rchild
31. Endif
32. Endif
33. Endif
34. Endwhile
35. Stop
CHAPTER - 8
GRAPHS
Graph is another important non-linear data structure.
8.1 GRAPH TERMINOLOGIES:
GRAPH:
A graph G consists of two sets.
i. A set V called the set of all vertices (or nodes).
ii. A set E called the set of all edges (or arcs).
For Example,
Graph G1

V = {V1, V2, V3, V4}


E = {(V1, V2), (V1, V3), (V1, V4), (V2, V3), (V3, V4)}
DIGRAPH:
A digraph is also called a directed graph.
It is a graph G, such that, G = (V, E) where V is the set of all vertices and E is the set of ordered
pairs of elements from V.

V = {V1, V2, V3, V4}


E = {(V1,V2), (V1,V3), (V2,V3), (V3,V4), (V4,V1)}
A undirected graph (simple graph), the pair(Vi, Vj)and (Vj, Vi) are the same edges, but in case
of diagram they correspond to two different edges.
WEIGHTED GRAPH:
A graph (or digraph) is termed weighted graph if all the edges in it are labeled with some
weights
ADJACENT VERTICES:
A vertex Vi is adjacent to another vertex say Vj if there is an edge from Vi to Vj.

V2 is adjacent to V3 and V4, V1 is not adjacent to V4 but to V2.


SELF LOOP:
If there is an edge where starting and ends vertices are same, that is (Vi, Vj)is an edge then it is
called a self loop.

PARALLEL EDGES:
If there is more than one edge between the same pair of vertices, then they are known as
parallel edges.

Parallel edges between V1, V2


A graph which has either self loop or parallel edges, or both is called multigraph.

SIMPLE GRAPH:
A graph if it does not have any self loop or parallel edges is called simple graph.
COMPLETE GRAPH:
A graph G is said to be complete if each vertex V1 is adjacent to every other vertex Vj in G.
ACYCLIC GRAPH:
If there is a path containing one or more edges which starts from a vertex Vi and terminates
into same vertex then the path is known as a cycle.

If a graph doesn‟t have any cycle then it is called acyclic graph.

ISOLATED VERTEX:
A vertex is isolated if there is no edge connected from any other vertex to the vertex.

The vertex u is an isolated vertex.

DEGREE OF VERTEX:
The number of edges connected with vertex Vi is called the degree of vertex Vi and is donated
by degree (Vi).
There are two degrees, indegrees and outdegree
In degree of Vi denoted as indegree(Vi)=number of edges incident into Vi.
Outdegree(Vi)=number of edges starting from Vi.

i. Indegree(V1)=2, Outdegree(V1)=1
ii. Indegree(V2)=2, Outdegree(V2)=0
iii. Indegree(V3)=1, Outdegree(V3)=2
iv. Indegree(V4)=0, Outdegree(V4)=2
PENDANT VERTEX:
A vertex Vi is pendant if its indegree(Vi)=1 and outdegree(Vi)=0.
CONNECTED GRAPH:
In a graph G, two vertices Vi and Vj are said to be connected if there is a path in G from Vi to
Vj.
8.2 REPRESENTATION OF GRAPHS:
A graph can be represented in many ways,
 Set representation
 Linked representation
 Sequential(matrix) representation
Types of Graphs,

8.2.1 Set Representation:


With this method, two sets are maintained
i. V1 is the set of vertices
ii. E, is the set of edges, which is the sub set of V x V.
If the graph is weighted, the set E is the ordered collection of three tuples, that is E=WxVxV.
Graph G1,
V(G1)={V1,V2,V3,V4,V5,V6,V7}
E(G1)={(V1,V2),(V1,V3),(V2,V4),(V2,V5),(V3,V6),(V3,V7)}
Graph G2,
V(G2)={A,B,C,D,E}
E(G2)={(3,A,C)(5,A,B)(3,C,A)(8,C,B)(6,D,B)(4,D,C)(2,D,A)(7,D,E)(9,C,A)(2,E,B)
If the graph is a multigraph and undirected, this method doesn‟t allow to store parallel edges.
8.2.2 Linked Representation:
Linked representation is another space-saving way of graph representation. In this
representation, two types of node structure are assumed.

Representation of Graph G1
Representation of Weighted Digraph,

8.2.3 Matrix Representation:


This representation uses a square matrix of order nxn, n-number of vertices.
Entries in the matrix can be decided as follows.
Aij=1, if there is an edge from Vi to Vj.
=0, otherwise

8.3 OPERATIONS ON GRAPH:


INSERTION:
a) To insert a vertex and hence establish connectivity with other vertices in the existing graph.
b) To insert an edge between two vertices in the graph.
DELETION:
a) To delete a vertex from the graph.
b) To delete an edge from the graph.
MERGING:
To merge two graphs G1 and G2 into a single graph.
TRAVERSAL:
To visit all the vertices in the graph
8.3.1 INSERTION:
If we insert a vertex into a graph, the different steps involved.
In case of insertion of a vertex into an undirected graph, if Vx is inserted and Vi be its adjacent
vertex then Vi has to be incorporated in the adjacent list of Vx and also Vx has to be incorporated in the
adjacent list of Vi.

ALGORITHM INSERT VERTEX LL_UG:


STEPS:
1. N=N+1,Vx=N
2. For i=1 to l do
3. Let j=X[i]
4. If (J≥N)then
5. Print ”No vertex labeled X[i] exists: Edge is not established”
6. Else
7. InsertEnd_SL(UGptr[N],X[i])
8. InsertEnd_SL(UGptr[j],Vx)
9. Endif
10. Endfor
11. Stop
ALGORITHM INSERT VERTEX_LL-DG:

If it is a digraph and if there is a path from Vx to Vi then we add a node for Vi into adjacency
list of Vx, if there is an edge from Vi to Vx we add a node for Vx in the adjacent list of Vi.
STEPS:
1. N=N+1,Vx=N
2. For (i=1 to m do)
3. Let j=x[i]
4. If j≥N then
5. Print “No vertex labeled X[i] exists: Edge from Vx to X[i] is not established”.
6. Else
7. InsertEnd_SL(DGptr[N],X[i])
8. Endif
9. Endfor
10. For i=1 to n do
11. Let j=y[i]
12. If j≥N then
13. Print “No vertex labeled y[i] exists: Edge from y[i] Vx is not established”
14. Else
15. InsertEnd_SL(DGptr[j],Vx)
16. Endif
17. Endfor
18. Stop
ALGORITHM INSERT EDGE-LL_UG:
STEPS:
1. Let N=number of vertices in the graph.
2. If(Vi>N) or (Vj>N)then
3. Print “Edge is not possible between Vi and Vj”
4. Else
5. InsertEnd_SL(UGptr[Vi],Vj)
6. InsertEnd_SL(UGptr[Vj],Vi)
7. Endif
8. Stop

ALGORITHM INSERT EDGE-LL_DG:


STEPS:
1. Let N=number of vertices in the graph.
2. If(Vi>N) or (Vj>N)then
3. Print “Edge is not possible between Vi and Vj”
4. Else
5. InsertEnd_SL(DGptr[Vi],Vj)
6. Endif
7. Stop
8.3.2 DELETION:
This operation is again different for undirected graph and directed graph.

If we want to delete the vertex V8 from the graph to do this ,first we have to look for the
adjacency list of Vg all the vertices which are parent in the adjacency list of V8,the node labeled V8 has
to be deleted from the adjacent lists of those vertices.
For example, in the adjacency list of V8,two vertices namely V1 and V4.so we have to delete
the node labeled V8 from the adjacent list of V1 and V4.
ALGORITHM DELETE VERTEX-LL-UG:
STEPS:
1. If (N=0)then
2. Print “graph is empty: No deletion”
3. Exit
4. Endif
5. Ptr=Ugptr[Vx]→LINK
6. While(ptr≠NULL)do
7. J=ptr→LABEL
8. Delete any_SL(Ugptr[j],Vx)
9. Delete any_SL(Ugptr[Vx],j)
10. Ptr=Ugptr[Vx]→LINK
11. Endwhile
12. UGptr[Vx]→LABEL=NULL
13. Ugptr[Vx]→LINK=NULL
14. Return Node(ptr)
15. N=N-1
16. Stop
DELETE A VERTEX_LL_DG:

We should delete whole of the adjacency list of the V8. This removes all the edges which
emanating from V8 and removed from the adjacency list of V4.
ALGORITHM:
STEPS:
1. If (N=0)then
2. Print “Graph is empty: No deletion”
3. Exit
4. Endif
5. Ptr=DGptr[Vx]→LINK
6. DGptr[Vx]→LINK=NULL
7. DGptr[Vx]→LABEL=NULL
8. N=N-1
9. Return Node(ptr)
10. For i=1 to N do
11. Delete any_SL(DGptr[i],Vx)
12. Endfor
13. Stop
ALGORITHM DELETE EDGE_LL_UG:
STEPS:
1. Let N=number of vertices in the graph
2. If (Vi>N)or(Vj>N)then
3. Print “vertex does not exist :error in edge removed”
4. Else
5. Delete any_SL(UGptr[Vi],Vj)
6. Delete any_SL (UGptr[Vij],Vi]
7. Endif
8. Stop
ALGORITHM DELETE EDGE_LL_DG:
STEPS:
1. Let N=number of vertices in the graph
2. If (Vi>N)or(Vj>N)then
3. Print “vertex does not exist: Error in edge removal”
4. Else
5. Deleteany _SL(DGptr[Vi],Vj)
6. Endif
7. Stop
8.3.3 GRAPH TRAVERSAL:
Traversal a graph means visiting all the vertices in the graph exactly once.
Several methods are known to traverse a graph systematically, out of them two methods are
accepted as standard,
 Depth First Search (DFS)
 Breadth First Search (BFS)
DEPTHS FIRST SEARCH (DFS):
Depth first search (DFS) traversal is similar to the in order traversal of a binary tree. Starting
from a given node we can visit all the nodes which are reachable from that starting node. This traversal
visits all the nodes up to the deepest level and so on.

DFS (G1) = V1 – V2 – V5 – V7 – V4 – V8 – V6 – V3

DFS (G2) = V1 – V2 – V5 – V7 – V4 – V8 – V3 – V6
In this case the traversals take place up to the deepest level, for example V1-V2-V5-V7 then V4-
V8 and V6-V3 (in G1), V3-V6(in G2)
DFS traversal beginning at vertex V is that of visiting the vertex V first, then visit all the
vertices along the path which begins at v.
Visit the vertex V then the vertex immediate adjacent to V, let it be Vx if Vx has an immediate
adjacent say Vy then visit it and so on. Till there is a „dead end‟ this result in a path p, V-Vx-Vy.
Dead end means a vertex which does not have an immediate adjacent or its immediate
adjacent has already been invited.
After coming to a „dead end‟, we backtrack along P to V to see has another adjacent vertex
other than Vx.
A stack can be used to maintain the track of all paths from any other.
Initially, the starting vertex will be pushed onto the stack. To visit a vertex ,We are to pop a
vertex from OPEN, and the PUSH all the adjacent vertices onto it .
A list, VISIT can be maintained to store the vertices already visited.
When a vertex is poped, whether it is already or not, that can be known by searching the list
VISIT: If the vertex is already visited, we want simply ignore it and we will pop the stack for the next
vertex to be visited this procedure will be continued till the stack is not empty.
ALGORITHM DFS (INFORMAL DESCRIPTION)
STEPS:
1.Push the standing vertex into stack OPEN
2.While OPEN is not empty do
3.Pop a vertex V
4.If V is not in VISIT
5.Visit the vertex V
6.Store V in VISIT
7.Push all the adjacent vertices of V onto OPEN
8.Endif
9.Endwhile
10. Stop
ALGORITHM DFS_LL:
STEPS:
1. If Gptr=NULL then
2. Print “graph=graph is empty”
3. Exit
4. Endif
5. U=v
6. Open PUSH(K)
7. While (open .top ≠NULL)do
8. U=OPEN.pop()
9. If =open pop()
10. Inserted end _SL(VISIT,u)=FALSE)then
11. Ptr=Gptr[u]
12. While(ptr→LINK≠NULL)do
13. Vptr=ptr→LINK
14. OPEN.PUSH(Vptr→Label
15. Endif
16. Endif
17. Return(VISIT)
18. Stop
BREADTH FIRST SEARCH (BFS)
This traversal is very similar to the level by level traversal of a tree here, any vertex to the
level will be visited only after the visit of all the vertices in its proceeding level, that is at i=1.
BFS traversal is roughly equivalents to the preorder of a tree

BFS (G1) = V1 - V2 - V8 - V3 - V5 - V4 – V6 - V7

BFS (G2) = V1 - V2 – V3 – V5 – V4 – V6 – V7 – V8
In G1, V1 is in the first level and visited first then V2, V3 and V8 are visited which are in the
same level similarly V4, V5 and V6 and so on.
In G2, V2 and V3 are in same level V4, V5 and V6 are in one level again V7, V8 are in one level.
In implementation idea of the BFS traversal is almost same as the DFS traversal except that is
BFS we will use a queue structure inserted of a stack structure as in DFS.
ALGORITHM BFS_LL:
STEPS:
1. Gptr=NULL)then
2. Print “Graph is empty”
3. Exit
4. Endif
5. U=v
6. OPEN Q.ENQUEUE(u)
7. While (OPEN Q.STATUS()≠EMPTY)do
8. U=OPEN Q.DEQUEUE
9. If (search_SL(VISIT,u)=FALSE)then
10. insertENd_SL(VISIT,u)
11. ptr=Gptr[u]
12. While(ptr→LINK≠NULL)do
13. Vptr=ptr→LINK
14. OPEN Q.ENQUEUE(Vptr→LABEL)
15. Endwhile
16. Endif
17. Endwhile
18. Return (VISIT)
19. Stop
8.3.4 MERGING TWO GRAPHS
Consider two graphs G1 and G2; by merging we can combine these two graphs into a single
component .This can be accomplished by establishing one or more edges between the vertices in G1
and G2.

ALGORITHM:
STEPS:
1. For i=1 to N1 do
2. UGPTR[i]→LINK=UGPTR[i]LINK
3. Endfor
4. For i=1 to N2 do
5. UGPTR[N=1+i]LINK=UG2PTR[i]LINK
6. Endfor
7. N=N1+N2
8. While(S≠NULL) do
9. Read(V,W)
10. If (V<N1) and (W<N2)then
11. InsertEdge_UG_LL(UGPTR,V,N1+W)
12. Endif
13. Endwhiile
14. Return(UGPTR)
15. Stop
8.4 APPLICATIONS OF GRAPH
Graph is an important data structure whose extensive application is known in almost all
areas.
Nowadays, many application related with computation can be merged efficiently with graph
structures.
For example,
Consider two simple problems,
Transportation problem
Map coloring
These problems can be solved by using graph structures.
Transportation problem:
This is a well known problem in shipping goods.
There are several warehouses which are located in different places. It is required to transport
goods from a given warehouse to another.
The problem is to find a path of transportation from warehouse A to warehouse B, the cost of
transportation is minimum.
This problem can easily be represented using a graph structure where each warehouse can be
considered a vertex and path can be edge and weight of an edge is cost of transportation.
If there are several paths from A to B then we have to find the path where the sum at weights
is minimum.

Graph of Transportation Network


Map coloring:
We have to color a map so that no two adjacent regions have the same color.
This can be presented with the help of graph, each region can be represented as vertex, and if
two regions are adjacent use can represent this by an edge between the two vertices which represent
two regions.

Map and its corresponding Graph


It is not possible for all the problems, where graph structures are involved because this
domain is very large.
8.4.1 SHORTEST PATH PROBLEM (or) DIJKSTRA’S ALGORITHM:
The single source shortest path problem is a kind of shortest path problem.
Here, there is a distinct vertex called source vertex and it requires finding the shortest path
from this source vertex to all other vertices.

For example,
Let us consider a simple graph as shown below.

The different shortest path, assuming V1 as the source vertex, as listed below.
The algorithm to find such paths was first proposed by E.W.Dijkstra and is popularly known
as Dijkstra‟s algorithm.
Assume that all the vertices in the graph are labeled as 1, 2, 3,……., N and the graph is
represented through an adjacency matrix.
Dijkstra‟s algorithm requires three arrays as follows.
LENGTH [1….N] = Array of Distances
PATH [1…..N] = Array of Vertices
SET [1…..N] = Array of Boolean Tags
The shortest distance from the source to any vertex is stored in LENGTH[i], PATH[i] contain
the nearest predecessor of vertex i on the path of shortest distance, from source to vertex.
The Boolean array SET is used during the execution of the algorithm. SET[i] = 1 means the
shortest distance, and the path from the source to vertex i is already enumerated.
This algorithm consists of two major parts,
An initialization part
An iteration part
Algorithm:
Input: Gptr, the pointer to the graph S, the source vertex. Let N be the number of vertices.
Output: LENGTH, an array of distance from S to all other vertices. PATH, an array of string vertices
giving the track of all shortest path.
Data structure: Matrix representation of graph with Gptr as the pointer to it.
Steps:
1. For i = 1 to N do
2. SET[i] = 0
3. End for
4. For i = 1 to N do
5. If Gptr[S][i] = 0 then
6. LENGTH[i] = ∞
7. PATH[i] = NULL
8. Else
9. LENGTH[i] = Gptr[S][i]
10. PATH[i] = S
11. End if
12. End for
13. SET[S] = 1
14. LENGTH[S] = 0
15. Complete = FALSE
16. While (not complete) do
17. j = SearchMin(LENGTH, SET)
18. SET[j] = 1
19. For i = 1 to N
20. If SET[i] = 1 then
21. i = i+1
22. Else
23. If Gptr[i][j] ≠ 0 then
24. If ((LENGTH[j] + Gptr[i][j]) < LENGTH[i]) then
25. LENGTH[i] = LENGTH[j] + Gptr[i][j]
26. PATH[i] = j
27. End if
28. End if
29. End if
30. End for
31. Complete = TRUE
32. For i = 1 to N do
33. If SET[i] = 0 then
34. Complete = FALSE
35. Break
36. Else
37. i = i+1
38. End if
39. End for
40. End while
41. Return (LENGTH, PATH)
42. Stop
In this algorithm we have assumed a procedure SearchMin. This procedure will return the
label of vertex which has minimum distance and which is not yet enumerated in the shortest path.
Let us consider the graph as shown below.

The result of the application of Dijkstra algorithm on the graph is given below.
The shortest path from the array can be obtained by backward movement.
For example,
For the vertex 5, its immediate predecessor is 3 (at PATH[5]), immediate predecessor of
vertex 3 is 2 (at PATH[3]), immediate predecessor of vertex 2 is 1 (at PATH[2]) and vertex 1 is the
source vertex. Thus, the shortest path vertex 5 from the source vertex 1 is 1-2-3-5, and the length of
this shortest path is 5.
8.4.2 TOPOLOGICAL SORTING:
Topological sorting is an ordering of the vertices of a graph, such that, if there is a path from
u to v in the graph then u appears before v in the ordering.
For example,
In the graph, the topological sorting of all vertices is A-B-D-C.
Topological ordering is not possible in cyclic graph.
It is only possible if it is a directed acyclic graph, there exists a topological ordering of
vertices.
A simple technique to find a topological ordering is to find out any vertex with indegree zero
and add it to ordering set and then remove it along with its edge.
We can repeat this on the remaining graph until it is empty.

The topological ordering for a graph is not necessarily unique. For example, another
topological ordering possible for the same graph is,
V2-V5-V7-V4-V6-V1-V3
The above technique implemented as, suppose that the given graph is stored as an adjacency
matrix. Vertices are labeled 1, 2, ….. N.
We assume two arrays:
POSET[1….N] – Array of vertices, to store the topological ordering of vertices.
INCLUDE[1….N] – Array of Boolean values, INCLUDE[i] = True implies that vertex i is
already in topological ordering.
Informal description of topological sorting:
1. K = 1, seeking for first vertex in POSET array.
2. Decide the left most column, corresponding to the vertex which is to included in topological
ordering and whose indegree is zero. If no such vertex found, there is an error in graph and
exit.
3. If found, let it be i. include it as POSET[K] = i, K = K+1. Set INCLUDE[i] = True, the i th vertex
is included. Delete the ith column in the adjacency matrix so that the entries of the i th vertex
will not be considered later.
4. Repeat steps 2 – 3 until the POSET array is not full.
Algorithm:
Input: Gptr, the pointer to a graph. Let N be the no. of vertices in the graph.
Output: POSET, the topological order of the vertices.
Data structure: INCLUDE[1…N], array of Boolean values.
Steps:
1. For i = 1 to N do
2. POSET[i] = NULL
3. INCLUDE[i] = FALSE
4. End for
5. K=1
6. Flag = TRUE
7. While (flag) do
8. For i = 1 to N do
9. If not INCLUDE[i] then
10. zeroIndegree = TRUE
11. For j = 1 to N do
12. If (Gptr[i][j] > 0) then
13. zeroIndegree = FALSE
14. Break
15. End if
16. End for
17. If (zeroIndegree) then
18. INCLUDE[i] = TRUE
19. POSET[K] = i
20. K = K+1
21. For j = 1 to N do
22. Gptr[i][j] = -1
23. Gptr[j][i] = -1
24. End for
25. Break
26. End if
27. End if
28. End for
29. If (i = N) and (zeroIndegree = FALSE) then
30. Flag = FALSE
31. Print “Graph is no acyclic”
32. Return (NULL)
33. End if
34. End while
35. If (K = N) then
36. Return (POSET)
37. End if
38. Stop
This is applicable on both weighted and non-weighted directed acyclic graphs.
8.4.3 MINIMUM SPANNING TREE:
The spanning tree of a graph G can be defined as a tree which includes all the vertices of G.
There are two types of spanning trees are,
1. DFS Spanning Tree
2. BFS Spanning Tree
These spanning trees are obtained by DFS and BFS traversals, respectively, starting from
vertex V1.
It may be noted that starting with a different vertex; different spanning trees are also possible
with the use of these traversals.
Here we used a simple, undirected and non-weighted graph. If a graph is connected then it
always has minimum spanning tree.
The minimum spanning tree problem is related to the weighted graph, where we find a
spanning tree so that the sum of all the weights of all the edges in the tree is minimum.
There are several methods available for finding a minimum spanning tree of a graph, but
only two methods are known to be very efficient. They are,
1. Kruskal‟s algorithm
2. Prim‟s algorithm
Kruskal’s algorithm:
To obtain the minimum spanning tree of a graph, an approach was developed by J.B.Kruskal
known as Kruskal‟s algorithm.
Let us assume an undirected weighted graph with n vertices, where initially the spanning
tree is empty. This algorithm can be informally stated as follows,
1. List all the edges of the graph G in the increasing order of weights.
2. Select the smallest edge from the list and add it into the spanning tree, if the inclusion of
this edge does not make a cycle.
3. If the selected edge with smallest weight forms a cycle, remove it from the list.
4. Repeat steps 2-3 until the tree contains n-1 edges (or) list is empty.
5. If the tree T contains less than n-1 edges and the list is empty, no spanning tree is
possible for the graph, else return the minimum spanning tree T.
Let us consider an example, for weighted graph G. Lists of edges with their weights are
maintained in the form of a table.
From this list, we have to select ( ) or reject (x) depending on whether it forms a cycle (or)
not. The length of the spanning tree can be calculated as 16.
Let us assume the data structure EDGE to maintain the list of edges, their weights and
whether a particular edge is included in the tree or not.
The structure of EDGE is,

The first two fields are to represent <Vi, Vj> an edge.


The field WEIGHT is the weight of an edge <Vi, Vj> and
SELECT is a Boolean field which stores TRUE (or) FALSE, to indicate if this edge is included
(or) not in the minimum spanning tree.
Algorithm:
Steps:
1. K=1
2. For i = 1 to N-1 do
3. For j = i+1 to N do
4. If Gptr[i][j] > 0 then
5. X[K]→Vi = i
6. X[K]→Vj = j
7. X[K]→WEIGHT = Gptr[i][j]
8. X[K]→SELECT = FALSE
9. K = K+1
10. End if
11. End for
12. End for
13. ne = K
14. If (ne < K-1) then
15. Print “No spanning tree possible in the edge”
16. Exit
17. End if
18. X.SortEdges( )
19. For i = 1 to N do
20. For j = 1 to N do
21. TREE[i][j] = 0
22. End for
23. End for
24. K = 1, l = 1
25. While (K < N) do
26. temp = TREE
27. i = X[l]→Vi, j = X[l]→Vj
28. temp[i][j] = l, temp[j][i] = l
29. Warshall(temp)
30. flag = FALSE
31. For p = 1 to N do
32. If (temp[p][p] = 1) then
33. flag = TRUE
34. Break
35. End if
36. End for
37. If (not flag) then
38. TREE[i][j] = 1, TREE[j][i] = 1
39. K = K+1
40. X[l]→SELECT = TRUE
41. End if
42. l = l + 1
43. End while
44. Return (TREE)
45. Stop
Prim’s Algorithm:
This approach does not require listing of all the edges in the increasing order of weights (or)
checking each step to find whether a newly selected edge forms a circuit (or) not.
According to prim‟s algorithm, a minimum spanning tree grows in successive stages. At any
stage in the algorithm, we can see that we have a set of vertices that have already been included in the
tree, the rest of vertices have not.
The prim‟s algorithm then finds a new vertex to add it to the tree by choosing the edge <Vi,
Vj>, the smallest among all edges, where Vi is in the tree and Vj is yet to be included in the tree.
The prim‟s algorithm can easily be implemented using the adjacency matrix representation of
graph.
Let us consider an N x N adjacency matrix for a given undirected weighted graph and
vertices are labeled V1, V2,….VN. Start from V1 and get its nearest which has the smallest entry in the
row of V1, let it be Vi.
Now consider V1 and Vi as one subgraph. Next obtain the closest neighbor to this subgraph,
that is a vertex other than V1 & Vi that has smallest entry among all entries in the rows of V1 & Vi, let it
be Vj.
The tree now grows with vertices V1, Vi & Vj as one subgraph. Continue this process, until all
N vertices have been connected by N-1 edges.
Consider an undirected weighted graph and its weighted adjacency matrix.

Here we start with V1 and pick the smallest entry in row 1 which is 2 in column 4,
thus V4 is the nearest neighbor to V1.
The closest neighbor of subgraph V1 and V4 is V2, which can be seen by examining all the
entries in rows 1 and 4.
The four remaining edges selected following the above procedure are <V 2, V5>, <V4, V6>, <V6,
V3> and <V6, V7>. The total length of the tree can be calculated as 16.
Let us consider an array SELECTED[1…N] of Boolean will be used in the algorithm and
SELECTED[i] = TRUE, that ith vertex is included in tree.
The output of the minimum spanning tree will be stored in an adjacency matrix of order N x
N.
In the adjacency matrix of the given graph, we assume the (i, j) entry as infinity if there is no
edge between the vertices i and j.
Algorithm:
Steps:
1. For i = 1 to N do
2. SELECTED[i] = FALSE
3. End for
4. For i = 1 to N do
5. For j = 1 to N do
6. TREE[i][j] = 0
7. End for
8. End for
9. SELECTED[1] = TRUE, ne = 1
10. While (ne < N) do
11. min = ∞
12. For i = 1 to N do
13. If (SELECTED[i] = TRUE) then
14. For j = 1 to N do
15. If (SELECTED[j] = FALSE) then
16. If (min > Gptr[i][j]) then
17. min = Gptr[i][j]
18. x = i, y = j
19. End if
20. End if
21. End for
22. End if
23. End for
24. TREE[x][y] = 1
25. SELECTED[y] = TRUE
26. ne = ne + 1
27. End while
28. Return (TREE)
29. Stop
QUESTIONS
CHAPTER – 7
1) What are the basic terminologies of tree? Explain.
2) Explain in detail about the representation of binary tree.
3) Explain the operations on binary tree in detail.
4) What is tree traversal? What are the types? Write the algorithm for each traversal and Explain
with example.
5) Explain in detail about expression tree.
6) Write a detailed note binary search tree.
7) Explain in detail about heap tree.
8) Describe about red black tree.
CHAPTER – 8
1) What are the graph terminologies? Explain in detail.
2) How to represent a graph? Explain.
3) Describe the operations on graph.
4) Describe in detail about graph traversal.
5) What are the applications of graph? Explain.
6) Describe shortest path problem with algorithm and an example.
7) Explain Dijkstra‟s algorithm with example.
8) Explain in detail about topological sorting with an example.
9) Explain in detail about minimum spanning tree with an example.

You might also like