# Section 2.5: Graphs and Trees

## Presentation on theme: "Section 2.5: Graphs and Trees"— Presentation transcript:

Section 2.5: Graphs and Trees

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.

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: 1 2 4 3 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 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.

Adjacency Matrix We will now construct an adjacency matrix for our example graph. 1 2 4 3 5 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 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 diagonal Start with vertex 1; there is an edge connecting it to vertex 2 so we place a “1” in row 1, column 2. VERTEX 1 2 3 4 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1

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.

Adjacency List We will now construct an adjacency list for our example graph. 1 2 4 3 5 Vertex Adjacent Vertices 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. 1 2, 3, 4 2 1, 3 3 1, 2, 4, 5 4 1, 3, 5 5 3, 4

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: 1 2 4 3 5

Adjacency Matrix for a Directed Graph
We will now construct an adjacency matrix for our example graph. 1 2 4 3 5 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. 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. VERTEX 1 2 3 4 5 1 1 1 1 1 1 1 1

Adjacency List for a Directed Graph
We will now construct an adjacency list for our example graph. 1 2 4 3 5 Vertex Adjacent Vertices 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 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. 1 2, 3 2 3 3 4, 5 4 1, 3, 5 5