diff --git a/.buildinfo b/.buildinfo new file mode 100644 index 0000000..63688d7 --- /dev/null +++ b/.buildinfo @@ -0,0 +1,4 @@ +# Sphinx build info version 1 +# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. +config: 6d00f62fe554e7a9fe331d18aec1fb94 +tags: 645f666f9bcd5a90fca523b33c5a78b7 diff --git a/.doctrees/environment.pickle b/.doctrees/environment.pickle new file mode 100644 index 0000000..84e6fc8 Binary files /dev/null and b/.doctrees/environment.pickle differ diff --git a/.doctrees/index.doctree b/.doctrees/index.doctree new file mode 100644 index 0000000..dac074e Binary files /dev/null and b/.doctrees/index.doctree differ diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 0000000..e69de29 diff --git a/_images/01.png b/_images/01.png new file mode 100644 index 0000000..05eb9d5 Binary files /dev/null and b/_images/01.png differ diff --git a/_modules/index.html b/_modules/index.html new file mode 100644 index 0000000..e630816 --- /dev/null +++ b/_modules/index.html @@ -0,0 +1,80 @@ + + + +
+ + +
+import itertools
+import numpy as np
+import networkx as nx
+
+
+
+[docs]
+def find_sap(G, start, target, path=None):
+
+ """
+ Finds all self-avoiding paths (SAPs) in a given graph from a start node to a target node.
+ A self-avoiding path is a path that does not revisit any node.
+
+ Parameters
+ ----------
+ graph : NetworkX graph
+ The input graph where SAPs will be found.
+ start : str or int
+ The node where the search for SAPs starts.
+ target : str or int
+ The node where the search for SAPs ends.
+ path : list, optional
+ Internal parameter used to keep track of the current path during the search.
+
+ Yields
+ ------
+ list
+ A self-avoiding path from the start node to the target node.
+
+ Examples
+ --------
+ >>> import networkx as nx
+ >>> G = nx.Graph()
+ >>> edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('E', 'F')]
+ >>> G.add_edges_from(edges)
+ >>> start_node = 'A'
+ >>> target_node = 'F'
+ >>> all_saps = list(find_sap(G, start_node, target_node))
+ >>> for path in all_saps:
+ >>> print("->".join(path))
+ """
+
+ if path is None:
+ path = []
+
+ if len(G.nodes()) == 0:
+ return []
+
+ path.append(start)
+
+ if start == target:
+ yield path[:]
+ else:
+ for neighbor in G.neighbors(start):
+ if neighbor not in path:
+ yield from find_sap(G, neighbor, target, path)
+
+ path.pop()
+
+
+
+
+[docs]
+def is_hamiltonian_path(G, path):
+ '''
+ Check if a given path is a Hamiltonian path in a graph.
+
+ Parameters:
+ -----------
+ G : networkx.Graph, networkx.DiGraph)
+ The input graph.
+ path : list of str or int
+ list of nodes in the path.
+
+ Returns:
+ -----------
+ bool :
+ True if the path is a Hamiltonian path, otherwise False
+
+ '''
+ return all(G.has_edge(path[i], path[i + 1]) for i in range(len(path) - 1))
+
+
+
+
+[docs]
+def find_hamiltonian_path(G):
+ '''
+ find the Hamiltonian path in given graph.
+
+ Parameters
+ -----------
+ G: nx.Graph or nx.DiGraph
+ input graph.
+
+ Returns
+ value : list of nodes in Hamiltonian path if exists, otherwise None.
+
+ '''
+ nodes = list(G.nodes())
+ for perm in itertools.permutations(nodes):
+ if is_hamiltonian_path(G, perm):
+ return perm
+ return None
+
+
+
+
+[docs]
+def check_connectivity(G):
+ '''
+ Check if the graph is connected.
+
+ Parameters
+ --------------
+ G : networkx.Graph, networkx.DiGraph
+ The input graph.
+
+ Returns
+ ------------
+
+ connectivity: (str)
+ for directed graphs, it returns
+ - "weakly connected"
+ - "strongly connected"
+ - "disconnected".
+ for undirected graphs,
+ - "connected"
+ - "disconnected".
+ '''
+
+ is_directed = isinstance(G, nx.DiGraph)
+
+ if is_directed:
+ if nx.is_weakly_connected(G):
+ return "weakly connected"
+ elif nx.is_strongly_connected(G):
+ return "strongly connected"
+ else:
+ return "disconnected"
+ else:
+ if nx.is_connected(G):
+ return "connected"
+ else:
+ return "disconnected"
+
+
+
+[docs]
+def graph_info(G, quick=True):
+ """
+ Generate various graph information.
+
+ Parameters
+ -------------
+ G : (networkx.Graph, networkx.DiGraph)
+ The input graph for which the information is to be generated.
+
+
+ """
+ is_directed = isinstance(G, nx.DiGraph)
+
+ # number_of_triangles = #TODO
+
+ connectivity = check_connectivity(G)
+
+ if not quick:
+ if connectivity == "strongly connected" or connectivity == "connected":
+ diameter = nx.diameter(G)
+ else:
+ diameter = -1
+
+ print("Graph information")
+ print(f"{'Directed':40s}: {str(is_directed):>20s}")
+ print(f"{'Number of nodes':40s}: {len(G.nodes()):20d}")
+ print(f"{'Number of edges':40s}: {len(G.edges()):20d}")
+ print(f"{'Average degree':40s}: {sum(dict(G.degree).values()) / len(G.nodes):20.4f}")
+ print(f"{'Connectivity':40s}: {connectivity:>20s}")
+ if not quick:
+ print(f"{'Diameter':40s}: {diameter:20d}")
+ print(f"{'Average clustering coefficient':40s}: {nx.average_clustering(G):20.6f}")
+
+
+ # return {
+ # "Directed": is_directed,
+ # "Number of nodes": len(G.nodes()),
+ # "Number of edges": len(G.edges()),
+ # "average_degree": sum(dict(G.degree).values()) / len(G.nodes),
+ # "diameter": diameter,
+ # "average clustering coefficient": nx.average_clustering(G),
+
+ # }
+
+
+[docs]
+def longest_shortest_path(G):
+ """
+ Calculate the longest shortest path (diameter) in a given graph.
+
+ Parameters
+ -------------
+ G (networkx.Graph or networkx.DiGraph):
+ The input graph, which can be directed or undirected.
+ The graph should be connected, otherwise the diameter will not be defined.
+
+ Returns
+ ---------
+ value : int, float
+ The longest shortest path (diameter) in the graph.
+ If the graph is empty, returns 0.
+ If the graph is not connected, returns float('inf').
+ """
+ path_lengths = dict(nx.all_pairs_shortest_path_length(G))
+ diameter = max(max(lengths.values()) for lengths in path_lengths.values())
+
+ return diameter
+
+
+
+
+[docs]
+def average_degree(G):
+ """
+ Calculate the average degree of a graph.
+
+ Parameters
+ -------------
+ G (networkx.Graph or networkx.DiGraph):
+ The input graph, which can be directed or undirected.
+
+ Returns
+ -----------
+ vlaue: float
+ The average degree of the graph.
+ """
+
+ degrees = [d for n, d in G.degree()]
+ average_degree = sum(degrees) / len(degrees)
+ return average_degree
+
+
+import itertools
+import numpy as np
+import networkx as nx
+import matplotlib.pyplot as plt
+
+
+
+
+[docs]
+def plot_graph(G, **kwargs):
+ """
+ Plots a NetworkX graph with customizable options.
+
+ Parameters
+ ----------
+ G : NetworkX graph
+ A NetworkX graph object (e.g., nx.Graph, nx.DiGraph).
+ **kwargs : keyword arguments
+ Additional keyword arguments to customize the plot. These can include:
+
+ node_color : str or list, optional
+ Color of the nodes (can be a single color or a list of colors).
+ node_size : int or list, optional
+ Size of the nodes (single value or list of sizes).
+ edge_color : str or list, optional
+ Color of the edges (can be a single color or a list of colors).
+ width : float, optional
+ Width of the edges.
+ with_labels : bool, optional
+ Whether to draw node labels or not.
+ font_size : int, optional
+ Size of the font for node labels.
+ font_color : str, optional
+ Color of the font for node labels.
+ title : str, optional
+ Title of the plot.
+ seed : int, optional
+ Seed for the random layout algorithm.
+ figsize : tuple, optional
+ Size of the figure.
+ ax: axes object
+ Axes object to draw the plot on. Defaults to None, which will create a new figure.
+ pos: object, optional
+ Graph layout (e.g., nx.spring_layout, nx.circular_layout), nx.kamada_kaway_layout(G).
+ Defaults to nx.spring_layout(G).
+
+ """
+
+ # Extracting optional arguments
+ node_color = kwargs.get("node_color", "lightblue")
+ node_size = kwargs.get("node_size", 300)
+ edge_color = kwargs.get("edge_color", "black")
+ width = kwargs.get("width", 1.0)
+ with_labels = kwargs.get("with_labels", True)
+ font_size = kwargs.get("font_size", 12)
+ font_color = kwargs.get("font_color", "black")
+ title = kwargs.get("title", None)
+ seed = kwargs.get("seed", None)
+ edge_labels = kwargs.get("edge_labels", None)
+ figsize = kwargs.get("figsize", (4, 4))
+ ax = kwargs.get("ax", None)
+ pos = kwargs.get("pos", None)
+
+ if ax is None:
+ fig, ax = plt.subplots(1, figsize=figsize)
+ ax.axis("off")
+
+ if seed is not None:
+ np.random.seed(seed)
+
+ if pos is None:
+ pos = nx.spring_layout(
+ G, seed=seed
+ )
+
+ # Draw the network
+ nx.draw(
+ G,
+ pos,
+ node_color=node_color,
+ node_size=node_size,
+ edge_color=edge_color,
+ width=width,
+ with_labels=with_labels,
+ font_size=font_size,
+ font_color=font_color,
+ ax=ax
+ )
+
+ if edge_labels is not None:
+ nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
+
+ # Set the plot title
+ if title is not None:
+ plt.title(title)
+
+ return ax
+
+
+ # Show the plot
+ # plt.show()
+
+import os
+import gzip
+import json
+import numpy as np
+import networkx as nx
+from numpy import power
+from os.path import join
+from cycler import cycler
+from scipy.special import zeta
+from scipy.optimize import bisect
+
+
+try:
+ import powerlaw
+except:
+ pass
+
+
+
+[docs]
+def get_adjacency_list(G):
+ """
+ Generate an adjacency list representation of a given graph.
+
+ Parameters
+ -------------
+ G (networkx.Graph, networkx.DiGraph):
+ The input graph for which the adjacency list is to be generated.
+
+ Returns
+ ---------
+ value: dict
+ A dictionary where each key is a node in the graph and the corresponding value is a list of neighboring nodes.
+ """
+ return {n: list(neighbors) for n, neighbors in G.adj.items()}
+
+
+
+# def _load_graph(file_path, kind, url):
+
+# path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+# path = os.path.join(path, 'netsci/datasets')
+
+# if not os.path.isfile(file_path):
+# os.system(f"wget -P {path} {url}")
+
+# if os.path.isfile(file_path):
+# os.system(f"gunzip -k {file_path}")
+
+# with gzip.open(file_path, 'rt') as f:
+# G = nx.read_adjlist(file_path, create_using=kind)
+
+# os.remove(file_path[:-3])
+# return G
+
+def _load_graph(file_path, url, directed, verbose=False):
+
+ path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ path = join(path, 'netsci/datasets')
+ path_zip = join(path, "networks.zip")
+
+ if not os.path.isfile(file_path):
+ if not os.path.isfile(path_zip):
+ os.system(f"wget -P {path} {url}")
+
+ if not os.path.isfile(file_path):
+ if os.path.isfile(path_zip):
+ os.system(f"unzip {path_zip} -d {path}")
+
+
+ # Step 1: Read the adjacency list from the file
+ edges = []
+ with open(file_path, 'r') as file:
+ for line in file:
+ if line.startswith('#'):
+ continue # Skip comments
+ A, B = map(int, line.split())
+ edges.append((A, B))
+
+ # Step 2: Create the graph
+ G = nx.DiGraph()
+ G.add_edges_from(edges)
+
+ # Step 3: Determine if the graph is directed
+ # is_directed = False
+ # for A, B in edges:
+ # if not G.has_edge(B, A):
+ # is_directed = True
+ # break
+
+ if not directed:
+ G = G.to_undirected()
+ return G
+
+
+
+
+[docs]
+def load_sample_graph(name, verbose=False):
+ """
+ Load a graph and return it as a NetworkX graph.
+
+ Parameters
+ --------------
+ name: str
+ The name of the graph. Get names from `netsci.utils.show_sample_graphs()`.
+ verbose: bool, optional
+ If True, print information about the loaded graph. Default is True.
+
+ Returns
+ -----------
+ value: networkx.Graph
+ Loaded graph.
+ """
+
+ path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ path = os.path.join(path, 'netsci/datasets/')
+
+ with open(os.path.join(path,'sample_graphs.json'), 'r') as f:
+ data = json.load(f)
+ if name in list(data.keys()):
+ filename = data[name]['filename']
+ file_path = os.path.join(path, f'{filename}')
+ directed = data[name]['directed']
+ G = _load_graph(file_path,
+ url=data[name]['url'],
+ directed=directed,
+ verbose=verbose)
+ if verbose:
+ print(f'Successfully loaded {name}')
+ print('================================')
+ print(data[name]['description'])
+ return G
+
+
+
+
+[docs]
+def list_sample_graphs():
+ '''
+ make a list of available real world graphs on datasets
+ '''
+
+ path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ path = os.path.join(path, 'netsci/datasets')
+ # names = [f[:-7] for f in os.listdir(path) if f.endswith('.txt.gz')]
+ # read json file sample_graphs.json
+
+ with open(os.path.join(path,'sample_graphs.json'), 'r') as f:
+ data = json.load(f)
+
+ return data
+
+
+
+
+[docs]
+def generate_power_law_dist_bounded(N:int, a:float, xmin:float, xmax:float, seed:int=-1):
+ '''
+ Generate a power law distribution of floats p(k) ~ x^(-a) for a>1
+ which is bounded by xmin and xmax
+
+ parameters :
+ N: int
+ number of samples in powerlaw distribution (pwd).
+ a:
+ exponent of the pwd.
+ xmin:
+ min value in pwd.
+ xmax:
+ max value in pwd.
+ '''
+
+ from numpy.random import rand, randint
+ from numpy import power
+
+ data = np.zeros(N)
+ x0p = power(xmin, (-a+1.0))
+ x1p = power(xmax, (-a+1.0))
+ alpha = 1.0/(-a+1.0)
+
+ for i in range(N):
+ r = rand()
+ data[i] = (power((x1p - x0p)*r + x0p, alpha))
+ return data
+
+
+
+
+[docs]
+def generate_power_law_dist(N:int, a:float, xmin:float):
+ '''
+ generate power law random numbers p(k) ~ x^(-a) for a>1
+
+ Parameters
+ -----------
+ N:
+ is the number of random numbers
+ a:
+ is the exponent
+ xmin:
+ is the minimum value of distribution
+
+ Returns
+ -----------
+ value: np.array
+ powerlaw distribution
+ '''
+
+ # generates random variates of power law distribution
+ vrs = powerlaw.Power_Law(xmin=xmin, parameters=[a]).generate_random(N)
+
+ return vrs
+
+
+
+[docs]
+def generate_power_law_discrete(
+ N:int, a:float, xmin:float, xmax:float, seed: int = -1
+ ):
+ """
+ Generate a power law distribution of p(k) ~ x^(-a) for a>1,
+ with discrete values.
+
+ Parameters:
+ -----------
+ N: int
+ Number of samples in the distribution.
+ a: float
+ Exponent of the power law distribution.
+ xmin: float
+ Minimum value in the power law distribution.
+ xmax: float
+ Maximum value in the power law distribution.
+ seed :int, optional
+ Seed for reproducibility. Defaults to -1.
+
+ Returns:
+ -------
+ np.array
+ Power law distribution with discrete values.
+ """
+
+ if seed != -1:
+ np.random.seed(seed)
+
+ if seed != None:
+ np.random.seed(seed)
+
+ X = np.zeros(N, dtype=int)
+ x1p = power(xmax, (-a + 1.0))
+ x0p = power(xmin, (-a + 1.0))
+ alpha = 1.0/(-a + 1.0)
+
+ for i in range(N):
+ r = np.random.rand()
+ X[i] = int(np.round(power(((x1p - x0p)*r + x0p), alpha)))
+
+ #sum of degrees should be positive
+ from random import randint
+ if ((np.sum(X)%2 )!= 0):
+ i = randint(0, N-1)
+ X[i] = X[i]+1
+
+ return X
+
+
+
+
+[docs]
+def tune_min_degree(
+ N:int, a:float, xmin:int, xmax:int, max_iteration:int=100
+ ):
+ '''
+ Find the minimum degree value of a power law graph that results in a connected graph
+ '''
+
+ for i in range(max_iteration):
+ seq = generate_power_law_discrete(N, a, xmin, xmax, seed=i)
+ if np.sum(seq) % 2 != 0:
+ raise ValueError("The sum of degrees should be even")
+ G = nx.configuration_model(seq)
+ G.remove_edges_from(G.selfloop_edges())
+ G = nx.Graph(G)
+ seq1 = np.asarray([deg for (node, deg) in G.degree_iter()])
+ avg_degree = np.mean(seq1)
+
+ if nx.is_connected(G):
+ break
+ if i == (max_iteration-1):
+ raise ValueError("Unable to find a connected graph with the given parameters")
+ return avg_degree, G
+
+
+
+[docs]
+def make_powerlaw_graph(
+ N: int, a: float, avg_degree:int, xmin:int=1, xmax:int=10000,
+ seed: int = -1, xtol=0.01, degree_interval=5.0, plot=False,
+ **kwargs
+ ):
+
+ '''
+ make a powerlaw graph with the given parameters
+
+ Parameters
+ ----------
+ N:
+ number of nodes
+ a: float
+ exponent of the power law distribution
+ avg_degree:
+ expected average degree
+ xmin: int, optional
+ minimum value in the power law distribution. Default is 1.
+ xmax: int, optional
+ maximum value in the power law distribution. Default is 10000.
+ seed: int, optional
+ Seed for reproducibility. Default is -1.
+ xtol: float, optional
+ tolerance for bisection method. Default is 0.01.
+ degree_interval: float, optional
+ interval for bisection method. Default is 5.0.
+ plot: bool, optional
+ If True, plot the power law distribution. Default is False.
+ kwargs: obtional
+ additional keyword arguments for plot_pdf function.
+
+ '''
+
+ color = kwargs.get('color', 'k')
+ linestyle = kwargs.get('linestyle', '-')
+ lw=kwargs.get('lw', 2)
+
+ xmin_tuned, G = bisect(lambda x: tune_min_degree(
+ N, a, x, xmax) - avg_degree, xmin, xmin+degree_interval, xtol=xtol)
+ sample_seq = np.asarray([deg for (node, deg) in G.degree_iter()])
+ avg_degree = np.mean(sample_seq)
+
+ fit = powerlaw.Fit(sample_seq, discrete=True)
+ if plot:
+ ax = fit.plot_pdf(linewidth=2, label=str('pdf, %.2f'% a));
+ fit.power_law.plot_pdf(c=color, linestyle=linestyle, lw=lw, ax=ax);
+
+ return {
+ "G": G,
+ "avg_degree": avg_degree,
+ "xmin_tuned": xmin_tuned,
+ "fit": fit,
+ "ax": ax,
+ }
+
+
+
+
+
+[docs]
+def generate_power_law_discrete_its(alpha:float, k_min:int, k_max:int, size:int=1):
+
+ """
+ Generates the power law discrete distributions using the inverse transform sampling method.
+
+ References
+ -----------
+
+ Devroye, L. (1986). "Non-Uniform Random Variate Generation." Springer-Verlag, New York.
+
+ Parameters
+ ----------
+ alpha :
+ Power law exponent.
+ k_min :
+ Minimum degree.
+ k_max :
+ Maximum degree.
+ size :
+ Number of samples to generate. Defaults to 1.
+
+ Returns
+ -------
+ np.array:
+ Array of generated power law discrete distributions.
+
+
+ Examples
+ ---------
+
+ >>> gamma = 2.5 # Power-law exponent
+ >>> k_min = 1 # Minimum value of k
+ >>> k_max = 1000 # Maximum value of k
+ >>> size = 10000 # Number of samples
+ >>> samples = power_law_discrete(gamma, k_min, k_max, size)
+ """
+
+
+ # Calculate the normalization constant
+ norm = zeta(alpha, k_min) - zeta(alpha, k_max + 1)
+
+ # Generate uniform random numbers
+ u = np.random.random(size=size)
+
+ # Initialize the result array
+ result = np.zeros(size, dtype=int)
+
+ # Inverse transform sampling
+ for i in range(size):
+ cdf = 0
+ for k in range(k_min, k_max + 1):
+ cdf += (k ** -alpha) / norm
+ if u[i] <= cdf:
+ result[i] = k
+ break
+
+ return result
+
+
' + + '' + + _("Hide Search Matches") + + "
" + ) + ); + }, + + /** + * helper function to hide the search marks again + */ + hideSearchWords: () => { + document + .querySelectorAll("#searchbox .highlight-link") + .forEach((el) => el.remove()); + document + .querySelectorAll("span.highlighted") + .forEach((el) => el.classList.remove("highlighted")); + localStorage.removeItem("sphinx_highlight_terms") + }, + + initEscapeListener: () => { + // only install a listener if it is really needed + if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) return; + + document.addEventListener("keydown", (event) => { + // bail for input elements + if (BLACKLISTED_KEY_CONTROL_ELEMENTS.has(document.activeElement.tagName)) return; + // bail with special keys + if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return; + if (DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS && (event.key === "Escape")) { + SphinxHighlight.hideSearchWords(); + event.preventDefault(); + } + }); + }, +}; + +_ready(() => { + /* Do not call highlightSearchWords() when we are on the search page. + * It will highlight words from the *previous* search query. + */ + if (typeof Search === "undefined") SphinxHighlight.highlightSearchWords(); + SphinxHighlight.initEscapeListener(); +}); diff --git a/genindex.html b/genindex.html new file mode 100644 index 0000000..4e90aef --- /dev/null +++ b/genindex.html @@ -0,0 +1,251 @@ + + + + + + ++ |
+ |
+ | + |
+ | + |
+ |
+ | + |
|
+
+ | + |
+ |
|
+
+ |
Barabási, A.L., 2013. Network science. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences, 371(1987), p.20120375.
using on Colab (Recommended)
++++
+- +
Go to examples
- +
Open a notebook and click on “open on colab”
- +
Uncomment the cell with pip install command to install the netsci package.
using on local machines
pip3 install -e .
+# or
+pip install "git+https://github.com/Ziaeemehr/netsci.git"
+
First import the necessary libraries:
+import networkx as nx
+import matplotlib.pyplot as plt
+from netsci.plot import plot_graph
+from netsci.analysis import find_sap
+
Next, create a simple graph with self-avoiding edges (SAE) between nodes.
+G = nx.Graph()
+edges = [("A", "B"), ("A", "C"), ("B", "D"), ("B", "E"), ("C", "F"), ("E", "F")]
+G.add_edges_from(edges)
+
Now, find all self-avoiding paths from a given start node to a target node.
+start_node = "A"
+target_node = "F"
+all_saps = list(find_sap(G, start_node, target_node))
+
+for path in all_saps:
+ print("->".join(path))
+
Finally, visualize the graph.
+plot_graph(G, seed=2, figsize=(3, 3))
+
Finds all self-avoiding paths (SAPs) in a given graph from a start node to a target node. +A self-avoiding path is a path that does not revisit any node.
+The input graph where SAPs will be found.
+The node where the search for SAPs starts.
+The node where the search for SAPs ends.
+Internal parameter used to keep track of the current path during the search.
+A self-avoiding path from the start node to the target node.
+Examples
+>>> import networkx as nx
+>>> G = nx.Graph()
+>>> edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('E', 'F')]
+>>> G.add_edges_from(edges)
+>>> start_node = 'A'
+>>> target_node = 'F'
+>>> all_saps = list(find_sap(G, start_node, target_node))
+>>> for path in all_saps:
+>>> print("->".join(path))
+
find the Hamiltonian path in given graph.
+input graph.
+Check if the graph is connected.
+The input graph.
+for directed graphs, it returns +- “weakly connected” +- “strongly connected” +- “disconnected”. +for undirected graphs, +- “connected” +- “disconnected”.
+Generate various graph information.
+The input graph for which the information is to be generated.
+Calculate the longest shortest path (diameter) in a given graph.
+The input graph, which can be directed or undirected. +The graph should be connected, otherwise the diameter will not be defined.
+The longest shortest path (diameter) in the graph. +If the graph is empty, returns 0. +If the graph is not connected, returns float(‘inf’).
+Generate an adjacency list representation of a given graph.
+The input graph for which the adjacency list is to be generated.
+A dictionary where each key is a node in the graph and the corresponding value is a list of neighboring nodes.
+Load a graph and return it as a NetworkX graph.
+The name of the graph. Get names from netsci.utils.show_sample_graphs()
.
If True, print information about the loaded graph. Default is True.
+Loaded graph.
+Generate a power law distribution of floats p(k) ~ x^(-a) for a>1 +which is bounded by xmin and xmax
+number of samples in powerlaw distribution (pwd).
+exponent of the pwd.
+min value in pwd.
+max value in pwd.
+generate power law random numbers p(k) ~ x^(-a) for a>1
+is the number of random numbers
+is the exponent
+is the minimum value of distribution
+powerlaw distribution
+Generate a power law distribution of p(k) ~ x^(-a) for a>1, +with discrete values.
+Find the minimum degree value of a power law graph that results in a connected graph
+make a powerlaw graph with the given parameters
+number of nodes
+exponent of the power law distribution
+expected average degree
+minimum value in the power law distribution. Default is 1.
+maximum value in the power law distribution. Default is 10000.
+Seed for reproducibility. Default is -1.
+tolerance for bisection method. Default is 0.01.
+interval for bisection method. Default is 5.0.
+If True, plot the power law distribution. Default is False.
+additional keyword arguments for plot_pdf function.
+Generates the power law discrete distributions using the inverse transform sampling method.
+Power law exponent.
+Minimum degree.
+Maximum degree.
+Number of samples to generate. Defaults to 1.
+Array of generated power law discrete distributions.
+References
+Devroye, L. (1986). “Non-Uniform Random Variate Generation.” Springer-Verlag, New York.
+Examples
+>>> gamma = 2.5 # Power-law exponent
+>>> k_min = 1 # Minimum value of k
+>>> k_max = 1000 # Maximum value of k
+>>> size = 10000 # Number of samples
+>>> samples = power_law_discrete(gamma, k_min, k_max, size)
+
Plots a NetworkX graph with customizable options.
+A NetworkX graph object (e.g., nx.Graph, nx.DiGraph).
+Additional keyword arguments to customize the plot. These can include:
+Color of the nodes (can be a single color or a list of colors).
+Size of the nodes (single value or list of sizes).
+Color of the edges (can be a single color or a list of colors).
+Width of the edges.
+Whether to draw node labels or not.
+Size of the font for node labels.
+Color of the font for node labels.
+Title of the plot.
+Seed for the random layout algorithm.
+Size of the figure.
+Axes object to draw the plot on. Defaults to None, which will create a new figure.
+Graph layout (e.g., nx.spring_layout, nx.circular_layout), nx.kamada_kaway_layout(G). +Defaults to nx.spring_layout(G).
++ n | ||
+ |
+ netsci | + |
+ |
+ netsci.analysis | + |
+ |
+ netsci.plot | + |
+ |
+ netsci.utils | + |
+ q | ||
+ |
+ quick_start | + |
+ Searching for multiple words only shows matches that contain + all words. +
+ + + + + + + + + +