A-level Computing 2009/AQA/Problem Solving, Programming, Operating Systems, Databases and Networking/Programming Concepts/Graphs





When there are two vertices connected by an edge, the two vertices are called neighbours. The degree of a vertex is how many other vertices it is connected to (or how many neighbours it has). Graphs are used to record relationships between things. Popular uses of graphs are mapping, social networks, chemical compounds and electrical circuits. We are going to focus on mapping where graphs will be used to model some East Anglian towns and find the shortest distance between two locations, for example in your direction finding GPS application on your phone. Below is an undirected graph (it has no arrows) showing a simplified map of East Anglian Towns: In the graph above we have 7 vertices and 11 edges. Dunwich is neighbour with Blaxhall and Harwich. Clacton has 3 edges directly connecting it to 3 other vertices, it therefore has a degree of 3.

Name all the neighbours of Tiptree:


 * Feering
 * Maldon
 * Clacton
 * Harwich

What is the degree of Tiptree:

4

What is the degree of Dunwich:

2

How many edges and vertices on the following graph:




 * edges = 5
 * vertices = 6

Labelled or Weighted Graphs
A Labelled Graph is almost the same as the graph above, but with something (usually numerical values) attached to the edges. This is useful in mapping applications when you want to find the shortest distance or time between vertices.



What is the shortest distance between Harwich and Feering?


 * 1) Harwich to Tiptree = 31
 * 2) Tiptree to Feering = 3
 * Total = 34

What is the shortest distance between Blaxhall and Clacton?


 * 1) Blaxhall to Harwich = 40
 * 2) Harwich to Clacton = 17
 * Total = 57

What is the shortest distance between Maldon and Dunwich?


 * 1) Maldon to Feering = 11
 * 2) Feering to Blaxhall = 46
 * 3) Blaxhall to Dunwich = 15
 * Total = 72

You might also have:
 * 1) Maldon to Tiptree = 8
 * 2) Tiptree to Feering = 3
 * 3) Feering to Blaxhall = 46
 * 4) Blaxhall to Dunwich = 15
 * Total = 72

Picking best routes is a very complicated task

Directed Graphs or Digraphs
Directed graphs are much like normal graphs, but unsurprisingly they have directions. You should treat them a little like a road system in a town or city. Some roads will be one-way street and others bi-directional. How does this affect our East Anglian Transport system:

What is the shortest distance from Maldon to Dunwich?


 * 1) Maldon to Tiptree = 8
 * 2) Tiptree to Feering = 3
 * 3) Feering to Blaxhall = 46
 * 4) Blaxhall to Dunwich = 15
 * Total = 72

There is now only one solution to this problem.

What is the shortest distance from Harwich to Clacton?


 * 1) Harwich to Tiptree = 31
 * 2) Tiptree to Clacton = 29
 * Total = 60

What is the shortest distance from Dunwich to Maldon?

There is no direct route in that direction

Undirected Graphs
Undirected graphs don't include arrows, there are no one way streets. Undirected graphs can be weighted and unweighted: Undirected graphs allow you to travel both directions down each edge, it works in the same way as a directed graph with two edges between each vertices. See Blaxhall and Dunwich above.

Simple Graphs
Before we can define a simple graph we need to know what loop and multi-edge are:
 * a loop is a vertex with a connection edge to itself
 * a multi-edge is where there are two or more connections between each vertex

In a simple graph with n vertices every vertex has a degree that is less than n.

Give the definition of a simple graph

It is an undirected graph that has no loops and no more than one edge between any two different vertices.

Which of the following graphs are simple graphs?

How to store graphs on computers
So far we have seen how to draw graphs using edges and vertices. If we want to store them as digital data we have to think of a computer friendly way as computers aren't very good at reading hand-drawn diagrams. We are going to look at two methods: the adjacency matrix and the adjacency list.

Adjacency matrix
An adjacency matrix records the relationship between every vertex to all other vertices. It records both when there is an edge between two vertices and when there isn't an edge:

Pros

Cons

Adjacency list
An adjacency list records the relationship between every vertex to all relevant vertices. It records only when there is an edge between two vertices.

Pros

Cons

Draw an Adjacency List and an Adjacency Matrix for the following graph:



Draw an Adjacency List and an Adjacency Matrix for the following graph:



Draw an Adjacency List and an Adjacency Matrix for the following graph:



Draw an Adjacency List and an Adjacency Matrix for the following graph:



Draw a graph for the following Adjacency List:



Draw a graph for the following Adjacency List:



Draw a graph for the following Adjacency Matrix:

Draw a graph for the following Adjacency Matrix



When might you use an adjacency list instead of an adjacency matrix? An adjacency list takes up less space for graphs with smaller number of connections.

When might you use an adjacency matrix instead of an adjacency list? An adjacency matrix is preferable when the graph has lots of connections.