-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Random graph generator hacktoberfest #5240
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
Merged
poyea
merged 12 commits into
TheAlgorithms:master
from
manueldilullo:random_graph_generator_hacktoberfest
Oct 25, 2021
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a078715
added complete graph generator function
manueldilullo 6490327
added doctest, type hints, wikipedia explanation
manueldilullo be997e6
added return type hint for function complete_graph
manueldilullo 54ecb1e
added descriptive name for the parameter: n
manueldilullo 9ac4b64
random graph generator with doctest and type hints
manueldilullo e2d9ec1
validated using pre-commit
manueldilullo 5a01771
Delete complete_graph_generator.py
manueldilullo bebbcac
fixed doctest
manueldilullo cf7dc21
updated following reviews
manueldilullo 6204ab0
simplified the code following reviews
manueldilullo 6ee751b
fixed doctest and solved consistency issues
manueldilullo e15b81a
consistency fixes
manueldilullo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
* Author: Manuel Di Lullo (https://github.com/manueldilullo) | ||
* Description: Random graphs generator. | ||
Uses graphs represented with an adjacency list. | ||
|
||
URL: https://en.wikipedia.org/wiki/Random_graph | ||
""" | ||
|
||
import random | ||
|
||
|
||
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 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]} | ||
""" | ||
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 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 random.random() < probability: | ||
graph[i].append(j) | ||
if not directed: | ||
# if the graph is undirected, add an edge in from j to i, either | ||
graph[j].append(i) | ||
return graph | ||
|
||
|
||
def complete_graph(vertices_number: int) -> dict: | ||
""" | ||
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: | ||
>>> print(complete_graph(3)) | ||
{0: [1, 2], 1: [0, 2], 2: [0, 1]} | ||
""" | ||
return { | ||
i: [j for j in range(vertices_number) if i != j] for i in range(vertices_number) | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can also try to use
random.seed(xxx)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cool if
directed
True anddirected
False did not generate exactly the same output.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manueldilullo I just thought that these tests generate complete graphs - but we may also make it random: https://docs.python.org/3/library/random.html#random.seed. With it, directed-ness can also be (simutaneously) tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn't think about it. I couldn't find a solution for that problem by myself ahahah. I'm updating my code right now