-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupC_Practical13b.cpp
124 lines (115 loc) · 2.84 KB
/
GroupC_Practical13b.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
/*
Experiment 13 : Represent a given grapg using adjacency matrix/list to perform DFS and using adjacency list to perform BFS.
Use the map of the area around the college as a graph. Identify the prominent land marks as nodes and perform DFS and BFS on that.
Adjecency List
*/
#include <iostream>
using namespace std;
#define MAX 10
#define TRUE 1
#define FALSE 0
// declaring an adjacency list for storing the graph
class lgra
{
private:
struct node1
{
int vertex;
struct node1 *next;
};
node1 *head[MAX];
int visited[MAX];
public:
//static int nodecount;
lgra();
void create();
void dfs(int);
};
//constructor
lgra::lgra()
{
int v1;
for (v1 = 0; v1 < MAX; v1++)
visited[v1] = FALSE;
for (v1 = 0; v1 < MAX; v1++)
head[v1] = NULL;
}
void lgra::create()
{
int v1, v2;
char ans;
node1 *N, *first;
cout << "Enter the vertices no. beginning with 0 : ";
do
{
cout << "\nEnter the Edge of a graph : \n";
cin >> v1 >> v2;
if (v1 >= MAX || v2 >= MAX)
cout << "Invalid Vertex Value!!\n";
else
{
//creating link from v1 to v2
N = new node1;
if (N == NULL)
cout << "Insufficient Memory!!\n";
N->vertex = v2;
N->next = NULL;
first = head[v1];
if (first == NULL)
head[v1] = N;
else
{
while (first->next != NULL)
first = first->next;
first->next = N;
}
//creating link from v2 to v1
N = new node1;
if (N == NULL)
cout << "Insufficient Memory!!\n";
N->vertex = v1;
N->next = NULL;
first = head[v2];
if (first == NULL)
head[v2] = N;
else
{
while (first->next != NULL)
first = first->next;
first->next = N;
}
}
cout << "\n Want to add more edges?(y/n) : ";
cin >> ans;
} while (ans == 'y');
}
//dfs function
void lgra::dfs(int v1)
{
node1 *first;
cout << endl
<< v1;
visited[v1] = TRUE;
first = head[v1];
while (first != NULL)
if (visited[first->vertex] == FALSE)
dfs(first->vertex);
else
first = first->next;
}
int main()
{
int v1;
lgra g;
g.create();
cout << endl << "Enter the vertex from where you want to traverse : ";
cin >> v1;
if (v1 >= MAX)
cout << "Invalid Vertex!!\n";
else
{
cout << "The Dfs of the graph : ";
g.dfs(v1);
}
return 0;
}