Skip to content

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
69 changes: 69 additions & 0 deletions graphs/random_graph_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
* 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 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 True: g 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]}
"""
Copy link
Member

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)

Copy link
Member

@cclauss cclauss Oct 15, 2021

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 and directed False did not generate exactly the same output.

Copy link
Member

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.

Copy link
Contributor Author

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

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 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]}
"""
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()