Uploaded by Tenshi Fx

Matrix final

advertisement
Graphs are essentially used in our everyday lives, we can look at Airline Traffic, where the
node is the airport, edges are direct flights between two airports and the weight is miles between two
airports. Another couple examples would be GPS navigation and networks routing. Some more clear
real-life examples would be flight reservations, Cell tower frequency planning, Amazon/Netflix which
uses these graphs to make suggestions for products/movies, social networks for an example,
Facebook which uses graphs for suggestions friends and finally Delivering goods which uses
logistics. There are movie examples, but these are the most essential as of right now, overall graphs
are used for just about anything and I will now explain the specific functions that are used.
Matrix is the rectangular array of numbers which are arranged in a definite number of rows &
1 2 3
columns. An example of a matrix would be [4 5 6]. In fact, this is called a square matrix because
7 8 9
the number of rows is equal to the number of columns, which makes it a “square”. To represent
graph using matrices, we can either use adjacency matrix or incidence matrix.
Adjacency matrix is the square matrix which is used to represent the finite graph and the
elements of the matrix shows whether the pair of vertices are adjoining or not adjoining in the graph.
The value of 1 in the adjacency matrix means that there is an edge between the two vertices; if there
was a value of 0, this would mean that an edge does not exist between the two vertices. Adjacency
matrices always have zeroes in the main diagonal for simple graphs, it is easy to see that adjacency
matrices for simple graphs are symmetric.
On the other hand, incidence matrix is the (0,1)-matrix that have a row for each vertex and
column for each edge and 1 is for the case when vertex is incident upon the edge. We will use the
following graphs to help you understand these definitions:
The adjacency matrices of G1, G2, and G3 are the following, respectively:
The incident matrices of G1, G2, and G3 are the following, respectively:
If a graph contains a loop at the j-th vertex, we simply put 2 in the jj-position of the matrix
Notice that if you add the columns or rows of the adjacency matrix, you get the degree of each
vertex. With the degree of vertex, we can create the degree matrix, that is the diagonal matrix having
the idea about the degree of every vertex means the number of edges adjoined to every vertex.
We have other graphs that contain multiple edges, which are those having two or more
edges connecting the same 2 vertices. Such graphs are called multigraphs. This is not restricted to
just edges. We can have multiple loops on the same vertex in multigraph. With that said, we can still
use adjacency matrix to represent multigraph the same way we represent graphs. If a graph has a
few edges, it is called a sparse graph. Oppositely, we call a graph, dense graph, if it has a lot of
edges. If all possible edges appear in a graph, it would be called a complete graph.
An Adjacency list is an array of linked lists that serves the purpose of representing a graph,
but also makes it easy to see which other vertices are adjacent to other vertices. Each vertex in a
graph can easily reference its neighbors through a linked list.
Some advantages for Adjacency matrices, there are very convenient to work with, Adding
and removing an edge can be in 0(1) time or 14 nanoseconds which is also the same time that is
required to check, if there is an edge there is an edge between vertices. It is also very simple to
program.
Some disadvantages are that, Adjacency matrices requires huge amounts of memory when
storying big graphs. Considering that the graphs can be divided into two categories, sparse and
dense graphs. Sparse doesn’t contain that many edges so it is easier to store. However, dense
graphs contain several edges that are comparable with square number of vertices. Adjacency matrix
is optimal for dense graphs but for sparse graphs it is redundant. Another setback is that the
Adjacency matrix, in many algorithms, you are required to know the edges that are adjacent to the
current vertex. To obtain such an information from the adjacency matrix, you have to scan over the
corresponding row, which results in O ( |V| ) complexity. Algorithms that are DFS or based on it, use
the adjacency matrix results in overall complexity of O( |V|^2 ), When it could be reduced to O ( | V |
+ |E| ), when using an adjacency list. The final disadvantage is, when using the adjacency matrix, it
requires huge efforts when adding/removing a vertex. If a graph is used for analysis only. Its not
necessary, but if you want, construct fully dynamic structure, using of adjacency matrix makes it
quite slow for big graphs.
Overall, the adjacency matrix is a good solution for dense graphs, which implies having constant
number of vertices.
Some advantages for adjacency list is that, we are able to store graphs into more of a
compact form compared to an adjacency matrix, however doing this would mean that the difference
in decreasing as a graph becomes denser. Another advantage is that adjacent list allows us to get
the list of adjacent vertices in O (1) time, which is a huge advantage for some algorithms
Some disadvantages to the adjacency list is, adding/removing an edge to/from an adjacent list is not
as easy compared to when you are doing it for an adjacency matrix. This requires, on average, O (
|E| / |V| ) time, which could possibly result in a cubical complexity for dense graphs when adding all
edges. Another disadvantage is, checking if there is an edge between two vertices, this can be done
by using O ( |E| / |V| ) when the list of adjacent vertices is unordered or O(log2( |E| / |V| )) when it is
sorted. This method usually is cheap. Finally, an adjacent list does not allow us to make a
proper/efficient implementation, if the dynamically change of vertices number is required. Adding a
new vertex can be done in O (V), but the removal of this would result int O (E) complexity.
Overall, an adjacency list is a good solution for sparse graphs, this lets us change numbers of
vertices quite more efficiently, compared to an adjacent matrix. However, there are still better
options/solutions to store fully dynamic graphs.
Download