Skip to content

Commit 6965299

Browse files
committed
TP2 plot
1 parent 2315421 commit 6965299

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

src/performance.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ def get_exec_time(function, *args) -> float:
1010
return default_timer() - start
1111

1212

13-
def get_n_exec_time(algorithm, n, size) -> int:
13+
def get_n_exec_time(algorithm, n, adjacency_list) -> int:
1414
exec_times = []
1515

1616
for i in range(n):
17-
adjacency_list = generate_adjacency_list(size)
1817
exec_times.append(get_exec_time(algorithm, adjacency_list))
1918

20-
return mean(exec_times) * 10 ** 6 # Convert seconds to microseconds
19+
return mean(exec_times)

src/tp1.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
import matplotlib.pyplot as plt
55

66
from src.performance import get_n_exec_time
7-
from src.utils import adjacency_list_to_adjacency_matrix
7+
from src.utils import adjacency_list_to_adjacency_matrix, generate_adjacency_list
88

99
""" ---------- Plot Roy Warshall exec times ---------- """
1010

1111

1212
def main() -> None:
13-
min_graph_size = 1
14-
max_graph_size = 21
15-
number_of_executions = 1000
13+
if len(sys.argv) != 4:
14+
sys.exit("Usage : <min_graph_size> <max_graph_size> <number_of_executions>")
15+
16+
min_graph_size = int(sys.argv[1])
17+
max_graph_size = int(sys.argv[2]) + 1
18+
number_of_executions = int(sys.argv[3])
19+
20+
if min_graph_size < 1 or max_graph_size < min_graph_size or number_of_executions < 1:
21+
sys.exit("Invalid arguments.")
1622

1723
print("{} executions".format(number_of_executions))
1824

@@ -21,14 +27,13 @@ def main() -> None:
2127
line3 = []
2228

2329
for i in range(min_graph_size, max_graph_size):
24-
sys.stdout.write('{0:.1f}% processed ({1} vertices) \r'.format(i / max_graph_size * 100, i))
30+
sys.stdout.write('{0:.1f}% processed ({1} vertices) \r'.format((i + 1) / max_graph_size * 100, i))
2531
sys.stdout.flush()
2632

27-
line1.append(get_n_exec_time(roy_warshall_1, number_of_executions, i))
28-
line2.append(get_n_exec_time(roy_warshall_1_bis, number_of_executions, i))
29-
line3.append(get_n_exec_time(roy_warshall_2, number_of_executions, i))
33+
line1.append(get_n_exec_time(roy_warshall_1, number_of_executions, generate_adjacency_list(i)))
34+
line2.append(get_n_exec_time(roy_warshall_1_bis, number_of_executions, generate_adjacency_list(i)))
35+
line3.append(get_n_exec_time(roy_warshall_2, number_of_executions, generate_adjacency_list(i)))
3036

31-
sys.stdout.write('100% processed ({} vertices) \r'.format(max_graph_size - 1))
3237
vertices_number = [*range(1, max_graph_size)]
3338

3439
plt.plot(vertices_number, line1, label='Roy Warshall 1')
@@ -37,7 +42,7 @@ def main() -> None:
3742

3843
plt.grid()
3944

40-
plt.ylabel('Average exec time (in µs)')
45+
plt.ylabel('Average exec time (in seconds)')
4146
plt.xlabel('Number of vertices')
4247
plt.title('Average exec time of the 3 different Roy Warshall algorithms ({} execs)'.format(number_of_executions))
4348
plt.legend()

src/tp2.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
1+
import sys
12
from operator import itemgetter
23
from typing import List
34

4-
from src.utils import print_adjacency_matrix_csv, adjacency_list_to_adjacency_matrix, \
5-
generate_adjacency_list_without_cycles
5+
import matplotlib.pyplot as plt
6+
7+
from src.performance import get_n_exec_time
8+
from src.utils import generate_adjacency_list_without_cycles
69

710

811
def main():
9-
# graph = [[1, 2], [2], [3], [4], []]
10-
# graph = [[1, 2], [2], [3], [4, 5], [], [6], []]
11-
# print(grundy(graph))
12+
if len(sys.argv) != 4:
13+
sys.exit("Usage : <min_graph_size> <max_graph_size> <number_of_executions>")
14+
15+
min_graph_size = int(sys.argv[1])
16+
max_graph_size = int(sys.argv[2]) + 1
17+
number_of_executions = int(sys.argv[3])
18+
19+
if min_graph_size < 1 or max_graph_size < min_graph_size or number_of_executions < 1:
20+
sys.exit("Invalid arguments.")
21+
22+
print("{} executions".format(number_of_executions))
23+
24+
line1 = []
25+
26+
for i in range(min_graph_size, max_graph_size):
27+
sys.stdout.write('{0:.1f}% processed ({1} vertices) \r'.format((i + 1) / max_graph_size * 100, i))
28+
sys.stdout.flush()
29+
30+
line1.append(get_n_exec_time(grundy, number_of_executions, generate_adjacency_list_without_cycles(i)))
31+
32+
vertices_number = [*range(1, max_graph_size)]
33+
plt.plot(vertices_number, line1, label='Grundy')
1234

13-
for i in range(1):
14-
print_adjacency_matrix_csv(adjacency_list_to_adjacency_matrix(generate_adjacency_list_without_cycles(8)))
35+
plt.grid()
1536

16-
# print(adjacency_list_to_adjacency_matrix(generate_adjacency_list_without_cycles(5)))
37+
plt.ylabel('Average exec time (in seconds)')
38+
plt.xlabel('Number of vertices')
39+
plt.title('Average exec time of grundy value algorithm ({} execs)'.format(number_of_executions))
40+
plt.legend()
41+
plt.show()
1742

1843

1944
def leveling(adjacency_list: List[List[int]]) -> List[int]:
@@ -31,7 +56,7 @@ def leveling(adjacency_list: List[List[int]]) -> List[int]:
3156
for k in range(len(adjacency_list[j])):
3257

3358
if adjacency_list[j][k] == no_successor_vertices[i]:
34-
no_successor_vertices.append(j)
59+
no_successor_vertices.append(j) # <-- bug here
3560

3661
if graph_leveling[j] < (graph_leveling[no_successor_vertices[i]] + 1):
3762
graph_leveling[j] = graph_leveling[no_successor_vertices[i]] + 1

0 commit comments

Comments
 (0)