-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph representation.cpp
More file actions
206 lines (179 loc) · 3.86 KB
/
graph representation.cpp
File metadata and controls
206 lines (179 loc) · 3.86 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//A structure to represent adjacency list node
struct AdjListNode
{
int dest;
AdjListNode * next;
};
//A structure to represent an adjacency list
struct AdjList
{
AdjListNode * head; //pointer to head node of the list
};
//A structure to represent a graph. A graph is an array of adjacency
//Size of array will be V (no. of vertices in graph)
struct Graph
{
int V;
AdjList * array;
};
//A utility fxn to create a new adjacency list node
AdjListNode * newAdjListNode(int dest)
{
AdjListNode * newNode = new AdjListNode;
newNode -> dest = dest;
newNode -> next = NULL;
return newNode;
}
//A utility fxn that creates a graph of V vertices
Graph * createGraph(int V)
{
Graph * graph = new Graph;
graph -> V = V;
//Create an array of adjacency lists. Size of array will be V
graph -> array = new AdjList [V];
//Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i)
{
graph -> array[i].head = NULL;
}
return graph;
}
//Adds an edge to an undirected graph
void addEdge(Graph * graph, int src, int dest)
{
//Add an edge from src to dest. A new node is added to the adjacency
//list of src. The node is added at the beginning
AdjListNode * newNode = newAdjListNode(dest);
newNode -> next = graph -> array[src].head;
graph -> array[src].head = newNode;
//Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode -> next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
//A utility function to print the adjacency list representation of graph
void printGraph(Graph * graph)
{
cout<<graph->V;
for (int v = 0; v < graph -> V; ++v)
{
AdjListNode *pCrawl = graph ->array[v].head;
cout<<"\n Adjacency list of vertex "<<v<<"\n head ";
while(pCrawl)
{
cout<<"-> "<<pCrawl -> dest;
pCrawl = pCrawl->next;
}
cout<<endl;
}
}
//Fxn to perform bfs on the graph
void bfs(Graph * graph, int start)
{
queue <int> q;
AdjListNode * temp;
int visited[graph->V];
for (int i = 0; i < graph->V; ++i)
{
visited[i] = 0;
}
cout<<"bfs ordering of the graph from vertex"<<start<<"is: ";
q.push(start);
visited[start] = 1;
while(!q.empty())
{
temp = graph->array[q.front()].head;
while(temp)
{
if(visited[temp->dest] == 0)
{
q.push(temp->dest);
visited[temp->dest] = 1;
}
temp = temp->next;
}
cout<<q.front();
q.pop();
}
}
void dfs(Graph * graph, int start)
{
stack <int> s;
AdjListNode * temp;
int visited[graph -> V];
for (int i = 0; i < graph-> V; ++i)
{
visited[i] = 0;
}
//cout<<"dfs ordering of the graph from vertex"<<start<<"is: ";
s.push(start);
visited[start] = 1;
while(!s.empty())
{
temp = graph->array[start].head;
while(temp)
{
if(visited[temp->dest] == 0)
{
visited[temp->dest] = 1;
cout<<temp->dest;
s.push(temp->dest);
start = temp->dest;
break;
}
else
{
temp= temp->next;
}
if(temp==NULL)
{
start = s.top();
s.pop();
}
}
}
}
void dfs_recurse(Graph * graph, int start, int visited[])
{
AdjListNode * temp;
visited[start] = 1;
cout<<start;
temp = graph->array[start].head;
while(temp)
{
if(visited[temp->dest] == 0)
dfs_recurse(graph, temp->dest, visited);
temp= temp->next;
}
}
int main()
{
int V = 5;
Graph * graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
//Print the adjacency list representation of the graph
//printGraph(graph);
//Print bfs implementation of the graph
//bfs(graph, 1);
//Print the dfs implementation of the graph
//Non-recursive implementation of bfs
//dfs(graph, 0);
int visited[V];
for (int i = 0; i < V; ++i)
{
visited[i] = 0;
}
//recursive implementation of bfs
dfs_recurse(graph, 0, visited);
return 0;
}