-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
217 lines (194 loc) · 5.9 KB
/
Copy pathgraph.cpp
File metadata and controls
217 lines (194 loc) · 5.9 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
// #include <iostream>
// #include "graphmatrix.h"
// #include "graphadj.h"
// using namespace std;
// bool isSubGraph(GraphAdjList*, GraphAdjList*);
// bool isPath(GraphAdjList*, int*, int);
// bool isSimplePath(GraphAdjList*, int*, int);
// void reachVector(GraphAdjList*, int, bool*);
// bool canReach(GraphAdjList*, int, int);
// int main(){
// // Teste função: isSimplePath
// int p0[]={0,1,2,3};
// int p1[]={0,1,2,3,0};
// int p2[]={1,2,3,4,2,3};
// int p3[]={0,1,2,5};
// int p4[]={0,1,2,3};
// GraphAdjList* G0;
// GraphAdjList G0Ob(6);
// G0 = &G0Ob;
// G0->addEdge(0, 1);
// G0->addEdge(0, 2);
// G0->addEdge(1, 3);
// G0->addEdge(1, 4);
// G0->addEdge(2, 4);
// G0->addEdge(3, 4);
// G0->addEdge(4, 1);
// G0->addEdge(4, 5);
// GraphAdjList* G1;
// GraphAdjList G1Ob(6);
// G1 = &G1Ob;
// G1->addEdge(0, 1);
// G1->addEdge(0, 2);
// G1->addEdge(1, 2);
// G1->addEdge(1, 3);
// G1->addEdge(2, 3);
// G1->addEdge(3, 4);
// G1->addEdge(4, 2);
// G1->addEdge(4, 1);
// G1->addEdge(4, 5);
// G1->addEdge(5, 0);
// GraphAdjList* G2;
// GraphAdjList G2Ob(7);
// G2 = &G2Ob;
// G2->addEdge(0, 1);
// G2->addEdge(0, 2);
// G2->addEdge(1, 2);
// G2->addEdge(2, 6);
// G2->addEdge(3, 2);
// G2->addEdge(3, 4);
// G2->addEdge(5, 2);
// G2->addEdge(5, 4);
// GraphAdjList* G3;
// GraphAdjList G3Ob(5);
// G3 = &G3Ob;
// G3->addEdge(0, 1);
// G3->addEdge(0, 2);
// G3->addEdge(1, 3);
// G3->addEdge(2, 4);
// G3->addEdge(3, 2);
// G3->addEdge(3, 4);
// GraphAdjList* subG0;
// GraphAdjList subG0Ob(3);
// subG0 = &subG0Ob;
// subG0->addEdge(0, 1);
// subG0->addEdge(1, 3);
// GraphAdjList* subG1;
// GraphAdjList subG1Ob(3);
// subG1 = &subG1Ob;
// subG1->addEdge(0, 1);
// subG1->addEdge(1, 2);
// GraphAdjList* subG2;
// GraphAdjList subG2Ob(3);
// subG2 = &subG2Ob;
// subG2->addEdge(1, 2);
// cout << "Teste função canReach: " << endl;
// cout << "G0, há um caminho entre 0 e 5?" << endl;
// if(canReach(G0, 0, 5)){
// cout << "Sim!" << endl;
// }
// else{
// cout << "Não." << endl;
// }
// cout << "G2, há um caminho entre 0 e 5?" << endl;
// if(canReach(G2, 0, 5)){
// cout << "Sim!" << endl;
// }
// else{
// cout << "Não." << endl;
// }
// return 0;
// };
// bool isSubGraph(GraphAdjList* G, GraphAdjList* H){
// /* Verificando se H é sub-grafo de G
// Condições mínimas e necessárias para ser um subgrafo:
// i) H(vértices) \in G(vértices);
// ii) H(arestas) \in G(arestas);
// iii) Se (a,b)\inH(arestas) então a,b\inH(vértices);
// */
// // Tirando casos triviais, do sub-grafo ter mais vértices ou mais arestas que o grafo original
// if((H->getNumVertex()>G->getNumVertex())||(H->getNumEdge()>G->getNumEdge())){
// return false;
// } // Até aqui ok
// // Note que precisamos apenas verificar se para toda aresta a em H, a é aresta de G //
// for(int i=0;i<H->getNumVertex();i++){
// if(H->getNumEdgeVertex()[i]>G->getNumEdgeVertex()[i]){
// return false;
// }
// EdgeNode* edge = H->getEdges()[i];
// while(edge!=nullptr){
// // Verificando se é verdade que há uma aresta entre i e j em H então não há uma aresta entre i e j em G, isso implica que H não é subgrafo de G.
// int j = edge->getVertex();
// if(!G->hasEdge(i,j)){
// return false;
// }
// edge = edge->getNext();
// }
// }
// return true;
// }
// bool isPath(GraphAdjList* G, int* path, int n){
// for(int i=0; i<n-1;i++){
// if(!G->hasEdge(path[i],path[i+1])){
// return false;
// }
// }
// return true;
// }
// bool isSimplePath(GraphAdjList* G, int* path, int n){
// int vertexPath[G->getNumVertex()];
// for(int i=0; i<G->getNumVertex(); i++){vertexPath[i]=0;}
// vertexPath[path[0]]++;
// for(int i=0; i<n-1;i++){
// vertexPath[path[i+1]]++;
// if(vertexPath[path[i+1]]>1){
// return false;
// }
// if(!G->hasEdge(path[i],path[i+1])){
// return false;
// }
// }
// return true;
// }
// void reachVector(GraphAdjList* G, int i, bool* visited){
// visited[i]=true;
// for(int j=0; j<G->getNumVertex(); j++){
// if(G->hasEdge(i,j) && !visited[j]){
// reachVector(G, j, visited);
// }
// }
// }
// bool canReach(GraphAdjList* G, int i, int j){
// bool visited[G->getNumVertex()];
// for(int k=0; k<G->getNumVertex(); k++){
// visited[k]=false;
// }
// reachVector(G,i,visited);
// for(int n=0; n<G->getNumVertex(); n++){
// cout << visited[n] << " ";
// }
// cout << endl;
// return visited[j];
// }
// bool hasTopologicalOrder(GraphAdjList* G,int order[]) {
// int inDegree[G->getNumVertex()];
// for (int i = 0; i < G->getNumVertex(); i++) {
// inDegree[i] = 0;
// }
// for (int i = 0; i < G->getNumVertex(); i++) {
// for (EdgeNode* e = G->getEdges()[i]; e != nullptr; e = e->getNext()) {
// inDegree[e->getVertex()]++;
// }
// }
// int queue[G->getNumVertex()];
// int start = 0;
// int end = 0;
// for (int i = 0; i < G->getNumVertex(); i++) {
// if (inDegree[i] == 0) {
// queue[end++] = i;
// }
// }
// int counter = 0;
// while (start < end) {
// int v = queue[start++];
// order[v] = counter++;
// for (EdgeNode* e = G->getEdges()[v]; e != nullptr; e = e->getNext()) {
// int other = e->getVertex();
// inDegree[other]--;
// if (inDegree[other] == 0) {
// queue[end++] = other;
// }
// }
// }
// return counter >= G->getNumVertex();
// }