BTech Data structures, Slides of Data Structures and Algorithms

Comprehensive B.Tech semester exam notes for Data Structures (DS) and Basic Electrical Engineering (BEE). Includes important theory questions, definitions, derivations, algorithms, short notes, diagrams, solved examples, and previous exam-oriented topics. Covers hashing, trees, graphs, sorting, searching, files, transformers, DC machines, AC circuits, semiconductor devices, Hall effect, lasers, quantum computing basics, and more. Useful for JNTUH engineering students for semester exam preparation and quick revision. Suitable for CSE, ECE, EEE, and related engineering branches.

Typology: Slides

2025/2026

Available from 06/28/2026

nidhi-goud
nidhi-goud 🇮🇳

4 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures N Bhaskar
Unit-IV
Graphs
Introduction to Graphs
Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a set of links
known as edges (or Arcs). Here edges are used to connect the vertices. A graph is defined as follows...
Graph is a collection of vertices and arcs in which vertices are connected with arcs
Graph is a collection of nodes and edges in which nodes are connected with edges
Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of edges.
Ex:
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Graph Terminology
We use the following terms in graph data structure...
Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In above example graph,
A, B, C, D & E are known as vertices.
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as
(startingVertex, endingVertex). For example, in above graph the link between vertices A and B is represented as
(A,B). In above example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).
Edges are three types.
1. Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge between vertices A
and B then edge (A , B) is equal to edge (B , A).
2. Directed Edge - A directed egde is a unidirectional edge. If there is directed edge between vertices A and B
then edge (A , B) is not equal to edge (B , A).
3. Weighted Edge - A weighted egde is a edge with value (cost) on it.
1 CMR Technical Campuscal Campus
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download BTech Data structures and more Slides Data Structures and Algorithms in PDF only on Docsity!

Unit-IV

Graphs

Introduction to Graphs

Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a set of links known as edges (or Arcs). Here edges are used to connect the vertices. A graph is defined as follows...

  • Graph is a collection of vertices and arcs in which vertices are connected with arcs
  • Graph is a collection of nodes and edges in which nodes are connected with edges Generally, a graph G is represented as G = ( V , E ) , where V is set of vertices and E is set of edges. Ex: The following is a graph with 5 vertices and 6 edges. This graph G can be defined as G = ( V , E ) Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Graph Terminology

We use the following terms in graph data structure... Vertex Individual data element of a graph is called as Vertex. Vertex is also known as node. In above example graph, A, B, C, D & E are known as vertices. Edge An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as (startingVertex, endingVertex). For example, in above graph the link between vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)). Edges are three types.

1. Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge between vertices A and B then edge (A , B) is equal to edge (B , A). 2. Directed Edge - A directed egde is a unidirectional edge. If there is directed edge between vertices A and B then edge (A , B) is not equal to edge (B , A). 3. Weighted Edge - A weighted egde is a edge with value (cost) on it.

Undirected Graph A graph with only undirected edges is said to be undirected graph. Directed Graph A graph with only directed edges is said to be directed graph. Mixed Graph A graph with both undirected and directed edges is said to be mixed graph. End vertices or Endpoints The two vertices joined by edge are called end vertices (or endpoints) of that edge. Origin If a edge is directed, its first endpoint is said to be the origin of it. Destination If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be the destination of that edge. Adjacent If there is an edge between vertices A and B then both A and B are said to be adjacent. In other words, vertices A and B are said to be adjacent if there is an edge between them. Incident Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge. Outgoing Edge A directed edge is said to be outgoing edge on its origin vertex. Incoming Edge A directed edge is said to be incoming edge on its destination vertex. Degree Total number of edges connected to a vertex is said to be degree of that vertex. Indegree Total number of incoming edges connected to a vertex is said to be indegree of that vertex.

Directed graph representation... Incidence Matrix In this representation, the graph is represented using a matrix of size total number of vertices by a total number of edges. That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6. In this matrix, rows represent vertices and columns represents edges. This matrix is filled with 0 or 1 or -1. Here, 0 represents that the row edge is not connected to column vertex, 1 represents that the row edge is connected as the outgoing edge to column vertex and -1 represents that the row edge is connected as the incoming edge to column vertex. For example, consider the following directed graph representation... Adjacency List In this representation, every vertex of a graph contains list of its adjacent vertices. For example, consider the following directed graph representation implemented using linked list...

This representation can also be implemented using an array as follows..

Graph Traversal

Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is also used to decide the order of vertices is visited in the search process. A graph traversal finds the edges to be used in the search process without creating loops. That means using graph traversal we visit all the vertices of the graph without getting into looping path. There are two graph traversal techniques and they are as follows...

  1. DFS (Depth First Search)
  2. BFS (Breadth First Search) DFS (Depth First Search) DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops. We use Stack data structure with maximum size of total number of vertices in the graph to implement DFS traversal. We use the following steps to implement DFS traversal...
  3. Define a Stack of size total number of vertices in the graph.
  4. Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
  5. Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and push it on to the stack.
  6. Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of the stack.
  7. When there is no new vertex to visit then use back tracking and pop one vertex from the stack.
  8. Repeat steps 3, 4 and 5 until stack becomes Empty.
  9. When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph Note: Back tracking is coming back to the vertex from which we reached the current vertex. Ex:
  1. Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and insert them into the Queue.
  2. When there is no new vertex to be visited from the vertex which is at front of the Queue then delete that vertex.
  3. Repeat steps 3 and 4 until queue becomes empty.
  4. When queue becomes empty, then produce final spanning tree by removing unused edges from the graph Ex:

Sorting Sorting is the process of arranging a list of elements in a particular order (Ascending or Descending). Insertion Sort Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort algorithm, every iteration moves an element from unsorted portion to sorted portion until all the elements are sorted in the list. The insertion sort algorithm is performed using the following steps...

  1. Assume that first element in the list is in sorted portion and all the remaining elements are in unsorted portion.
  2. Take first element from the unsorted portion and insert that element into the sorted portion in the order specified.
  3. Repeat the above process until all the elements from the unsorted portion are moved into the sorted portion. Ex:

//Insertion Sorting #include void main() { int size, i, j, temp, list[100]; printf("Enter the size of the list: "); scanf("%d", &size); printf("Enter %d integer values: ", size); for (i = 0; i < size; i++) scanf("%d", &list[i]); //Insertion sort logic for (i = 1; i < size; i++) { temp = list[i]; j = i - 1; while ((temp < list[j]) && (j >= 0)) { list[j + 1] = list[j]; j = j - 1; } list[j + 1] = temp; } printf("List after Sorting is: "); for (i = 0; i < size; i++) printf(" %d", list[i]); }

Bubble sort Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n^2 ) where n is the number of items. Ex: We take an unsorted array for our example. Bubble sort takes Ο(n^2 ) time so we're keeping it short and precise. Bubble sort starts with very first two elements, comparing them to check which one is greater. //Selection Sort #include void main() { int size,i,j,temp,list[100]; printf("Enter the size of the List: "); scanf("%d",&size); printf("Enter %d integer values: ",size); for(i=0; i list[j]) { temp=list[i]; list[i]=list[j]; list[j]=temp; } } } printf("List after sorting is: "); for(i=0; i And when there's no swap required, bubble sorts learns that an array is completely sorted. Heap Sort Heap sort is one of the sorting algorithms used to arrange a list of elements in order. Heapsort algorithm uses one of the tree concepts called Heap Tree. In this sorting algorithm, we use Max Heap to arrange list of elements in Descending order and Min Heap to arrange list elements in Ascending order. Max Heap Heap data structure is a specialized binary tree-based data structure. Heap is a binary tree with special characteristics. In a heap data structure, nodes are arranged based on their values. A heap data structure some times also called as Binary Heap. There are two types of heap data structures and they are as follows...

  1. Max Heap

  2. Min Heap //Bubble Sort #include int main() { int a[50],n,i,j,temp; printf("Enter the size of array: "); scanf("%d",&n); printf("Enter the array elements: "); for(i=0;ia[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf("\nArray after sorting: "); for(i=0;i Every heap data structure has the following properties... 1 (Ordering): Nodes must be arranged in an order according to their values based on Max heap or Min heap. 2 (Structural): All levels in a heap must be full except the last level and all nodes must be filled from left to right strictly. Max heap data structure is a specialized full binary tree data structure. In a max heap nodes are arranged based on node value. Max heap is a specialized full binary tree in which every parent node contains greater or equal value than its child nodes. Ex: Above tree is satisfying both Ordering property and Structural property according to the Max Heap data structure. Operations Finding Maximum Value Finding the node which has maximum value in a max heap is very simple. In a max heap, the root node has the maximum value than all other nodes. So, directly we can display root node value as the maximum value in max heap. Insertion Insertion Operation in max heap is performed as follows...

  3. Insert the newNode as last leaf from left to right.

  4. Compare newNode value with its Parent node.

  5. If newNode value is greater than its parent, then swap both of them.

  6. Repeat step 2 and step 3 until newNode value is less than its parent node (or) newNode reaches to root. Ex: Consider the above max heap. Insert a new node with value 85.