Skip to content
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

Change G(n,p) to accept exact 0 or 1 probabilities #174

Merged
merged 1 commit into from Oct 18, 2020

Conversation

MoAllabbad
Copy link
Contributor

@MoAllabbad MoAllabbad commented Oct 18, 2020

When p=0, we'll have an empty graph with n nodes and zero edges.
When p=1, we'll have a complete graph with n(n-1) edges
for directed graphs and n(n-1)/2 edges for undirected graphs.
The time complexity stays the same. Let m be the max number of edges,
then run time is O(n+p*m), which reduces to O(n) when p=0 and, when
p=1 becomes O(n+n(n-1)) = O(n^2).
Fixes #172

@CLAassistant
Copy link

CLAassistant commented Oct 18, 2020

CLA assistant check
All committers have signed the CLA.

When p=0, we'll have an empty graph with n nodes and zero edges.
When p=1, we'll have a complete graph with n n(n-1) edges
for directed graphs and n(n-1)/2 edges for undirected graphs.
The time complexity stays the same. Let m be the max number of edges,
then run time is O(n+p*m), which reduces to O(n) when p=0 and, when
p=1 becomes O(n+n(n-1)) = O(n^2).
@coveralls
Copy link

Pull Request Test Coverage Report for Build 313529560

  • 52 of 52 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 93.423%

Totals Coverage Status
Change from base Build 303299955: 0.04%
Covered Lines: 2216
Relevant Lines: 2372

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, thanks for pushing this up!

@mtreinish mtreinish merged commit 7db288c into Qiskit:master Oct 18, 2020
mtreinish added a commit to mtreinish/retworkx that referenced this pull request Oct 18, 2020
In Qiskit#174 the gnp random functions were extended to support values of 0
and 1 for the probability representing either empty of full graphs. In
that change comparisons between the value of probability and 1 were used
to check if it's a full graph and we should just fast path adding an
edge between every node. However, when running 'cargo clippy' on this it
rightfully points out that we should probably check for equality to 1
within an error margin (given that floating point is never exact).
Clippy suggested replacing using std::f64::EPSILON as the error value,
which is ~2.2e-16 [1] and replacing the comparison with:

(probability - 1.0).abs() < error

This commit just makes that change. While this is unlikely to cause any
issues in practice, because probability is a parameter and if someone is
going to want a full graph they'll likely call the function with
probability=1 in python. It's better to be safe just in case someone is
computing the probability value and could have some error on the value.

[1] https://doc.rust-lang.org/std/f64/constant.EPSILON.html
mtreinish added a commit that referenced this pull request Oct 18, 2020
In #174 the gnp random functions were extended to support values of 0
and 1 for the probability representing either empty of full graphs. In
that change comparisons between the value of probability and 1 were used
to check if it's a full graph and we should just fast path adding an
edge between every node. However, when running 'cargo clippy' on this it
rightfully points out that we should probably check for equality to 1
within an error margin (given that floating point is never exact).
Clippy suggested replacing using std::f64::EPSILON as the error value,
which is ~2.2e-16 [1] and replacing the comparison with:

(probability - 1.0).abs() < error

This commit just makes that change. While this is unlikely to cause any
issues in practice, because probability is a parameter and if someone is
going to want a full graph they'll likely call the function with
probability=1 in python. It's better to be safe just in case someone is
computing the probability value and could have some error on the value.

[1] https://doc.rust-lang.org/std/f64/constant.EPSILON.html
@MoAllabbad
Copy link
Contributor Author

This LGTM, thanks for pushing this up!

Awesome! Thanks as well.

MoAllabbad added a commit to MoAllabbad/retworkx that referenced this pull request Oct 19, 2020
This adds two G(n,m) graph generators, one directed and one undirected.
The generated graph will be a random graph out of all the possible
graphs that are n nodes and m edges with max n*(n-1) edges for directed
graphs and n*(n-1)/2 for undirected graphs, avoiding self-loops and
multigraphs. The run time is O(n+m)
Fixes Qiskit#174
mtreinish pushed a commit that referenced this pull request Oct 19, 2020
* Add G(n,m) random graph generator

This adds two G(n,m) graph generators, one directed and one undirected.
The generated graph will be a random graph out of all the possible
graphs that are n nodes and m edges with max n*(n-1) edges for directed
graphs and n*(n-1)/2 for undirected graphs, avoiding self-loops and
multigraphs. The run time is O(n+m)
Fixes #174

* Change !.is_some() to is_none()

Apply suggestions from code review
@MoAllabbad MoAllabbad deleted the gnp-zero-one-probabilities branch October 21, 2020 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

retworkx random G(n,p) does not accept exact 0 and 1 probabilities
4 participants