Presentation on theme: "Section 2.5: Graphs and Trees"— Presentation transcript:
1 Section 2.5: Graphs and Trees Adjacency Matrix and Adjacency List
2 Learning ObjectivesIn this presentation you will learn how a graph (directed or undirected) can be represented as an adjacency matrix or as an adjacency list.You will be able to explain the difference between these two methods, including the advantages of each.
3 Representing a graphThere are two ways of representing a graph so that it can be processed by a computer program:Adjacency MatrixAdjacency ListTo look at how these work we will use the following undirected graph:12435
4 Adjacency MatrixA graph can be represented using a two-dimensional matrix; this is called an adjacency matrix.Visually, this is like a table which records information about which vertices have an edge connecting them i.e. which vertices are adjacent.Each vertex has a row in the table.Each vertex has a column in the table.A “1” is placed in the intersection of a vertex’s row with another vertex’s column if there is an edge between them.A “0” is placed in all of the other cells to denote that there is no edge connecting the two vertices.
5 Adjacency MatrixWe will now construct an adjacency matrix for our example graph.12435There is an edge connecting vertex 1 to vertex 4 so place a “1” in row 1, column 4 and also in row 4, column 1.Then place a “0” in all the other cells, as there are no edges connecting the corresponding vertices in the graph.Our example undirected graph has now been represented as an adjacency matrix.Then look at the remaining vertices, placing a “1” in the cells which represent vertices that are connected in the graph.As there is an edge connecting vertex 1 to vertex 2 there is also one from vertex 2 to vertex 1 so place a “1” in row 2, column 1.Then look at vertex 2. There is an edge between vertices 2 & 3 so put a “1” in row 2, column 3 and in row 3, column 2.There is an edge connecting vertex 1 to vertex 3 so place a “1” in row 1, column 3 and also in row 3, column 1.Notice that the adjacency matrix is symmetric about the top-left to bottom-right diagonalStart with vertex 1; there is an edge connecting it to vertex 2 so we place a “1” in row 1, column 2.VERTEX1234511111111111111
6 Adjacency ListAn alternative to an adjacency matrix is an adjacency list, which can be used to indicate which vertices are next to each other.For each vertex, a list of the vertices connected to is maintained.
7 Adjacency ListWe will now construct an adjacency list for our example graph.12435VertexAdjacent VerticesStart with vertex 1.It is connected to vertices 2, 3 and 4. So this is recorded in the adjacency list.Then look at vertex 2.It is connected to vertices 1 and 3. This is recorded in the adjacency list.Then do the same for the other vertices in the graph.The graph has now been represented as an adjacency list.12, 3, 421, 331, 2, 4, 541, 3, 553, 4
8 Directed graphsDirected graphs can also be represented as an adjacency matrix or adjacency list.The method is very similar to that for undirected graphs, there are slight differences so that the matrix / list reflect the direction of each edge.In the examples that follow we will use this directed graph:12435
9 Adjacency Matrix for a Directed Graph We will now construct an adjacency matrix for our example graph.12435Then do the same for the other vertices.There is an edge from vertex 3 to 4 and from vertex 4 to 3 so both these cells have a “1” in the table.Start with vertex 1; there is an edge from it to vertex 2 so we place a “1” in row 1, column 2.There is also an edge from vertex 1 to vertex 3 so we put a “1” in row 1, column 3.Place “0”s in the other cells. The adjacency matrix is now complete. Note it is not symmetric.We do not put a “1” in row 2 , column 1 though as there is not an edge from vertex 2 to vertex 1.VERTEX1234511111111
10 Adjacency List for a Directed Graph We will now construct an adjacency list for our example graph.12435VertexAdjacent VerticesStart with vertex 1.There is an edge from it to vertices 2 and 3. So this is recorded in the adjacency list. We do not add 4 to the list as there is no edge from vertex 1 to vertex 4.Then do the same for the other vertices in the graph.The graph has now been represented as an adjacency list.Then look at vertex 2.There is an edge from vertex 2 to vertex 3. This is recorded in the adjacency list.12, 32334, 541, 3, 55
11 Adjacency Matrix vs Adjacency List It is quicker to find out if there is an edge between 2 vertices using an adjacency matrix (you only have to look at one item; in an adjacency list you may have to look at all items in a vertex’s list to see if there is an edge to another vertex).However, an adjacency list can require significantly less space as there is no wastage – it only stores information about which edges do exist in the graph; an adjacency matrix stores information about each possible edge (whether it exists or not).For large graphs which have many vertices but few edges an adjacency list is normally preferable. If a graph has many edges an adjacency matrix is normally the best way of representing the graph.