Jesh wants to implement a program for basic distance vector routing in a computer network.
Given the number of nodes and an adjacency matrix representing link costs, determine the distance vector routing table for each node starting from a specified source node. Output the node number, distance from the source, and the next hop for each node.
Input format:
The first line of input is an integer N representing the number of nodes in the network.
The next N lines of input consists of N space-separated integers each, representing the adjacency matrix. Each integer is either the cost of the link or -1 if there is no direct link.
The last line of input consists of an integer source representing the source node for the algorithm.
Output format:
The first line of output displays "Distance Vector Routing Table:", indicating that the following lines will contain the routing table information.Output format:
The first line of output displays "Distance Vector Routing Table:", indicating that the following lines will contain the routing table information.
The next line of output should display "Node", "Distance", and "Next Hop are column headers.
Each subsequent line contains the information for a single node, with three space-separated values: the node number, the distance from the source node, and the next hop node.
Refer to the sample output for the formatting specifications.
Code constraints:
In this scenario, the given test cases will fall under the following constraints:
15 N510
Cost of link s 10Sample test cases:
give me the code in c
Assistant
Sure! Below is a C program that implements a basic Distance Vector Routing algorithm using the Bellman-Ford approach. This program reads the number of nodes, the adjacency matrix representing the link costs, and the source node. It then calculates the shortest distance from the source to every other node and determines the next hop for each node in the routing table.
Program Explanation:
Input Handling:
Number of Nodes (N): The program first reads an integer N representing the number of nodes in the network.
Adjacency Matrix: It then reads an N x N adjacency matrix where each element represents the cost of the link between nodes. A value of -1 indicates that there is no direct link between the nodes, which is treated as infinite cost (INF).
Source Node: Finally, the program reads the source node from which the routing tables will be calculated.
Initialization:
Distance Array: An array distance[N+1] is initialized to store the minimum distance from the source to each node. All distances are initially set to INF except for the source node, which is set to 0.
Next Hop Array: An array next_hop[N+1] is used to keep track of the next hop node for each destination node. Initially, it's set to -1 for all nodes.
Bellman-Ford Algorithm:
The algorithm iteratively relaxes all edges N-1 times. For each node, it checks all its neighbors and updates the distance and next hop if a shorter path is found through that neighbor.
Output:
The program prints the "Distance Vector Routing Table" with headers "Node", "Distance", and "Next Hop".
For each node, it displays the node number, the computed distance from the source, and the next hop node required to reach that node from the source.
Node 1: It's the source node, so the distance is 0 and the next hop is itself (1).
Node 2: Directly connected to the source with a cost of 1. Next hop is 2.
Node 3: The shortest path from node 1 is through node 2 with a total cost of 2. Thus, the next hop is 2.
Node 4: The shortest path from node 1 is through node 3 with a total cost of 3. Thus, the next hop is 3.
Notes:
The program assumes that nodes are numbered starting from 1 to N.
If a node is not reachable from the source, its distance will remain INF (represented by 1000000), and the next hop is displayed as -.
This implementation uses the Bellman-Ford algorithm, which works well for networks with potential negative weight edges (though in typical routing scenarios, link costs are non-negative).
Feel free to test the program with different inputs to verify its correctness!