Download presentation

Presentation is loading. Please wait.

1
**Section 2.5: Graphs and Trees**

Adjacency Matrix and Adjacency List

2
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
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

4
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.

5
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

6
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.

7
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

8
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

9
**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

10
**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

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.

Similar presentations

OK

Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.

Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on company act 1956 of india Ppt on cells and tissues Ppt on principles of peace building Ppt on potential energy for class 9 Ppt on ideal gas law practice Ppt on point contact diode symbol Ppt on polynomials in maths sign Download ppt on festivals of france Ppt on different types of computer languages Ppt on packaged drinking water