You are on page 1of 30

Regulation : 2022 BCS304- Data Structures and Applications

Module-4: Trees and Graphs


Binary Search Trees
ADT Dictionary
Objects: a collection of n > 0 pairs, each pair has a key and an associated item
functions:for all d ∈ Dictionary, item ∈ Item, k ∈ Key, n ∈ integer

DictionaryCreate(max_size) create an empty Dictionary


::=
Boolean IsEmpty(d, n) ::= if (n > 0) return TRUE else return
FALSE
Element Search(d, k) ::= return item with key k, return
NULL if no such element.
Element Delete(d, k) ::= delete and return item (if any) with key k;
void Insert(d,item,k) ::= insert item with key k into d.

Definition Binary search tree


A binary search tree is a binary tree. It may be empty. If it is not empty then it satisfies the following
properties:
1) Each node has exactly one key and the keys in the tree are distinct.
2) The keys (if any) in the left subtree are smaller than the key in the root.

3) The keys (if any) in the right subtree are larger than the key in the root.
4) The left and right subtrees are also binary search trees.
5) The root has a key.
Example:

Binary search Tree


Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

Searching a Binary Search Tree


To search for a node whose key is k. We begin at the root of the binary search tree.
• If the root is NULL, the search tree contains no nodes and the search is unsuccessful.
• we compare k with the key in root. If k equals the root's key, then the search terminates
successfully.
• If k is less than root's key, then, we search the left subtree of the root.
• If k is larger than root's key value, we search the right subtree of the root.

Structure of the node can be defined as follows


struct node
{
Struct node *lchild;
struct
{
int item; /* Itype represents the data type of the element*/ int key;
}data;
Struct node *rchild;
};
Typedef struct node TreeNode;

Recursive search of a binary search tree: Return a pointer to the element whose key is k, if there
is no such element, return NULL. We assume that the data field of the element is of type elemenet
and it has two components key and item.
rootnode * search(rootnode * root, int k)
{
if
(root==NULL)
return NULL;
if (k ==root→data.key)
return (tree);
if (k <root→data.key)
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

return search(root→leftChild, k);


return search(root→rightChild, k); }

Iterative search of a Binary Search tree

Treenode* iterSearch(TreeNode * tree, int k)


{
while (root!=null)
{
if (k == root→data.key)

return(root);
if (k <root→data.key)
root = root→leftChild;
else
root = root→rightChild;

}
return NULL;
}

Inserting in to a Binary Search Tree: Binary search tree has distinct values, first we search the tree for
the key and if the search is unsuccessful the key is inserted at the point the search terminated

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Example : consider the tree given below

BST Tree Insert 80 Insert 35

Inserting a dictionary pair into a binary search tree


If k is in the tree pointed at by node do nothing. Otherwise add a new node with data = (k, item)
TreeNode* insert(TreeNode *root, int k, int Item)
{
TreeNode * ptr, *lastnode;
lastnode=Modifiedsearch(root,k);
Ptr=(TreeNode*)malloc(sizeof(TreeNode));

ptr→data.key = k;
ptr→data.item = Item;
ptr→leftChild = ptr→rightChild = NULL;

if (root==NULL)
{
root=ptr;
return(root);
}

if(lastnode!=NULL)
{
if (k < lastnode→data.key)
lastnode→leftChild = ptr;
else
lastnode→rightChild =

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

ptr;return (root);
}
}

If the element is present or if the tree is empty the function Modifiedsearch returns NULL. If the
element is not present it return a pointer to the last node searched.

Modifiedsearch(Treenode *root,int k)
{
TreeNode *temp,*prev;
temp==node;
prev=NULL;
If(temp==NULL)
return(NULL);
while(temp!=NULL)
{ if(temp->data.key==k)
{ printf(“element already found”);
return(NULL);
}
if(key<temp->data.key)
{
prev=temp;
temp=temp->lchild;
} else
{
Prev=temp;
Temp=temp->rchild;
}
} retrun(prev);

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Deletion from a binary search tree:


Suppose T is a binary search tree. The function to deletean item from tree T first search the tree to find
the location of the node with the item and the location of the parent of N and the deletion of the node N
depends on three cases:

Case 1: N has no children. Then N is deleted from T by replacing the location of the node N in the
parent(N) by the NULL pointer

Example: Deleting Node 66 with NO children

Deleting node

Case 2: If N has exactly one child. Then N is deleted from T by replacing the location of N in Parent
(N) by the location of the only child of N.
Example: Deleting Node 75 with exactly one children

Case 3: N has Two children. Let S(N) denote the inorder successor of N(S(N) does not have a left
child).Then N is deleted from T by first deleting S(N) from T (by using case1 or cae 2) and then
replacing node N in T by the node S(N).

Example: Deleting Node 25 with two children

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Now replace the node 25 with its inorder successor 33

Recursive function to delete a node in a BST


TreeNode *delete_element(TreeNode *node, int key)
{
TreeNode * temp;
if (node ==
NULL) return
node;
if(key<node->data.key)
node->lchild=delete_element(node->lchild,
key);else if (key > node->data.key)
node->rchild = delete_element(node->rchild,
key);else
{

// node with only one


child if(node->lchild ==
NULL)
{
temp = node->rchild;
free(node);returntemp;
}
else if (node->rchild == NULL)

{
temp = node-
>lchild; free(node);
return temp;
}
// node with two
childrenelse
{
temp = node->rchild;
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

while(temp-
>lchild!=NULL)
//Get the inorder successor

temp=temp->lchild;
node->data.item = temp-
>data.item;node-
>data.key=temp->data.key;
node->rlink = delete_element(node->rchild, temp->data.key); return node;
}
}
}

Selection Trees/Tournament Trees


The Tournament tree is a complete binary tree with n external nodes and n – 1 internal nodes. The external
nodes represent the players, and the internal nodes are representing the winner of the match between the
two players. This tree is also known as Selection tree.

There are some properties of Tournament trees. These are like below −

 This tree is rooted. So the link in the tree and directed path from parent to children, and there
is a unique element with no parents
 The parent value is less or equal to that node to general any comparison operators, can be used as
long as the relative value of the parent and children are invariant throughout the tree
 Trees with a number of nodes not a power of 2, contain holes. Holes can be present at any place
in the tree.
 This tree is a proper generalization of binary heaps
 The root will represent overall winner of the tournament.

There are two types of Tournament Trees −

 Winner Tree
 Looser Tree

Winner Tree

Winner tree is a complete binary tree, in which each node is representing the smaller or greater of its two
children, is called winner tree. The root is holding the smallest or greatest node of the tree. The winner of
the tournament tree is the smallest or greatest n key in all the sequences. It is easy to see that winner tree
can be formed in O(log n) time.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

If smallest node is selected between 2 nodes as winner then it is min winner

tree If greatest node is selected between 2 nodes as winner then it is max

winner treeExample − Suppose there are some keys, 3, 5, 6, 7, 20, 8, 2, 9

Looser Tree

Looser Trees are complete binary tree for n players where n external nodes and n – 1 internal nodes are
present. The looser of the match is stored in the internal nodes. But in this overall winner is stored at tree[0].
The looser is an alternative representation, that stores the looser of a match at the corresponding node. An
advantage of the looser is that, to restructure the tree after winner tree been output, it is sufficient to examine
node on the path from leaf to root rather than the sibling of the nodes on this path.

Same as min winner and max winner, min looser and max looser trees also available.

Example − To form a looser tree, we have to create winner tree at first.

Suppose there are some keys, 10, 2, 7, 6, 5, 9, 12, 1. So we will create minimum winner tree at first.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Now, we will store looser of the match in each internal node.

Forests

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

A forest is a disjoint set of trees. In other words, it's a collection of trees where there are no cycles present.
Each tree in a forest consists of nodes connected by edges, similar to a real-life tree structure with branches
and leaves.

Transforming Forest into Binary Tree


To Transform a forest into a single binary tree, we first obtain the binary tree representation of each of the
trees in the forest and then link theses binary trees together through the righchild field of the root nodes.

Forest

Binary Tree

Forest Traversals

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Forest preorder
1. If F is empty then return
2. Visit the root of the first tree of Forest
3. Traverse the subtrees of the first tree of forest in preorder
4. Traverse the remaining trees of Forest in preorder
Forest Inorder
1. If F is empty then return
2. Traverse the subtrees of the first tree of forest in Inorder
3. Visit the root of the first tree
4. Traverse the remaining trees of Forest in Inorder
Forest Postorder
1. If F is empty then return
2. Traverse the subtrees of the first tree of forest in postorder
3. Traverse the remaining trees of Forest in Postorder
4. Visit the root of the first tree

Representation of Disjoint Sets

The disjoint set data structure is also known as union-find data structure and merge-find set. It is a data
structure that contains a collection of disjoint or non-overlapping sets. The disjoint set means that when
the set is partitioned into the disjoint subsets. The various operations can be performed on the disjoint
subsets. In this case, we can add new sets, we can merge the sets, and we can also find the representative
member of a set. It also allows to find out whether the two elements are in the same set or not efficiently.

The disjoint set can be defined as the subsets where there is no common element between the two sets.
Let's understand the disjoint sets through an example.
s1 = {1, 2, 3, 4}
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

s2 = {5, 6, 7, 8}

We have two subsets named s1 and s2. The s1 subset contains the elements 1, 2, 3, 4, while s2 contains
the elements 5, 6, 7, 8. Since there is no common element between these two sets, we will not get anything
if we consider the intersection between these two sets. This is also known as a disjoint set where no elements
are common. Now the question arises how we can perform the operations on them. We can perform only
two operations, i.e., find and union.

In the case of find operation, we have to check that the element is present in which set. There are two sets
named s1 and s2 shown below:

Suppose we want to perform the union operation on these two sets. First, we have to check whether the
elements on which we are performing the union operation belong to different or same sets. If they belong
to the different sets, then we can perform the union operation; otherwise, not. For example, we want to
perform the union operation between 4 and 8. Since 4 and 8 belong to different sets, so we apply the union
operation. Once the union operation is performed, the edge will be added between the 4 and 8 shown as
below:

When the union operation is applied, the set would be represented as:

s1Us2 = {1, 2, 3, 4, 5, 6, 7, 8}

For example, if we have 10 elements numbered 0 through 9, we may partition them into three dis- joint sets,
S1 = {0, 6, 7, 8}, S2 {1, 4, 9}, and S3 {2, 3, 5).
The minimal operations that we wish to perform on these sets are:
(1) Disjoint set union. If S i and Sj are two disjoint sets, then their union Si U Sj = {all elements, x, such
that x is in Si or Sj}. Thus, S1 U S2 = {0, 6, 7, 8, 1, 4, 9}. Since we have assumed that all sets are disjoint,
following the union of Si and Sj we can assume that the sets Si and Sj no longer exist independently. That
is, we replace them by Si U Sj.
(2) Find(i). Find the set containing the element, i. For example, 3 is in set S3 and 8 is in set S1.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Two or more sets have no element in common are called disjoint sets. Disjoint set data structure is also

referred to as union find data structure because of its union and find operations.

Disjoint Sets:
Disjoint Sets is one of the most powerful and yet simple data structure. The idea of Disjoint Sets can be
perfectly applied in finding the minimal spanning tree.

The Disjoint Sets consist of two basic operations: finding the group (parent) of any element, unioning two or
more sets.

Considering above forests (a set of trees), each tree represents a set. We use an one-dimension array to store
the fathers of every node. We can initialize them by -1 or itself, meaning they belong to their own group at
the beginning. Otherwise, the value represents its father node.

Finding the root-parent of any group can be implemented by two styles: recursive or iterative. The recursive
method is short and straightforward.
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

1 def find(x):
2 global father
3 if x != father[x]:
4 father[x] = find(x) // compress the paths
5 return father[x]

Graphs

Definitions

Graph: A graph G consist of two sets V and E


1. V is a finite nonempty set of vertices and
2. E is a set of pairs of vertices these pairs are called edges
A graph can be represented as G = (V, E). V(G) will represent the set of vertices and E(G) will
represent the set of edges of the graph G

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Example:

V(G)= {v1, v2, v3, v4, v5, v6}


E(G) = {e1, e2, e3, e4, e5, e6} E(G) = {(v1, v2) (v2, v3) (v1, v3) (v3, v4), (v3, v5) (v5, v6)}.
There are six edges and six vertices in the graph

Undirected Graph: In an undirected graph the pair of vertices representing an edge is unordered.
Example:

V(G)={a,b,c,d}

E(G)={(a,b),(a,d),(b,d),(b,c)

thus the pairs (u,v) and (v,u) represent the same edge.

Directed Graph (digraph): In a directed graph each edge is represented by a directed pair (u,v), v is
the head and u is the tail of the edge. Therefore (v,u) and(u,v) represent two different edges.
Example:

V(G)={a,b,d}

E(G)={(a,d), (a,b), (d,b)}

Self Edges/Self Loops: Edges of the form(v,v) are called self edges or self loops . It is an edge
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

which starts and ends at the same vertex.


Example:

Mutigraph: A graph with multiple occurrences of the same edge is called a multigraph

Example:

Complete Graph: An undirected graph with n vertices and exactly n(n-1)/2 edges is said to be a
complete graph. In a graph all pairs of vertices are connected by an edge.
Example : A complete graph with n=3 vertices

Adjacent Vertex
If (u,v) is an edge in E(G), then we say that the vertices u and v are adjacent and the edge(u,v) is
incident on vertices u and v.

Path: A path from vertex u to v in graph g is a sequence of vertices u, i1,i2,…….ik, v such that
(u,i1),(i1,i2)………(ik,v) are edges in E(G). if G‘ is directed then the path consists of
<u,i1>,<i1,i2>………<ik,v> edges in E(G‘).

The length of the path is the number of edges in it.


Example:

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

(B,C),(C,D) is a path from B to D the length of the path is 2

A simple path is a path in which all the vertices are distinct.

Cycle: A cycle is a simple path in which all the vertices except the first and last vertices are distinct.
The first and the last vertices are same.
Example :
(B,C),(C,D)(D,E)(E,A)(A,B) is a cycle

Degree of a vertex : In a undirected graph degree of a vertex is the number of edges incident on a
vertex.

In a directed graph the in-degree if a vertex v is the number of edges for which v is the head i.e. the
number of edges that are coming into a vertex. The out degree is defined as the number of edges for
which v is the tail i.e. the number of edges that are going out of a vertex

Subgraph: A subgraph of G is a graph G‘ such that V(G‘) is a subset of V(G) and E(G‘) is a subset of
E(G)
Example :

Graph(G) Subgraph(G‘)

Connected Graph: An undirected graph G is said to be connected if for every pair of distinct vertices
u and v in V(G) there is a path from u to v in G.

Connected Component is a maximal connected subgraph.

Strongly connected graph : A directed graph G is said to be strongly connected if for every pair of
distinct vertices u an v in V(G), there is a directed path from u to v and from v to u.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Tree: A tree is a connected acyclic connected graph.

ADT Graph

Objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices.

Functions: for all graph Graph, v,v1,v2 vertices

Example: Graph create():=: return an empty graph


Graph InsertVertex(graph,v):= return a graph with v inserted. v has no incident edges
Graph InsertEdge(graph,v1,v2) := retrun a graph with a new edge between v1 and v2
Graph DeleteVertex(graph,v) := return a graph in which v and all edges incident to it is
removed

Graph DeleteEdge(graph,v1,v2):= retrun a graph in which the edge (v1,v2) is removed,


leave the incident nodes in the graph

Boolean IsEmpty:= If (graph == empty graph) retrun TRUE else Retrun


FALSE

List Adjacent(graph,v) := retrun a list of all vertices that are adjacent to v

Graph Representation

The three most commonly used representations are

• Adjacency Matrix
• Adjacency List
• Adjacency Multilist

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Adjacency Matrix: Let G=(V,E) be a graph with n vertices, n>=1. The adjacency matrix of G is a
two dimensional n*n array for example a, with the property that a[i][j]=1 if there exist ane edge (i,j)
(for a directed graph edge <i,j> is in E(G).a[i][j]=0 if no such edge in G.
Example: Graph G1

0
Adjacency Matrix

0 1 2 3
0 0 1 1 1
1 2
1 1 0 1 1
2 1 1 0 1
3 1 1 1 0

• The space requirement to store an adjacency matrix is n2 bits.


• The adjacency matrix for a undirected graph is symmetric .About half the space can be saved
in an undirected graph by storing only the upper or lower triangle of the matrix.
• For an undirected graph the degree of any vertex i is its row sum. For a directed graph the row
sum is the out-degree and the column sum is the in-degree.

Adjacency list: In adjacency matrix the n rows of the adjacency matrix are represented as n chains.
There is one chain for each vertex in G. The nodes in chain i represent the vertices that are adjacent
from vertex i. The data field of a chain node stores the index of an adjacent vertex.

Example: The adjacency list of graph G1 is shown below

Example AdjLists: data link

• For an undirected graph with n vertices and e edges. The linked adjacency lists representation
requires an array of size n and 2e chain nodes.
• The degree of any vertex in an undirected graph may be determined by counting the number
of nodes in the adjacency list. For a digraph the number of list nodes is only e.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Adjacency Multi lists: For each edge there will be exactly one node, but this node will be in two
list(i.e., the adjacency list for each of the two nodes to which it is incident). A new field is necessary
to determine if the edge is determined and mark it as examined.

The new node structure is

m Vertex1 Vertex2 Link1 Link2

Where m is a Boolean mark field that may be used to indicate whether or not the edge has been
examined.
Example: The adjacency multilist for graph G1 is shown below

weights may represent the distance from one vertex to another or the cost for going from one vertex
to an adjacent vertex. The adjacency matrix and list maintains the weight information also. A graph
with weighted edges are also called network.
Example:

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Elementary Graph Operations

Given an undirected graph G=(V,E) and a vertex v in V(G) ,there are two ways to find all the vertices
that are reachable from v or are connected to v .

• Depth First Search and


• Breadth First Search

Depth First Search

1. Visit the starting vertex v. (visiting consist of printing node‘s vertex)


2. Select an unvisited vertex w from v‘s adjacency and carry a depth first search on w.
3. A stack is maintained to preserve the current position in v‘s adjacency list.
4. When we reach a vertex u that has no unvisited vertices on adjacency list, remove a vertex
from the stack and continue processing its adjacency list. Previously visited vertices are
discarded and unvisited vertices are placed on stack

5. The search terminates when the stack is empty.

A recursive implementation of depth first search is shown below.

A global array visited is maintained , it is initialized to false, when we visit a vertex i we change the
visited[i] to true.

Global Declaraions

# define FALSE 0void dfs(int v)


{
int i;
visited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("%d ", i);
dfs(i);
}

}
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

Example: For the graph given below if the search is initiated from vertex 0 then the vertices are
visited in the order vertex 3, 1, 2

1 2

[0] 1 2 0
3
[1] 0 3 0
2
[2] 0 3 0
1
[3] 1 2 0
0

Breadth first Search

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

1. Search starts at vertex v marks it as visited.


2. It then visits each of the vertices on v‘s adjacency list.
3. As we visit each vertex it is placed on a queue.
4. When all the vertices in the adjacency list is visited we remove a vertex from the queue and
proceed by examining each of the vertices in its adjacency list.
5. Visited vertices are ignored and unvisited vertices are placed on the queue
6. The search terminates when the queue is empty.

The queue definition and the function prototypes

struct node
{ int vertex;
struct node * link;
};
typedef struct node queue;
queue * front,*rear;:
int visied[max_vertics];
void addq(int);
int delete();

void bfs(int v)

int i, cur;

visited[v] = 1;

q[++rear] = v;

while(front!=rear)

cur = q[++front];

for(i=0;i<n;i++)

if((a[cur][i]==1)&&(visited[i]==0))

q[++rear] = i;

visited[i] = 1;

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

printf("%d ", i);

Connected components
In an undirected graph refers to a group of vertices that are connected to each other through edges, but
not connected to other vertices outside the group.
For example in the graph shown below, {0, 1, 2} form a connected component and {3, 4} form another
connected component.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Characteristics of Connected Component:

A connected component is a set of vertices in a graph that are connected to each other.
A graph can have multiple connected components.
Inside a component, each vertex is reachable from every other vertex in that component.

How to identify Connected Component:

There are several algorithms to identify Connected Components in a graph. The most popular ones are:
Depth-First Search (DFS)
Breadth-First Search (BFS)
Union-Find Algorithm (also known as Disjoint Set Union)

Applications of Connected Component:

Graph Theory: It is used to find subgraphs or clusters of nodes that are connected to each
other.
Computer Networks: It is used to discover clusters of nodes or devices that are linked and
have similar qualities, such as bandwidth.
Image Processing: Connected components also have usage in image processing.

void connected(void)
{
int i;
for (i=0;i<n;i++)
if(!visited[i])
{
dfs(i);
printf(“\n”);
}
}

Spanning Trees:

A spanning tree is a subset of Graph G, such that all the vertices are connected using minimum possible
number of edges. Hence, a spanning tree does not have cycles and a graph may have more than one
spanning tree.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Properties of a Spanning Tree:


A Spanning tree does not exist for a disconnected graph.
For a connected graph having N vertices then the number of edges in the spanning tree for
that graph will be N-1.
A Spanning tree does not have any cycle.
We can construct a spanning tree for a complete graph by removing E-N+1 edges,
where E is the number of Edges and N is the number of vertices.
Cayley’s Formula: It states that the number of spanning trees in a complete graph with N
vertices is NN-2
 For example: N=4, then maximum number of spanning tree possible =44-2= 16
(shown in the above image).

A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among
all the possible spanning trees.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Regulation : 2022 BCS304- Data Structures and Applications

Biconnected Graph is and how to check if a given graph is Biconnected or not.

A graph is said to be Biconnected if:

1. It is connected, i.e. it is possible to reach every vertex from every other vertex, by a simple path.
2. Even after removing any vertex the graph remains connected.

For example, consider the graph in the following figure

The given graph is clearly connected. Now try removing the vertices one by one and observe. Removing
any of the vertices does not increase the number of connected components. So the given graph is
Biconnected.
Now consider the following graph which is a slight modification in the previous graph.
Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal
Regulation : 2022 BCS304- Data Structures and Applications

In the above graph if the vertex 2 is removed, then here's how it will look:

Clearly the number of connected components have increased. Similarly, if vertex 3 is removed there will
be no path left to reach vertex 0 from any of the vertices 1, 2, 4 or 5. And same goes for vertex 4 and 1.
Removing vertex 4 will disconnect 1 from all other vertices 0, 2, 3 and 4. So the graph is not Biconnected.

Prepared by: Sowmya H D/Asst Prof, CSE Dept SSCE, Anekal


Data Structures and Applications [BCS304]

You might also like