### Install igraph from source using pip (no binary) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Forces pip to install igraph by compiling from source, even if a binary package is available. This is useful for troubleshooting or when targeting specific build configurations. It ensures that the installation process builds directly from the source code. ```bash pip install --no-binary ":all:" igraph ``` -------------------------------- ### Verify PyCairo Installation with igraph Plotting Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst A simple Python script to verify that PyCairo is installed correctly and can be used by igraph to plot a graph. Successful execution displays a Petersen graph. ```python import igraph as ig g = ig.Graph.Famous("petersen") ig.plot(g) ``` -------------------------------- ### Run python-igraph Unit Tests directly using tox Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Runs all unit tests for python-igraph by invoking 'tox' directly from the root of the igraph source tree. This requires 'tox' to be installed system-wide. ```bash $ tox ``` -------------------------------- ### Import igraph and create a graph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/visualisation.rst This snippet demonstrates the basic setup for graph visualization in igraph by importing the library and creating a sample graph object. ```python import igraph as ig g = ig.Graph(edges=[[0, 1], [2, 3]]) ``` -------------------------------- ### Install Matplotlib for igraph Plotting Backend Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs the Matplotlib library, an alternative plotting backend for igraph. This is used when the default Cairo backend is not available or preferred. ```bash $ pip install matplotlib ``` -------------------------------- ### Enable GraphML support on Windows with vcpkg Source: https://github.com/igraph/python-igraph/blob/main/README.md To enable GraphML support on Windows, libxml2 must be installed using vcpkg. This involves installing vcpkg, then installing libxml2, integrating vcpkg into the build environment, and setting specific environment variables before building igraph. ```bash vcpkg.exe install libxml2:x64-windows-static-md ``` ```bash vcpkg.exe integrate install ``` ```bash set IGRAPH_CMAKE_EXTRA_ARGS=-DVCPKG_TARGET_TRIPLET=x64-windows-static-md -DCMAKE_TOOLCHAIN_FILE=[vcpkg build script] ``` ```bash set IGRAPH_EXTRA_LIBRARY_PATH=[vcpkg directory]/installed/x64-windows-static-md/lib/ ``` ```bash set IGRAPH_STATIC_EXTENSION=True ``` ```bash set IGRAPH_EXTRA_LIBRARIES=libxml2,lzma,zlib,iconv,charset ``` ```bash set IGRAPH_EXTRA_DYNAMIC_LIBRARIES: wsock32,ws2_32 ``` ```bash pip install . ``` -------------------------------- ### Build python-igraph Wheel using build and pipx Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Builds an installable Python wheel for python-igraph using the 'build' package, invoked via 'pipx' to ensure a clean, isolated build environment. ```bash $ pipx run build ``` -------------------------------- ### Run python-igraph Unit Tests using tox and pipx Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Executes all unit tests for python-igraph within an isolated environment managed by 'pipx' and 'tox'. This is a standard way to verify the installation and code integrity. ```bash $ pipx run tox ``` -------------------------------- ### Import igraph Python Library Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates the standard method for importing the igraph library in Python using the alias 'ig'. It also shows how to print the installed version of the library. ```python import igraph as ig print(ig.__version__) ``` -------------------------------- ### Install igraph using pip (Virtual Environment - Direct Execution) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs igraph in a virtual environment without activating it first. This method directly calls the pip executable within the virtual environment's directory, useful for scripting or when activation is not desired. ```bash python -m venv my_environment my_environment/bin/pip install igraph ``` -------------------------------- ### Install PyCairo for igraph Plotting Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs the PyCairo package, which is a dependency for igraph's default plotting backend. This command should be run after installing Cairo headers using your system's package manager or Homebrew. ```bash $ pip install pycairo ``` -------------------------------- ### Install development version of igraph using pip Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs the development version of igraph directly from its GitHub repository using pip. This command fetches the latest code, compiles the C core as a submodule, and then builds the Python interface. ```bash pip install git+https://github.com/igraph/python-igraph ``` -------------------------------- ### Plotting Graphs with Layouts Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates how to plot a graph using a generated layout object with the `ig.plot` function. This example shows how to use the Kamada-Kawai layout and plot it directly or with Matplotlib. It also illustrates adding vertex labels and colors based on attributes. ```python >>> import igraph as ig >>> import matplotlib.pyplot as plt >>> # Assume 'g' is a pre-defined igraph Graph object >>> # g.vs["label"] = g.vs["name"] >>> # color_dict = {"m": "blue", "f": "pink"} >>> # g.vs["color"] = [color_dict[gender] for gender in g.vs["gender"]] >>> # Using Kamada-Kawai layout and Cairo backend >>> layout = g.layout("kk") >>> ig.plot(g, layout=layout, bbox=(300, 300), margin=20) >>> # Using Matplotlib backend >>> fig, ax = plt.subplots() >>> ig.plot(g, layout=layout, target=ax) >>> # For rooted trees with Matplotlib, you might invert the y-axis: >>> # ax.invert_yaxis() ``` -------------------------------- ### Install igraph using pip (Global) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs the igraph Python interface globally using pip. This command may require administrator or root privileges. It fetches the latest stable version from PyPI. ```bash $ pip install igraph ``` -------------------------------- ### Install igraph in a virtual environment using pip Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs the igraph Python interface within a Python virtual environment. This isolates the installation from the global Python environment. It first creates a virtual environment, activates it, and then installs igraph using pip. ```bash python -m venv my_environment source my_environment/bin/activate pip install igraph ``` -------------------------------- ### Install python-igraph from Source with External C Core Source: https://github.com/igraph/python-igraph/blob/main/README.md This command installs the python-igraph package directly from its source tree, linking against an external igraph C core installation discoverable by pkg-config. The IGRAPH_USE_PKG_CONFIG environment variable must be set to '1'. ```bash IGRAPH_USE_PKG_CONFIG=1 pip install . ``` -------------------------------- ### Install igraph in a Conda virtual environment Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs igraph within a Conda virtual environment. This approach leverages Conda's environment management capabilities for isolated installations. It creates a new environment, activates it, and then installs igraph. ```bash conda create -n my_environment conda activate my_environment conda install -c conda-forge python-igraph ``` -------------------------------- ### Install python-igraph from Source using pip Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Installs the python-igraph package from the current directory after downloading the source tarball. Ensure any vendored igraph C core folders are removed if you are not using the bundled version. ```bash $ pip install . ``` -------------------------------- ### Install python-igraph with External C Core using pkg-config Source: https://github.com/igraph/python-igraph/blob/main/README.md This command installs the python-igraph package, instructing the build system to link against an igraph C core installation discoverable by pkg-config. The IGRAPH_USE_PKG_CONFIG environment variable must be set to '1'. ```bash IGRAPH_USE_PKG_CONFIG=1 pip install igraph ``` -------------------------------- ### Initialize Git submodules for igraph C core Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Initializes and updates the Git submodules for the igraph C core library within a cloned Python igraph repository. This step is crucial for compiling the Python interface against the correct version of the C library. ```bash git submodule update --init ``` -------------------------------- ### Perform linear transformations on layout Source: https://github.com/igraph/python-igraph/blob/main/doc/source/visualisation.rst Provides examples of applying linear transformations such as translation, centering, scaling, rotation, and mirroring to a graph layout object. ```python # Translate the layout translated_layout = layout.translate(x=10, y=5) # Center the layout centered_layout = layout.center() # Scale the layout scaled_layout = layout.scale(scale=2) # Rotate the layout rotated_layout = layout.rotate(angle=90) # Mirror the layout mirrored_layout = layout.mirror(axis='x') ``` -------------------------------- ### Install python-igraph with External C Core on MSYS2/MinGW Source: https://github.com/igraph/python-igraph/blob/main/README.md This command installs the python-igraph package on MSYS2/MinGW, linking against an external igraph C core. It uses pkg-config and explicitly enables stdlib for setuptools. The IGRAPH_USE_PKG_CONFIG environment variable must be set to '1'. ```bash IGRAPH_USE_PKG_CONFIG=1 SETUPTOOLS_USE_DISTUTILS=stdlib pip install igraph ``` -------------------------------- ### Import and Initialize igraph Graph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Demonstrates how to import the igraph library and initialize a simple graph with a predefined set of edges. This is a common starting point for most igraph operations. ```python >>> import igraph as ig >>> from igraph import Graph >>> g = Graph(edges=[[0, 1], [2, 3]]) ``` -------------------------------- ### Compile python-igraph Development Version from Local Clone Source: https://github.com/igraph/python-igraph/blob/main/README.md Installs the development version of python-igraph after cloning the repository locally. This process requires initializing git submodules and installing build tools like flex and bison. The installation can then be performed using pip. ```bash git submodule update --init ``` ```bash sudo apt install bison flex ``` ```bash pip install . ``` -------------------------------- ### Create a new Layout object from coordinates Source: https://github.com/igraph/python-igraph/blob/main/doc/source/visualisation.rst Illustrates how to create a new `igraph.layout.Layout` object from a list of coordinates, for example, when working with a subset of vertex positions. ```python layout_subgraph = ig.Layout(coords=layout[:2]) ``` -------------------------------- ### Clone igraph Python repository Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Clones the Python igraph source code repository from GitHub. This is the first step for manual compilation or for accessing the bleeding-edge development version. ```bash git clone https://github.com/igraph/python-igraph.git ``` -------------------------------- ### Plot igraph Graph using Matplotlib Backend (Single Plot) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Demonstrates how to use Matplotlib as the plotting backend for a single igraph plot by explicitly creating Matplotlib Figure and Axes objects and passing them to the plot function. ```python import matplotlib.pyplot as plt import igraph as ig fig, ax = plt.subplots() g = ig.Graph.Famous("petersen") ig.plot(g, target=ax) plt.show() ``` -------------------------------- ### Graph Layout Algorithms in Python-igraph Source: https://context7.com/igraph/python-igraph/llms.txt Applies various layout algorithms to position vertices in a graph using python-igraph. This example uses the Zachary's Karate Club graph. ```python import igraph as ig import matplotlib.pyplot as plt g = ig.Graph.Famous("Zachary") # Example of applying a layout (e.g., Fruchterman-Reingold) # layout = g.layout("fruchterman_reingold") # ig.plot(g, layout=layout) ``` -------------------------------- ### Compile igraph from source on Windows Source: https://github.com/igraph/python-igraph/blob/main/README.md This section outlines the process of compiling igraph from source on Windows, requiring Microsoft Visual Studio and Python 3.7+. It involves extracting the source, navigating to the directory in a Developer Command Prompt, and using 'pip install .'. Environment variables can be set to specify build architecture. ```bash set IGRAPH_CMAKE_EXTRA_ARGS=-A [arch] ``` ```bash pip install . ``` -------------------------------- ### Install python-igraph using pip Source: https://github.com/igraph/python-igraph/blob/main/doc/source/index.rst Installs the python-igraph library using the pip package manager. This is a common method for installing Python packages. ```bash pip install igraph ``` -------------------------------- ### Create igraph Graph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Shows how to create a new igraph graph. This includes creating an empty graph and creating a graph with a specified number of nodes and edges. ```python g = ig.Graph() g = ig.Graph(n=10, edges=[[0, 1], [0, 5]]) print(g) ``` -------------------------------- ### Apply GraphOpt Layout Algorithm for Large Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Implements the GraphOpt algorithm, designed for laying out large graphs efficiently. This method optimizes vertex positions to minimize edge crossings and improve the overall clarity of large network visualizations. ```python layout_graphopt(g) ``` -------------------------------- ### Install python-igraph using conda Source: https://github.com/igraph/python-igraph/blob/main/doc/source/index.rst Installs the python-igraph library using the conda package manager from the conda-forge channel. This is an alternative installation method, often used in scientific computing environments. ```bash conda install -c conda-forge python-igraph ``` -------------------------------- ### Apply 3D Kamada-Kawai Layout Algorithm for Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Applies the Kamada-Kawai force-directed algorithm in three-dimensional space. This layout method generates a 3D spatial arrangement of graph vertices, useful for exploring complex network topologies in three dimensions. ```python layout_kamada_kawai_3d(g) ``` -------------------------------- ### Install python-igraph in Editable Mode Source: https://github.com/igraph/python-igraph/blob/main/README.md Installs python-igraph in editable mode, allowing direct changes to the Python source code to be reflected immediately without reinstallation. If the C extension source code is modified, the installation needs to be rerun to rebuild it. ```bash pip install -e . ``` -------------------------------- ### Apply Kamada-Kawai Layout Algorithm for Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Utilizes the Kamada-Kawai force-directed algorithm for graph layout. Similar to Fruchterman-Reingold, it uses simulated forces but adjusts edge lengths based on graph-theoretic distances, often resulting in a more uniform vertex distribution. ```python layout_kamada_kawai(g) ``` -------------------------------- ### Python: Select vertices by providing a list of indices with igraph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates selecting vertices by providing a list of integers as indices to the `select` method in `python-igraph`. It also shows how to further filter an existing vertex sequence. ```python >>> seq = graph.vs.select([2, 3, 7]) >>> len(seq) 3 >>> [v.index for v in seq] [2, 3, 7] >>> seq = seq.select([0, 2]) # filtering an existing vertex set >>> [v.index for v in seq] [2, 7] >>> seq = graph.vs.select([2, 3, 7, "foo", 3.5]) >>> len(seq) 3 ``` -------------------------------- ### igraph: Specifying vertex colors using string names Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Illustrates how to set the color for all vertices in an igraph plot using a simple string representing a color name (e.g., 'green'). ```python >>> import igraph as ig >>> g = ig.Graph.GRG(10, 0.2) >>> # Set all vertex colors to green >>> ig.plot(g, vertex_color="green") ``` -------------------------------- ### Construct Graphs in python-igraph Source: https://context7.com/igraph/python-igraph/llms.txt Demonstrates how to create graphs from scratch in python-igraph, including basic vertex and edge definitions, and adding graph, vertex, and edge attributes. It also shows how to access basic graph properties. ```python import igraph as ig # Basic graph construction with 5 vertices and edge list g = ig.Graph(5, [(0, 1), (0, 2), (1, 3), (2, 3), (3, 4)]) # Create graph with attributes g = ig.Graph( n=5, edges=[(0, 1), (0, 2), (1, 2), (2, 3), (3, 4)], directed=False, graph_attrs={"title": "My Network", "date": "2025"}, vertex_attrs={"name": ["Alice", "Bob", "Charlie", "David", "Eve"]}, edge_attrs={"weight": [1.0, 2.5, 3.0, 1.5, 2.0]} ) # Access graph properties print(f"Vertices: {g.vcount()}, Edges: {g.ecount()}") print(f"Directed: {g.is_directed()}") # Output: Vertices: 5, Edges: 5 # Output: Directed: False ``` -------------------------------- ### igraph: Writing graphs to DOT format (GraphViz) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates how to export an igraph `Graph` object to the DOT language format, which is used by GraphViz. This allows for visualization and manipulation of graphs using GraphViz tools. ```python >>> import igraph as ig >>> g = ig.Graph.GRG(10, 0.2) >>> # Assuming 'graph.dot' is the desired output file path >>> g.write_dot("graph.dot") ``` -------------------------------- ### Apply Fruchterman-Reingold Layout Algorithm for Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Utilizes the Fruchterman-Reingold force-directed algorithm for graph layout. This algorithm simulates attractive forces between connected vertices and repulsive forces between all vertices to create a balanced visual representation. ```python layout_fruchterman_reingold(g) ``` -------------------------------- ### Python: Select vertices by providing None using igraph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Illustrates how to use `select(None)` in `python-igraph` to obtain an empty vertex sequence. This is a way to initialize an empty selection or reset a sequence. ```python >>> seq = g.vs.select(None) >>> len(seq) 0 ``` -------------------------------- ### igraph Graph Summary Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Illustrates how to obtain a summary of an igraph graph using the `igraph.summary()` function. This is useful for large graphs where printing all edges might be inefficient. ```python ig.summary(g) ``` -------------------------------- ### Apply Grid Layout Algorithm for Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Positions graph vertices on a regular grid. This layout is deterministic and suitable for graphs where a structured, grid-like arrangement is desired for clarity or comparison. ```python layout_grid(g) ``` -------------------------------- ### Apply 3D Fruchterman-Reingold Layout Algorithm for Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Applies the Fruchterman-Reingold force-directed algorithm in three-dimensional space. This layout method provides a 3D representation of the graph, useful for visualizing more complex relationships and structures. ```python layout_fruchterman_reingold_3d(g) ``` -------------------------------- ### Get Graph Isomorphisms Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Retrieves all isomorphic mappings between two graphs or subgraphs. This is useful for understanding structural similarities. The VF2 and LAD algorithms can be used for this purpose. ```python >>> isomorphisms = g.get_isomorphisms_vf2(other_graph) >>> subisomorphisms = g.get_subisomorphisms_vf2(other_graph) >>> subisomorphisms_lad = g.get_subisomorphisms_lad(other_graph) ``` -------------------------------- ### Python: Select vertices by providing integer indices with igraph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Explains the shorthand for selecting vertices using multiple integer arguments directly in the `select` method in `python-igraph`. This is equivalent to passing a list of these integers. ```python >>> seq = graph.vs.select(2, 3, 7) >>> len(seq) 3 ``` -------------------------------- ### Generate a Stochastic Geometric Random Graph in Python Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates the generation of a stochastic geometric random graph (GRG) using igraph's Graph.GRG method. Unlike deterministic generators, GRG produces different graph structures each time with the same parameters. It also shows how to compare graphs using get_edgelist() and isomorphic(). ```python >>> g = ig.Graph.GRG(100, 0.2) >>> summary(g) IGRAPH U---- 100 516 -- + attr: x (v), y (v) >>> g2 = ig.Graph.GRG(100, 0.2) >>> g.get_edgelist() == g2.get_edgelist() False >>> g.isomorphic(g2) False ``` -------------------------------- ### Compute Dominator Tree Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Obtains the dominator tree of a directed graph starting from a specified root node. The dominator tree is a fundamental structure in control flow analysis. ```python >>> dominator_tree = g.dominator(root=0) ``` -------------------------------- ### Apply generic nonlinear transformation to layout Source: https://github.com/igraph/python-igraph/blob/main/doc/source/visualisation.rst Demonstrates the use of the `Layout.transform` method for applying arbitrary nonlinear transformations to a graph layout. ```python # Example of a nonlinear transformation (e.g., using a lambda function) nonlinear_layout = layout.transform(lambda x, y: (x**2, y**2)) ``` -------------------------------- ### Reingold-Tilford Tree Layout Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Applies the Reingold-Tilford algorithm for tree-like graph layouts. It can be called directly or through the common `Graph.layout` method. The `root` parameter can be specified to set the root of the tree. This layout is particularly useful for hierarchical structures. ```python >>> layout = g.layout_reingold_tilford(root=[2]) >>> layout = g.layout("rt", root=[2]) ``` -------------------------------- ### Converting Graphs to NetworkX and Graph-tool with Python Source: https://github.com/igraph/python-igraph/blob/main/doc/source/visualisation.rst This snippet shows how to convert an igraph graph object into compatible objects for the popular Python libraries NetworkX and Graph-tool. This requires the respective libraries to be installed. ```python >>> n = g.to_networkx() >>> gt = g.to_graph_tool() ``` -------------------------------- ### Save Matplotlib as Default igraph Plotting Backend Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Configures igraph to persistently use Matplotlib as its default plotting backend by saving the configuration to the default configuration file. This preference will be retained across sessions. ```python import igraph as ig ig.config["plotting.backend"] = "matplotlib" ig.config.save() ``` -------------------------------- ### Maximum Flow and Minimum Cut Calculations in Python Source: https://context7.com/igraph/python-igraph/llms.txt Demonstrates how to calculate maximum flow, minimum cut, and identify bottlenecks in a directed graph using the python-igraph library. Requires the igraph library. ```python import igraph as ig # Create directed graph with capacities g = ig.Graph( 6, [(0, 1), (0, 2), (1, 3), (2, 3), (1, 4), (3, 5), (4, 5)], directed=True ) g.es["capacity"] = [10, 10, 4, 8, 9, 10, 6] # Calculate maximum flow from source to sink flow = g.maxflow(source=0, target=5, capacity="capacity") print(f"Maximum flow value: {flow.value}") print(f"Flow on each edge: {flow.flow}") print(f"Cut partition: {flow.partition}") # Calculate minimum cut mincut = g.st_mincut(source=0, target=5, capacity="capacity") print(f"Min cut value: {mincut.value}") print(f"Cut edges: {mincut.cut}") # Global minimum cut (any bipartition) globalcut = g.mincut(capacity="capacity") print(f"Global min cut: {globalcut.value}") ``` -------------------------------- ### Apply Davidson-Harel Layout Algorithm for Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Applies the Davidson-Harel simulated annealing algorithm for graph layout. This algorithm is suitable for complex graphs and aims to find aesthetically pleasing arrangements by simulating physical forces between vertices. ```python layout_davidson_harel(g) ``` -------------------------------- ### Configure igraph to use Matplotlib Backend (Session) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/install.rst Sets the default plotting backend for igraph to Matplotlib for the current session. This change affects all subsequent calls to igraph.plot() within the same Python script or notebook. ```python import matplotlib.pyplot as plt import igraph as ig ig.config["plotting.backend"] = "matplotlib" g = ig.Graph.Famous("petersen") ig.plot(g) plt.show() ``` -------------------------------- ### Iterate Through Vertices and Edges (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Provides examples of iterating through all vertices and edges of a graph using the `vs` and `es` attributes. These attributes return special sequence objects that allow iteration. ```python >>> for v in g.vs: >>> print(v) >>> >>> for e in g.es: >>> print(e) ``` -------------------------------- ### Pathfinding and Cut Algorithms in Python Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Provides various pathfinding and cut algorithms for graphs. This includes shortest path calculations, minimum spanning trees, minimum cuts (edge and vertex connectivity), and all-pairs shortest paths. ```python import igraph as ig # Assuming g is an igraph.Graph object and source/target vertices are defined # Shortest paths shortest_paths = g.shortest_paths(source=0) all_shortest_paths = g.get_all_shortest_paths(source=0) all_simple_paths = g.get_all_simple_paths(source=0, target=5) # Spanning tree spanning_tree = g.spanning_tree() # Minimum cuts min_cut = g.mincut(source=0, target=1) st_min_cut = g.st_mincut(source=0, target=1) min_cut_value = g.mincut_value(source=0, target=1) all_st_cuts = g.all_st_cuts(source=0, target=1) all_st_min_cuts = g.all_st_mincuts(source=0, target=1) # Connectivity edge_connectivity = g.edge_connectivity(source=0, target=1) vertex_connectivity = g.vertex_connectivity(source=0, target=1) ``` -------------------------------- ### Get Graph Adjacency and Incidence Lists (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Illustrates how to obtain the full adjacency list (`get_adjlist`) or incidence list (`get_inclist`) representation of a graph. For bipartite graphs, `get_biadjacency` is available. ```python >>> adj_list = g.get_adjlist() >>> incl_list = g.get_inclist() ``` -------------------------------- ### Count Vertices and Edges (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Demonstrates how to get the total number of vertices and edges in a graph using the `vcount` and `ecount` methods, respectively. These methods return integers representing the counts. ```python >>> n = g.vcount() >>> m = g.ecount() ``` -------------------------------- ### Get Graph Summary Representation (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Illustrates how to obtain a summary of the graph's structure and properties using the `summary` method. The `verbosity` parameter controls the level of detail in the output. ```python >>> g.summary(verbosity=1) ``` -------------------------------- ### Apply DRL Layout Algorithm for Large Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Implements the Distributed Recursive Layout (DRL) algorithm, optimized for large graphs. This force-directed layout algorithm helps in visualizing the structure of extensive networks by distributing vertices based on simulated forces. ```python layout_drl(g) ``` -------------------------------- ### Find Graph Neighborhood (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Shows how to find all vertices within a specified distance (order) from a set of starting vertices using the `neighborhood` method. `neighborhood_size` provides the count of such vertices. ```python >>> neighborhood_vertices = g.neighborhood([0, 1], order=2) >>> neighborhood_count = g.neighborhood_size([0, 1], order=2) ``` -------------------------------- ### Check igraph C Core Flags with pkg-config Source: https://github.com/igraph/python-igraph/blob/main/README.md This command checks if pkg-config can locate the igraph C core and provide the necessary compiler and linker flags. This is a prerequisite for linking to an external igraph installation. ```bash pkg-config --cflags --libs igraph ``` -------------------------------- ### Read Pajek Format File Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Loads a graph from a file in Pajek format using the `Graph.Read_Pajek` method. This format is widely compatible with various network analysis tools. ```python # Example usage for reading Pajek format # graph = ig.Graph.Read_Pajek('my_graph.net') ``` -------------------------------- ### Set and Save igraph Configuration (Python) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/configuration.rst This snippet demonstrates how to set various configuration options for igraph, such as plotting backend and palette, and then save these settings to a default configuration file. It requires the igraph library to be installed. ```python import igraph as ig # Set configuration variables ig.config["plotting.backend"] = "matplotlib" ig.config["plotting.palette"] = "rainbow" # Save configuration to default file location ig.config.save() ``` -------------------------------- ### Filter Edges by Source Vertex (Python) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Selects edges based on their source vertices. The `_source` or `_from` keywords can be used. For example, to select all edges originating from vertex with index 2, use `g.es.select(_source=2)`. ```python >>> g.es.select(_source=2) ``` -------------------------------- ### igraph Error Handling for Invalid Vertex IDs Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Provides an example of attempting to add an edge to a non-existent vertex in an igraph graph, which results in an `igraph.InternalError`. The traceback shows the error message and the source code location. ```python g.add_edges([(5, 4)]) ``` -------------------------------- ### Create Watts-Strogatz Random Graph - python Source: https://github.com/igraph/python-igraph/blob/main/doc/source/generation.rst Generates a random graph based on the Watts-Strogatz model, which produces small-world networks. It starts with a regular ring lattice and then rewires edges randomly with a given probability. ```python from igraph import Graph # Creates a Watts-Strogatz graph with 100 vertices, initial degree 4, and rewiring probability 0.3 g = Graph.Watts_Strogatz(dim=1, size=100, nei=2, p=0.3) ``` -------------------------------- ### Plotting with Keyword Arguments in python-igraph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates how to pass visual properties directly as keyword arguments to the `ig.plot` function. This method is useful for separating visual styling from the graph's data structure. It requires a graph object `g`, a layout, and potentially custom mappings for vertex colors based on attributes. ```python import igraph as ig # Assuming 'g' is an igraph Graph object and 'layout' is a precomputed layout color_dict = {"m": "blue", "f": "pink"} ig.plot(g, layout=layout, vertex_color=[color_dict[gender] for gender in g.vs["gender"]]) ``` -------------------------------- ### Install igraph dependencies on Debian/Ubuntu Source: https://github.com/igraph/python-igraph/blob/main/README.md Before compiling igraph from source on Debian/Ubuntu systems, these development packages are required. They provide the necessary build tools and libraries, such as a C compiler, Python development headers, and XML/Zlib support. ```bash sudo apt install build-essential python-dev libxml2 libxml2-dev zlib1g-dev ``` -------------------------------- ### Apply LGL Layout Algorithm for Large Graph Visualization Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Implements the Large Graph Layout (LGL) algorithm, specifically designed for visualizing very large graphs. It aims to produce a clear and informative layout for networks with a high number of vertices and edges. ```python layout_lgl(g) ``` -------------------------------- ### Get Adjacency Matrix Representation of a Graph Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates how to obtain the adjacency matrix of a graph using the `get_adjacency()` method in python-igraph. The output is a Matrix object where elements indicate the presence (1) or absence (0) of an edge between vertices. ```python >>> g.get_adjacency() Matrix([ [0, 1, 1, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 0, 0], [1, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 1, 0] ]) ``` -------------------------------- ### Access and Manipulate Vertices/Edges in python-igraph Source: https://context7.com/igraph/python-igraph/llms.txt Covers how to access, select, find, and modify vertices and edges in python-igraph using sequence objects. It demonstrates setting and getting attributes, adding/deleting elements, and accessing edges by vertex names. ```python import igraph as ig g = ig.Graph(5, [(0, 1), (0, 2), (1, 2), (2, 3), (3, 4)]) g.vs["name"] = ["A", "B", "C", "D", "E"] g.es["weight"] = [1.0, 2.0, 3.0, 4.0, 5.0] # Access vertices print(g.vs[0]) # First vertex print(g.vs["name"]) # All vertex names: ['A', 'B', 'C', 'D', 'E'] # Select vertices by criteria high_degree = g.vs.select(_degree_gt=2) print([v["name"] for v in high_degree]) # Find specific vertex vertex_c = g.vs.find(name="C") print(f"Vertex C has index {vertex_c.index}") # Access edges print(g.es[0]) # First edge print(g.es["weight"]) # All weights: [1.0, 2.0, 3.0, 4.0, 5.0] # Access edge by vertex names (when vertices are named) g["A", "B"] = 10.0 # Set edge weight print(g["A", "B"]) # Get edge weight: 10.0 # Add and delete elements g.add_vertices(2) g.add_edges([(5, 1), (6, 2)]) g.delete_vertices([6]) ``` -------------------------------- ### Plotting with igraph: Saving to PDF Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Demonstrates how to save an igraph plot to a PDF file. The file format is inferred from the filename extension. This method relies on the Cairo backend for rendering. ```python >>> import igraph as ig >>> g = ig.Graph.GRG(10, 0.2) >>> # Assuming 'visual_style' is defined elsewhere and contains plotting preferences >>> ig.plot(g, "social_network.pdf", **visual_style) ``` -------------------------------- ### igraph: Reading graphs from DIMACS format Source: https://github.com/igraph/python-igraph/blob/main/doc/source/tutorial.rst Shows how to load graph data from a file in the DIMACS standard format. This format is often used in graph algorithm competitions and research. ```python >>> import igraph as ig >>> # Assuming 'graph.dimacs' contains the graph data >>> g = ig.Graph.Read_DIMACS("graph.dimacs") ``` -------------------------------- ### Get Edges Incident on a Vertex (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Shows how to retrieve all edges connected to a specific vertex using `Vertex.incident` (on a Vertex object) or `Graph.incident` (using vertex ID). `out_edges` and `in_edges` are specific to directed graphs. ```python >>> v = g.vs[0] >>> incident_edges_v = v.incident() >>> incident_edges_g = g.incident(0) ``` -------------------------------- ### Get Edge Source and Target Vertices (igraph) Source: https://github.com/igraph/python-igraph/blob/main/doc/source/analysis.rst Illustrates how to retrieve the source and target vertex IDs for a given edge using the `source` and `target` attributes of an `Edge` object. This is fundamental for understanding edge connections. ```python >>> e = g.es[0] >>> v1, v2 = e.source, e.target ```