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

Data Structures Unit 4

Uploaded by

ganaroy2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Structures Unit 4

Uploaded by

ganaroy2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

1

Aurora’s Technological and Research Institute


Department of Computer Science and Engineering

Data Structures
R18 CSE/IT II year

By
D. Subhashini
Associate Professor
Department of Computer Science and Engineering
Aurora’s Technological and Research Institute

D. Subhashini, Assoc. prof


2
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

UNIT IV
Graphs: Graph Implementation Methods, Graph
Traversal Methods.
Sorting: Heap Sort, External Sorting- Model for
external sorting, Merge Sort.

D. Subhashini, Assoc. prof


3
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Graph
A graph can be defined as group of vertices and edges that are used to connect these
vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes) maintain any
complex relationship among them instead of having parent child relationship.

Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges which are used to connect these vertices.

A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following figure.

Directed and Undirected Graph


A graph can be directed or undirected. However, in an undirected graph, edges are not
associated with the directions with them. An undirected graph is shown in the above
figure since its edges are not attached with any of the directions. If an edge exists
between vertex A and B then the vertices can be traversed from B to A as well as A to B.

In a directed graph, edges form an ordered pair. Edges represent a specific path from
some vertex A to another vertex B. Node A is called initial node while node B is called
terminal node.

A directed graph is shown in the following figure.

D. Subhashini, Assoc. prof


4
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Graph Terminology
Path
A path can be defined as the sequence of nodes that are followed in order to reach some
terminal node V from the initial node U.

Closed Path
A path will be called as closed path if the initial node is same as terminal node. A path
will be closed path if V0=VN.

Simple Path
If all the nodes of the graph are distinct with an exception V 0=VN, then such path P is
called as closed simple path.

Cycle
A cycle can be defined as the path which has no repeated edges or vertices except the
first and last vertices.

Connected Graph
A connected graph is the one in which some path exists between every two vertices (u,
v) in V. There are no isolated nodes in connected graph.

D. Subhashini, Assoc. prof


5
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Complete Graph
A complete graph is the one in which every node is connected with all other nodes. A
complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.

Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or weight. The
weight of an edge e can be given as w(e) which must be a positive (+) value indicating
the cost of traversing the edge.

Digraph
A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.

Loop
An edge that is associated with the similar end points can be called as Loop.

Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called as
neighbors or adjacent nodes.

Degree of the Node


A degree of a node is the number of edges that are connected with that node. A node
with degree 0 is called as isolated node.

D. Subhashini, Assoc. prof


6
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Graph Representation
By Graph representation, we simply mean the technique which is to be used in order to
store some graph into the computer's memory.

There are two ways to store Graph into the computer's memory.

Sequential Representation or Adjacency Matrix


In sequential representation, we use adjacency matrix to store the mapping represented
by vertices and edges. In adjacency matrix, the rows and columns are represented by
the graph vertices. A graph having n vertices, will have a dimension n x n.

An entry Mij in the adjacency matrix representation of an undirected graph G will be 1 if


there exists an edge between Vi and Vj.

An undirected graph and its adjacency matrix representation is shown in the following
figure.

in the above figure, we can see the mapping among the vertices (A, B, C, D, E) is
represented by using the adjacency matrix which is also shown in the figure.

There exists different adjacency matrices for the directed and undirected graph. In
directed graph, an entry Aij will be 1 only when there is an edge directed from Vi to Vj.

A directed graph and its adjacency matrix representation is shown in the following figure.

D. Subhashini, Assoc. prof


7
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Representation of weighted directed graph is different. Instead of filling the entry by 1,


the Non- zero entries of the adjacency matrix are represented by the weight of respective
edges.

The weighted directed graph along with the adjacency matrix representation is shown in
the following figure.

D. Subhashini, Assoc. prof


8
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Linked Representation or Adjacency Lists


Adjacency Lists are represented in two ways.

 Linked Adjacency Lists


 Array Adjacency Lists

Linked Adjacency Lists:

In the linked representation, an adjacency list is used to store the Graph into the
computer's memory. Consider the undirected graph shown in the following figure and
check the adjacency list representation.

An adjacency list is maintained for each node present in the graph which stores the node
value and a pointer to the next adjacent node to the respective node. If all the adjacent
nodes are traversed then store the NULL in the pointer field of last node of the list. The
sum of the lengths of adjacency lists is equal to the twice of the number of edges present
in an undirected graph.

Consider the directed graph shown in the following figure and check the adjacency list
representation of the graph.

D. Subhashini, Assoc. prof


9
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

In a directed graph, the sum of lengths of all the adjacency lists is equal to the number
of edges present in the graph.

In the case of weighted directed graph, each node contains an extra field that is called
the weight of the node. The adjacency list representation of a directed graph is shown in
the following figure.

Array Adjacency Lists:

• Each adjacency list is an array list.

Array Length = n
No. of list elements = 2e (undirected graph)
No. of list elements = e (digraph)

D. Subhashini, Assoc. prof


10
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Graph Traversal Algorithm


Traversing the graph means examining all the nodes and vertices of the graph. There are
two standard methods by using which, we can traverse the graphs.

o Breadth First Search


o Depth First Search

Breadth First Search (BFS) Algorithm


Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and
uses a queue to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.

As in the example given above, BFS algorithm traverses from A to B to E to F first then
to C and G lastly to D. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert
it in a queue.
 Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

D. Subhashini, Assoc. prof


11
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Step Traversal Description

Initialize the queue.

We start from visiting S (starting


node), and mark it as visited.

We then see an unvisited


adjacent node from S. In this
example, we have three nodes
but alphabetically we choose A,
mark it as visited and enqueue it.

Next, the unvisited adjacent node


from S is B. We mark it as visited
and enqueue it.

D. Subhashini, Assoc. prof


12
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Next, the unvisited adjacent node


from S is C. We mark it as visited
and enqueue it.

Now, S is left with no unvisited


adjacent nodes. So, we dequeue
and find A.

From A we have D as unvisited


adjacent node. We mark it as
visited and enqueue it.

At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm
we keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied,
the program is over.
Here enqueue is a entering a new element into the queue and dequeuer is deleting an
element from the queue.

D. Subhashini, Assoc. prof


13
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

BFS example2
Let's see how the Breadth First Search algorithm works with an example. We use an

undirected graph with 5 vertices.

Undirected graph with 5 vertices

We start from vertex 0, the BFS algorithm starts by putting it in the Visited list and
putting all its adjacent vertices in the stack.

Visit start vertex and add its adjacent vertices to queue

Next, we visit the element at the front of queue i.e. 1 and go to its adjacent nodes.

Since 0 has already been visited, we visit 2 instead.

D. Subhashini, Assoc. prof


14
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Visit the first neighbour of start node 0, which is 1

Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the back of the queue

and visit 3, which is at the front of the queue.

Visit 2 which was added to queue earlier to add its neighbours

4 remains in the queue

Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already visited.

We visit it.

Visit last remaining item in the stack to check if it has unvisited neighbors

Since the queue is empty, we have completed the Breadth First Traversal of the graph.

D. Subhashini, Assoc. prof


15
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Breadth first search Program in c:

#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

void bfs(int v) {
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
}
}

void main() {
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");

D. Subhashini, Assoc. prof


16
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n %d Not visited using bfs", i);
getch();
}

D. Subhashini, Assoc. prof


17
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Depth First Search (DFS) Algorithm


Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses
a stack to remember to get the next vertex to start a search, when a dead end occurs
in any iteration.

As in the example given above, DFS algorithm traverses from S to A to D to G to E to B


first, then to F and lastly to C. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it
in a stack.
 Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will
pop up all the vertices from the stack, which do not have adjacent vertices.)
 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.

Step Traversal Description

D. Subhashini, Assoc. prof


18
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Initialize the stack.

Mark S as visited and put it onto


the stack. Explore any unvisited
adjacent node from S. We have
three nodes and we can pick any
of them. For this example, we
shall take the node in an
alphabetical order.

Mark A as visited and put it onto


the stack. Explore any unvisited
adjacent node from A.
Both S and D are adjacent
to A but we are concerned for
unvisited nodes only.

D. Subhashini, Assoc. prof


19
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Visit D and mark it as visited and


put onto the stack. Here, we
have B and C nodes, which are
adjacent to D and both are
unvisited. However, we shall
again choose in an alphabetical
order.

We choose B, mark it as visited


and put onto the stack.
Here B does not have any
unvisited adjacent node. So, we
pop B from the stack.

We check the stack top for return


to the previous node and check if
it has any unvisited nodes. Here,
we find D to be on the top of the
stack.

D. Subhashini, Assoc. prof


20
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Only unvisited adjacent node is


from D is C now. So we visit C,
mark it as visited and put it onto
the stack.

As C does not have any unvisited adjacent node so we keep popping the stack until we
find a node that has an unvisited adjacent node. In this case, there's none and we keep
popping until the stack is empty.
As the DFS procedure uses stack, we can easily implement it using recursion.

D. Subhashini, Assoc. prof


21
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Algorithm:

dfs(v)
{
Label vertex v as reached.
for (each unreached vertex u
adjacenct from v)
dfs(u);
}

Depth First Search Example2:


Let's see how the Depth First Search algorithm works with an example. We use an

undirected graph with 5 vertices.

Undirected graph with 5 vertices

We start from vertex 0, the DFS algorithm starts by putting it in the Visited list and

putting all its adjacent vertices in the stack.

Visit the element and put it in the visited list

D. Subhashini, Assoc. prof


22
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Next, we visit the element at the top of stack i.e. 1 and go to its adjacent nodes. Since

0 has already been visited, we visit 2 instead.

Visit the element at the top of stack

Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack

and visit it.

Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.

Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.

After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we

have completed the Depth First Traversal of the graph.

D. Subhashini, Assoc. prof


23
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we

have completed the Depth First Traversal of the graph.

Depth first search Program in c:


#include<stdio.h>
#include<conio.h>
int a[20][20],visited[20],n,i,j;

void dfs(int v)
{
visited[i]=1;
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
dfs(i);
}

void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++)
visited[i]=0;
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
dfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
getch();
}

D. Subhashini, Assoc. prof


24
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Heap Data Structures


Heap is a special case of balanced binary tree data structure where the root-node key is
compared with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap.
Based on this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its children.

Max-Heap − Where the value of the root node is greater than or equal to either of its
children.

Both trees are constructed using the same input and order of arrival.

D. Subhashini, Assoc. prof


25
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Max Heap Construction Algorithm


We shall use the same example to demonstrate how a Max Heap is created. The
procedure to create Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At
any point of time, heap must maintain its property. While insertion, we also assume that
we are inserting a node in an already heapified tree.
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Note − In Min Heap construction algorithm, we expect the value of the parent node to
be less than that of the child node.
Let's understand Max Heap construction by an animated illustration. We consider the
same input sample that we used earlier.

Example: Insert 17 into a heap with elements 19,12,16,1,4,7.

D. Subhashini, Assoc. prof


26
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Max Heap Deletion Algorithm


Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap
always happens at the root to remove the Maximum (or minimum) value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.

Example:Covert the following into heap.

The complete binary tree representation of above data is:

Steps to convert above tree into heap:

Heap Sort Procedure:

D. Subhashini, Assoc. prof


27
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

 The heapsort algorithm consists of two phases:


build a heap from an arbitrary array
use the heap to sort the data

 To sort the elements in the decreasing order, use a min heap


 To sort the elements in the increasing order, use a max heap
Steps to sort above Heap:
Step 1:

Step 2:

Step 3:

D. Subhashini, Assoc. prof


28
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Step 4:

Step 5:

Step 6:

D. Subhashini, Assoc. prof


29
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Step 7:

Step 8:

Step 9:

D. Subhashini, Assoc. prof


30
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Step 10:

Step 11:

Step 12:

D. Subhashini, Assoc. prof


31
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Step 13:

Step 14:

Heap Sort Program in C

#include <stdio.h>

void adjust(int a[10],int,int);


void heapify(); // creating a heap of n arbitrary elements
void heapsort(int a[10],int n);

void main()
{
int a[10],n,i;
printf("Enter no. of elements");
scanf("%d",&n);
printf("\n enter data to be sored ..");
for (i=1;i<=n;i++)

D. Subhashini, Assoc. prof


32
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

scanf("%d",&a[i]);
heapsort(a,n);
printf("sorted data:\n");
for (i=1;i<=n;i++)
printf(" %d",a[i]);
}

void heapsort(int a[10],int n)


{
int i,t;
heapify(a,n);
for (i=n;i>1;i--)
{
t = a[i];
a[i] = a[1];
a[1] = t;
adjust(a,1,i-1);
}
}

void adjust(int a[10],int i,int l)


{
// the complete binary trees with roots a[2*i] and a[2*i+1] are
// combined with a[i] to form a single heap;
int item;
int j = 2 * i;
item = a[i];
while (j <= l)
{
if ((j<l) && (a[j]<a[j+1]))
j = j+1;
if (item >= a[j])
break;
a[j/2] = a[j];

D. Subhashini, Assoc. prof


33
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

j = 2*j;
}
a[j/2] = item;
}

void heapify(int a[10],int n)


{
int i;
for (i=n/2;i>=1;i--)
adjust(a,i,n);
}

D. Subhashini, Assoc. prof


34
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Categories of Sorting
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.

The Complexity of Sorting Algorithms


The complexity of sorting algorithm calculates the running time of a function in which 'n'
number of items are to be sorted. The choice for which sorting method is suitable for a
problem depends on several dependency configurations for different problems. The
most noteworthy of these considerations are:

 The length of time spent by the programmer in programming a specific sorting program
 Amount of machine time necessary for running the program
 The amount of memory necessary for running the program

D. Subhashini, Assoc. prof


35
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

Merge sort
Merge sort is the algorithm which follows divide and conquer approach. Consider an
array A of n number of elements. The algorithm processes the elements in 3 steps.

1. If A Contains 0 or 1 elements then it is already sorted, otherwise, Divide A into


two sub-array of equal number of elements.

2. Conquer means sort the two sub-arrays recursively using the merge sort.

3. Combine the sub-arrays to form a single final sorted array maintaining the
ordering of the array.

The main idea behind merge sort is that, the short list takes less time to be sorted.

Complexity

Complexity Best case Average Case Worst Case

Time Complexity O(n log n) O(n log n) O(n log n)

Space Complexity O(n)

Example :
Consider the following array of 7 elements. Sort the array by using merge sort.

A = {10, 5, 2, 23, 45, 21, 7}

D. Subhashini, Assoc. prof


36
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

C Program
#include<stdio.h>
void mergeSort(int[],int,int);
void merge(int[],int,int,int);

void main ()
{
int n,a[10];
int i;
printf("Enter no. of elements");
scanf("%d",&n);
printf("\n enter data to be sored ..");
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
mergeSort(a,0,n);
printf("printing the sorted elements");

D. Subhashini, Assoc. prof


37
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

for(i=0;i<n;i++)
printf("\n%d\n",a[i]);
}

void mergeSort(int a[], int beg, int end)


{
int mid;
if(beg<end)
{
mid = (beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
}

void merge(int a[], int beg, int mid, int end)


{
int i=beg,j=mid+1,k,index = beg;
int temp[10];
while(i<=mid && j<=end)
{
if(a[i]<a[j])
{
temp[index] = a[i];
i = i+1;
}
else
{
temp[index] = a[j];
j = j+1;
}
index++;
}

D. Subhashini, Assoc. prof


38
Aurora’s Technological and Research Institute
Department of Computer Science and Engineering

if(i>mid)
{
while(j<=end)
{
temp[index] = a[j];
index++;
j++;
}
}
else
{
while(i<=mid)
{
temp[index] = a[i];
index++;
i++;
}
}
k = beg;
while(k<index)
{
a[k]=temp[k];
k++;
}
}

D. Subhashini, Assoc. prof

You might also like