Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

Gautam Buddha University

INTERNET OF THINGS (IOT) LAB


Course-Code: IT481

Department of Computer Science and Engineering


School of Information, Communication and Technology
(SOICT)
Gautam Buddha University
Greater Noida

Submitted To: Submitted By:


Mr. Ashutosh Dixit Ujjwal Baliyan
19/BCS/106
INDEX
Progra Name of Program Date Teacher’s
m No. Signature
01 Matrix addition and 09/02/2022
multiplication
02 Bubble Sort 16/02/2022
03 Binary search using Divide 23/02/2022
and Conquer technique
04 Merge sort 02/03/2022
05 Quick sort 09/03/2022
06 Insertion sort 30/03/2022
07 Breadth First Search 20/04/2022
08 Depth First Search 04/05/2022

Experiment No: 1
Aim: Matrix Addition and Multiplication

A)Matrix Addition
public class Main {
public static void main (String args[]){
int a [] [] = {{1,3,4}, {2,4,3}, {3,4,5}};
int b [] [] = {{1,3,4}, {2,4,3}, {1,2,4}};

int c [] [] =new int [3][3];

for (int i=0; i<3; i++) {


for (int j=0; j<3; j++) {
c[i] [j]=a[i] [j]+b[i] [j];
System.out.println(c[i] [j]+" ");
}}
}}

OUTPUT

B) Matrix Multiplication
public class Main {
public static void main (String args []) {

int a [] [] = {{1,1,1}, {2,2,2}, {3,3,3}};


int b [] [] = {{1,1,1}, {2,2,2}, {3,3,3}};

int c [] [] =new int [3][3];

for (int i=0; i<3; i++) {


for (int j=0; j<3; j++) {
c[i] [j]=0;
for (int k=0; k<3; k++)
{
c[i] [j]+=a[i] [k]* b[k][j];
}//end of k loop
System.out.println(c[i] [j]+");
}}
}}

OUTPUT:

Experiment No: 2

Aim: Bubble Sorting


Program:
Public class Main {
public static void main (String args []) {
int [] a= {11,45,23,67,54,6,2,10};
int flag=0;
int temp;
for (int j=0; j<a.length; j++) {
for (int i=0; i<a.length -1-i; i++) {
if (a[i]>a[i+1]) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
flag=1;
}}
if (flag==0) {
break;
}}
System.out.println(Arrays.toString(a));
}}

OUTPUT:

Experiment: 3
Aim: Binary Search using Divide and Conquer Technique.
Program:
Public class Main {
public static void main (String args []) {
int [] a = {2,3,7,9,12,14,16,17,19,20,24,28};
Scanner sc = new Scanner (System.in);
int item=sc.nextInt();
int li=0;
int hi= a.length -1;
while (li<=hi) {
int mid = (li+hi)/2;
if (a[mid] == item) {
System.out.println (“Element is present at” + mid+ “index
position”); break;
}
else if (a[mid]<item) {
li=mid+1;}
else {
hi=mid-1;
}
Mid= (li+hi)/2; }
if (li>hi) {
System.out.println(“Element is not present int the list”);
}}}

OUTPUT:

Experiment No: 4
Aim: Merge Sorting
Program:
Public class Merge_sort {
public static void main (String [] args) {
int [] a= {48,36,13,52,19,94, 21};
Merge_sort m= new Merge_sort();
int n =a.length;
System.out.println("\nbefore sorting array elements are: ");
m.printArray(a,n);
m.mergeSort (a,0, n-1);
System.out.println("\nafter sorting array elements are: ");
m.printArray(a,n);
System.out.println(" "); }
void mergeSort(int []a , int b, int e){
if(b<e) {
int m=(b+e)/2;
mergeSort(a,b,m);
mergeSort(a,m+1, e);
sort(a,b,m,e); } }
public void sort (int [] a, int b, int m, int e) {
int n1=m-b+1;
int n2= e-m;
int [] leftArray= new int[n1];
int [] rightArray= new int[n2];
for (int i=0; i<n1; i++) {
leftArray[i]=a[b+i]; }
for (int j=0; j<n2; j++) {
rightArray[j]=a[m+j+1];
}
int i=0, j=0, k=b;
while (i<n1 && j<n2) {
if(leftArray[i]<=rightArray [j]) {
a[k]=leftArray[i];
i++;
}
else {
a[k] = rightArray[j];
j++;
}
k++;
}

while(i<n1) {
a[k]=leftArray[i];
i++;
k++;
}
while (j<n2) {
a[k]=rightArray[j];
j++;
k++;
}}
void printArray (int [] a, int n) {
for (int i=0; i<n; i++) {
System.out.print(a[i]+" ");
}}}

OUTPUT

Experiment No: 5

Aim: Quick Sorting


Program:
public class Quick_sort {
int partition (int [] a, int s, int e) {
int pivot=a[e];
int i=s-1;
for (int j=s; j<=e-1; j++) {
if(a[j]<pivot) {
i++;
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
int t=a[i+1];
a[i+1] =a[e];
a[e]=t;
return i+1;
}

void quick (int [] a, int s, int e) {


if(s<e) {
int p= partition (a, s, e);
quick (a, s, p-1);
quick (a, p+1, e);
}
}
void printArray(int a[], int n){
for (int i=0; i<n; i++) {
System.out.print(a[i]+" ");
}
}

public static void main (String [] args) {


int [] a = {13,18,27,2,19,25};
int n=a.length;
System.out.println("Element before sorting: ");
Quick_sort m=new Quick_sort();
m.printArray(a,n);
m.quick(a,0,n-1);
System.out.println("Elements after sorting: ");
m.printArray(a,n);
System.out.println();

}
}

OUTPUT:

Experiment No: 6

Aim: Insertion Sorting

Program:
import java.util.*;
public class Insertion_sort_Integer {
public static void main (String [] args) {
int [] a = {1,25,7,4,3,8,6};
int temp,j;
for (int i=1; i<a.length; i++) {
temp=a[i];
j=i;
while (j>0 && a[j-1]>temp) {
a[j]=a[j-1];
j=j-1;
}
a[j]=temp;
}
System.out.println(Arrays.toString(a));
}
}

OUTPUT:

Experiment No: 7
Aim: BFS

Program:
import java.io.*;
import java.util.*;

// This class represents a directed graph using adjacency list


// representation
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists

// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}

// Function to add an edge into the graph


void addEdge(int v,int w)
{
adj[v].add(w);
}

// prints BFS traversal from a given source s


void BFS(int s)
{
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];

// Create a queue for BFS


LinkedList<Integer> queue = new LinkedList<Integer>();
// Mark the current node as visited and enqueue it
visited[s]=true;
queue.add(s);

while (queue.size() != 0)
{
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");

// Get all adjacent vertices of the dequeued vertex s


// If a adjacent has not been visited, then mark it
// visited and enqueue it
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}

// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

System.out.println("Following is Breadth First Traversal "+


"(starting from vertex 2)");

g.BFS(2);
}
}

Output:

Experiment No: 8
Aim: DFS

Program:
import java.io.*;
import java.util.*;
class Graph {
private int V; // No. of vertices
private LinkedList<Integer> adj[];
@SuppressWarnings("unchecked") Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v, int w)
{
adj[v].add(w); // Add w to v's list.
}
void DFSUtil(int v, boolean visited[])
{
visited[v] = true;
System.out.print(v + " ");
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
void DFS(int v)
{
boolean visited[] = new boolean[V];
DFSUtil(v, visited);
}
public static void main(String args[])
{
Graph g = new Graph(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

System.out.println(
"Following is Depth First Traversal "
+ "(starting from vertex 2)");

g.DFS(2);
}
}

Output:

Experiment No: 9

Aim: Prims Algorithm

Program:
import java.io.*;
import java.util.*;
class Graph {
import java.util.*;
import java.lang.*;
import java.io.*;

class MST {
// Number of vertices in the graph
private static final int V = 5;
int minKey(int key[], Boolean mstSet[])
{
// Initialize min value
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}
void printMST(int parent[], int graph[][])
{
System.out.println("Edge \tWeight");
for (int i = 1; i < V; i++)
System.out.println(parent[i] + " - " + i + "\t" + graph[i]
[parent[i]]);
}
void primMST(int graph[][])
{
// Array to store constructed MST
int parent[] = new int[V];

// Key values used to pick minimum weight edge in cut


int key[] = new int[V];
// To represent set of vertices included in MST
Boolean mstSet[] = new Boolean[V];

// Initialize all keys as INFINITE


for (int i = 0; i < V; i++) {
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
}

// Always include first 1st vertex in MST.


key[0] = 0; // Make key 0 so that this vertex is
// picked as first vertex
parent[0] = -1; // First node is always root of MST

// The MST will have V vertices


for (int count = 0; count < V - 1; count++) {
// Pick thd minimum key vertex from the set of vertices
// not yet included in MST
int u = minKey(key, mstSet);

// Add the picked vertex to the MST Set


mstSet[u] = true;

for (int v = 0; v < V; v++){


if (graph[u][v] != 0 && mstSet[v] == false &&
graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
printMST(parent, graph);
}

public static void main(String[] args)


{

MST t = new MST();


int graph[][] = new int[][] { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };

// Print the solution


t.primMST(graph);
}
}
}

Output:

Experiment No- 10

Aim: Krushkal

Program:
import java.io.*;
import java.util.*;
class Graph {
import java.util.*;
import java.lang.*;
import java.io.*;
class MST {
private static final int V = 5;
int minKey(int key[], Boolean mstSet[])
{
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}
void printMST(int parent[], int graph[][])
{
System.out.println("Edge \tWeight");
for (int i = 1; i < V; i++)
System.out.println(parent[i] + " - " + i + "\t" + graph[i]
[parent[i]]);
}
void primMST(int graph[][])
{
// Array to store constructed MST
int parent[] = new int[V];
int key[] = new int[V];
Boolean mstSet[] = new Boolean[V];
for (int i = 0; i < V; i++) {
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);

// Add the picked vertex to the MST Set


mstSet[u] = true;
for (int v = 0; v < V; v++)

// graph[u][v] is non zero only for adjacent vertices of


m
// mstSet[v] is false for vertices not yet included in
MST
// Update the key only if graph[u][v] is smaller than
key[v]
if (graph[u][v] != 0 && mstSet[v] == false &&
graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}

// print the constructed MST


printMST(parent, graph);
}

public static void main(String[] args)


{

MST t = new MST();


int graph[][] = new int[][] { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };

// Print the solution


t.primMST(graph);
}
}
}

Output:

Experiment No: 11

Aim: Dijkstra

Program:
import java.util.*;
import java.lang.*;
import java.io.*;

class ShortestPath {
// A utility function to find the vertex with minimum distance
value,
// from the set of vertices not yet included in shortest path tree
static final int V = 9;
int minDistance(int dist[], Boolean sptSet[])
{
// Initialize min value
int min = Integer.MAX_VALUE, min_index = -1;

for (int v = 0; v < V; v++)


if (sptSet[v] == false && dist[v] <= min) {
min = dist[v];
min_index = v;
}

return min_index;
}

// A utility function to print the constructed distance array


void printSolution(int dist[])
{
System.out.println("Vertex \t\t Distance from Source");
for (int i = 0; i < V; i++)
System.out.println(i + " \t\t " + dist[i]);
}
void dijkstra(int graph[][], int src)
{
Boolean sptSet[] = new Boolean[V];

// Initialize all distances as INFINITE and stpSet[] as false


for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}

// Distance of source vertex from itself is always 0


dist[src] = 0;

// Find shortest path for all vertices


int u = minDistance(dist, sptSet);

// Mark the picked vertex as processed


sptSet[u] = true;

// Update dist value of the adjacent vertices of the


// picked vertex.
for (int v = 0; v < V; v++)

if (!sptSet[v] && graph[u][v] != 0 && dist[u] !=


Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

// print the constructed distance array


printSolution(dist);
}

// Driver method
public static void main(String[] args)
{
/* Let us create the example graph discussed above */
int graph[][] = new int[][] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
ShortestPath t = new ShortestPath();
t.dijkstra(graph, 0);
}
}

Output:

You might also like