-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directed_Weighted Incidence Matrix.c
68 lines (60 loc) · 1.22 KB
/
Directed_Weighted Incidence Matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Problem statement - Implementation of directed, weighted graph using Incidence matrix
*/
#include <stdio.h>
#define MAX 10
void main()
{
//Declare necessary variables
int inc[MAX][MAX],i=0,j=0,v=0,e=0,w=0,n1=0,n2=0;
//Accept the number of nodes
printf("Enter the no of vertices : ");
scanf("%d",&v);
//Accept the number edges
printf("Enter the no of edges : ");
scanf("%d",&e);
//Initializing the incidence matrix
for(i=0;i<v;i++)
{
for(j=0;j<e;j++)
inc[i][j]=9999;
}
//Create the matrix
for (i=0;i<e;i++)
{
printf("Enter the end vertices of edge %d along with the edge weight : ", i+1);
scanf("%d %d %d",&n1,&n2,&w);
n1--;
n2--;
if(w<1)
{
printf("Unacceptable edge weight. Enter again\n");
i--;
}
else
{
if(((n1>=0)&&(n1<v))&&(n2<v)&&(n2>=0))
{
inc[n1][i]=w;
inc[n2][i]=-w;
}
else
{
printf("Unavailable Vertex. Enter again\n");
i--;
}
}
}
//Displaying the matrix
printf("\n\nThe incidence matrix is :\n\n \t");
for(i=0;i<e;i++)
printf(" E%d\t",i+1);
printf("\n\n");
for(i=0;i<v;i++)
{
printf(" V%d\t",i+1);
for(j=0;j<e;j++)
printf(" %d\t",inc[i][j]);
printf("\n\n");
}
}