-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransposeGraph.cpp
More file actions
274 lines (242 loc) · 6.58 KB
/
Copy pathTransposeGraph.cpp
File metadata and controls
274 lines (242 loc) · 6.58 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//DFS using STACK
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
using namespace std;
//static int time;
//Creating the structure for the Node vertices
struct Node{
int key;
char color;
struct Node* p;
int d_time; //discovered time
int f_time;//finishing time
struct Node *next;
};
//Creating a Graph structure which contains an array of node vertices
struct Graph{
int V;
struct Node** Array;
};
//An utility function to create new vertices node
struct Node* newNode(int _key)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->key=_key;
newNode->color='W';//white
newNode->p=NULL;
newNode->d_time=0;
newNode->f_time=0;
newNode->next=NULL;
return newNode;
};
//An utility function to create a Graph by passing the no of vertices needed
struct Graph* createGraph(int _v){
struct Graph* graph=(struct Graph*)malloc(sizeof(struct Graph));
graph->V=_v;
graph->Array=(struct Node **)malloc(sizeof(struct Node* )*graph->V); //check the diff between bfs as well
int i;
//The above thing we cant do as we havent declared memory for the pointers present in the array.So we have to first allocate memory for them.
for(i=0;i<_v;i++)
{
graph->Array[i]=(struct Node *)malloc(sizeof(struct Node));
}
for(i=0;i<graph->V;i++){
graph->Array[i]->key=-1;
graph->Array[i]->color='W';
graph->Array[i]->p=NULL;
graph->Array[i]->d_time=0;
graph->Array[i]->f_time=0;
graph->Array[i]->next=NULL;
}
return graph;
};
void addVertices(struct Graph* graph,int _key)
{
graph->Array[_key]->key=_key;
}
void addEdge(struct Graph* graph,int src,int dest)
{
struct Node* newnode=newNode(dest);//(struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newnode->next=graph->Array[src]->next;
graph->Array[src]->next=newnode;
}
struct Graph* TransposeGraph(struct Graph* g)
{
// struct Graph* gT=(struct Graph*)malloc(sizeof(Graph));
// gT->V=g->V;
// gT->Array=(struct Node **)malloc(sizeof(Node *)*gT->V);
// int i;
// for(i=0;i<gT->V;i++)
// {
// gT->Array[i]=(struct Node*)malloc(sizeof(struct Node));
// };
struct Graph* gT;
gT= createGraph(g->V);
int i,src,dest;
for(i=0;i<gT->V;i++)
{
addVertices(gT,i);
}
for(i=0;i<gT->V;i++)
{
struct Node* pCrawl = g->Array[i]->next;
while (pCrawl)
{
//gT->Array[pCrawl->key]=
src=pCrawl->key;
dest=i;
// cout<<src<<endl;
addEdge(gT,src,dest);
pCrawl = pCrawl->next;
}
}
return gT;
};
struct Node* findVertex(Graph *g,int key)
{
return g->Array[key];
}
void CopyAttributes(Graph *g,Node* srcNode){
for(int i=0;i<g->V;i++){
struct Node* head=g->Array[i];
while(head)
{
if(head->key==srcNode->key)
{
head->color=srcNode->color;
head->d_time=srcNode->d_time;
head->f_time=srcNode->f_time;
head->p=srcNode->p;//cant do next because it would be pointing to a different address
// cout<<"-"<<head->color<<"-";
//cout<<"-"<<srcNode->color<<"-";
}
head=head->next;
}
}
//cout<<endl;
}
/*
void DFS(struct Graph* G)
{
static int time=0;
}
*/
void DFS_Visit(struct Graph* G,int _u)
{
struct Node* u=findVertex(G,_u);
struct Node* v,*x;
static int time=0;//even without static will do as we are not making recursive call
stack<Node *>Stack;
time+=1;
//u->key=_u;
u->d_time=time;
u->color='G';
//Since this the first node we have to this
CopyAttributes(G,u);
Stack.push(u);
cout<<u->key<<" ";
//v=Stack.top();
while(!Stack.empty())
{
v=Stack.top();
struct Node *u_next=G->Array[v->key]->next;
while(u_next!=NULL && u_next->color!='W')
u_next=u_next->next;
if(u_next && u_next->color=='W') //These two nested if 's can be combined into one as if(cond1&&cond2);
{
cout<<(u_next->key)<<" ";//<<u_next->color<<"+ ";
u_next->color='G';
u_next->p=v;
u_next->d_time=time+1;
CopyAttributes(G,u_next);
//findVertex(G,u_next->key)->d_time=u_next->d_time;
//cout<<"B"<<endl;
Stack.push(u_next);
}else{
Stack.pop();
//cout<<v->key<<"?";
time=time+1;
v->f_time=time;//u_next->f_time=time;//x->f_time=time;//v->f_time=time;//x->f_time=time;
v->color='B';//u_next->color='B';//v->color='B';//x->color='B';
CopyAttributes(G,v);
}
//cout<<"Y";
}
// cout<<v->key;
}
// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct Node* pCrawl = graph->Array[v]->next;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->key);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
void printGraphT(struct Graph* graph)
{
int v;
//cout<<"Inside"<<endl;
//cout<<graph->V<<" ";
for (v = 0; v < graph->V; ++v)
{
struct Node* pCrawl = graph->Array[v]->next;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->key);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
int main()
{
// create the graph given in above fugure
int V = 5;
struct Graph* graph = createGraph(V);
// addVertices(graph,0);
// addVertices(graph,1);
// addVertices(graph,2);
// addVertices(graph,3);
// addVertices(graph,4);
// //addVertices(graph,4);
// //addVertices(graph,0);
// addEdge(graph, 0, 2);
// addEdge(graph, 0, 1);
// addEdge(graph, 1, 2);
// addEdge(graph, 2, 3);
// addEdge(graph, 2, 0);
// //addEdge(graph, 2, 3);
// addEdge(graph, 3, 3);
// addEdge(graph, 1, 4);
addVertices(graph,0);
addVertices(graph,1);
addVertices(graph,2);
addVertices(graph,3);
addVertices(graph,4);
addEdge(graph,0, 1);
addEdge(graph,1, 2);
addEdge(graph,2, 3);
addEdge(graph,3, 0);
addEdge(graph,2, 4);
addEdge(graph,4, 2);
printGraph(graph);
printf("\n");
printf("Transpose\n");
struct Graph* gT;// = createGraph(V);
gT=TransposeGraph(graph);
printGraphT(gT);
cout<<endl;
cout<<"Done"<<endl;
return 0;
}