Problem Statement: Methodology:: Write A Program in C To Represent A Communication Network and

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Problem statement: Write a program in C to represent a communication network and

print its Adjacency Matrix.

Methodology:
Design: A communication network is a set of nodes interconnected by links. It can have unidirectional or bidirectional links. Links have specific cost associated with it based on several factors. These factors include distance between nodes, bandwidth of the link etc. If it is bidirectional link, then the cost associated in either directions of the link is the same, if it is undirected both the cost may not be the same and each direction may have different costs associated. The network is represented in this program using an adjacency matrix. An adjacency matrix is a means of representing which vertices or nodes of a graph are adjacent to which other vertices. The adjacency matrix of a finite graph G on n vertices is the n n matrix where the non-diagonal entry aij is the cost from vertex i to vertex j, and the diagonal entry aii, depending on the convention, is either zero or infinity from vertex i to itself. Implementation: The code is implemented by making functions to initialize the adjacency matrix, take the input for both the unidirectional and bidirectional cases. For bidirectional case, user has to input the cost between any two nodes only once. Hence, the adjacency matrix of the network with bidirected links is symmetric. Another function is written to give the output in matrix form. The main function calls all the above functions.

Discussions:
1. The user is asked to enter the cost between two nodes. But if two nodes are not connected, user has to unnecessarily input the cost. This situation is handled by asking whether nodes are connected or not. 2. If the links are bidirectional, then only one input is asked, either cost from i to j or from j to i. 3. The max size of the adjacency matrix is taken as 20X20, but it can be extended to 100X100 or even larger as per user requirements.

Results: The output is the adjacency matrix, for the network shown below:

9999 2 9999 9999 9999 9999 8

2 9999 5 9999 9999 7 9999

9999 5 9999 24 9999 9999 9999

9999 9999 24 9999 1 3 9999

9999 9999 9999 1 9999 9999 6

9999 7 9999 3 9999 9999 9

8 9999 9999 9999 6 9 9999

Conclusion:Thus, any communication network can be represented by an unique


Adjacency matrix using this program, with its elements as the cost of the links of that network.

Code section:
#include<stdio.h> #include<math.h>

int nodes,mat[20][20]; void matdefine() { int a,b; for(a=1;a<=nodes;a++) { for(b=1;b<=nodes;b++) { mat[a][b]=9999; } } } void matrbi() { int m,n,temp,s; printf("enter the cost between nodes:\n"); for(m=1;m<=nodes;m++) { for(n=m+1;n<=nodes;n++) { printf("If following nodes are connected: %d and %d, press 1 and 0:if not connected",m,n); scanf("%d",&s); if(s==1) // for bidirectional matrix // to define all matrix elements to 9999

printf("enter the cost between %d and %d:",m,n); scanf("%d",&temp); mat[m][n]=mat[n][m]=temp;

} } } } void matruni() { int m,n,temp,s; printf("enter the cost between nodes:\n"); for(m=1;m<=nodes;m++) { for(n=1;n<=nodes;n++) { if(m==n) { goto down; } else { printf("press 1: following nodes are connected: %d and %d and 0: not connected",m,n); scanf("%d",&s); if(s==1) { // for unidirectional matrix

printf("enter the cost between %d and %d:",m,n); scanf("%d",&mat[m][n]);} } down: ; } } } void matrout() { printf("the adjacency matrix is:\n"); int i,j; for(i=1;i<=nodes;i++) { printf("\n"); //to display matrix

for(j=1;j<=nodes;j++) { printf("%d\t",mat[i][j]); } } } void main() { int x; printf("enter the number of nodes"); scanf("%d",&nodes); matdefine(); printf("enter 1 for unidirectional graph or 2 for bidirectional graph"); scanf("%d",&x);

if(x==1) { matruni(); } else { matrbi(); } matrout(); } -------------------------------------------------------------------------------------------------------------------------------

Sample output:
enter the number of nodes7 enter 1 for unidirectional graph or 2 for bidirectional graph2 enter the cost between nodes: If following nodes are connected: 1 and 2, press 1 and 0:if not connected1 enter the cost between 1 and 2:2 If following nodes are connected: 1 and 3, press 1 and 0:if not connected0 If following nodes are connected: 1 and 4, press 1 and 0:if not connected0 If following nodes are connected: 1 and 5, press 1 and 0:if not connected0 If following nodes are connected: 1 and 6, press 1 and 0:if not connected0 If following nodes are connected: 1 and 7, press 1 and 0:if not connected1 enter the cost between 1 and 7:8 If following nodes are connected: 2 and 3, press 1 and 0:if not connected1 enter the cost between 2 and 3:5 If following nodes are connected: 2 and 4, press 1 and 0:if not connected0 If following nodes are connected: 2 and 5, press 1 and 0:if not connected0

If following nodes are connected: 2 and 6, press 1 and 0:if not connected1 enter the cost between 2 and 6:7 If following nodes are connected: 2 and 7, press 1 and 0:if not connected0 If following nodes are connected: 3 and 4, press 1 and 0:if not connected1 enter the cost between 3 and 4:24 If following nodes are connected: 3 and 5, press 1 and 0:if not connected0 If following nodes are connected: 3 and 6, press 1 and 0:if not connected0 If following nodes are connected: 3 and 7, press 1 and 0:if not connected0 If following nodes are connected: 4 and 5, press 1 and 0:if not connected1 enter the cost between 4 and 5:1 If following nodes are connected: 4 and 6, press 1 and 0:if not connected1 enter the cost between 4 and 6:3 If following nodes are connected: 4 and 7, press 1 and 0:if not connected0 If following nodes are connected: 5 and 6, press 1 and 0:if not connected0 If following nodes are connected: 5 and 7, press 1 and 0:if not connected1 enter the cost between 5 and 7:6 If following nodes are connected: 6 and 7, press 1 and 0:if not connected1 enter the cost between 6 and 7:9 the adjacency matrix is: 9999 2 9999 9999 9999 9999 8 2 9999 5 9999 9999 7 9999 9999 5 9999 24 9999 9999 9999 3 9999 9999 9999 24 9999 1 9999 9999 9999 1 9999 9999 6 7 9999 3 9999 8

9999 9999 9999 9999 6 9 9999

9999 9

You might also like