Skip to content

added complete graph generator function #5239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions graphs/complete_graph_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
* 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:
"""
Generates 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: [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


if __name__ == "__main__":
import doctest

doctest.testmod()

# print(f"Complete graph:\n{complete_graph(5)}")