-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0525Topologyassignment.cpp
More file actions
68 lines (65 loc) · 1.69 KB
/
Copy path0525Topologyassignment.cpp
File metadata and controls
68 lines (65 loc) · 1.69 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
#include <vector>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
class Toplogy {
private:
unsigned vtx;
vector<int> *inDegree ; //차수
vector<int> *result ;
vector <int> *Edges = new vector<int>[vtx];
public:
Toplogy(unsigned vtx = 0) : vtx(vtx) {
inDegree = new vector<int>(vtx, 0);
result = new vector<int>();
}
void inputEdge(int startV, int endV);
void printResult();
void topologySorting();
};
void Toplogy::printResult(){
vector<int>::iterator it;
for(it=result->begin(); it!=result->end(); it++){
cout<<*it<<' ';
}
}
void Toplogy::inputEdge(int StartV, int EndV){
Edges[StartV].push_back(EndV);
inDegree->at(EndV)++;
}
void Toplogy::topologySorting(){
queue<int> q;
int i=0;
vector<int>::iterator it;
for(it = inDegree->begin(); it != inDegree->end(); it++){
if(*it ==0)
q.push(i);
i++;
}
for(unsigned i=0; i<vtx; i++) {
if(q.empty()){
cout<<"cycle occured!";
return;
}
int cur = q.front(); q.pop();
result->push_back(cur);
for(it = Edges[cur].begin(); it != Edges[cur].end(); it++){
int next = *it;
inDegree->at(next)--;
if(inDegree->at(next)==0)
q.push(next);
}
}
}
int main() {
Toplogy tpl(9);
tpl.inputEdge(2, 1); tpl.inputEdge(2, 0);
tpl.inputEdge(0, 1); tpl.inputEdge(1, 3);
tpl.inputEdge(1, 4); tpl.inputEdge(4, 5);
tpl.inputEdge(5, 3); tpl.inputEdge(5, 7);
tpl.inputEdge(3, 6); tpl.inputEdge(6, 7);
tpl.inputEdge(7, 8);
tpl.topologySorting();
tpl.printResult();
}