# 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.
* Exercise: Graphs
* Name all the neighbours of Tiptree:
* Answer:
* Feering
* Maldon
* Clacton
* Harwich
* What is the degree of Tiptree:
* Answer:
* 4
* What is the degree of Dunwich:
* Answer:
* 2
* How many edges and vertices on the following graph:
* Answer:
* 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.
* Exercise: Labelled or Weighted Graphs
* What is the shortest distance between Harwich and Feering?
* Answer:
* Harwich to Tiptree = 31
* Tiptree to Feering = 3
* Total = 34
* What is the shortest distance between Blaxhall and Clacton?
* Answer:
* Blaxhall to Harwich = 40
* Harwich to Clacton = 17
* Total = 57
* What is the shortest distance between Maldon and Dunwich?
* Answer:
* Maldon to Feering = 11
* Feering to Blaxhall = 46
* Blaxhall to Dunwich = 15
* Total = 72
* You might also have:
* Maldon to Tiptree = 8
* Tiptree to Feering = 3
* Feering to Blaxhall = 46
* 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:
* Exercise: Labelled or Weighted Graphs
* What is the shortest distance from Maldon to Dunwich?
* Answer:
* Maldon to Tiptree = 8
* Tiptree to Feering = 3
* Feering to Blaxhall = 46
* Blaxhall to Dunwich = 15
* Total = 72
* There is now only one solution to this problem.
* What is the shortest distance from Harwich to Clacton?
* Answer:
* Harwich to Tiptree = 31
* Tiptree to Clacton = 29
* Total = 60
* What is the shortest distance from Dunwich to Maldon?
* Answer:
* 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:
| un-directed and un-weighted | un-directed and weighted | un-directed and un-weighted |
| | | A simple graph with three vertices and three edges. Each vertex has degree two, so this is also a regular graph. |
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
| Simple Graph | Non Simple Graph |
| This a simple graph as there are no loops or multiedges | This is NOT a simple graph A loop in the above example is where someone going on a walk from Dunwich ends up back in Dunwich without visiting any other vertices. There is a multi-edge between Harwich and Blaxhall, as you could take the ferry, or drive |
In a simple graph with n vertices every vertex has a degree that is less than n.
simple graphs :
* no-loops, no multi-edges, no directions ||
Exercise: Simple Graphs
Give the definition of a simple graph
Answer:
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?
ABCD
Answer:
ABCD
No as it has a loop (on 1)YesNo as it has a multiedge (FN) and is directedNo, it is directed :

## 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:
| Undirected-Unweighted | Directed-Unweighted | Undirected-Weighted | Directed-Weighted |
| BCDFHMT B 0011100 C 0000111 D 1000100 F 1000011 H 1110001 M 0101001 T 0101110 | BCDFHMT B 0010000 C 0000110 D 1000000 F 1000010 H 1010001 M 0000001 T 0101000 | BCDFHMT B ∞∞154640∞∞ C ∞∞∞∞174029 D 15∞∞∞53∞∞ F 46∞∞∞∞113 H 401753∞∞∞31 M ∞40∞11∞∞8 T ∞29∞3318∞ | BCDFHMT B ∞∞15∞∞∞∞ C ∞∞∞∞1740∞ D 17∞∞∞∞∞∞ F 46∞∞∞∞11∞ H 40∞53∞∞∞31 M ∞∞∞∞∞∞∞ T ∞29∞3∞∞∞ |
| edge exists between two vertices = 1 no edge between vertices = 0 | edge exists between two vertices = 1 no edge between vertices = 0 | edge exists between two vertices = use weighting no edge between vertices = ∞ (infinity symbol) | edge exists between two vertices = use weighting no edge between vertices = ∞ (infinity symbol) |
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.
| Undirected-Unweighted | Directed-Unweighted | Undirected-Weighted | Directed-Weighted |
| Connected to BlaxhallD; H; F ClactonH; M; T DunwichB; H FeeringB; M; T HarwichB; C; D; T MaldonC; F; T TiptreeC; F; H; M | Connected to BlaxhallD ClactonH; M DunwichB FeeringB; M HarwichB; D; T MaldonT TiptreeC; F | Connected to BlaxhallD,15; H,40; F,46 ClactonH,17; M,40; T,29 DunwichB,15; H,53 FeeringB,46; M,11; T,3 HarwichB,40; C,17; D,53; T,31 MaldonC,40; F,11; T,8 TiptreeC,29; F,3; H,31; M,8 | Connected to BlaxhallD,15 ClactonH,17; M,40 DunwichB,17 FeeringB,46; M,11 HarwichB,40; D,53; T,31 MaldonT,8 TiptreeC,29; F,3 |
| List each connection | List each connection | List each connection and the weighting | List each connection and the weighting |
Pros
Cons
Exercise: Adjacency Matrices and Lists
Draw an Adjacency List and an Adjacency Matrix for the following graph:
Answer:
ABCDEF
A010011
B101000
C010000
D000000
E100001
F100010
Connected to
AB; E; F;
BA; C
CB
D
EA; F
FA; E
Draw an Adjacency List and an Adjacency Matrix for the following graph:
Answer:
123456
1110010
2101010
3010100
4001011
5110100
6000100
Connected to
11; 2; 5
21; 3; 5
32; 4
43; 5; 6
51; 2; 4
64
Draw an Adjacency List and an Adjacency Matrix for the following graph:
Answer:
1234
10101
20001
31100
40010
Connected to
12; 4
24
31; 2
43
Draw an Adjacency List and an Adjacency Matrix for the following graph:
Answer:
ABCD
A∞204235
B20∞3034
C4230∞12
D353412∞
Connected to
AB,20; C,42; D,35
BA,20; C,30; D,34
CA,42; B,30; D,12
DA,35; B,34; C,12
Draw a graph for the following Adjacency List:
Connected to
AB
BA; C; D
CB; D
DB; C
Answer:
Draw a graph for the following Adjacency List:
Connected to
AC,12; D,60
BA,10
CB,20; D,32
D
EA,7
Answer:
Draw a graph for the following Adjacency Matrix:
ABDFN
A00110
B00011
D00101
F10100
N00100
Answer:
Draw a graph for the following Adjacency Matrix
ABDFN
A∞14∞∞∞
B∞∞2313∞
D5∞∞∞∞
F43∞∞∞33
N∞22∞11∞
Answer:
When might you use an adjacency list instead of an adjacency matrix?
Answer:
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?
Answer:
An adjacency matrix is preferable when the graph has lots of connections. :