-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjMatrix.h
114 lines (87 loc) · 2.63 KB
/
adjMatrix.h
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<stdlib.h>
int* initializeGraph(int*);
void displayGraph(int*, int);
int inputGraph(int*, int);
int* initializeGraph(int *v){
int i, j, ver, *adjMat;
while(1){
printf("Enter the number of vertices: ");
scanf("%d", &ver);
if(ver>0) break;
printf("Invalid number of vertices given!\n");
}
(*v) = ver;
adjMat=(int*)malloc(ver*ver*sizeof(int)); // adjacency Matrix created
printf("The vertices are: ");
for(i=0; i<ver; i++){
if(i!=0) printf(", ");
printf("%d", i);
for(j=0; j<ver; j++)
*(adjMat+(i*ver)+j)=0; // adjMat[i][j]=0
}
printf("\n");
return adjMat;
}
void displayGraph(int *adjMat, int v){
if(adjMat==NULL || v<=0) return; // invalid matrix for display [ not initialised ]
int i, j;
printf("\nAdjacency Matrix\n");
for(i=0; i<v; i++){
for(j=0; j<v; j++)
printf("%d\t", *(adjMat+(i*v)+j) ); // adjMat[i][j]
printf("\n");
}
printf("\n");
}
int inputGraph(int *adjMat, int v){
if(adjMat==NULL || v<=0) return 0; // invalid matrix for display [ not initialised ]
int choice, s, e, w;
printf("\n1. Undirected-Unweighted\n");
printf("2. Undirected-Weighted\n");
printf("3. Directed-Unweighted\n");
printf("4. Directed-Weighted\n");
printf("5. Exit\n");
while(1){
printf("Enter choice (-999 to stop): ");
scanf("%d", &choice);
if(choice>0 && choice<6) break;
printf("Invalid choice!\n");
}
if(choice==5) return 0; // 0 means no graph given
printf("\n");
while(1){
// TAKING start and end vertices from user
while(1){ // taking start vertex from user
printf("Enter start vertex: ");
scanf("%d", &s);
if(s==-999) return choice; // no more edge to add therefore return
if(s>=0 && s<v) break;
printf("Invalid start vertex!\n");
}
while(1){ // taking end vertex from user
printf("Enter end vertex: ");
scanf("%d", &e);
if(e>=0 && e<v) {
break; // un-comment this line to allow self loop
if(s!=e) break; // self loop not allowed
}
printf("Invalid end vertex!\n");
}
if(choice==1 || choice==3){ // unweighted
*(adjMat+(s*v)+e)=1; // adjMat[s][e]=1
if(choice==1) *(adjMat+(e*v)+s)=1; // adjMat[e][s]=1 undirected-unweighted
}
else { // weighted [ choice=2 OR choice=4 ]
while(1){ // taking start vertex from user
printf("Enter weight: ");
scanf("%d", &w);
if(w>0) break;
printf("Invalid weight!\n");
}
*(adjMat+(s*v)+e)=w; // adjMat[s][e]=weight
if(choice==2) *(adjMat+(e*v)+s)=w; // adjMat[e][s]=weight undirected-weighted
}
}
return choice;
}