Repository navigation
Expand file tree
/
Copy pathgraph.c
More file actions
161 lines (144 loc) · 3.75 KB
/
Copy pathgraph.c
File metadata and controls
161 lines (144 loc) · 3.75 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
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICIES 100
#define QUEUE_SIZE 100
//implementing unweighted directed graphs
//using adjacency matrix
typedef struct Graph{
int numVertices;
int adjMatrix[MAX_VERTICIES][MAX_VERTICIES];
}Graph;
typedef struct Queue{
int items[QUEUE_SIZE];
int front;
int rear;
}Queue;
void initQueue(Queue* q){
q->front = -1;
q->rear = -1;
}
int isEmpty(Queue* q){
return q->front == -1;
}
void enqueue(Queue* q, int value){
if(q->front == -1){
q->front = value;
}
q->rear++;
q->items[q->rear] = value;
}
int dequeue(Queue* q){
int item = q->items[q->front];
q->front++;
if(q->front > q->rear){
q->front = q->rear = -1;
}
return item;
}
void initGraph(Graph* g, int vertices){
g->numVertices = vertices;
for(int i = 0; i < vertices; i++){
for(int j = 0; j < vertices; j++){
g->adjMatrix[i][j] = 0;
}
}
}
//Add a directed edge from src to dest
void addEdge(Graph* g, int src, int dest){
if(src >= g->numVertices || dest >= g->numVertices){
printf("invalid vertex");
return;
}else{
g->adjMatrix[src][dest] = 1;
}
}
void removeEdge(Graph* g, int src, int dest){
if(src >= g->numVertices || dest >= g->numVertices){
printf("invalid vertex");
return;
}else{
g->adjMatrix[src][dest] = 0;
}
}
int hasEdge(Graph* g, int src, int dest){
if(src >= g->numVertices || dest >= g->numVertices){
return 0;
}
return g->adjMatrix[src][dest];
}
void printGraph(Graph* g){
printf("\nAdjacency Matrix:\n");
printf(" ");
for(int i = 0; i < g->numVertices; i++){
printf("%2d ", i);
}
printf("\n");
for(int i = 0; i < g->numVertices; i++){
printf("%2d ", i);
for(int j = 0; j < g->numVertices; j++){
printf("%2d ", g->adjMatrix[i][j]);
}
printf("\n");
}
}
// get number of outgoing edges from vertex
int getOutDegree(Graph* g, int vertex){
int count = 0;
for(int i = 0; i < g->numVertices; i++){
if(g->adjMatrix[vertex][i] == 1){
count++;
}
}
return count;
}
int getInDegree(Graph* g, int vertex){
int count = 0;
for(int i = 0; i < g->numVertices; i++){
if(g->adjMatrix[i][vertex] == 1){
count++;
}
}
return count;
}
void BFS(Graph* g, int startVertex) {
// Create visited array
int visited[MAX_VERTICIES] = {0}; // All false initially
// Create queue
Queue q;
initQueue(&q);
// Mark start vertex as visited and enqueue it
visited[startVertex] = 1;
enqueue(&q, startVertex);
printf("BFS starting from vertex %d: ", startVertex);
while(!isEmpty(&q)) {
// Dequeue a vertex
int currentVertex = dequeue(&q);
printf("%d ", currentVertex);
// Get all adjacent vertices
for(int i = 0; i < g->numVertices; i++) {
// If there's an edge and vertex not visited
if(g->adjMatrix[currentVertex][i] == 1 && !visited[i]) {
visited[i] = 1;
enqueue(&q, i);
}
}
}
printf("\n");
}
int main(){
Graph g;
initGraph(&g, 5);
// Create directed edges: 0→1, 0→2, 1→3, 2→3, 3→4
addEdge(&g, 0, 1);
addEdge(&g, 0, 2);
addEdge(&g, 1, 3);
addEdge(&g, 2, 3);
addEdge(&g, 3, 4);
printGraph(&g);
printf("\nOutdegree of vertex 0: %d\n", getOutDegree(&g, 0));
printf("Indegree of vertex 3: %d\n", getInDegree(&g, 3));
printf("\nEdge 0→1 exists? %s\n", hasEdge(&g, 0, 1) ? "Yes" : "No");
printf("Edge 1→0 exists? %s\n", hasEdge(&g, 1, 0) ? "Yes" : "No");
BFS(&g,0 );
return 0;
}