WMC Lab
WMC Lab
AIM :
WAP to create sensor nodes and establish communication between them.
THEORY :
A sensor network is a group of specialized devices (nodes) that detect environmental
conditions like temperature, humidity, pressure, etc., and share this data with other
nodes or a central hub. As sensor nodes may be randomly deployed in interest, they
must be aware of their locations to provide meaningful data to the users.
Components of WSN:
1. Sensors: Sensors in WSN are used to capture the environmental variables and
which are used for data acquisition. Sensor signals are converted into electrical
signals.
2. Radio Nodes: It is used to receive the data produced by the Sensors and sends
it to the WLAN access point. It consists of a microcontroller, transceiver,
external memory, and power source.
3. WLAN Access Point: It receives the data which is sent by the Radio nodes
wirelessly, generally through the internet.
4. Evaluation Software: The data received by the WLAN Access Point is processed
by a software called Evaluation Software for presenting the report to the users
for further processing of the data, which can be used for processing, analysis,
storage, and mining of the data.
CODE :
#include <bits/stdc++.h>
int a = 1;
if (a & v[i][j] == 0)
return false;
return true;
int main()
int sensor_count;
cout << "Enter the graph in adjacency matrix format to create connections : \n";
cout << "Is graph all nodes connected to each other : ";
if (check(v, sensor_count))
cout
<< "YES"
<< endl;
else
OUTPUT :
LEARNING OUTCOME :
• Wireless sensor networks are self-configured and infrastructure less/ad-hoc
networks.
• There is no central node. Each node must perform all the tasks such as sending,
receiving, and analyzing data.
• We learned how sensor nodes are connected in a network and how they
communicate with each other.
Experiment 2
AIM :
WAP to implement shortest path algorithm in a sensor network.
THEORY :
Some algorithm can be used here As Dijkstra’s algorithm and Bellman ford algorithm .
Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a
graph, which may represent, for example, road networks. It was conceived by
computer scientist Edsger W. Dijkstra in 1956 and published three years later.
The algorithm exists in many variants. Dijkstra's original algorithm found the shortest
path between two given nodes, but a more common variant fixes a single node as the
"source" node and finds the shortest paths from the source to all other nodes in the
graph, producing a shortest-path tree.
Dijkstra’s algorithm is the iterative algorithmic process to provide us with the shortest
path from one specific starting node to all other nodes of a graph. It is different from
the minimum spanning tree as the shortest distance among two vertices might not
involve all the graph's vertices.
It is important to note that Dijkstra’s algorithm is only applicable when all weights are
positive because, during the execution, the weights of the edges are added to find
the shortest path.
Bellman-Ford algorithm finds the minimum distance from the source vertex to any
other vertex. The main difference between this algorithm with Dijkstra’s algorithm is
in Dijkstra’s algorithm, we cannot handle the negative weight, but here we can handle
it easily.
CODE :
#include <bits/stdc++.h>
if (currentVertex == NO_PARENT)
return;
showThePath(parents[currentVertex], parents);
if (vertexIndex != startVertex)
showThePath(vertexIndex, parents);
minDistances[startNode] = 0;
parentNode[startNode] = NO_PARENT;
for (int step = 0; step < numNodes - 1; step++)
currentNode = i;
currentMin = minDistances[i];
visited[currentNode] = true;
parentNode[j] = currentNode;
// Driver Code
int main()
vector<vector<int>> adjacencyMatrix =
{0, 6, 0, 7, 5, 4, 0, 0, 2},
int src;
return 0;
OUTPUT :
LEARNING OUTCOME :
• Graphs are used to model connections between objects, people, or entities.
They have two main elements: nodes and edges. Nodes represent objects and
edges represent the connections between these objects.
• Dijkstra's Algorithm finds the shortest path between a given node (which is
called the "source node") and all other nodes in a graph.
• This algorithm uses the weights of the edges to find the path that minimizes
the total distance (weight) between the source node and all other nodes.
Experiment 3
AIM :
WAP to implement Distance Vector Routing Protocol
THEORY :
In distance-vector routing (DVR), each router is required to inform the topology
changes to its neighbouring routers periodically. Historically it is known as the old
ARPNET routing algorithm or Bellman-Ford algorithm.
How the DVR Protocol Works: In DVR, each router maintains a routing table. It
contains only one entry for each router. It includes two parts − a preferred outgoing
line to use for that destination and an estimate of time (delay). Tables are updated by
exchanging the information with the neighbour’s nodes.
Each router knows the delay in reaching its neighbours (Ex − sending echo requests).
Routers periodically exchange routing tables with each of their neighbours. It
compares the delay in its local table with the delay in the neighbour’s table and the
cost of reaching that neighbour. If the path via the neighbour has a lower cost, then
the router updates its local table to forward packets to the neighbour .
CODE :
#include <bits/stdc++.h>
int n = graph.size();
{
cout << (char)('A' + i);
if (graph[i][j] == INT_MAX)
printf("%3d", -1);
else
printf("%3d", graph[i][j]);
int n = ans.size();
printGraphOfAdjacencyMatrix(ans);
ans[start_node][j] = prev[start_node][j];
int y = neighbores[start_node][t];
int val;
val = INT_MAX;
else
val = prev[start_node][y] + prev[y][j];
if (k + 2 > 3)
number_prefix = "th";
cout << "After " << k + 2 << number_prefix << " iteration : \n\n";
printGraphOfAdjacencyMatrix(ans);
int main()
vector<vector<int>> graph_step = {{0, inf, inf, 1}, {1, 1, 1, 7}, {inf, 7, inf, 6}, {12, 8, inf, 8}};
int sz = graph_step.size();
vector<int> neighbors[sz];
if (graph_step[i][j] != INT_MAX)
neighbors[i].push_back(j);
}
solve(graph_step, neighbors);
return 0;
OUTPUT :
LEARNING OUTCOME :
1) DVR protocol uses Bellman ford algorithm to find the shortest path to the
destination node.
2) In the distance vector routing protocol, only distance vector information is
exchanged and next-hop values are not exchanged.
3) The process of updating the routing table keeps on repeating periodically to
update the shortest path in case the link goes down or there is a topology
change.
4) Distance vector routing faces an account-to-infinity prob.
Experiment 4
AIM :
WAP to implement Link State Routing Protocol
THEORY :
Link state routing is a technique in which each router shares the knowledge of its
neighbourhood with every other router on the internetwork.
The three keys to understanding the Link State Routing algorithm:
1. Knowledge about the neighbourhood: Instead of sending its routing table, a
router sends the information about its neighbourhood only. A router
broadcasts its identities and cost of the directly attached links to other routers.
2. Flooding: Each router sends the information to every other router on the
internetwork except its neighbours. This process is known as Flooding. Every
router that receives the packet sends copies to all its neighbours. Finally, each
and every router receives a copy of the same information.
3. Information sharing: A router sends the information to every other router only
when the change occurs in the information.
CODE :
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
dist[src] = 0;
parent[src] = -1;
for (int c = 0; c < V - 1; c++)
{
int minVal = inf, minIndex;
for (int i = 0; i < V; i++)
{
if (flag[i] == false and dist[i] < minVal)
{
minIndex = i;
minVal = dist[i];
}
}
int u = minIndex;
flag[u] = true;
for (int v = 0; v < V; v++)
{
if (!flag[v] and graph[u][v] != inf and dist[u] + graph[u][v] < dist[v])
{
dist[v] = dist[u] + graph[u][v];
parent[v] = u;
}
}
}
cout << "\nSPT for router " << ch[src] << ":\n";
printSPT(dist, parent);
return {parent, dist};
}
return 0;
}
OUTPUT :
LEARNING OUTCOME :
• The Link State Routing protocol uses Dijkstra’s Algorithm to find the shortest
path from source to destination.
• The link state routing algorithm exchanges information only when there is a
change in the connection.
• It requires large memory as it maintains a routing database.
• It requires the computation of the shortest path, which is an overhead for the
CPU.
The information of each router needs to be transmitted all over the network.
Experiment 5
AIM :
WAP to implement AODV Routing Protocol
THEORY :
An Ad Hoc On-Demand Distance Vector (AODV) is a routing protocol designed for
wireless and mobile ad hoc networks. This protocol establishes routes to destinations
on demand and supports both unicast and multicast routing. The AODV protocol was
jointly developed by the Nokia Research Centre, the University of California, Santa
Barbara, and the University of Cincinnati in 1991.
The AODV protocol builds routes between nodes only if they are requested by source
nodes. AODV is therefore considered an on-demand algorithm and does not create
any extra traffic for communication along links. The routes are maintained as long as
the sources require them. They also form trees to connect multicast group members.
AODV makes use of sequence numbers to ensure route freshness. They are self-
starting and loop-free, besides scaling to numerous mobile nodes.
The advantage of source routing is: intermediate nodes do not need to maintain up-
to-date routing information to route the packets they forward.
CODE :
#include <bits/stdc++.h>
if (currentVertex == NO_PARENT)
return;
showThePath(parents[currentVertex], parents);
cout << currentVertex << " ";
showThePath(destination, parents);
minDistances[startNode] = 0;
parentNode[startNode] = NO_PARENT;
currentNode = i;
currentMin = minDistances[i];
}
}
visited[currentNode] = true;
parentNode[j] = currentNode;
// Driver Code
int main()
vector<vector<int>> adjacencyMatrix =
{0, 6, 0, 7, 5, 4, 0, 0, 2},
return 0;
OUTPUT :
LEARNING OUTCOME :
• In AODV, nodes discover routes in request-response cycles.
• A node requests a route to a destination by broadcasting an RREQ message to
all its neighbours.
• When a node receives an RREQ message but does not have a route to the
requested destination, it in turn broadcasts the RREQ message.
• Also, it remembers a reverse-route to the requesting node which can be used
to forward subsequent responses to this RREQ.
• This process repeats until the RREQ reaches a node that has a valid route to the
destination
Experiment 6
AIM :
WAP to implement DSDV Routing Protocol.
THEORY :
The full form of DSDV is: Destination Sequenced Distance Vector Routing Destination
Sequenced Distance Vector (DSDV) is a hop-by-hop vector routing protocol requiring
each node to periodically broadcast routing updates. This is a table-driven algorithm
based on modifications made to the Bellman-Ford routing mechanism. Each node in
the network maintains a routing table that has entries for each of the destinations in
the network and the number of hops required to reach each of them. Each entry has
a sequence number associated with it that helps in identifying stale entries.
This mechanism allows the protocol to avoid the formation of routing loops. Each
node periodically sends updates tagged throughout the network with a
monotonically increasing even sequence number to advertise its location. New route
broadcasts contain the address of the destination, the number of hops to reach the
destination, and the sequence number of the information received regarding the
destination, as well as a new sequence number unique to the broadcast. The route
labelled with the most recent sequence number is always used. When the neighbours
of the transmitting node receive this update, they recognize that they are one hop
away from the source node and include this information in their distance vectors.
CODE :
#include <bits/stdc++.h>
int dis[V];
dis[i] = INT_MAX;
dis[src] = 0;
int x = graph[i][0];
int y = graph[i][1];
cout << "Vertex Distance from Source and sequence number" << endl;
if (dis[i] != INT_MAX)
cout << i << "\t\t" << dis[i] << "\t\t1" << endl;
else
<< "~"
int main()
int V = 5;
int E = 8;
int graph[][3] = {{0, 1, -1}, {0, 2, 4}, {1, 2, 3}, {1, 3, 2}, {1, 4, 2}, {3, 2, 5}, {3, 1, 1}, {4, 3, -3}};
BellmanFord(graph, V, E, 0);
BellmanFord(graph, V, E, 1);
BellmanFord(graph, V, E, 2);
cout << "\nRouting table for node 3" << endl;
BellmanFord(graph, V, E, 3);
BellmanFord(graph, V, E, 4);
return 0;
OUTPUT :
LEARNING OUTCOME :
• Cannot be implemented commercially or on a larger scale.
• Not suitable for many networks which are dynamic in nature. Slower protocol
processing time .
Experiment 7
AIM :
WAP to implement Dynamic Source Routing Protocol .
THEORY :
The Dynamic Source Routing protocol (DSR) is a simple and efficient routing protocol
designed specifically for use in multi-hop wireless ad hoc networks of mobile nodes.
DSR allows the network to be completely self-organizing and self-configuring without
needing any existing network infrastructure or administration.
It is a reactive protocol, and all aspects of the protocol operate entirely on-demand
basis. It works on the concept of source routing. Source routing is a routing technique
in which the sender of a packet determines the complete sequence of nodes through
which the packets are forwarded.
The advantage of source routing is: intermediate nodes do not need to maintain up-
to date routing information to route the packets they forward. The protocol
comprises the two main mechanisms of "Route Discovery" and "Route Maintenance."
DSR requires each node to maintain a route – cache of all known self–to–destination
pairs. If a node has a packet to send, it attempts to use this cache to deliver the
packet.
If the destination does not exist in the cache, then a route discovery phase is initiated
to discover a route to the destination by sending a route request. This request
includes the destination address, source address, and unique identification number. If
a route is available from the route – cache but is not valid anymore, a route
maintenance procedure may be initiated.
A node processes the route request packet only if it has not previously processed the
packet and its address is not present in the route cache. A route reply is generated by
the destination or by any of the intermediate nodes when it knows how to reach the
destination .
CODE :
#include <bits/stdc++.h>
return min_id;
vector<int> path;
path.push_back(dest);
dest = from[dest];
path.push_back(dest);
cout << "From source = " << src << " to node " << destination << " : Path = ";
if (i != 0)
else
{
int dist[vertices];
int from[vertices];
bool sptSet[vertices];
dist[source] = 0;
from[source] = -1;
sptSet[u] = true;
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
from[v] = u;
int main()
int vertices = 9;
int temp_graph[9][9] = {
{1, 0, 1, 0, 1, 0, 0, 1, 0},
{1, 0, 1, 0, 1, 0, 0, 0, 1},
{0, 1, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 1, 1, 0, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 0, 1, 0},
{1, 1, 1, 0, 0, 1, 0, 0, 1},
{0, 0, 1, 0, 0, 0, 0, 1, 1}};
graph_step[i][j] = temp_graph[i][j];
delete[] graph_step[i];
delete[] graph_step;
return 0;
OUTPUT :
LEARNING OUTCOME :
• Routers are maintained only between nodes That need to communicate. This
reduces the overhead of route maintenance.
• Route caching can further reduce route discovery overhead.
• A single route Discovery may yield routes to the destination due to
intermediate nodes replying from local caches.
• Stale caches will lead to increased overhead.
Experiment 8
AIM :
WAP to implement Leach Protocol.
THEORY :
Leach Protocol stands for Low Energy Adaptive Clustering Hierarchy. In Clustered
Network Architecture, Sensor Nodes autonomously clubs into groups called clusters.
It is based on the Leach Protocol which makes use of clusters.
Properties of Leach Protocol:
• It is a 2-tier hierarchy clustering architecture.
• It is a distributed algorithm for organizing the sensor nodes into groups called
clusters.
• The cluster head nodes in each of the autonomously formed clusters create the
Time-division multiple access (TDMA) schedules.
• It makes use of the concept called Data Fusion which makes it energy efficient.
CODE :
#include <iostream>
#include <vector>
#include <string>
int main()
{
int num_node;
string graph_part;
graph_step[i][j] = Inf;
if (graph_part != "I")
graph_step[i][j] = stoi(graph_part);
int num_clust;
vector<int> cluster_heading(num_clust);
vector<vector<int>> clusters_group(num_clust);
for (int i = 0; i < num_clust; ++i)
cout << "Enter The members of cluster " << i + 1 << ": " << flush;
string x;
while (true)
cin >> x;
if (x == "N")
break;
clusters_group[i].push_back(stoi(x));
cout << "Enter the batteries of the nodes: " << flush;
vector<int> battery(num_node);
int num_iter;
cout << "Iteration " << i + 1 << endl; // Pooling of new cluster head
cluster_heading[i] = head;
clusters_group[i].clear();
cout << "node" << cluster_head << " " << flush;
nearest_cluster = cluster;
clusters_group[nearest_cluster].push_back(i);
cout << "node" << node << " " << flush;
battery[cluster_head] -= (int)clusters_group[i].size();
return 0;
OUTPUT :
Enter the number of nodes: 4
124I
10 20 10 I
30 40 50 60
120 1 2 1
Node 0: 100
Node 1: 50
Node 2: 75
Node 3: 95
Cluster 1: Node 0
Cluster 2: Node 3
Cluster Details:
Cluster 2: Node 3
Updating Battery Levels...
Node 0: 97
Node 1: 50
Node 2: 75
Node 3: 94
Cluster 1: Node 0
Cluster 2: Node 3
Cluster Details:
Cluster 2: Node 3
Cluster 1: Node 0
Cluster 2: Node 3
Cluster 1: Node 0
Cluster 2: Node 3
Cluster Details:
Cluster 2: Node 3
Battery Levels of All Nodes:
Node 0: 94
Node 1: 50
Node 2: 75
Node 3: 93