-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCCSSEQ.cpp
190 lines (160 loc) · 4.27 KB
/
SCCSSEQ.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
/*
* SCCSSEQ.cpp
*
* Created on: 15/lug/2013
* Author: ilariamartinelli
*/
#include "SCCSSEQ.h"
#include <iostream>
/**
* @brief Method for computing the the sequence of SCC sorted in a topological way
* @param gamma the framework which contains the graph
* @retval a list of pointer to SCC
*/
list<SCC*> SCCSSEQ(AF gamma){
stack<DFS_node*> G = initialize_stack(gamma);
list<SCC*> list_SCC,dummy_list;
// first call of DFS
DFS(G,true,&dummy_list);
// second call of DFS, considering edges in the opposite direction
DFS(G,false,&list_SCC);
list<SCC*>::iterator it;
SCC* temp, temp2;
for(it=list_SCC.begin();it!=list_SCC.end();it++){
temp=*it;
cout<<temp->set<<endl;
}
cout<<endl;
return list_SCC;
}
/**
* @brief sort of the stack
* @param S stack which is to sort
* @retval the sorted stack
*/
stack<DFS_node*> sort_stack(stack<DFS_node*> S){//e azzero colore
DFS_node * temp_node;
stack<DFS_node*> stack;
list<DFS_node*> list;
while(!S.empty()){
temp_node=S.top();
temp_node->color=0;
list.push_back(temp_node);
S.pop();
}
list.sort(compareTime);
while(!list.empty()){
stack.push(list.back());
list.pop_back();
}
return stack;
}
/**
* @brief Criterion used to compare two nodes: finish time
* @param a first node to compare
* @param b second node to compare
* @retval true if the first is greater then the second
*/
bool compareTime(DFS_node* a,DFS_node* b){
return (a->f)>(b->f);
}
/**
* @brief depth first search
* @param S Stack in which we have to do the DFS
* @param first true if is the first time which we are using this method in the SCCSSEQ method
* @retval void
*/
void DFS(stack<DFS_node*> S, bool first, list<SCC*> *SCCSSEQ){
int time = 0;
DFS_node* u;
SCC * scc_tmp;
//copy of S stack, in this way we can extract element from S, and G doesn't change
stack<DFS_node*> G = stack<DFS_node*>(S);
// if is the second time, we sort the stack
if(!first){
S=sort_stack(S);
}
// visit every node of the stack if the color is white
while(!S.empty()){
u = S.top();
S.pop();
if( u->color == 0 ){
scc_tmp=new SCC;
//cout << u->argument->getName() << " ";
DFS_visit(G,u,&time, first, scc_tmp);
if(!first){
SCCSSEQ->push_back(scc_tmp);
//cout<<scc_tmp->set<<endl;
}
}
}
}
/**
* @brief visit in the depth first search
* @param u node which we are visiting
* @param first true if is the first time which we are using DFS in the SCCSSEQ method
* @retval SCC return the SCC computed
*/
void DFS_visit(stack<DFS_node*> S, DFS_node* u, int* time, bool first, SCC *tmp_scc){
SCC tmp_scc2;
(*time)++;
u->d = *time;
u->color = 1;//gray
DFS_node * temp;
// if is the first time we consider the attacked nodes, otherwise we consider the attackers ( way to compute G trasposed )
SetArguments * adj;
if(first){
adj = u->argument->get_attacks();
}
else{
adj = u->argument->get_attackers();
}
// if the color is white call DFS_visit, compute SCC by union
for(SetArgumentsIterator it = adj->begin(); it != adj->end(); it++ ){
temp = get_DFS_node(S, **it );
//cout << (temp->argument->getName())<<" sub"<<endl;
if( temp->color == 0 ){
temp->p = u;
DFS_visit(S,temp,time,first,&tmp_scc2);
if(!first){
tmp_scc->set.setunion(&(tmp_scc2.set),&(tmp_scc->set));
}
}
}
u->color = 2;
if (!first){
tmp_scc->set.add_Argument(u->argument);
}
(*time)++;
u->f = *time;
//cout << u->argument->getName() <<" : "<< u->f << endl;
}
/**
* @brief get a DFS_node if is present in the stack a node with the corresponding Argument
* @param a argument which we are looking for
* @retval DFS_node* pointer of the corresponding DFS_node if is presente in the stack, otherwise NULL
*/
DFS_node* get_DFS_node(stack<DFS_node*> S, Argument a ){
DFS_node * u;
while(!S.empty()){
u = S.top();
S.pop();
if( *(u->argument) == a )
return u;
}
return NULL;
}
/**
* @brief initialization of the stack
* @param gamma framework
* @retval stack corresponding stack of DFS_node initialized
*/
stack<DFS_node*> initialize_stack(AF gamma){
SetArguments* args;
stack<DFS_node*> stack;
args = gamma.get_arguments();
for(SetArgumentsIterator it = args->begin(); it != args->end(); it++){
stack.push( new DFS_node(*it));
}
return stack;
}