Unit 05 DS
Unit 05 DS
Unit 05 DS
➢ V = {1,2,3,4,5,6}
➢ E = {{1,2},{1,5},{2,3},{2,5},{3,4},{4,5},{4,6}}
Graph Terminology
Directed Graph:
A pair of vertices between the edges must be ordered then that graph
is known as directed graph
Weighted Graph :
Some graphs contain weights on their edges, such type of graphs is called
as weighted graph and the number associated with an edge is called as
weight.
The weight of an edge is often referred to as the "cost" of the edge.
In applications, the weight may be a measure of the length of a
route, the capacity of a line, the energy required to move between
locations along a route, etc.
Complete graph :
• A graph in which every vertex is directly connected to every other
vertex.
Path :
A path in a graph represents a way to get from an origin to a destination by
traversing edges in the graph. For example, in the undirected graph G=(V,E)
drawn below, there are many paths from node 6 to node 1.
Isolated Node:
In a graph if a node that does not adjacent to any other node then that node
is called as an isolated node.
Null Graph
•The graph containing only isolated nodes is called as a Null graph.
2. Incidence Matrix
In this representation, graph can be represented using a matrix of size total number of vertices by total
number of edges. That means if a graph with 4 vertices and 6 edges can be represented using a matrix
of 4X6 class.
In this matrix, rows represent vertices and columns represent edges.
This matrix is filled with either 0 or 1 or -1.
Here, 0 represents row edge is not connected to column vertex, 1 represents row edge is connected as
outgoing edge to column vertex and -1 represents row edge is connected as incoming edge to column
vertex.
3. Adjacency List
• In this representation, every vertex of graph contains list of its adjacent vertices. For example,
consider the following directed graph representation implemented using linked list.
Q. What is Graph Traversal? Explain different Graph Traversal
techniques. Graph Traversal:
Graph traversal is technique used for searching a vertex in a graph. The graph traversal is also used to decide
the order of vertices to be visit 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 vertices of 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)
BFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph
without any loops. We use Queue data structure with maximum size of total number of vertices in the graph
Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
Step 3: Visit all the adjacent vertices of the vertex which is at front of the Queue which is not visited and
Step 4: When there is no new vertex to be visit from the vertex at front of the Queue then delete that vertex
Step 6: When queue becomes Empty, then produce final spanning tree by removing unused edges from the
graph
SEARCHINGS AND SORTINGS
Binary search algorithm finds given element in a list of elements with O(log n) time complexity where
n is total number of elements in the list. The binary search algorithm can be used with only sorted list of
element. That means, binary search can be used only with list of element which are already arranged in an
order. The binary search cannot be used for list of element which are in random order.
1. Read the search element from the user
2. Find the middle element in the sorted list
3. Compare, the search element with the middle element in the sorted list.
4. If both are matching, then display "Given element found!!!" and terminate the function
5. If both are not matching, then check whether the search element is smaller or larger than middle element.
6. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5 for the left sub list
of the middle element.
7. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the right sub list
of the middle element.
8. Repeat the same process until we find the search element in the list or until sublist contains only
one element.
9. If that element also doesn't match with the search element, then display "Element not found in the
list!!!" and terminate the function.
Example 1
Example 2
/*Binary
Search*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, low, high, mid, n, search,
a[100]; clrscr();
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( i = 0 ; i < n ; i++ )
scanf("%d",&a[i]);
printf("Enter value to find\n");
scanf("%d",&search);
low = 0;
high = n - 1;
mid= (low+high)/2;
while( low <= high )
{
if ( a[mid] < search )
low = mid+ 1;
else if ( a[mid] == search )
{
printf("%d found at location %d.\n", search, mid+1);
break;
}
else
high = mid- 1;
mid= (low + high)/2;
}
if ( low> high )
printf("Not found! %d is not present in the list.\n", search);
getch();
}
First Pass
Sorting will start from the initial two elements. Let compare them to check which is greater.
Here, 32 is greater than 13 (32 > 13), so it is already sorted. Now, compare 32 with 26.
Here, 26 is smaller than 36. So, swapping is required. After swapping new array will look like -
Here, 35 is greater than 32. So, there is no swapping required as they are already sorted.
Now, the comparison will be in between 35 and 10.
Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now, we reach at the end of the
array. After first pass, the array will be -
Here, 10 is smaller than 32. So, swapping is required. After swapping, the array will be -
/*Bubble Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp,n;
clrscr();
printf("\n Enter the size of the array \n");
scanf("%d",&n);
printf("\n Enter the Array Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=n-1; i<n; i++)
for(j=0; j<i; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("Array after sorting is\n");
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
getch();
}
2. Compare minimum with the second element. If the second element is smaller than minimum, assign
the second element as minimum.
Compare
minimum with the third element. Again, if the third element is smaller, then
assign
minimum to the third element otherwise do nothing. The process goes on until the last
element.
3. After each iteration, minimum is placed in the front of the unsorted list.
4. For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until
all the elements are placed at their correct positions.
Write a program for Selection sort
/*Selection Sort*/
#include <stdio.h>
#include<conio.h>
void main()
{
int a[100], n, i, j,temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d Numbers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Sorted Array is :\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
getch();
}
Example:2