Data Structures Unit-4 Notes
Data Structures Unit-4 Notes
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.
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.
Example
The following is a graph with 5 vertices and 7 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)).
V1 V2
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).
Directed Edge
V1 V2
3.Weighted Edge - A weighted egde is a edge with value (cost) on it.
weighted Edge
3
V1 V2
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.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.
Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.
Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other
vertex such that each edge is incident to its predecessor and successor vertex.
GRAPH REPRESENTATIONS
Graph data structure is represented using following representations...
1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List
1.Adjacency Matrix
• In this representation, the graph is represented using a matrix of size total number of
vertices by a total number of vertices.
• That means a graph with 4 vertices is represented using a matrix of size 4X4.
• In this matrix, both rows and columns represent vertices.
• This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row
vertex to column vertex and 0 represents that there is no edge from row vertex to
column vertex.
• 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.
3.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..
There are two graph traversal techniques and they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth First Search)
Back tracking is coming back to the vertex from which we reached the current vertex.
Example:
BFS (BREADTH FIRST SEARCH)
• BFS traversal of a graph produces a spanning tree as final result.
• Spanning Tree is a graph without loops.
• We use Queue data structure with maximum size of total number of vertices in the
graph to implement BFS traversal.
Example
SORTING
Sorting is nothing but arranging the elements in ascending or descending order.
Sorting arranges data in a sequence which makes searching easier.
HEAP SORTING
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
To perform heap sort,first create heap tree for the given set of elements and then sort heap tree.
Example
Example:
Construct Heap tree for the following sequence of elements then sort the elements into
ascending order using heap sort technique.
50,25,30,75,100,45,80
1.Insert 50
• First element 50 is root.
2.Insert 25
25
3. Insert 30
• 30 is inserted at right of 50.
• The tree is satisfying heap condition.So it is a Heap tree.
50
25 30
4. Insert 75
• 75 is inserted at left of 25.
• The tree is not satisfying heap condition.Because 75 is greater than 25 as well as 50.
• So exchange it with its root 25 and again 50.
• After exchange it is a heap tree.
50 50 75
25 30 75 30 50 30
75 25 25
5. Insert 100
• 100 is inserted at right of 50.
• The tree is not satisfying heap condition.Because 100 is greater than 50 as well as 75.
• So exchange it with its root 50 and again 75.
• After exchange it is a heap tree.
75 75 100
50 30 100 30 75 30
25 100 25 50 25 50
6. Insert 45
• 45 is inserted at left of 30.
• The tree is not satisfying heap condition.Because 45 is greater than 30.
• So exchange 45 with its root 30.
• After exchange it is a heap tree.
100 100
75 30 75 45
25 50 25 50
45 30
7. Insert 80
• 80 is inserted at right of 45.
• The tree is not satisfying heap condition.Because 80 is greater than 45.
• So exchange 80 with its root 45.
• After exchange it is a heap tree.
100 100
75 45 75 80
25 50 25 50
30 80 30 45
SORT THE HEAP TREE
0
100 45
Exchange
1 2
75 80 75 80
a[0],a[6]
3 4 5 6 25
25 50 30 45 50 30 100
0
80 30
Exchange
1 2
75 45 75 45
a[0],a[5]
3 4 5 6 25
25 50 30 100 50 80 100
Sorted Path
75 30
50 45 75 45
25 100 25 50
30 80 80 100
0
75 30
Exchange
1 2
50 45 50 45
a[0],a[4]
3 4 5 6 25
25 30 80 100 75 80 100
Sorted Path
0
50 25
Exchange
1 2
30 45 30 45
a[0],a[3]
3 4 5 6
25 75 80 100 50 75 80 100
Sorted Path
0
45 25
Exchange
1 2
30 25 30 45
a[0],a[2]
3 4 5 6
50 75 80 100 50 75 80 100
Sorted Path
25
30 45
50 75 80 100
0
30
Exchange 25
1 2
25 45 a[0],a[1]
30 45
3 4 5 6
50 75 80 100
50 75 80 100
Sorting Categories
The techniques of sorting can be divided into two categories. These are:
• Internal Sorting
• External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main
memory, the internal sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in the memory
at the same time and some has to be kept in auxiliary memory such as hard disk, floppy disk,
magnetic tapes etc, then external sorting methods are performed.
EXTERNAL SORTING
• All the internal sorting algorithms require that the input fit into main memory.
• There are, however, applications where the input is much too large to fit into memory.
• For those external sorting algorithms, which are designed to handle very large inputs.
• Repeat
1. Read M records into main memory & sort internally.
2. Write this sorted sub-list into disk. (This is
one “run”). Until data is processed into runs.
• Repeat
1. Merge two runs into one sorted run twice as long.
2. Write this single run back onto
disk Until all runs processed into runs
twice as long
• Merge runs again as often as needed until only one large run: The sorted
list.
Example:
Next step is to merge the runs of length 6 into runs of length 12.
Next step is to merge the runs of length 12 into runs of length 24.
Here we have less than 24, so we’re finished.
MERGE SORT
Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls
itself for the two halves, and then merges the two sorted halves.
• It divides the given unsorted array into two halves- left and right sub arrays.
• The sub arrays are divided recursively.
• This division continues until the size of each sub array becomes 1.
• After each sub array contains only a single element, each sub array is sorted trivially.
• The merge procedure combines these trivially sorted arrays to produce a final sorted
array.
Example:
• The following diagram shows the complete merge sort process for an example array
{38, 27, 43, 3, 9, 82, 10}.
• If we take a closer look at the diagram, we can see that the array is recursively divided
in two halves till the size becomes 1.
• Once the size becomes 1, the merge processes come into action and start merging
arrays back till the complete array is merged.