-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuGraph.cpp
210 lines (172 loc) · 4.43 KB
/
uGraph.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
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
/*
@Chiranjeevi
Date: 18-Jun-17
*/
/*
assumptions: no self loop, no parallel edges, all inputs are valid
*/
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
struct node{
int v;
node *next;
};
node *newNode(int a){
node *temp = new node();
temp->v = a;
temp->next = NULL;
return temp;
}
void initialize(int V, bool mark[], int parent[], int distance[], int cc[]){
for(int i=0; i<V; i++){
mark[i]=false;
parent[i]=-1;
distance[i]=0;
cc[i]=0;
}
}
void addUEdge(vector<node *> &vertices, int a, int b){
if(vertices[a] == NULL){
vertices[a]=newNode(b);
}
else{
node *ptr = vertices[a];
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newNode(b);
}
if(vertices[b] == NULL){
vertices[b] = newNode(a);
}
else{
node *ptr = vertices[b];
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next=newNode(a);
}
}
void createUGraph(int E, vector<node *> &vertices){
int a, b;
for(int i=0; i<E; i++){
cin>>a>>b;
addUEdge(vertices,a,b);
}
}
int getDegree(vector<node *> &vertices, int a){
node *ptr=vertices[a];
int count = 0;
while(ptr!=NULL){
ptr=ptr->next;
count++;
}
return count;
}
int getMaxDegree(vector<node *> &vertices, int V){
int max=getDegree(vertices, 0);
int temp;
for(int i=1; i<V; i++){
temp = getDegree(vertices, i);
if(temp>max)
max=temp;
}
return max;
}
void printUGraph(vector<node *> &vertices, int V){
node *ptr=NULL;
for(int i=0; i<V; i++){
ptr = vertices[i];
while(ptr!=NULL){
cout<<i<<"--"<<ptr->v<<endl;
ptr=ptr->next;
}
}
}
void printAdjOfv(vector<node *> &vertices, int a){
node *ptr = vertices[a];
while(ptr!=NULL){
cout<<a<<"--"<<ptr->v<<endl;
ptr=ptr->next;
}
}
void dfs(int w, vector<node *> &vertices, bool mark[], int parent[], int cc[], int id){
node *ptr=vertices[w];
mark[w]=true;
cc[w]=id;
while(ptr!=NULL){
if(!mark[ptr->v]){
parent[ptr->v]=w;
dfs(ptr->v, vertices, mark, parent, cc, id);
}
ptr=ptr->next;
}
}
void printPath(int source, int v, int parent[]){
if(v!=source)
printPath(source, parent[v], parent);
cout<<v<<"-->";
}
void bfs(int source, vector<node *> &vertices, bool mark[], int parent[], int distance[]){
queue<int> q;
int element, count=1;
node *ptr=NULL;
q.push(source);
mark[source]=true;
while(!q.empty()){
element=q.front();
q.pop();
ptr=vertices[element];
while(ptr!=NULL){
if(!mark[ptr->v]){
q.push(ptr->v);
mark[ptr->v]=true;
parent[ptr->v]=element;
distance[ptr->v]=count;
}
ptr=ptr->next;
}
count++;
}
}
int connectedComponents(vector<node *> &vertices, bool mark[], int parent[], int cc[]){
int V=vertices.size(), id=1;
for(int i=0; i<V; i++){
mark[i]=false;
parent[i]=0;
}
for(int i=0; i<V; i++){
if(!mark[i]){
dfs(i, vertices, mark, parent, cc, id++);
}
}
return *max_element(cc, cc+V);
}
void checkIfCycleExists(int w, vector<node *> &vertices, bool mark[], int parent[]){
mark[w]=true;
node *ptr=vertices[w];
while(ptr!=NULL){
if(mark[ptr->v] and parent[w]!=ptr->v){
cout<<"Cycle exists--"<<w<<"---"<<ptr->v<<endl;
}
if(!mark[ptr->v]){
parent[ptr->v]=w;
cout<<w<<"--"<<ptr->v<<endl;
checkCycle(ptr->v, vertices, mark, parent);
}
ptr=ptr->next;
}
}
int main(){
int V,E;
cin>>V>>E;
bool mark[V];
int parent[V];
int distance[V];
int cc[V];
vector<node *> vertices(V, NULL); //initialize all V to NULL
initialize(V, mark, parent, distance, cc);
createUGraph(E, vertices);
}