From a0787151a9a78f61606f23174e86d6412f898d00 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Mon, 11 Oct 2021 16:39:35 +0200 Subject: [PATCH 01/12] added complete graph generator function --- graphs/complete_graph_generator.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 graphs/complete_graph_generator.py diff --git a/graphs/complete_graph_generator.py b/graphs/complete_graph_generator.py new file mode 100644 index 000000000000..4e41f0b60a1b --- /dev/null +++ b/graphs/complete_graph_generator.py @@ -0,0 +1,24 @@ +""" +* Author: Manuel Di Lullo (https://github.com/manueldilullo) +* Python Version: 3.9 +* Description: Complete graphs generator. Uses graphs represented with an adjacency list +""" + +def complete_graph(n): + """ + function that generate a complete graph with n vertices + @input: n (number of vertices), directed (False if the graph is undirected, True otherwise) + @example: + >>> print(complete_graph(3)) + {0: [1, 2], 1: [2], 2: []} + """ + g = {} + for i in range(n): + g[i] = [] + for j in range(0,n): + if i != j: + g[i].append(j) + return g + +if __name__ == "__main__": + print(f"Complete graph:\n{complete_graph(5)}") \ No newline at end of file From 64903275090564d5f4eba3567e34854da9f37a60 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Mon, 11 Oct 2021 16:47:23 +0200 Subject: [PATCH 02/12] added doctest, type hints, wikipedia explanation --- graphs/complete_graph_generator.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/graphs/complete_graph_generator.py b/graphs/complete_graph_generator.py index 4e41f0b60a1b..f657f8d01d6e 100644 --- a/graphs/complete_graph_generator.py +++ b/graphs/complete_graph_generator.py @@ -2,9 +2,11 @@ * Author: Manuel Di Lullo (https://github.com/manueldilullo) * Python Version: 3.9 * Description: Complete graphs generator. Uses graphs represented with an adjacency list + +URL: https://en.wikipedia.org/wiki/Complete_graph """ -def complete_graph(n): +def complete_graph(n:int): """ function that generate a complete graph with n vertices @input: n (number of vertices), directed (False if the graph is undirected, True otherwise) @@ -21,4 +23,7 @@ def complete_graph(n): return g if __name__ == "__main__": - print(f"Complete graph:\n{complete_graph(5)}") \ No newline at end of file + import doctest + doctest.testmod() + + # print(f"Complete graph:\n{complete_graph(5)}") \ No newline at end of file From be997e672aa0520216e90658cb043aaac1f9b214 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Mon, 11 Oct 2021 16:48:42 +0200 Subject: [PATCH 03/12] added return type hint for function complete_graph --- graphs/complete_graph_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/complete_graph_generator.py b/graphs/complete_graph_generator.py index f657f8d01d6e..d1033f70a155 100644 --- a/graphs/complete_graph_generator.py +++ b/graphs/complete_graph_generator.py @@ -6,7 +6,7 @@ URL: https://en.wikipedia.org/wiki/Complete_graph """ -def complete_graph(n:int): +def complete_graph(n:int) -> dict: """ function that generate a complete graph with n vertices @input: n (number of vertices), directed (False if the graph is undirected, True otherwise) From 54ecb1e692ceaf3c1f488abfad2bb8c74dff7ad0 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Mon, 11 Oct 2021 16:50:22 +0200 Subject: [PATCH 04/12] added descriptive name for the parameter: n --- graphs/complete_graph_generator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/graphs/complete_graph_generator.py b/graphs/complete_graph_generator.py index d1033f70a155..81d376464599 100644 --- a/graphs/complete_graph_generator.py +++ b/graphs/complete_graph_generator.py @@ -6,7 +6,7 @@ URL: https://en.wikipedia.org/wiki/Complete_graph """ -def complete_graph(n:int) -> dict: +def complete_graph(vertices_number:int) -> dict: """ function that generate a complete graph with n vertices @input: n (number of vertices), directed (False if the graph is undirected, True otherwise) @@ -15,9 +15,9 @@ def complete_graph(n:int) -> dict: {0: [1, 2], 1: [2], 2: []} """ g = {} - for i in range(n): + for i in range(vertices_number): g[i] = [] - for j in range(0,n): + for j in range(0,vertices_number): if i != j: g[i].append(j) return g From 9ac4b646f0d2e87ce28853965e9af41b66f4ec08 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Mon, 11 Oct 2021 16:53:19 +0200 Subject: [PATCH 05/12] random graph generator with doctest and type hints --- graphs/random_graph_generator.py | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 graphs/random_graph_generator.py diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py new file mode 100644 index 000000000000..fc5a0cadef85 --- /dev/null +++ b/graphs/random_graph_generator.py @@ -0,0 +1,64 @@ +""" +* Author: Manuel Di Lullo (https://github.com/manueldilullo) +* Python Version: 3.9 +* Description: Random graphs generator. Uses graphs represented with an adjacency list. + +URL: https://en.wikipedia.org/wiki/Random_graph +""" + +import numpy as np + +def random_graph(vertices_number:int, probability:float, directed:bool=False) -> dict: + """ + Function that generates a random graph + @input: vertices_number (number of vertices), probability (probability that a generic edge (u,v) exists), + directed (if it's True, g will be a directed graph, otherwise it will be an undirected graph) + @examples: + >>> print(random_graph(5, 0.5)) + {0: [1, 2, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2, 4], 4: [2, 3]} + >>> print(random_graph(5, 0.5, True)) + {0: [2], 1: [], 2: [3, 4], 3: [4], 4: []} + """ + g = dict(zip(range(vertices_number), [[] for _ in range(vertices_number)])) + + # if probability is greater or equal than 1, then generate a complete graph + if probability >= 1: + return complete_graph(vertices_number) + # if probability is lower or equal than 0, then return a graph without edges + if probability <= 0: + return g + + # for each couple of nodes, add an edge from u to v + # if the number randomly generated is greater than probability probability + for i in range(vertices_number): + for j in range(i+1, vertices_number): + if np.random.random() < probability: + g[i].append(j) + if not directed: + # if the graph is undirected, add an edge in from j to i, either + g[j].append(i) + return g + + +def complete_graph(vertices_number:int) -> dict: + """ + function that generate a complete graph with vertices_number vertices + @input: vertices_number (number of vertices), directed (False if the graph is undirected, True otherwise) + @example: + >>> print(complete_graph(3)) + {0: [1, 2], 1: [2], 2: []} + """ + g = {} + for i in range(vertices_number): + g[i] = [] + for j in range(0,vertices_number): + if i != j: + g[i].append(j) + return g + +if __name__ == "__main__": + import doctest + doctest.testmod() + + # print(f"Random undirected graph:\vertices_number{random_graph(5, 0.5)}") + # print(f"Random directed graph:\vertices_number{random_graph(5, 0.5, True)}") \ No newline at end of file From e2d9ec1c6365c24789db10b46ad1d8901e207822 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Mon, 11 Oct 2021 20:25:14 +0200 Subject: [PATCH 06/12] validated using pre-commit --- graphs/random_graph_generator.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py index fc5a0cadef85..616946caf74d 100644 --- a/graphs/random_graph_generator.py +++ b/graphs/random_graph_generator.py @@ -1,18 +1,24 @@ """ * Author: Manuel Di Lullo (https://github.com/manueldilullo) * Python Version: 3.9 -* Description: Random graphs generator. Uses graphs represented with an adjacency list. +* Description: Random graphs generator. + Uses graphs represented with an adjacency list. URL: https://en.wikipedia.org/wiki/Random_graph """ import numpy as np -def random_graph(vertices_number:int, probability:float, directed:bool=False) -> dict: + +def random_graph( + vertices_number: int, probability: float, directed: bool = False +) -> dict: """ Function that generates a random graph - @input: vertices_number (number of vertices), probability (probability that a generic edge (u,v) exists), - directed (if it's True, g will be a directed graph, otherwise it will be an undirected graph) + @input: vertices_number (number of vertices), + probability (probability that a generic edge (u,v) exists), + directed (if True: g will be a directed graph, + otherwise it will be an undirected graph) @examples: >>> print(random_graph(5, 0.5)) {0: [1, 2, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2, 4], 4: [2, 3]} @@ -31,7 +37,7 @@ def random_graph(vertices_number:int, probability:float, directed:bool=False) -> # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(vertices_number): - for j in range(i+1, vertices_number): + for j in range(i + 1, vertices_number): if np.random.random() < probability: g[i].append(j) if not directed: @@ -40,10 +46,11 @@ def random_graph(vertices_number:int, probability:float, directed:bool=False) -> return g -def complete_graph(vertices_number:int) -> dict: +def complete_graph(vertices_number: int) -> dict: """ function that generate a complete graph with vertices_number vertices - @input: vertices_number (number of vertices), directed (False if the graph is undirected, True otherwise) + @input: vertices_number (number of vertices), + directed (False if the graph is undirected, True otherwise) @example: >>> print(complete_graph(3)) {0: [1, 2], 1: [2], 2: []} @@ -51,14 +58,13 @@ def complete_graph(vertices_number:int) -> dict: g = {} for i in range(vertices_number): g[i] = [] - for j in range(0,vertices_number): + for j in range(0, vertices_number): if i != j: g[i].append(j) return g + if __name__ == "__main__": import doctest - doctest.testmod() - # print(f"Random undirected graph:\vertices_number{random_graph(5, 0.5)}") - # print(f"Random directed graph:\vertices_number{random_graph(5, 0.5, True)}") \ No newline at end of file + doctest.testmod() From 5a01771862fd8f232e337d056ce7bfa080bd4ce5 Mon Sep 17 00:00:00 2001 From: Manuel Di Lullo <39048927+manueldilullo@users.noreply.github.com> Date: Mon, 11 Oct 2021 20:31:23 +0200 Subject: [PATCH 07/12] Delete complete_graph_generator.py --- graphs/complete_graph_generator.py | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 graphs/complete_graph_generator.py diff --git a/graphs/complete_graph_generator.py b/graphs/complete_graph_generator.py deleted file mode 100644 index 81d376464599..000000000000 --- a/graphs/complete_graph_generator.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -* Author: Manuel Di Lullo (https://github.com/manueldilullo) -* Python Version: 3.9 -* Description: Complete graphs generator. Uses graphs represented with an adjacency list - -URL: https://en.wikipedia.org/wiki/Complete_graph -""" - -def complete_graph(vertices_number:int) -> dict: - """ - function that generate a complete graph with n vertices - @input: n (number of vertices), directed (False if the graph is undirected, True otherwise) - @example: - >>> print(complete_graph(3)) - {0: [1, 2], 1: [2], 2: []} - """ - g = {} - for i in range(vertices_number): - g[i] = [] - for j in range(0,vertices_number): - if i != j: - g[i].append(j) - return g - -if __name__ == "__main__": - import doctest - doctest.testmod() - - # print(f"Complete graph:\n{complete_graph(5)}") \ No newline at end of file From bebbcac2b0f54cac031b99f30dc8d50b7798350d Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Tue, 12 Oct 2021 16:52:39 +0200 Subject: [PATCH 08/12] fixed doctest --- graphs/random_graph_generator.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py index 616946caf74d..d9ee469195d3 100644 --- a/graphs/random_graph_generator.py +++ b/graphs/random_graph_generator.py @@ -20,10 +20,10 @@ def random_graph( directed (if True: g will be a directed graph, otherwise it will be an undirected graph) @examples: - >>> print(random_graph(5, 0.5)) - {0: [1, 2, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2, 4], 4: [2, 3]} - >>> print(random_graph(5, 0.5, True)) - {0: [2], 1: [], 2: [3, 4], 3: [4], 4: []} + >>> random_graph(4, 1) + {0: [1, 2, 3], 1: [0, 2, 3], 2: [0, 1, 3], 3: [0, 1, 2]} + >>> random_graph(4, 1, True) + {0: [1, 2, 3], 1: [0, 2, 3], 2: [0, 1, 3], 3: [0, 1, 2]} """ g = dict(zip(range(vertices_number), [[] for _ in range(vertices_number)])) @@ -53,7 +53,7 @@ def complete_graph(vertices_number: int) -> dict: directed (False if the graph is undirected, True otherwise) @example: >>> print(complete_graph(3)) - {0: [1, 2], 1: [2], 2: []} + {0: [1, 2], 1: [0, 2], 2: [0, 1]} """ g = {} for i in range(vertices_number): From cf7dc21af0e3ba963eeba376212865495d9572c2 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Fri, 15 Oct 2021 14:39:20 +0200 Subject: [PATCH 09/12] updated following reviews --- graphs/random_graph_generator.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py index d9ee469195d3..15fae37da0c2 100644 --- a/graphs/random_graph_generator.py +++ b/graphs/random_graph_generator.py @@ -1,6 +1,5 @@ """ * Author: Manuel Di Lullo (https://github.com/manueldilullo) -* Python Version: 3.9 * Description: Random graphs generator. Uses graphs represented with an adjacency list. @@ -48,7 +47,7 @@ def random_graph( def complete_graph(vertices_number: int) -> dict: """ - function that generate a complete graph with vertices_number vertices + function that generates a complete graph with vertices_number vertices @input: vertices_number (number of vertices), directed (False if the graph is undirected, True otherwise) @example: From 6204ab076e983ee1762d6ff1cdf5c622d2c813ad Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Fri, 15 Oct 2021 15:31:23 +0200 Subject: [PATCH 10/12] simplified the code following reviews --- graphs/random_graph_generator.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py index 15fae37da0c2..cce8310ac8b0 100644 --- a/graphs/random_graph_generator.py +++ b/graphs/random_graph_generator.py @@ -6,7 +6,7 @@ URL: https://en.wikipedia.org/wiki/Random_graph """ -import numpy as np +import random def random_graph( @@ -16,7 +16,7 @@ def random_graph( Function that generates a random graph @input: vertices_number (number of vertices), probability (probability that a generic edge (u,v) exists), - directed (if True: g will be a directed graph, + directed (if True: graph will be a directed graph, otherwise it will be an undirected graph) @examples: >>> random_graph(4, 1) @@ -24,25 +24,25 @@ def random_graph( >>> random_graph(4, 1, True) {0: [1, 2, 3], 1: [0, 2, 3], 2: [0, 1, 3], 3: [0, 1, 2]} """ - g = dict(zip(range(vertices_number), [[] for _ in range(vertices_number)])) + graph = {i: [] for i in range(vertices_number)} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(vertices_number) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: - return g + return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(vertices_number): for j in range(i + 1, vertices_number): - if np.random.random() < probability: - g[i].append(j) + if random.random() < probability: + graph[i].append(j) if not directed: # if the graph is undirected, add an edge in from j to i, either - g[j].append(i) - return g + graph[j].append(i) + return graph def complete_graph(vertices_number: int) -> dict: @@ -54,13 +54,9 @@ def complete_graph(vertices_number: int) -> dict: >>> print(complete_graph(3)) {0: [1, 2], 1: [0, 2], 2: [0, 1]} """ - g = {} - for i in range(vertices_number): - g[i] = [] - for j in range(0, vertices_number): - if i != j: - g[i].append(j) - return g + return { + i: [j for j in range(vertices_number) if i != j] for i in range(vertices_number) + } if __name__ == "__main__": From 6ee751b3d86a65d77e5950784be87d3129199dbb Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Tue, 19 Oct 2021 16:54:31 +0200 Subject: [PATCH 11/12] fixed doctest and solved consistency issues --- graphs/random_graph_generator.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py index cce8310ac8b0..73cbb480b3af 100644 --- a/graphs/random_graph_generator.py +++ b/graphs/random_graph_generator.py @@ -19,10 +19,10 @@ def random_graph( directed (if True: graph will be a directed graph, otherwise it will be an undirected graph) @examples: - >>> random_graph(4, 1) - {0: [1, 2, 3], 1: [0, 2, 3], 2: [0, 1, 3], 3: [0, 1, 2]} - >>> random_graph(4, 1, True) - {0: [1, 2, 3], 1: [0, 2, 3], 2: [0, 1, 3], 3: [0, 1, 2]} + >>> random_graph(4, 0.5) + {0: [1], 1: [0, 2, 3], 2: [1, 3], 3: [1, 2]} + >>> random_graph(4, 0.5, True) + {0: [1], 1: [2, 3], 2: [3], 3: []} """ graph = {i: [] for i in range(vertices_number)} @@ -33,6 +33,7 @@ def random_graph( if probability <= 0: return graph + random.seed(1) # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(vertices_number): @@ -47,7 +48,7 @@ def random_graph( def complete_graph(vertices_number: int) -> dict: """ - function that generates a complete graph with vertices_number vertices + Function that generates a complete graph with vertices_number vertices. @input: vertices_number (number of vertices), directed (False if the graph is undirected, True otherwise) @example: From e15b81aebf2018cfeb089bfe70990a5834d7ac33 Mon Sep 17 00:00:00 2001 From: manueldilullo Date: Wed, 20 Oct 2021 16:29:29 +0200 Subject: [PATCH 12/12] consistency fixes --- graphs/random_graph_generator.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/graphs/random_graph_generator.py b/graphs/random_graph_generator.py index 73cbb480b3af..d7d5de8a37c0 100644 --- a/graphs/random_graph_generator.py +++ b/graphs/random_graph_generator.py @@ -13,14 +13,16 @@ def random_graph( vertices_number: int, probability: float, directed: bool = False ) -> dict: """ - Function that generates a random graph + Generate a random graph @input: vertices_number (number of vertices), probability (probability that a generic edge (u,v) exists), directed (if True: graph will be a directed graph, otherwise it will be an undirected graph) @examples: + >>> random.seed(1) >>> random_graph(4, 0.5) {0: [1], 1: [0, 2, 3], 2: [1, 3], 3: [1, 2]} + >>> random.seed(1) >>> random_graph(4, 0.5, True) {0: [1], 1: [2, 3], 2: [3], 3: []} """ @@ -33,7 +35,6 @@ def random_graph( if probability <= 0: return graph - random.seed(1) # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(vertices_number): @@ -48,7 +49,7 @@ def random_graph( def complete_graph(vertices_number: int) -> dict: """ - Function that generates a complete graph with vertices_number vertices. + Generate a complete graph with vertices_number vertices. @input: vertices_number (number of vertices), directed (False if the graph is undirected, True otherwise) @example: