Chapter 9 – Graphs and trees

AQA A level Computer Science
Teaching and Learning Resources
Chapter 9 – Graphs and trees
TASK QUESTIONS (WITH ANSWERS)
1
Create an adjacency list and matrix from the following graphs.
A
B
A
B
C
D
C
D
First graph adjacency matrix:
A
B
A
0
1
B
1
0
C
1
0
D
1
1
C
1
0
0
1
D
1
1
1
0
Second graph adjacency matrix:
A
B
C
A
0
1
0
B
0
0
0
C
0
0
0
D
0
1
1
D
1
0
0
0
First graph adjacency list:
Node
Adjacent nodes
A
B, C, D
B
A, D
C
A, D
D
A, B, C
Second graph adjacency table:
Node
Adjacent nodes
A
B, D
B
C
D
C
AQA A level Computer Science
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
2
Draw an undirected graph from the following adjacency matrix.
A
B
C
D
3
A
0
0
1
1
B
1
0
1
1
C
1
1
0
0
D
1
1
0
0
A
B
C
D
Draw a directed graph from the following adjacency list.
Vertex Adjacent vertices
4
A
B
B
D
C
D
D
A
A
B
C
D
Draw a weighted directed graph from the following data adjacency list.
Vertex Adjacent vertices
A
B,5,C,3
B
A,5,D,2
C
A,3
D
B,2
5
A
2
3
C
AQA A level Computer Science
B
D
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
5
Draw a weighted undirected graph from the following data adjacency matrix.
A
B
C
D
A
∞
10
20
∞
B
10
∞
∞
30
C
20
∞
∞
20
D
∞
30
20
∞
10
A
B
20
30
C
D
20
6
Explain where it might be more appropriate to use an adjacency list compared to an
adjacency matrix.
Adjacency list
Adjacency matrix
Only stores data where there is an
Stores a value for every combination of
adjacency (edge) so requires less memory. node adjacencies so requires more
memory.
The list has to be parsed to identify
Adjacencies can be identified more
whether particular adjacencies exist,
quickly as every combination is already
which increases the time taken to process stored. Effectively the matrix forms a
the data.
look-up table of each node to every
other node.
Where there are not that many edges
Where there are many edges (lots of
(few adjacencies), this method would be
adjacencies), this method would be
more suitable for the reasons stated
more suitable for the reasons stated
above. This is known as a sparse graph.
above. This is known as a dense graph.
AQA A level Computer Science
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
7
Draw a binary tree from the following array:
Sequence
1
2
3
4
5
Vertex
E
D
A
B
C
Left
2
3
0
0
0
Right
0
0
4
5
0
E
D
A
B
C
8
Represent the following binary tree using arrays:
Blue
Aqua
Green
Orange
Sequence
1
2
3
4
5
Node
Blue
Aqua
Green
Orange
Purple
Purple
Left
2
0
4
0
0
Right
3
0
5
0
0
Show what would happen to the tree and the array if the following two items of data were
added: Yellow, Magenta
AQA A level Computer Science
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
Blue
Aqua
Green
Orange
Purple
Magenta
Sequence
1
2
3
4
5
6
7
AQA A level Computer Science
Yellow
Node
Blue
Aqua
Green
Orange
Purple
Yellow
Magenta
Left
2
0
4
7
0
0
0
Right
3
0
5
0
6
0
0
© Hodder & Stoughton Limited 2015