Link-State Routing Algorithm
The Link-State Routing Algorithm is a dynamic routing protocol that ensures all routers in a network have a consistent and complete view of the network's topology. Each router uses this information to compute the shortest path to every other router using Dijkstra's Algorithm.
Key Steps in Link-State Routing
Neighbor Discovery:
- Each router identifies its directly connected neighbors and the cost (link metrics) to reach them.
- This is done using a protocol such as Hello packets.
Link-State Advertisement (LSA) Flooding:
- Each router creates a Link-State Packet (LSP) containing:
- Its ID.
- List of neighbors and their respective link costs.
- The LSP is flooded across the entire network, ensuring all routers receive the same topology information.
- Each router creates a Link-State Packet (LSP) containing:
Topology Database Creation:
- Each router constructs a Link-State Database (LSDB), which represents the entire network topology.
- This database is built using the received LSAs from all routers.
Shortest Path Calculation (Dijkstra's Algorithm):
- Using the LSDB, each router computes the shortest path tree (SPT) with itself as the root.
- The shortest path is computed using Dijkstra's Algorithm, which finds the least-cost path to each destination.
Forwarding Table Construction:
- Based on the SPT, the router builds its forwarding table, indicating the next hop to reach each destination.
C Program: Link-State Routing Algorithm
#include <stdio.h>
#include <limits.h>
#define MAX 10
#define INF 9999
void dijkstra(int graph[MAX][MAX], int nodes, int start) {
int cost[MAX][MAX], distance[MAX], visited[MAX], next_hop[MAX];
int i, j, count, min_distance, next_node;
// Initialize cost matrix
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
if (graph[i][j] == 0) {
cost[i][j] = INF;
} else {
cost[i][j] = graph[i][j];
}
}
}
// Initialize distance, visited, and next_hop arrays
for (i = 0; i < nodes; i++) {
distance[i] = cost[start][i];
visited[i] = 0;
if (distance[i] != INF && i != start) {
next_hop[i] = i; // Initially, the next hop is the direct neighbor
} else {
next_hop[i] = -1; // No route yet
}
}
distance[start] = 0;
visited[start] = 1;
count = 1;
// Main loop to calculate shortest paths
while (count < nodes - 1) {
min_distance = INF;
next_node = -1;
// Find the node with the smallest tentative distance
for (i = 0; i < nodes; i++) {
if (!visited[i] && distance[i] < min_distance) {
min_distance = distance[i];
next_node = i;
}
}
if (next_node == -1) {
break; // No more reachable nodes
}
visited[next_node] = 1;
// Update distances for neighbors of the next node
for (i = 0; i < nodes; i++) {
if (!visited[i] && cost[next_node][i] != INF) {
if (distance[next_node] + cost[next_node][i] < distance[i]) {
distance[i] = distance[next_node] + cost[next_node][i];
next_hop[i] = next_hop[next_node]; // Update next hop
}
}
}
count++;
}
// Print the results
printf("\nRouting Table for Router %d:\n", start + 1);
printf("Destination\tCost\tNext Hop\n");
for (i = 0; i < nodes; i++) {
if (i != start) {
printf("%d\t\t%d\t", i + 1, distance[i]);
if (next_hop[i] != -1) {
printf("%d\n", next_hop[i] + 1);
} else {
printf("No route\n");
}
}
}
}
int main() {
int graph[MAX][MAX], nodes, i, j, start;
printf("Enter the number of routers: ");
scanf("%d", &nodes);
printf("Enter the cost adjacency matrix (use 0 for no direct link):\n");
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
scanf("%d", &graph[i][j]);
}
}
printf("Enter the source router: ");
scanf("%d", &start);
dijkstra(graph, nodes, start - 1);
return 0;
}
Try The Following
Output:
Enter the number of routers: 4
Enter the cost adjacency matrix (use 0 for no direct link):
0 1 4 0
1 0 2 6
4 2 0 3
0 6 3 0
Enter the source router: 1
Routing Table for Router 1:
Destination Cost Next Hop
2 1 2
3 3 2
4 6 2
Program Explanation
Graph Input:
- The user enters the adjacency matrix, where:
graph[i][j]
represents the cost of the link between routeri
and routerj
.- Use
0
to indicate no direct connection between routers.
- The user enters the adjacency matrix, where:
Cost Matrix Initialization:
- The
cost
matrix is derived from the adjacency matrix. Ifgraph[i][j] == 0
, the cost is set toINF
(9999).
- The
Dijkstra's Algorithm:
- Initialization:
- The
distance
array stores the shortest known distances from the source to each router. - The
visited
array tracks whether a router's shortest path has been finalized. - The
next_hop
array tracks the next hop to reach each destination.
- The
- Iteration:
- Find the nearest unvisited router.
- Update distances to its neighbors if a shorter path is found.
- Termination:
- Stop when all reachable routers have been visited.
- Initialization:
Routing Table:
- For each destination, the table shows:
- Destination router.
- Total cost of the shortest path.
- The next hop router on the shortest path.
- For each destination, the table shows:
Comments
Post a Comment