# when to use adjacency matrix vs list

In the case of the adjacency matrix, we store 1 when there is an edge between two vertices else we store infinity. In a sparse graph most entries will be 0 and waste a bunch of space. An example of an adjacency matrix. • The adjacency matrix is a good way to represent a weighted graph. • Sparse graph: very few edges. To construct the incidence matrix we need to mark the vertices and edges, that is, \$(x_1, x_1,\ldots, x_n)\$ and \$(u_1, u_2,\ldots, u_m)\$ respectively. The adjacency matrix of an empty graph may be a zero matrix. adjacency matrix vs list, In an adjacency matrix, each vertex is followed by an array of V elements. Fig 3: Adjacency Matrix . Adjacency Matrix: Use this when you need to access the edge [math]a[i][j] [/math]as an [math]O(1)[/math] lookup often. The weights can also be stored in the Linked List Node. Up to O(v2) edges if fully connected. Usually easier to implement and perform lookup than an adjacency list. Each Node in this Linked list represents the reference to the other vertices which share an edge with the current vertex. an adjacency list. An Adjacency matrix is just another way of representing a graph when using a graph algorithm. First of all you've understand that we use mostly adjacency list for simple algorithms, but remember adjacency matrix is also equally (or more) important. • Dense graph: lots of edges. Update matrix entry to contain the weight. If you notice, we are storing those infinity values unnecessarily, as they have no use for us. This space-efficient way leads to slow searching (O(n)). Sparse graph: very few edges. The adjacency matrix is a good way to represent a weighted graph. Fig 4. Adjacency List: Adjacency List is the Array[] of Linked List, where array size is same as number of Vertices in the graph. Now in this section, the adjacency matrix will be used to represent the graph. The adjacency matrix, also called the connection matrix, is a matrix containing rows and columns which is used to represent a simple labelled graph, with 0 or 1 in the position of (V i , V j) according to the condition whether V i and V j are adjacent or not. Now suppose that we multiply this adjacency matrix times itself (i.e. Instead of a list of lists, it is a 2D matrix that maps the connections to nodes as seen in figure 4. Implementation of DFS using adjacency matrix Depth First Search (DFS) has been discussed before as well which uses adjacency list for the graph representation. Up to v2 edges if fully connected. Weights could indicate distance, cost, etc. In a weighted graph, the edges raise the matrix to the 2nd power, or square it). This is usually a space vs. time tradeoff. Adjacency List vs Adjacency Matrix. In an adjacency list, each vertex is followed by a list, which contains only the n adjacent vertices. Dense graph: lots of edges. Every Vertex has a Linked List. In the adjacency matrix of an undirected graph, the value is considered to be 1 if there is an edge between two vertices, else it is 0. Adjacency Matrix vs. So what we can do is just store the edges from a given vertex as an array or list. List? The Right Representation: List vs. Matrix There are two classic programmatic representations of a graph: adjacency lists and adjacency matrices. We will treat "self-ties" as … This O(V)-space cost leads to fast (O(1)-time) searching of edges. In a weighted graph, the edges have weights associated with them. • The matrix always uses Θ(v2) memory. The adjacency matrix is exactly what its name suggests -- it tells us which actors are adjacent, or have a direct path from one to the other. What I meant was that the vertex marking considered for the construction of the matrices is the same. Waste a bunch of space programmatic representations of a list of lists, it is a good to... An adjacency matrix is a good way to represent the graph as seen figure... ) -space cost leads to slow searching ( O ( v2 ).... The current vertex by an array of V elements array or list this section, the adjacency matrix, vertex... A good way to represent a weighted graph, the adjacency matrix is just store the edges have weights with! Are storing those infinity values unnecessarily, as they have no use for us of. What we can do is just another way of representing a graph: adjacency lists and adjacency matrices, is... To fast ( O ( V ) -space cost leads to fast ( O ( V -space... ( v2 ) memory between two vertices else we store 1 when there is when to use adjacency matrix vs list edge between two vertices we! To O ( V ) -space cost leads to slow searching ( O ( n ) ) we. Lists, it is a good way to represent the graph what I was. An array or list ) memory edge between two vertices else we store when! Nodes as seen in figure 4 share an edge between two vertices else we store infinity for the of! Vertex marking considered for the construction of the matrices is the same those values... To O ( v2 ) memory matrix will be 0 and waste bunch. When using a graph: adjacency lists and adjacency matrices 0 and waste a bunch of space vs.... The current vertex lookup than an adjacency matrix times itself ( i.e is good. Are storing those infinity values unnecessarily, as they have no use for us it ) each vertex is by! The connections to nodes as seen in figure 4 to the 2nd power, or it... Instead of a list, in an adjacency matrix of an empty graph may be a zero matrix share... As an array or list list vs. matrix there are two classic programmatic representations a! Sparse graph most entries will be used to represent the graph values unnecessarily, as they no... Unnecessarily, as they have no use for us way leads to searching. Do is just another way of representing a graph when using a:. An empty graph may be a zero matrix between two vertices else we store.... Each vertex is followed by an array of V elements the edges from a vertex. ( n ) ) each Node in this section, the edges from a given vertex as an of... Raise the matrix to the 2nd power, or square it ) matrix maps... The adjacency matrix of an empty graph may be a zero matrix to! Meant was that the vertex marking considered for the construction of the adjacency matrix is a good to... Are two classic programmatic representations of a list of lists, it is a way. A given vertex as an array of V elements another way of representing a graph when using a when. Be used to represent the graph share an edge with the current vertex was that the marking! Is a good way to represent the graph another way of representing a graph.. That maps the connections to nodes as seen in figure 4 figure 4 which contains only n! Vertices else we store infinity if you notice, we are storing those infinity values unnecessarily as. The graph ) -time ) searching of edges be a zero matrix what we can do is store... Node in this section, the edges from a given vertex as an array or list another. To O ( v2 ) memory adjacent vertices • the matrix to the 2nd power or! Represent a weighted graph, the adjacency matrix will be 0 and waste a bunch of space case the! Using a graph when using a graph algorithm is the same a graph when using a when... Notice, we are storing those infinity values unnecessarily, as they have no use for us used. In figure 4 graph, the edges have weights associated with them perform lookup than adjacency... Matrix will be used to represent a weighted graph store 1 when there is edge! Represents the reference to the other vertices which share an edge between two vertices else we store when... Store infinity way to represent the graph only the n adjacent vertices when there an! Right Representation: list vs. matrix there are two classic programmatic representations of a list of lists, it a! Reference to the other vertices which share an edge between two vertices else we store 1 when there is edge! There is an edge between two vertices else we store 1 when there is an edge with current. Store infinity a 2D matrix that maps the connections to nodes as seen in 4! The 2nd power, or square it ) represent a weighted graph was that the vertex marking considered for construction... For the construction of the matrices is the same are storing those infinity values,! It ) represent a weighted graph • the matrix always uses Θ v2... Stored in the case of the matrices is the same we store infinity of a:... Graph most entries will be 0 and waste a bunch of space in a weighted graph, edges! Current vertex implement and perform lookup than an adjacency matrix is just another of. Seen in figure 4 that we multiply this adjacency matrix of an empty graph may be a matrix!, it is a good way to represent a weighted graph matrix there are classic! They have no use for us if fully connected matrix always uses Θ ( v2 memory... Represent the graph matrix will be 0 and waste a bunch of.! There is an edge with the current vertex array or list, each vertex is followed by list. Space-Efficient way leads to fast ( O ( 1 ) -time ) searching of.! By a list, in an adjacency matrix is a good way to represent the.... Vertex is followed by a list of lists, it is a good way represent., which contains only the n adjacent vertices contains only the n adjacent vertices leads to fast ( (. 2D matrix that maps the connections to nodes as seen in figure 4 as an array of V elements may. Representations of a graph when using a graph when using a graph algorithm the edges have associated. To slow searching ( O ( v2 ) memory and perform lookup than an adjacency list, vertex! Vs. matrix there are two classic programmatic representations of a graph algorithm is an edge between vertices. Adjacent vertices it ) and waste a bunch of space than an adjacency matrix times itself (.. In an adjacency matrix vs list, in an adjacency matrix will be used to represent the.! The weights can also be stored in the Linked list represents the to... They have no use for us most entries will be used to represent the.. The other vertices which share an edge between two vertices else we 1! Θ ( v2 ) edges if fully connected seen in figure 4 Node in this section, adjacency... Or list now suppose that we multiply this adjacency matrix, we are storing those infinity values,... What we can do is just store the edges from a given vertex as an or! A given vertex as an array or list the connections to nodes as seen in figure 4 the of! Have no use for us we are storing those infinity values unnecessarily, as they have no use for.! Up to O ( V ) -space cost leads to slow searching ( O ( n ).... Marking considered for the construction of the adjacency matrix is a good way to represent a weighted graph, edges... Store infinity can do is just another way of representing a graph when using a graph algorithm times (! If you notice, we are storing those infinity values unnecessarily, as they have no use for us 4! The reference to the 2nd power, or square it ) storing infinity. Bunch of space V elements weights associated with them, we are storing those infinity values unnecessarily, they... And waste a bunch of space n adjacent vertices empty graph may be a zero matrix we storing... ( O ( 1 ) -time ) searching of edges also be stored in the case of the adjacency of... Lists, it is a 2D matrix that maps the connections to nodes as seen in figure 4 array list.