Data Structures And Algorithm(sem-II)-1
Data Structures And Algorithm(sem-II)-1
Algorithm
Sem-II
❖ TREE (non-linear Data Structure)
A tree is a finite set of nodes with one specially designated node called as root and
the remaining nodes are partition into sets called as subtrees of the root.
to that node.
hierarchy (root=level 0)
1.2) Strictly Binary Tree – Where all non-leaf nodes(parent) have two branches.
1.3) Complete Binary Tree - Completely filled expect last level(d-1) and last level filled
from left to right
1.4) Skewed Binary Tree (Degenerate Tree) – All nodes have only one child
Left skewed – all nodes only in left subtrees
Right skewed – all nodes only in right subtrees
2) Binary Search Tree – It is a binary tree in which the node are arranged
according to their values. The left node has a value<its parent and
right node has a value≥parent node.
3) Expression Tree – represents expression containing operators
and operands.
4) Heap Tree – is complete binary tree such that the key in the parent is greater than or
equal to the keys in children . Largest value in root node it is called Max heap. smallest
value in root node it is called Min heap.
❖ Representation of Binary Tree
Preorder(Data-Left-Right)
Inorder(Left-Data-Right)
Postorder(Left-Right-Data)
7) Mirror
7) Pairs of siblings.
0
1
2
3
❖ Graph :- A graph is non-linear data structure consisting vertices(node) and
edges(connections between nodes)
OR
Is a collection of two sets V and E (G=(V,E)).V is finite non-empty sets of vertices
and E is finite non-empty sets of edges connecting pairs of vertices.
❖ Types of graph
1) Undirected Graph :- the edge do not have a direction.
This means the relationship between two nodes is bidirectional.
(Degree=indegree + outdegree)
vertex to another
7) Cycle :- A path that starts and ends at the same vertex without
9) Spanning tree :- Is a subsets of graph with all vertices and n(number of vertices)-1 edges.
0 0 0
1 0 1
1 0 0
2) Adjacency List :-
❖ Topological Sort
• It is linear ordering of graph vertices such that for every directed edge UV
3) After deleting the vertex of indegree=0 ,Again calculate Indegree of all vertex .
Visited :- 0 3
Visited :- 0 1 2
Step 7) Calculate indegree after deleting vertex 2
0 1 2 3
0 0 0 0
Visited :- 0 1 2 3
❖ Practice Question
Answer
Topological Sort :- 7 6 5 4 3 2 1 0
OR
75643210
❖ There are two algorithm for finding the minimum spanning tree of graph :-
1. Prim’s Algorithm
2. Kruskal’s Algorithm
❖ Prim’s Algorithm
To start algorithm, all loops and parallel edges are removed from the graph. The algorithm construct
The edge is selected by considering three factors :-1) select the edge which has lowest cost.
as end vertex.
❖ Practice Question
Answer
❖ Kruskal’s Algorithm
To start algorithm, all loops and parallel edges are removed from the graph. The algorithm
The edge is selected by considering two factors :-1) select the edge which has lowest cost from
graph.
• Dijkstra algorithm is “single source to all destinations” which gives the shortest path
from a given vertex(source) to all other vertices in a graph. It uses the “Greedy”
strategy.
• A greedy is a way of solving a problem step by step by always picking the best option
3
1
2
0
❖ Extra Practice Questions
➢ Prim’s Algorithm
➢ Kruskal’s Algorithm
➢ Topological Sort
➢ Dijkstra’s Algorithm
➢ Create Adjacency Matrix and Adjacency List