-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
155 lines (136 loc) · 2.57 KB
/
main.cpp
File metadata and controls
155 lines (136 loc) · 2.57 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
#include <iostream>
#include <time.h>
#include <fstream>
#include <chrono>
using namespace std;
struct list
{
list *next;
int v;
};
int iter, *stos;
list **graph;
char *visited;
const char WHITE = 0;
const char GRAY = 1;
const char GREEN = 2;
void generate(int vertex)
{
srand(time(NULL));
ofstream wynik;
wynik.open("dane.txt");
int edges = 0.3*((vertex*vertex) - vertex);
char **matrix;
matrix = new char *[vertex];
for (int i = 0; i < vertex; i++)
matrix[i] = new char[vertex];
for (int i = 0; i < vertex; i++)
for (int j = 0; j < vertex; j++) matrix[i][j] = 0;
int iterator=0;
struct graph
{
int begin;
int end;
};
graph *tab = new graph[edges];
while (iterator < edges)
{
int i = rand()%vertex;
int j = rand() % vertex;
if (i < j && matrix[i][j]==0)
{
tab[iterator].begin = i;
tab[iterator].end = j;
matrix[i][j] = 1;
iterator++;
}
}
for (int i = 0; i < edges; i++)
{
wynik << tab[i].begin << " " << tab[i].end << endl;
}
}
bool TP_SORT(int v)
{
list *p;
if (visited[v] == GRAY)
{
return false;
}
if (visited[v] == WHITE)
{
visited[v] = GRAY;
for (p = graph[v]; p; p = p->next)
if (!TP_SORT(p->v)) return false;
visited[v] = GREEN;
stos[iter++] = v;
}
return true;
}
int main()
{
int vertex = 100;
for (int k = 0; k < 15; k++)
{
ifstream wejscie;
wejscie.open("dane.txt");
ofstream wynik;
wynik.open("sortowanie_top.txt", ios::app);
int begin;
int end;
list *p, *r;
int edges = 0.3*((vertex*vertex) - vertex);
generate(vertex);
graph = new list *[vertex];
stos = new int[vertex];
iter = 0;
visited = new char[vertex];
for (int i = 0; i < vertex; i++)
{
graph[i] = NULL;
visited[i] = WHITE;
}
for (int i = 0; i < edges; i++)
{
wejscie >> begin;
wejscie >> end;
p = new list;
p->v = end;
p->next = graph[begin];
graph[begin] = p;
}
long long time = 0;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < vertex; i++)
{
if (visited[i] == WHITE)
{
if (!TP_SORT(i)) break;
}
}
auto stop = std::chrono::high_resolution_clock::now();
time = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
/*
if (iter == vertex)
for (int i = vertex - 1; i >= 0; i--) cout << stos[i] << " ";
cout << endl;
*/
wynik << vertex << " " << time << endl;
for (int i = 0; i < vertex; i++)
{
p = graph[i];
while (p)
{
r = p;
p = p->next;
delete r;
}
}
delete[] graph;
delete[] visited;
delete[] stos;
vertex = vertex + 200;
cout << k << endl;
}
system("pause");
}