1. Kruskal's algorithm to find minimum cost spanning tree uses greedy approach. This algorithm treats
the graph as a forest and every node as an individual tree. A tree connects to another only if it has least
cost among all available options and does not violate MST properties.
Remove all loops and parallel edges from the given graph.
In case of parallel edges, keep the one which has least cost associated and remove all others.
Step 2 - Arrange all edges in their increasing order of weight
Next step is to create a set of edges & weight and arrange them in ascending order of weightage (cost).
Now we start adding edges to graph beginning from the one which has least weight. At all time, we shall
keep checking that the spanning properties are remain intact. In case, by adding one edge, the spanning
tree property does not hold then we shall consider not to include the edge in graph.
The least cost is 2 and edges involved are B,D and D,T so we add them. Adding them does not violate
spanning tree properties so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. So we add them −
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph −
...and we ignore it. And in the process we shall ignore/avoid all edges which create circuit.
We observe that edges with cost 5 and 6 also create circuits and we ignore them and move on.
Now we are left with only one node to be added. Between two least cost edges available 7, 8 we shall
add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we have minimum cost spanning
tree.
2. Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses greedy approach.
Prim's algorithm shares similarity with shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on
adding new nodes to the spanning tree from the given graph.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shall use the same
example −
In this case, we choose S node as root node of Prim's spanning tree. This node is arbitrarily chosen so
any node can be root node. One may wonder why can any video be a root node, so the answer is, in
spanning tree all the nodes of a graph are included and because it is connected then there must be at
least one edge which will join it to the rest of the tree.
Step 3 - Check outgoing edges and select the one with less cost
After choosing root node S, we see that S,A and S,C are two edges with weight 7 and 8 respectively. And
we choose S,A edge as it is lesser than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We select the
one which has the lowest cost and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat is as a node and will check all the edges
again and will choose only the least cost edge one. Here in this case C-3-D is the new edge which is less
than other edges' cost 8, 6, 4 etc.
After adding node D to the spanning tree, we now have two edges going out of it have same cost, i.e.
D-2-T and D-2-B. So we can add either one. But the next step will again yield the edge 2 as the least cost.
So here we are showing spanning tree with both edges included.
We may find that the output spanning tree of the same graph using two different algorithm is same.
Red - Black Tree is another varient of Binary Search Tree in which every node is colored either RED or
BLACK.
In a Red Black Tree the color of a node is decided based on the Red Black Tree properties. Every Red
Black Tree has the following properties.
Property #3: The children of Red colored node must colored BLACK. (There should not be two
consecutive RED nodes).
Property #4: In all the paths of the tree there must be same number of BLACK colored nodes.
Property #5: Every new node must inserted with RED color.
Property #6: Every leaf (e.i. NULL node) must colored BLACK.
Example
THe following is a Red Black Tree which created by inserting numbers from 1 to 9.
Note: Every Red Black Tree is a binary search tree but all the Binary Search Trees need not to be Red
Black trees.
In a Red Black Tree, every new node must be inserted with color RED. The insertion operation in Red
Black Tree is similar to insertion operation in Binary Search Tree. But it is inserted with a color property.
After every insertion operation, we need to check all the properties of Red Black Tree. If all the
properties are satisfied then we go to next operation otherwise we need to perform following operation
to make it Red Black Tree.
1. Recolor
The insertion operation in Red Black tree is performed using following steps...
Step 2: If tree is Empty then insert the newNode as Root node with color Black and exit from the
operation.
step 3: If tree is not Empty then insert the newNode as a leaf node with Red color.
Step 4: If the parent of newNode is Black then exit from the operation.
Step 5: If the parent of newNode is Red then check the color of parent node's sibling of newNode.
Step 6: If it is Black or NULL node then make a suitable Rotation and Recolor it.
Step 7: If it is Red colored node then perform Recolor and Recheck it. Repeat the same until tree
becomes Red Black Tree.
Example
Splay Tree
Splay tree is another varient of binary search tree. In a splay tree, the recently accessed element is
placed at the root of the tree.
Splay Tree is a self - adjusted Binary Search Tree in which every operation on an element rearrange the
tree so that the element is placed at the root position of the tree.
In a splay tree, every operation is performed at root of the tree. All the operations on a splay tree are
involved with a common operation called "Splaying".
Splaying an element is the process of bringing it to the root position by performing suitable rotation
operations.
In a splay tree, to splay any element we use the following rotation operations...
1. Zig Rotation
2. Zag Rotation
Zig Rotation
The Zig Rotation in a splay tree is similar to the single right rotation in AVL Tree rotations. In zig rotation
every node moves one position to thr righ from its current position. Consider the following example...
Zag Rotation
The Zag Rotation in a splay tree is similar to the single left rotation in AVL Tree rotations. In zag rotation
every node moves one position to the left from its current position. Consider the following example...
Zig-Zig Rotation
The Zig-Zig Rotation in a splay tree is a double zig rotation. In zig-zig rotation every node moves two
position to the right from its current position. Consider the following example...
Zag-Zag Rotation
The Zag-Zag Rotation in a splay tree is a double zag rotation. In zag-zag rotation every node moves two
position to the left from its current position. Consider the following example...
Zig-Zag Rotation
The Zig-Zag Rotation in a splay tree is a sequence of zig rotation followed by zag rotation. In zig-zag
rotation every node moves one position to the right followed by one position to the left from its current
position.
Zag-Zig Rotation
The Zag-Zig Rotation in a splay tree is a sequence of zag rotation followed by zig rotation. In zag-zig
rotation every node moves one position to the left followed by one position to the right from its current
position.