2. DAA Lectures - (Unit -2)
2. DAA Lectures - (Unit -2)
Note: Undirected and Directed Graph can further have many forms like, Weighted graph vs
Unweighted Graph; Simple Graph vs Multigraph and many others. To understand these
classifications better, first let us know some terminologies related to Graph.
Graph Terminologies
Degree of a vertex: In an Undirected Graph, the number of
Edges connected to a Node (or vertex) is called the degree of
that vertex.
In case of Directed Graph (or Diagraph) there are two types of
Degrees for every vertices: In-Degree and Out-Degree.
The Indegree of a vertex is the number of edges coming towards
that vertex.
The Outdegree of a vertex is the number of edges going
outwards from that vertex.
Graph Terminologies
Degree of a vertex:
Deg(A)=0,
Deg(B)= Deg(E) =3,
Deg(C)=Deg(D)=2
Graph Terminologies
Adjacent Nodes:
A node u is adjacent to (or is a neighbor of) another node v, if there is an edge from node
u to node v.
In a directed graph, if (𝑣1 , 𝑣2 ) is an edge then 𝑣1 is adjacent to and 𝑣2 but vice-versa is not
true.
Graph Terminologies
Multiple Edges:
If between a pair of nodes in a graph, there are more than one
edges, then they are called multiple edges or parallel edges.
Ex: 𝑒4 and 𝑒5 .
Graph Terminologies
Source and Sink:
A node, which has no incoming edges, but has outgoing edges, is
called a Source. The indegree of source is zero.
A node, which has no outgoing edges but has incoming edges, is
called Sink. The outdegree of sink is zero.
Different forms of Graph
1. Weighted Graph:
A graph in which edges have a weight (or cost) associated with
them.
Ex. A road network graph where the weights can be the distance
between the two cities, otherwise the graph is called Unweighted
graph. Weighted graph is also termed as Labelled graph.
Different forms of Graph
1. Weighted Graph:
A graph in which edges have a weight (or cost) associated with
them.
Ex. A road network graph where the weights can be the distance
between the two cities, otherwise the graph is called Unweighted
graph. Weighted graph is also termed as Labelled graph.
Different forms of Graph
2. Finite Graph:
A graph in which number of vertices and edges are finite
(limited) and can be counted is called Finite graph, otherwise
the graph is called infinite graph.
3. Trivial Graph:
A finite graph that contains only one vertex and no edge is
called a Trivial graph, or single-vertex graph or Singleton graph.
4. Null Graph:
A graph with any number of vertices but there are no edges
connecting them is called Null graph or edgeless graph, isolated
graph or discrete graph.
Different forms of Graph
5. Simple Graph:
If a graph does not contain self-loop and no parallel edges, then
such a graph is called Simple graph.
Note: Number of Simple graph with n-labelled vertices is 2𝑛𝐶2 .
6. Multi-Graph:
Any graph that contains parallel edges but does not contain any
self-loop is called Multi-graph. Another kind of graph that
contains parallel edges as well as self-loop; i.e. A multi-graph
with self-loop is called- Pseudograph.
Different forms of Graph
7. Sparse Graph vs Dense Graph:
A graph with relatively few edges compared to the number of
vertices is called Sparse Graph.
A graph with many edges compared to the number of vertices is
called Dense Graph.
2) The BFS algorithm has been used to traverse the following graph. Which of the
following can be the possible order of the traversal, if start at vertex G?
1) GAHJIBCEKD
2) GAHJIBCDEK
3) GAHIJBDEKC
4) GAHIJBCEKD
QUIZ
3) The DFS algorithm has been used to traverse the following
graph. Which of the following can be the possible order of the
traversal?
1) ABEGHF
2) ABFEHG
3) ABFHGE
4) AFGHBE
Ex.
Deg(A)=3, Deg(I)=0, Deg(C)=1
Note:
o Degree of all the leaf node = 0
o Degree of a Tree = maximum
degree of any node in the tree.
Therefore, the degree of the given
tree = 3.
Tree Terminologies
Depth: Depth of a node is the length of the path from root to
that node.
Ex.
Depth of F=2, Depth of M =3
Note:
o Degree of Root node = 0
o Depth of nodes at each level of
the tree are the same.
Tree Terminologies
Height: Height of a node is the length of the longest path from
the leaf node to that node. Ex.
Height of B=2, Depth of D=1
Note:
o Height of Leaf node = 0
o Height of a tree is the height of
root node of that tree.
o Height and Depth of a node may
or may not be same.
o Depth is calculated from Top
(Root) and Height is calculated
from bottom (Leaf).
Tree Terminologies
Level: Nodes which are at the same depth in a tree are
known to be at the same level.
Ex.
level of A=0, level of B,C and D is
1.
Note:
❖ Since the balancing rules are strict compared to Red-Black tree, AVL
tree in general have relatively less height and hence the search is faster.
2. Delete the following nodes in order from the given AVL tree:
9, 60, 12, 15.
HEAP
Heap
Heap is a special case of Balanced as well as Complete Binary Tree, where
each node is compared with all its child nodes and based on this
comparison, Heap can be of two types:
MIN HEAP MAX HEAP
The Value of each node is either The Value of each node is either
less than or equal to either of its greater than or equal to either of
child node. its child node.
Heap
MIN HEAP MAX HEAP
70 50 40 45 35 39 16 10 9 23
1 2 3 4 5 6 7 8 9 10
MAX HEAP
Insertion in Max Heap
o Heap is a Complete Binary Tree, so new node is always to b inserted at
the leaf and from the left most side.
o After insertion of each node, we must check for the properties of Max
Heap should be satisfied.
MAX HEAP
Insertion in Max-Heap
Example:
1. Insert 60.
2. Insert 5.
MAX HEAP
Deletion in Max Heap
o Deletion in Heap is allowed only to the root Node, i.e. we can only delete
the root node.
o The last element in the array will take the place of the root node in the
updated heap.
o For max-Heap, let x and y be the child of u, and u is not greater than x
and y, then replace u with the larger between x and y.
Hashing Mechanism
In Hashing,
• An Array data structure, called Hash Table is used to store the data items, based on the Hash Key value.
• Hash Key value is a special value that serves as index for a data item. It indicates where the data item should be stored
in the Hash Table. Hash Key value is generated using Hash Function.
• Hash Function is a function that maps any bigger number or string to a small integer value.
➢ Hash Function takes the data item as input and returns a small integer value as an output.
➢ The small integer value is called Hash Value.
➢ Hash Value of the data item is then used as an index for storing it into the Hash Table.
Properties of a Hash Function
Good Hash function are-
➢ It is efficiently computable
➢ It minimizes the number of Collision
➢ It distribute the keys uniformly over the Hash Table
NOTE: A collision in hashing occurs when two different pieces of data produce the same hash value.
2) Modulo-Division Method
3) Folding Technique
Collision Resolution Techniques