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 : A B C D A 0 1 1 1 B 1 0 0 1 C 1 0 0 1 D 1 1 1 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 matrix: A B C D A 0 1 0 1 B 0 0 0 0 C 0 0 0 0 D 0 1 1 0

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 A 0 1 1 1 B 0 0 1 1 C 1 1 0 0 D 1 1 0 0

3 Draw a directed graph from the following adjacency list.

Vertex Adjacent vertices

A B A B B D

C D

D A C D

4 Draw a weighted directed graph from the following data adjacency list.

Vertex Adjacent vertices

A B,5,C,3 5 B A,5,D,2 A B C A,3 2 3 D B,2 C D

AQA A level Computer Science © 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.

10 A B C D A B A ∞ 10 20 ∞ B 10 ∞ ∞ 30 20 30 C 20 ∞ ∞ 20 D ∞ 30 20 ∞ 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 from the following array:

Sequence Vertex Left Right 1 E 2 0 2 D 3 0 3 A 0 4 4 B 0 5 5 C 0 0

E

D

A

B

C

8 Represent the following binary tree using arrays:

Blue

Aqua Green

Orange Purple

Sequence Node Left Right

1 Blue 2 3

2 Aqua 0 0 3 Green 4 5 4 Orange 0 0 5 Purple 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 Yellow

Sequence Node Left Right

1 Blue 2 3

2 Aqua 0 0 3 Green 4 5 4 Orange 7 0 5 Purple 0 6 6 Yellow 0 0 7 Magenta 0 0

AQA A level Computer Science © Hodder & Stoughton Limited 2015