-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
184 lines (116 loc) · 2.83 KB
/
Graph.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int matrix [5][5],int No_Ofvertices){
printf("\nMatrix is\n");
for(int i=0; i<No_Ofvertices;i++){
for (int j=0;j<No_Ofvertices;j++){
printf("value at %d row %d column is %d\n",i,j,matrix[i][j]);
}
}
}
int index(int vertex,int* arr,int No_Ofvertices){
int i;
for (i=0;i<No_Ofvertices;i++){
if(arr[i]==vertex){
break;
}
}
return i;
}
void Adjacent (int vertex,int * arr,int No_Ofvertices,int matrix [5][5]){
int row =index(vertex,arr,No_Ofvertices);
int index=0;
int Adjacent [No_Ofvertices];
for(int j=0;j<No_Ofvertices;j++){
if(matrix[row][j]==1){
Adjacent[index]=j;
index++;
}
}
if(index!=0){
printf("\nAdjacent Vertices Are\n");
for (int i=0;i<index;i++){
int ind =Adjacent[i];
printf("%d\n",arr[ind]);
}
}
else{
printf("\nVertex %d does not possess any Adjacent Vertex\n",vertex);
}
}
void insertEdge (int vertex1, int vertex2,int *arr,int No_Ofvertices,int matrix[5][5]){
int row = index(vertex1,arr,No_Ofvertices);
int col= index(vertex2,arr,No_Ofvertices);
matrix[row][col]=1;
matrix[col][row]=1;
}
void deleteEdge (int vertex1, int vertex2,int *arr,int No_Ofvertices,int matrix[5][5]){
int row = index(vertex1,arr,No_Ofvertices);
int col= index(vertex2,arr,No_Ofvertices);
matrix[row][col]=0;
matrix[col][row]=0;
}
int main(){
int vertex1,vertex2;
int No_Ofvertices;
printf("How many Vertices you want to create\n");
scanf("%d",&No_Ofvertices);
int size = 4*(No_Ofvertices);
int *arr= (int*)malloc(No_Ofvertices *sizeof(int));
int matrix [5][5];
int count=0;
int i;
for(i=0; i<5;i++){
printf("Enter data for %d Vertices\n",++count);
scanf("%d",&arr[i]);
}
printf("Vertices are\n");
for(int i=0; i<No_Ofvertices;i++){
printf("%d\n",arr[i]);
}
for(int i=0; i<5;i++){
for (int j=0;j<5;j++){
matrix[i][j]=0;
}
}
bool logout=true;
while(logout==true){
int num;
int choice;
int cont,repeat,data;
printf("\nSelect Menu!\n");
printf("1.Insert Edge\n2.Delete Edge\n3.Display\n4.Adjacent Vertex\n5.Logout\n");
printf("\nYour Selection is :");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\nEnter the starting vertex\n");
scanf("%d",&vertex1);
printf("\nEnter the Adjacent vertex\n");
scanf("%d",&vertex2);
insertEdge (vertex1,vertex2,arr,No_Ofvertices,matrix);
break;
case 2 :
printf("\nEnter the starting vertex\n");
scanf("%d",&vertex1);
printf("\nEnter the Adjacent vertex\n");
scanf("%d",&vertex2);
deleteEdge (vertex1,vertex2,arr,No_Ofvertices,matrix);
break;
case 3 :
display(matrix,No_Ofvertices);
break;
case 4:
printf("\nEnter the starting vertex\n");
scanf("%d",&vertex1);
Adjacent (vertex1,arr,No_Ofvertices,matrix);
break;
case 5:
logout=false;
break;
default :
printf("Invalid Selection!\n");
}
}
}