Incidence Matrix in Python Last Updated : 30 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In graph theory, an incidence matrix is a matrix that shows the relationship between vertices and edges in a graph. This matrix is a fundamental tool used in various applications, including network analysis, circuit design, and computer science. This article will cover the basics of incidence matrices, how to create and manipulate them using Python. What is an Incidence Matrix?An incidence matrix is a rectangular matrix with dimensions (V x E), where: V is the number of vertices in the graph.E is the number of edges in the graph.Each element a_ij of the matrix represents the connection between the i-th vertex and the j-th edge: aij = 1 if the i-th vertex is incident to the j-th edge (i.e., the edge connects to the vertex).aij = 0 if the i-th vertex is not incident to the j-th edge.Building an Incidence Matrix from a GraphHere's how to build an incidence matrix for a given graph: Identify the number of vertices (V) and edges (E).Create a (V x E) matrix filled with zeros.Iterate through each edge in the graph.For each edge, find the two vertices it connects.Set the corresponding elements in the matrix to 1 for those vertices and that edge.Implementation of Incidence Matrix in PythonImport numpy as npDefine your graph as a dictionaryGet the number of vertices (V) and edges (E)Create a zero-filled matrix of size (V x E)Iterate through edges and set corresponding elements to 1for i, edge in enumerate(edges)print the matrixBelow is the implementation of an incidence matrix in Python: Python import numpy as np def build_incidence_matrix(graph): # A NumPy array representing the incidence matrix. vertices = list(graph.keys()) edges = [(v1, v2) for v1 in vertices for v2 in graph[v1] if v1 < v2] matrix = np.zeros((len(vertices), len(edges))) for i, edge in enumerate(edges): v1, v2 = edge matrix[vertices.index(v1), i] = 1 matrix[vertices.index(v2), i] = 1 return matrix # Example usage graph = { "A": ["B", "C"], "B": ["A", "C", "D"], "C": ["A", "B", "E"], "D": ["B"], "E": ["C"], } incidence_matrix = build_incidence_matrix(graph) print(incidence_matrix) Output[[1. 1. 0. 0. 0.] [1. 0. 1. 1. 0.] [0. 1. 1. 0. 1.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]] Comment More infoAdvertise with us Next Article Incidence Matrix in Python C code_r Follow Improve Article Tags : Python Python-DSA Practice Tags : python Similar Reads Graph Representation using Incidence Matrix in C++ A graph is a non-linear data structure that is represented as a collection of vertices and edges that connect pairs of vertices. One way to represent a graph in computer memory is using an incidence matrix. An incidence matrix is a 2D array where rows represent edges and columns represent vertices. 5 min read Python List index() - Find Index of Item index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example:Pythona = ["cat", "dog", "tiger" 3 min read Python most_common() Function most_common() function is a method provided by the Counter class in Python's collections module. It returns a list of the n most common elements and their counts from a collection, sorted by frequency. Example:Pythonfrom collections import Counter a = ['apple', 'banana', 'apple', 'orange', 'banana', 2 min read Intersection() function Python Python set intersection() method returns a new set with an element that is common to all set The intersection of two given sets is the largest set, which contains all the elements that are common to both sets. The intersection of two given sets A and B is a set which consists of all the elements whi 2 min read Python | Pandas Index.intersection() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.intersection() function form the intersection of two Index objects. This 2 min read Like