-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path17-Graph.c
More file actions
125 lines (108 loc) · 2 KB
/
17-Graph.c
File metadata and controls
125 lines (108 loc) · 2 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 13
int adj[SIZE][SIZE], q[SIZE], rear=0, front=0;
bool visited[SIZE];
void addEdge(int x, int y)
{
adj[x][y] = 1;
adj[y][x] = 1;
}
void enqueue(int element)
{
if(rear == SIZE-1)
printf("Queue overflow\n");
else
{
if(front == 0)
front=1;
rear++;
q[rear] = element;
}
}
int isQEmpty()
{
if(front==0 || front>rear)
return 1;
else
return 0;
}
int dequeue()
{
int deleted;
if(front==0 || front>rear)
{
printf("Queue Underflow\n");
exit(0);
}
else
{
deleted = q[front];
front++;
return deleted;
}
}
void DFS(int src)
{
visited[src] = true;
printf("%d ", src);
for(int i=0; i<SIZE; i++)
{
if(adj[src][i]==1 && !visited[i])
DFS(i);
}
}
void BFS(int src)
{
visited[src] = true;
enqueue(src);
while(!isQEmpty())
{
src = dequeue();
printf("%d ", src);
for(int i=0;i<SIZE;i++)
{
if(adj[src][i]==1 && !visited[i])
{
visited[i] = true;
enqueue(i);
}
}
}
}
void main()
{
int src;
addEdge(1,2);
addEdge(1,4);
addEdge(2,3);
addEdge(2,4);
addEdge(2,6);
addEdge(4,5);
addEdge(4,7);
addEdge(5,7);
addEdge(6,3);
addEdge(6,8);
addEdge(6,9);
addEdge(7,10);
addEdge(7,11);
addEdge(8,9);
addEdge(9,10);
addEdge(9,12);
addEdge(10,12);
for(int i=0; i<SIZE; i++)
visited[i] = false;
printf("Enter the starting vertex for Depth First Search\n");
scanf("%d", &src);
printf("\nDepth First Search:\n");
DFS(src);
printf("\n");
for(int i=0; i<SIZE; i++)
visited[i] = false;
printf("\nEnter the starting vertex for Breadth First Search\n");
scanf("%d", &src);
printf("\nBreadth First Search:\n");
BFS(src);
printf("\n");
}