Take an example subnet of hosts and obtain a broadcast tree for the subnet.
Implement Broadcast Tree
Input Graph:
Source Code:
File Name: broadcast.c
#include<stdio.h>
#define MAX 10
int adjacencyMatrix[MAX][MAX];
int numNodes;
// Function to display adjacent nodes of a given node
void displayAdjacentNodes(int rootNode) {
int i;
printf("\nAdjacent nodes of node %d:\n", rootNode);
for (i = 1; i <= numNodes; i++) {
if (adjacencyMatrix[rootNode][i] == 1 || adjacencyMatrix[i][rootNode] == 1) {
printf("%d\t", i);
}
}
printf("\n");
}
int main() {
int i, j, rootNode;
// Input number of nodes
printf("Enter number of nodes: ");
scanf("%d", &numNodes);
// Input the adjacency matrix
printf("Enter adjacency matrix:\n");
for (i = 1; i <= numNodes; i++) {
for (j = 1; j <= numNodes; j++) {
printf("Is there a connection from node %d to node %d (1=yes, 0=no): ", i, j);
scanf("%d", &adjacencyMatrix[i][j]);
}
}
// Input the root node
printf("Enter root node (1 to %d): ", numNodes);
scanf("%d", &rootNode);
// Display adjacent nodes
displayAdjacentNodes(rootNode);
return 0;
}
Output:
$ gcc broadcast.c
$ ./a.out
Enter number of nodes: 5
Enter adjacency matrix:
Is there a connection from node 1 to node 1 (1=yes, 0=no): 0
Is there a connection from node 1 to node 2 (1=yes, 0=no): 1
Is there a connection from node 1 to node 3 (1=yes, 0=no): 0
Is there a connection from node 1 to node 4 (1=yes, 0=no): 1
Is there a connection from node 1 to node 5 (1=yes, 0=no): 0
Is there a connection from node 2 to node 1 (1=yes, 0=no): 1
Is there a connection from node 2 to node 2 (1=yes, 0=no): 0
Is there a connection from node 2 to node 3 (1=yes, 0=no): 1
Is there a connection from node 2 to node 4 (1=yes, 0=no): 0
Is there a connection from node 2 to node 5 (1=yes, 0=no): 1
Is there a connection from node 3 to node 1 (1=yes, 0=no): 0
Is there a connection from node 3 to node 2 (1=yes, 0=no): 1
Is there a connection from node 3 to node 3 (1=yes, 0=no): 0
Is there a connection from node 3 to node 4 (1=yes, 0=no): 1
Is there a connection from node 3 to node 5 (1=yes, 0=no): 0
Is there a connection from node 4 to node 1 (1=yes, 0=no): 1
Is there a connection from node 4 to node 2 (1=yes, 0=no): 0
Is there a connection from node 4 to node 3 (1=yes, 0=no): 1
Is there a connection from node 4 to node 4 (1=yes, 0=no): 0
Is there a connection from node 4 to node 5 (1=yes, 0=no): 1
Is there a connection from node 5 to node 1 (1=yes, 0=no): 0
Is there a connection from node 5 to node 2 (1=yes, 0=no): 1
Is there a connection from node 5 to node 3 (1=yes, 0=no): 0
Is there a connection from node 5 to node 4 (1=yes, 0=no): 1
Is there a connection from node 5 to node 5 (1=yes, 0=no): 0
Enter root node (1 to 5): 4
Adjacent nodes of node 4:
1 3 5
๐ป Run This C Program
Take an example subnet of hosts and obtain a broadcast tree for the subnet.
The selected C program is loaded
automatically into the editor.
Edit the program, provide input through
STDIN if required
and click Run.
The compiler is embedded using OneCompiler.
Your C program runs inside the embedded
C environment; Study Glance does not execute
C code directly on your web server.
Enter the input values of the program in the
STDIN box before clicking Run.