Presentation is loading. Please wait.

Presentation is loading. Please wait.

AQA Computing A2 © Nelson Thornes 2009 Section 2.5 1 Section 2.5: Graphs and Trees Adjacency Matrix and Adjacency List.

Similar presentations


Presentation on theme: "AQA Computing A2 © Nelson Thornes 2009 Section 2.5 1 Section 2.5: Graphs and Trees Adjacency Matrix and Adjacency List."— Presentation transcript:

1 AQA Computing A2 © Nelson Thornes 2009 Section Section 2.5: Graphs and Trees Adjacency Matrix and Adjacency List

2 AQA Computing A2 © Nelson Thornes 2009 Section Learning Objectives In 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 AQA Computing A2 © Nelson Thornes 2009 Section Representing a graph There are two ways of representing a graph so that it can be processed by a computer program: Adjacency Matrix Adjacency List To look at how these work we will use the following undirected graph:

4 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Adjacency Matrix A 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 vertexs row with another vertexs 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. 4

5 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Adjacency Matrix We will now construct an adjacency matrix for our example graph VERTEX Start with vertex 1; there is an edge connecting it to vertex 2 so we place a 1 in row 1, column 2. 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. 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. There 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 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. Then look at the remaining vertices, placing a 1 in the cells which represent vertices that are connected in the graph. 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. Notice that the adjacency matrix is symmetric about the top-left to bottom-right diagonal

6 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Adjacency List An 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. 6

7 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Adjacency List We will now construct an adjacency list for our example graph VertexAdjacent Vertices , 4 1, 3, 5 1, 2, 4, 5 1, 3 2, 3, 4 Start 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.

8 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Directed graphs Directed 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:

9 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Adjacency Matrix for a Directed Graph We will now construct an adjacency matrix for our example graph. 9 VERTEX Start with vertex 1; there is an edge from it to vertex 2 so we place a 1 in row 1, column We do not put a 1 in row 2, column 1 though as there is not an edge from vertex 2 to vertex 1. There is also an edge from vertex 1 to vertex 3 so we put a 1 in row 1, column 3. Then 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. Place 0s in the other cells. The adjacency matrix is now complete. Note it is not symmetric.

10 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 Adjacency List for a Directed Graph We will now construct an adjacency list for our example graph. 10 VertexAdjacent Vertices , 3, 5 4, 5 3 2, 3 Start 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 look at vertex 2. There is an edge from vertex 2 to vertex 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

11 AQA Computing A2 © Nelson Thornes 2009 Section 2.5 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 vertexs 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. 11


Download ppt "AQA Computing A2 © Nelson Thornes 2009 Section 2.5 1 Section 2.5: Graphs and Trees Adjacency Matrix and Adjacency List."

Similar presentations


Ads by Google