-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph Representations
More file actions
57 lines (48 loc) · 1.06 KB
/
Graph Representations
File metadata and controls
57 lines (48 loc) · 1.06 KB
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
The First step of graph is representing the nodes and edges in the matrix form.
Write a program to obtain the adjacency matrix representation of a graph .
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void matrix(int **,int);
int main()
{
int n,e,i,**w,c,b,we;
char ch[5];
printf("Please enter the number of nodes in the graph\n");
scanf("%d",&n);
printf("Please enter the number of edges in the graph\n");
scanf("%d",&e);
w=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
{
*(w+i)=(int *)calloc(n,sizeof(int));
}
printf("Is the graph directed\n");
scanf("%s",ch);
for(i=0;i<e;i++){
printf("Enter the start node, end node and weight of edge no %d\n",i);
scanf("%d%d%d",&c,&b,&we);
if(strcmp("yes",ch)!=0)
{
w[c][b] = we;
w[b][c] = we;
}
else
{
w[c][b] = we;
}
} matrix(w,n);
return 0;
}
void matrix(int **w,int n)
{
int i,j;
printf("\n\nAdjacency Matrix Representation:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
printf("%d ",w[i][j]);
}
printf("\n");
}
}