-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph1.cpp
148 lines (137 loc) · 3.23 KB
/
graph1.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
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct node *A[10];
// Adjacency list
struct node
{
int vertex;
struct node *next;
};
void create(struct node *A[])
{
struct node *p, *temp, *temp2;
int v1, v2, ch;
do
{
cout << "Enter v1 v2" << endl;
cin >> v1 >> v2;
temp = (struct node *)malloc(sizeof(struct node ));
temp->vertex = v2;
temp->next = nullptr;
p = A[v1];
if (p == nullptr)
{
A[v1] = temp;
}
else
{
while (p->next != nullptr)
{
p = p->next;
p->next = temp;
temp2 = (struct node *)malloc(sizeof(struct node *));
temp2->vertex = v1;
temp2->next = NULL;
p = A[v2];
if (p == nullptr)
{
A[v2] = temp2;
}
else
{
while (p->next != nullptr)
{
p = p->next;
p->next = temp2;
}
cout << "Enter 1 : Yes 2 : No" << endl;
cin >> ch;
}
}
}
} while (ch == 1);
};
void BFS(node *a[], int start, int n)
{
int visited[10] = {0}, v;
queue<int> q;
visited[start] = 1;
q.push(start);
cout << "\nBFS Traversal: ";
while (!q.empty())
{
int v = q.front();
q.pop();
cout << v << " ";
struct node *p = A[v];
while (p != nullptr)
{
if (!visited[p->vertex])
{
visited[p->vertex] = 1;
q.push(p->vertex);
}
p = p->next;
}
}
cout << endl;
}
void display(struct node *A[], int vertices)
{
cout << "\nAdjacency List Representation:\n";
for (int i = 0; i < vertices; i++)
{
cout << i << " -> ";
struct node *temp = A[i];
while (temp != nullptr)
{
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}
}
void DFS(struct node *A[], int start, int vertices)
{
int visited[10] = {0};
stack<int> s;
s.push(start);
cout << "\nDFS Traversal: ";
while (!s.empty())
{
int v = s.top();
s.pop();
if (!visited[v])
{
cout << v << " ";
visited[v] = 1;
}
struct node *p = A[v];
while (p != nullptr)
{
if (!visited[p->vertex])
{
s.push(p->vertex);
}
p = p->next;
}
}
cout << endl;
}
int main()
{
int vertices, start;
cout << "Enter the number of vertices (max 10): ";
cin >> vertices;
create(A);
display(A, vertices);
cout << "Enter starting vertex for BFS traversal: ";
cin >> start;
BFS(A, start, vertices);
cout << "Enter starting vertex for DFS traversal: ";
cin >> start;
DFS(A, start, vertices);
return 0;
}