0% found this document useful (0 votes)
29 views3 pages

Unit 3 Kruskal Algorithm

Uploaded by

yaxekav386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Unit 3 Kruskal Algorithm

Uploaded by

yaxekav386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UNIT 3 Kruskal Algorithm

Q.Write an algorithm for Breadth First Search Step 1: Create a forest F in such a way that every
traversal of a graph. vertex of the graph is a separate tree. Step 2: Create
Ans. In BFS we start from some vertex and find all the a set E that contains all the edges of the graph. Step
adjacent vertices of it. This process will be repeated 3: Repeat Steps 4 and 5 while E is NOT EMPTY and
for all the vertices so that the vertices lying on same F is not spanning Step 4: Remove an edge from E
breadth get printed. For avoiding repetition of vertices, with minimum weight Step 5: IF the edge obtained in
we maintain array of visited nodes.A queue data Step 4 connects two different trees, then add it to the
structure is used to store adjacent vertices. forest F (for combining two trees into one tree).
Algorithm:1. Create a graph. Depending on the type ELSE Discard the edge Step 6: END
of graph Le. directed or undirected set the value of ___________________________________________
the flag as either 0 or 1 respectively.2. Read the UNIT 4
vertex from which you want to traverse the graph, say OBST Optimal Binary Search Tree OBST 1)An OBST
Vi 3. Initialize the visited array to 1 at the index of Vi 4. is a binary search tree for which nodes are arranged
Insert the visited vertex V; in the queue.5. Visit the on levels such cost is minimum 2)Let {a1,a2,a3,.....an}
vertex which is at the front of the queue. Delete it from be set of identifiers such that a1<a2<a3 3)Let p(i) be
the queue and place its adjacent nodes in the probability of successful search & q(i) be Probability
queue.6. Repeat step 5, till the queue is not of unsuccessful search 4)Σp(i) + Σq(i) gives us the
empty.7.stop tree with minimum Cost
Time complexity = O(V), space complexity = O(h) Advantages of OBST:1) The cost of searching a
___________________________________________ node from OBST is reduced.2) The optimal binary
Q.Write an algorithm for Depth First Search traversal search is not modified once it is constructed. 3) The
of a graph. input sequence is already known for building the
Ans: search tree and hence this data structure is suitable
In DFS, we start from one vextex and traverse the for offline algorithms.4) The most frequently accessed
path as deeply as we can go. When there is no rextex node is nearer to root node. Hence most frequently
further, we toerse back and search. for unvisited accessed nodes can be accessed efficiently.
vertex. Algorithm 1. Define stack with size as total ___________________________________________
number of vertices. 2. Select any vertex as a starting AVL Tree : AVL tree is a self-balancing Binary Search
point for traversal. Visit it, print it and push it onto the Tree (BST) where the difference between heights of
stack.3. Visit any one of the adjacent node which is on left and right subtrees cannot be more than one for all
the top of the stack and that adjacent node must be nodes. Insertion in AVL Tree:To make sure that the
unvisited, print it and push it onto the stack, marks it given tree remains AVL after every insertion, we must
as visited.4. Repeat step 3, until there is no new augment the standard BST insert operation to perform
vertex to be visited for the vertex which is on the top some re-balancing. Following are two basic
of the stack.5. When there is no new vertex to be operations that can be performed to balance a BST
visited, backtrack and pop one vertex from the without violating the BST property (keys(left) <
stack.6. Repeat step 3,4,5 until stack becomes empty. key(root) < keys(right)). Left Rotation & Right
___________________________________________ Rotation
Topological Ordering Steps to be followed in this Algorithm to Insert node in AVL tree- 1)Insert new
algorithm. 1) From given graph, find vertex with no node as new leaf just as ordinary binary search tree
Incoming edges Delete it along with all the outgoing 2) Now, trace the path from insertion point For each
edges 2) Note the vertices that are deleted 3) All nade 'n' encountered check if heights of left (n) and
these vertices given topologically sorted list. right (n) differ by almost 11)If yes,move towards
___________________________________________ parent (n).2) If no restructure by doing rotation
Prims Algorithm Algorithm to Delete node in AVL tree -1) Search the
Step 1: Select a starting vertex Step 2: Repeat Steps node which is to be deleted2) a)If node to be deleted
3 and 4 until there are fringe vertices Step 3: Select is leaf node, Simply make it NULL to remove b)If not,
an edge 'e' connecting the tree vertex and fringe then that node must be swapped with its inorder
vertex that has minimum weightStep 4: Add the Successor and then we can remove it 3) After that,
selected edge and the vertex to the minimum traverse back up path towards root, checking balance
spanning tree T [END OF LOOP] Step 5: EXIT factor of every node.If unbalanced, then balance it
___________________________________________ using rotation
_________________________________________
___________________________________________ children.3)The root nodes must have at least 2
_________________________________________ nodes.4)All leaf nodes must be at the same level.It is
AA Tree :AA trees use the concept of levels to aid in not necessary that, all the nodes contain the same
balancing binary trees. The level of node (instead of number of children but, each node must have m/2
colour) is used for balancing information. A link where number of nodes.
child and parent’s levels are same, is called a Insertion in B Tree:Insertions are done at the leaf
horizontal link, and is analogous to a red link in the node level. The following algorithm needs to be
red-black tree.The level of red nodes are same as the followed in order to insert an item into B
level of their parent nodes and the links are called Tree.1)Traverse the B Tree in order to find the
horizontal links.The level of black nodes are one less
appropriate leaf node at which the node can be
than the level of their parent node.Additional storage
inserted.2)If the leaf node contain less than m-1 keys
requirement with every node is O(Log n) in red black
then insert the element in the increasing order.3)Else,
trees instead of O(1) (only color in Red Black Trees),
if the leaf node contains m-1 keys, then follow the
but AA trees simplify restructuring by removing many
following steps.a)Insert the new element in the
cases .An AA tree follows same rule as red-black
increasing order of elements.b)Split the node into the
trees with the addition of single new rule that red
nodes cannot be present as left child.1)Every node two nodes at the median.c)Push the median element
can be either red (linked horizontally) or black.2)There upto its parent node.d)If the parent node also contain
are no two adjacent red nodes (or horizontal m-1 number of keys, then split it too by following the
links).3)Every path from root to a NULL node has same steps.
same number of black nodes (on black links).4)Left Deletion in B Tree: Deletion is also performed at the
link cannot NOT be red (horizontal). leaf nodes. The node which is to be deleted can either
_________________________________________ be a leaf node or an internal node. Following
K-Dimensional tree: A K-D Tree(also called as algorithm needs to be followed in order to delete a
K-Dimensional Tree) is a binary search tree where node from a B tree.1)Locate the leaf node2)If there
data in each node is a K-Dimensional point in space. are more than m/2 keys in the leaf node then delete
In short, it is a space partitioning(details below) data the desired key from the node.3)If the leaf node
structure for organizing points in a K-Dimensional doesn't contain m/2 keys then complete the keys by
space. A non-leaf node in K-D tree divides the space taking the element from eight or left sibling.a)If the
into two parts, called as half-spaces. Points to the left left sibling contains more than m/2 elements then
of this space are represented by the left subtree of push its largest element up to its parent and move the
that node and points to the right of the space are intervening element down to the node where the key
represented by the right subtree. We will soon be is deleted.b)If the right sibling contains more than
explaining the concept on how the space is divided
m/2 elements then push its smallest element up to
and tree is formed. For the sake of simplicity, let us
the parent and move intervening element down to the
understand a 2-D Tree with an example. The root
node where the key is deleted.4)If neither of the
would have an x-aligned plane, the root’s children
sibling contain more than m/2 elements then create a
would both have y-aligned planes, the root’s
new leaf node by joining two leaf nodes and the
grandchildren would all have x-aligned planes, and
the root’s great-grandchildren would all have y-aligned intervening element of the parent node.5)If parent is
planes and so on. left with less than m/2 nodes then, apply the above
___________________________________________ process on the parent too.If the the node which is to
UNIT 5 be deleted is an internal node, then replace the node
B Tree : B Tree is a specialized m-way tree that can with its in-order successor or predecessor. Since, the
be widely used for disk access. A B-Tree of order m successor or predecessor will always be on the leaf
can have at most m-1 keys and m children. One of the node hence, the process will be similar as the node is
main reason of using B tree is its capability to store a being deleted from the leaf node.
large number of keys in a single node and large key ___________________________________________
values by keeping the height of the tree relatively B+ Tree: B+ Tree is an extension of B Tree which
small.A B tree of order m contains all the properties of allows efficient insertion, deletion and search
an M way tree. In addition, it contains the following operations.In B Tree, Keys and records both can be
properties.1)Every node in a B-Tree contains at most stored in the internal as well as leaf nodes. Whereas,
m children.2)Every node in a B-Tree except the root in B+ tree, records (data) can only be stored on the
node and the leaf node contain at least m/2 leaf nodes while internal nodes can only store the key
values.The leaf nodes of a B+ tree are linked together record from the master file. Advantages 1. Desired
in the form of a singly linked lists to make the search record can be accessed efficiently by using an index
queries more efficient.B+ Tree are used to store the which is maintained in a separate file. 2. Variable
large amount of data which can not be stored in the length records can also be handled using index
main memory. Due to the fact that size of main sequential files. Disadvantages 1. At least two files
memory is always limited, the internal nodes (keys to need to be maintained : One master file and another
access records) of the B+ tree are stored in the main index file. Hence extra amount of memory is required
memory whereas leaf nodes are stored in the in order to maintain the index file. 2. While performing
secondary memory.The internal nodes of B+ tree are insertion and deletion index manipulation is required.
often called index nodes. A B+ tree of order 3 is ___________________________________________
shown in the following figure. Sequential Organization : •This type of file
Insertion in B+ Tree: Step 1: Insert the new node as organization makes use of ISMA scheme. •The ISMA
a leaf nodeStep 2: If the leaf doesn't have required scheme ie. Index Sequential Memory Access scheme
space, split the node and copy the middle node to the is a kind of file organization in which record can be
next index node.Step 3: If the index node doesn't have arranged using cylinder and surfaces. Ex. Consider
required space, split the node and copy the middle the arrangement of students’ record in which
element to the next index page. cylinders and surfaces are used. FIG Now if we want
Deletion in B+ Tree:Step 1: Delete the key and data to find record of Roll_number 40 then we can search it
from the leaves.Step 2: if the leaf node contains less based on the cylinder. That is we will first search for
than minimum number of elements, merge down the the cylinder number 2, then we will make the search
node with its sibling and delete the key in between based on the surface. When we reach at the surface 2
them.Step 3: if the index node contains less than of cylinder 2 then the desired record can be obtained
minimum number of elements, merge the node with within the track. Advantages of sequential
the sibling and move down the key in between them. organization 1. The sequential file organization is
___________________________________________ simple to implement. 2. It is suitable for the small
Unit 6 database applications. Disadvantages of sequential
External Sort:- organization 1. If database is very huge then it turns
• In external sorting, data stored in memory out to be a non efficient technique for retrieval of
past-by-part loaded into main memory, sorting can be record. 2. If the number of tracks are more on the
done over there.• Then this sorted data is stored in surfaces then multiple disk access may be required to
intermediate files.•Finally, these intermediate files are obtain the desired record. Basic operations that can
merged repeatedly to get sorted data•Eg:-Considex be performed on sequential file organization are 1.
10000 records to be sorted & main memory has Create 2. Display 3. Delete 4. Update 5. Append 6.
capacity to store 500. records in blocks, with Size of Search
100 records i.e 5 blocks•Sorted 5 blocks are stored in ___________________________________________
an intermediate file and process repeated 20 times to _________________________________________
get all records Sorted. file Handling in C++ :C++ provides following classes
___________________________________________ to perform input and output of characters to and from
_________________________________________ the files. 1)ofstream This stream class is used to
Index Sequential File Organization :1)The main write on files. 2)ifstream This stream class is used to
drawback of sequential file is that searching operation read from files. 3)fstream This stream class is used
is not efficient. Because in sequential organization for both read and write from/to files. 1)To create an
primary key of every record is compared with the input stream, declare an object of type ifstream. 2. To
searching key. To optimize this operation concept of create an output stream, declare an object of type
index sequential file is introduced.2)In index ofstream. 3. To create an input/output stream, declare
sequential file organization, a separate file for storing an object of type fstream.
indexes of every record is maintained along with the ___________________________________________
master file.3)The index sequential organization, _________________________________________
accelerates the retrieval of any desired record. In this
case, we need not have to scan the entire memory
block of the record. Instead of that using primary key
(such as EMP_ID) and position we can access the

You might also like