0% found this document useful (0 votes)
2 views

2. DAA Lectures - (Unit -2)

The document provides an overview of data structures and algorithms, emphasizing their importance in organizing data and solving problems efficiently. It covers various types of data structures, including linear and non-linear, static and dynamic, as well as primitive and non-primitive structures, and introduces graph theory with definitions and classifications of graphs. Additionally, it discusses tree structures, their terminologies, and traversal algorithms like BFS and DFS.

Uploaded by

indiandj432
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)
2 views

2. DAA Lectures - (Unit -2)

The document provides an overview of data structures and algorithms, emphasizing their importance in organizing data and solving problems efficiently. It covers various types of data structures, including linear and non-linear, static and dynamic, as well as primitive and non-primitive structures, and introduces graph theory with definitions and classifications of graphs. Additionally, it discusses tree structures, their terminologies, and traversal algorithms like BFS and DFS.

Uploaded by

indiandj432
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/ 98

|| Recap ||

Data Structures: A way of organizing data so it can be


used efficiently. Common data structures include
arrays, stack, Queue, linked lists, trees and Graphs.

Algorithms: A set of step-by-step instructions to solve


a specific problem.

Theory about Data Structures and Algorithms (DSA)


helps us to use large amounts of data to solve problems
efficiently.
|| Recap ||

Time Complexity: A measure of the amount of time an


algorithm takes to run, depending on the amount of
data the algorithm is working on.

Space Complexity: A measure of the amount of main


memory an algorithm uses, depending on the amount
of data the algorithm is working on.
|| Recap ||

Recursion: A programming technique where a function


calls itself.
Divide and Conquer: A method of solving complex
problems by breaking them into smaller, more
manageable sub-problems, solving the sub-problems,
and combining the solutions.
Brute-Force: A simple and straight forward way an
algorithm can work by simply trying all possible
solutions and then choosing the best one.
|| Recap ||

What are Data Structures?


A data structure is a way to store data.
We structure data in different ways depending on what
data we have, and what we want to do with it.
|| Recap ||

Classification of Data Structures?


|| Recap ||
Linear Data Structure: Data structure in which data
elements are arranged sequentially or linearly, where
each element is attached to its previous and next
adjacent elements, is called a linear data structure.
Example: Array, Stack, Queue, Linked List, etc.

Non-Linear Data Structure: Data structures where data


elements are not placed sequentially or linearly are
called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single
run only.
|| Recap ||

Static Data Structure: Static data structure has a fixed


memory size. It is easier to access the elements in a
static data structure.
Example: array.

Dynamic Data Structure: In dynamic data structure,


the size is not fixed. It can be randomly updated during
the runtime which may be considered efficient
concerning the memory (space) complexity of the code.
Example: Queue, Stack, etc.
|| Recap ||
Primitive Data Structures: are basic data structures
provided by programming languages to represent single
values, such as integers, floating-point numbers,
characters, and Booleans.

Non-Primitive or Abstract Data Structures: are higher-


level data structures that are built using primitive data
types and provide more complex and specialized
operations. Some common examples of abstract data
structures include arrays, linked lists, stacks, queues,
trees, and graphs.
GRAPH
Definition of Graph
A Graph G is a collection of two sets V and E, where V is the
collection of vertices 𝑣1 , 𝑣2 , 𝑣3 , …, 𝑣𝑛 also called Nodes and E is
the collection of edges 𝑒1 , 𝑒2 , 𝑒3 , …, 𝑒𝑛 , where an Edge is an arc
that connects two nodes. This can be represented as G=(V,E).

Note: In G, set V can not be empty, where set E can be empty.

Based on the orientation of edges, A Graph can be of two types:


Undirected Graph and Directed Graph.
Undirected & Directed Graph
Undirected Graph: A Graph which has unordered pair of
vertices, is called Undirected graph.
Suppose, there is an edge between 𝑣1 and 𝑣2 , then it can be
represented in either as (𝑣1 , 𝑣2 ) or (𝑣2 , 𝑣1 ).

Here V(G)= {A, B, C, D} and


E(G)={(A,B), (A,C), (B,C), (C,D), (D,A)}

Note: edge (A,B) in Undirected graph means there is no


direction associated from vertex A to vertex B in that edge, so
writing (B,A) to represent that edge is also valid.
Undirected & Directed Graph
Directed Graph: A Graph with ordered pair of vertices, is called
Directed graph or Diagraph.
In Directed graph, the representation of edge (𝑣1 , 𝑣2 ) and (𝑣2 ,
𝑣1 ) are different. Here each edge is associated with a direction.

Here V(G)= {A, B, C, D} and


E(G)={(A,B), (B,C), (C,D), (D,B),
(D,A), (D,E), (E,A)}

Note: edge (A,B) in Directed graph means there is a


direction associated from vertex A to vertex B in that
edge, so writing (B,A) to represent that edge is not valid.
Undirected & Directed Graph
Mixed Graph: If in a Graph some of the edges are Directed and
rest of the edges are undirected, then such a Graph is called
Mixed graph.
Underlying Undirected Graph: A Graph obtained by removing
the direction of the edges in a Directed Graph.

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 an undirected graph, if (𝑣1 , 𝑣2 ) is an edge then 𝑣1 is adjacent to and 𝑣2 is also said to be


adjacent to 𝑣1 .

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.

8. Cyclic Graph vs Wheel Graph:


A graph consisting of Single cycle is called Cyclic Graph or
Circular graph.
A Wheel graph is formed by connecting a single universal vertex
to all other vertices of a cycle.
Different forms of Graph
9. Regular Graph:
A graph in which all the vertices are of equal degree is called a
Regular graph. A n-regular graph means, every vertices of the
graph has degree n.

10. Connected Graph:


A graph is said to be a connected graph if there is at least one
path between every pair of vertices in that graph, otherwise it is
said to be a disconnected graph.
Graph Representation

Graph is a non-linear logical data structure, so its


representation in memory can be done basically through Array
(Matrix) and Linked-list.
(1) Array (Matrix) Representation:
a) Adjacency matrix
b) Incidence matrix
(2) List Representation:
c) Adjacency list
Graph Representation
1.a) Adjacency matrix
(ii) For Directed Graph:
Graph Representation
1.a) Adjacency matrix
(iii) For Weighted-Undirected Graph:
Graph Representation
1.a) Adjacency matrix
(iv) For Weighted-Directed Graph:
Graph Representation
1.b) Incidence matrix
In this representation, the matrix constitutes of vertices (rows)
and edges (columns). If edge 𝐞𝟏 is incident on vertex 𝒗𝟏 then
A[i][j]=1 or the 1×cost, If edge 𝐞𝟏 is incident from vertex 𝒗𝟏
then A[i][j]=-1 or the (-1)×cost, Otherwise 0.
(i) For Undirected Graph:
Graph Representation
2.c) Adjacency list
(i) For Undirected Graph:
Graph Representation
2.c) Adjacency list
(ii) For Directed Graph:
Graph Representation
2.c) Adjacency list
(iii) For Directed-weighted Graph:
Graph Representation
QUIZ
Graph Traversal
Breadth-First Search (BFS) and Depth-First Search (DFS) are
two fundamental algorithms used for traversing or searching
graphs and trees. Both algorithms have distinct characteristics
and applications.
Graph Traversal
Breadth-First Search (BFS)

BFS is a vertex-based technique that uses a Queue data


structure. It explores all the vertices at the present depth level
before moving on to the vertices at the next depth level. BFS is
particularly useful for finding the shortest path in an
unweighted graph.
Graph Traversal
Depth-First Search (DFS)

DFS is an edge-based technique that uses a Stack data


structure. It explores as far as possible along each branch before
backtracking. DFS is useful for tasks such as topological sorting,
finding strongly connected components, and detecting cycles in
a graph.
Graph Traversal
BFS Traversal
Find the BFS traversal of the following graph:
Graph Traversal
DFS Traversal
Find the DFS traversal of the following graph:
QUIZ
1) The BFS algorithm has been used to traverse the following graph. Which of the
following can be the possible order of the traversal?
1) MNOPQR
2) NQMPOR
3) QMNROP
4) POQNMR

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

4) The DFS algorithm has been used to traverse the following


graph. Which of the following can be the possible order
of the traversal?
1) ABCGHF
2) ABFCHG
3) ABFHGC
4) AFGHBC
TREE
Tree
o Tree is a connected acyclic graph.
o Tree is a non-linear, Hierarchical data structure.
o Tree can be defined as a
collection of entities
(called “Nodes”),
linked together with
“edges” to simulates
hierarchical construct.
Tree Terminologies
Node: Each vertex of a tree is called its Node. The node
may contain some information. Each node is connected
to its child node.

Root node: The top most


Node is called root of the
Tree. A tree can have only
One root node.
e.g. ‘A’ is the root node.
Tree Terminologies
Parent node: The immediate predecessor of any node in
a tree is called Parent of that node.
e.g. ‘B’ is the parent of ‘E’
and ‘F’.
Child node: The immediate
successor of any node in
a tree is called Child of that
node.
e.g. ‘L’ and ‘M’ are the
Child of ‘G’.
Note: Root node is not having any Parent node and Leaf node is not having any child node.
Tree Terminologies
Leaf node: The nodes that are in the bottom-most level
or having no child are called Leaf node or external
node.
e.g. ‘I’, ‘J’, ‘K’, ‘F’, ‘L’, ‘M’, ‘H’.
Non-leaf node: Nodes having
At least one child is called
Non-leaf nodes i.e. all the
Nodes that are not leaf are
called Non-leaf or Internal
node.,
e.g. ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘G’.
Tree Terminologies
Path: Sequence of consecutive edges from a source node
to a destination node is called Path between that
source and destination nodes.
Sibling: All the nodes having a common parent are
called Sibling.
Neighbor: Parents and child nodes of any node are
called Neighbor of that node.
Tree Terminologies
Ancestor: The ancestor of a node ‘u’ is defined as all the
predecessor nodes on the path from the root to ‘u’.

Descendant: The descendant of a node ‘u’ is defined as


all the successor nodes on the path from ‘u’ to a leaf
node on that path.
Tree Terminologies
Degree: Degree of a node is the number of children of that
node.

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:

o Level of node is the length of that node from the


root.
o Each hierarchy from top to bottom of a tree is
called a Level.
o Level of a tree = maximum level of any node in
that tree.
o Level of a tree = Height of a tree.
o Level of Node = Depth of that node
≠ Height of that node
Binary Tree
Each node in a tree can have at most two children, then such a
tree is called Binary tree.
Ex.
Types of Binary Tree
1) Full / Strict / Proper Binary tree
Each node can have either 0 or 2 children or in other word,
each node can have exactly 2 child except the leaf nodes.

2) Complete Binary tree


All levels are completely filled except possibly the last level and
the last level has nodes as left as possible.

3) Perfect Binary tree


All internal nodes have exactly two children and all leaf nodes
are at the same level.
Tree Traversal
Binary Search Tree
AVL Tree
Definition:
An AVL tree is a self balancing Binary search
tree with balance factor of any node not
exceeding 1.
(i.e., Balance factor allowed are -1, 0, +1)
Balance Factor:
The difference between the height of the left
subtree and the right subtree for any node is
known as the Balance Factor.
AVL tree is named after its inventors-
Georgy Adelson Velsky and Evgenii Landis.
AVL Tree
Advantages of AVL Tree:

❖ AVL tree can self-balance themselves and therefore provides time


complexity as O(log n) for search, insert and delete.

❖ As it is a BST, so items can be traversed in sorted order.

❖ 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.

❖ AVL tree is relatively less complex to understand and implement


compared to Red-Black tree.
AVL Tree
Disadvantages of AVL Tree:

❖ Difficult to implement compared to normal BST.

❖ AVL trees provide complicated insertion and deletion operation as


more numbers of rotation are to be performed.
AVL Tree
Rotations:
AVL Tree
Construction of AVL tree:

Construct an AVL tree by inserting the following nodes:


14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20.
AVL Tree
Deletion in of AVL tree:
Given an AVL tree:

Delete the nodes in order- 8, 7, 11,


14, 17.
QUIZ
1. Construct an AVL tree for the following data:
21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7.

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

NOTE: If Node is at location i, then,


𝐢
Parent of (i) =
𝟐

Left-Child of (i) = 2 × i ; Right-Child of (i) = 2 × i + 1


MAX HEAP
Array Representation of Max Heap

Form an Array just using level-


order traversal of the 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.

o Repeat step-3 till we reach to the leaf node.


MAX HEAP
Deletion in Max Heap
Example:

Delete one node from the given


Max-Heap
QUIZ
1. Given a Binary Max-Heap. The elements are stored in an array as
50, 45, 35, 33, 16, 25, 34, 12, 10.
What will be the content of the array after two insert operations of
values 70 and 28?

2. Given a Binary Max-Heap. The elements are stored in an array as


25, 14, 16, 13, 10, 8, 12.
What will be the content of the array after two delete operations?
HASHING
Hashing
There are several searching algorithm like Linear search, Binary Search, Tree Search etc. In these techniques the time taken
to search any particular key depends on the total number of elements.
In order to improve the searching time, Hashing is an efficient method, used in Data Structure. The time taken by it to
perform the search does not depend on the total number of elements. It completes the search with constant time
complexity i.e. O(1).

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.

Types of Hash Function


➢ Direct Hashing
➢ Subtraction Method
➢ Mid Square Method
➢ Modulo Division Method
➢ Digit Extraction Method
➢ Folding Technique
➢ Shifting Method
➢ Rotation Method
➢ Pseudo Random Method
1) Mid-Square Method

2) Modulo-Division Method

3) Folding Technique
Collision Resolution Techniques

Closed Hashing Open Hashing


(Open Addressing) (Closed Addressing)

Linear Probing Quadratic Probing Double Hashing Separate Chaining

You might also like