Representing Graphs
Edge Lists¶
A simple way to define edge list is that it has a list of pairs. We just have a list of objects consisting of the vertex numbers of 2 nodes and other attributes like weight or the direction of edges. [16]
- + For some specific algorithms you need to iterate over all the edges, (i.e. kruskal's algorithm)
- + All edges are stored exactly once.
- - It is hard to determine whether two nodes are connected or not.
- - It is hard to get information about the edges of a specific vertex.
Adjacency Matrices¶
Stores edges, in a 2-D matrix. matrix[a][b] keeps an information about road from a to b. [16] - + We can easily check if there is a road between two vertices. - - Looping through all edges of a specific node is expensive because you have to check all of the empty cells too. Also these empty cells takes huge memory in a graph which has many vertices. (For example representing a tree)
Adjacency List¶
Each node has a list consisting of nodes each is adjacent to. So, there will be no empty cells. Memory will be equal to number of edges. The most used one is in algorithms. [16]
- + You do not have to use space for empty cells.
- + Easily iterate over all the neighbors of a specific node.
- - If you want to check if two nodes are connected, in this form you still need to iterate over all the neighbors of one of them. But, there are some structures that you can do this operation in O(log N). For example if you won't add any edge, you can sort every vector with nodes' names, so you can find it by binary search.