-
Notifications
You must be signed in to change notification settings - Fork 0
Use igraph instead of NetworkX #70
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
960f11c
Drop networkx from util.py
mdbrnowski 285911d
fixup! Drop networkx from util.py
mdbrnowski 0b968be
Use igraph where possible
mdbrnowski 1ac1876
Update README
mdbrnowski 202f101
Minor optimization
mdbrnowski 4694421
Bump version
mdbrnowski 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
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
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
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
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 |
|---|---|---|
| @@ -1,54 +1,65 @@ | ||
| import networkx as nx | ||
| import igraph as ig | ||
|
|
||
|
|
||
| def _find_closure(G: nx.Graph) -> nx.Graph: | ||
| def _find_closure(G: ig.Graph) -> ig.Graph: | ||
| """Returns a graph cl(G) defined as in Bondy-Chvátal Theorem.""" | ||
| G = G.copy() | ||
| n = G.number_of_nodes() | ||
| n = G.vcount() | ||
| edges_to_add = [] | ||
| for v in G.nodes: | ||
| for u in G.nodes - G.neighbors(v) - {v}: | ||
| if G.degree[v] + G.degree[u] >= n: | ||
| edges_to_add.append((v, u)) | ||
| for v in range(n): | ||
| for u in range(v + 1, n): | ||
| if not G.are_adjacent(u, v): | ||
| if G.degree(v) + G.degree(u) >= n: | ||
| edges_to_add.append((v, u)) | ||
|
|
||
| while edges_to_add: | ||
| v, u = edges_to_add.pop() | ||
| G.add_edge(v, u) | ||
| for w in G.nodes - G.neighbors(v) - {v}: | ||
| if G.degree[w] + G.degree[v] >= n: | ||
| edges_to_add.append((w, v)) | ||
| for w in G.nodes - G.neighbors(u) - {u}: | ||
| if G.degree[w] + G.degree[u] >= n: | ||
| edges_to_add.append((w, u)) | ||
| if not G.are_adjacent(v, u): # check if edge already exists | ||
| G.add_edges([(v, u)]) | ||
| for w in range(n): | ||
| if w != v and not G.are_adjacent(w, v): | ||
| if G.degree(w) + G.degree(v) >= n: | ||
| edges_to_add.append((w, v)) | ||
| for w in range(n): | ||
| if w != u and not G.are_adjacent(w, u): | ||
| if G.degree(w) + G.degree(u) >= n: | ||
| edges_to_add.append((w, u)) | ||
|
|
||
| return G | ||
|
|
||
|
|
||
| def is_hamiltonian(G: nx.Graph) -> bool: | ||
| def is_hamiltonian(G: ig.Graph) -> bool: | ||
| """Checks whether a graph G is hamiltonian or not.""" | ||
| n = G.number_of_nodes() | ||
| n = G.vcount() | ||
| if n == 1: | ||
| return True | ||
| if n == 2: | ||
| return False | ||
| if not nx.is_connected(G) or nx.is_tree(G): | ||
| if not G.is_connected(): | ||
| return False | ||
| if not nx.is_biconnected(G): | ||
| if G.ecount() == n - 1: # is a tree | ||
| return False | ||
| if len(G.articulation_points()) > 0: # is not biconnected | ||
| return False | ||
|
|
||
| cl_G = _find_closure(G) | ||
| if ( | ||
| cl_G.number_of_edges() | ||
| == cl_G.number_of_nodes() * (cl_G.number_of_nodes() - 1) / 2 | ||
| ): | ||
| if cl_G.ecount() == n * (n - 1) / 2: # is a complete graph | ||
| return True | ||
|
|
||
| return nx.isomorphism.GraphMatcher(G, nx.cycle_graph(n)).subgraph_is_monomorphic() | ||
| cycle = ig.Graph.Ring(n) | ||
| return G.subisomorphic_vf2(cycle) | ||
|
|
||
|
|
||
| def is_eulerian(graph: ig.Graph) -> bool: | ||
| """Checks whether a graph has an Eulerian circuit.""" | ||
| if not graph.is_connected(): | ||
| return False | ||
| return all(degree % 2 == 0 for degree in graph.degree()) | ||
|
|
||
|
|
||
| def max_degree(graph: nx.Graph) -> int: | ||
| return max(d for _, d in graph.degree()) | ||
| def max_degree(graph: ig.Graph) -> int: | ||
| return max(graph.degree()) | ||
|
|
||
|
|
||
| def min_degree(graph: nx.Graph) -> int: | ||
| return min(d for _, d in graph.degree()) | ||
| def min_degree(graph: ig.Graph) -> int: | ||
| return min(graph.degree()) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.