Graphs Worksheet
Graphs Worksheet
Worksheet 5 Graphs
Task 1
1. (a) Create an adjacency matrix to represent the following graph:
E T G M F H S P
E 0 - - 7 - 9 10 21
T - 0 - 5 9 7 8 -
G - -
A B C D E F G H
A 15 13 20
B 15 18
C 13 14
1
Worksheet 5 Graphs
Unit 7 Data structures
D 20 17 8
E 18 17 16
F 16 7
G 14 8 10
H 7 10
2
Worksheet 5 Graphs
Unit 7 Data structures
Task 2
2. (a) Draw the graph associated with the adjacency list below.
A {B:2, C:3}
B {D:7, E:3, C:5}
C {E:4, B:6}
D {C:1}
E {D:7}
A {F:6}
B {A:2, B:8, D:5}
C {E:4}
D {G:3, H:14}
E {B:9, H:6}
F -
G {F:2}
H -
E [M]
F [S, T]
G [F, P]
H []
M [H,P,S]
P []
S []
T [M, F]
(The shape of the graph is not important; the edges and nodes are all that is relevant)
3
Worksheet 5 Graphs
Unit 7 Data structures
(b) Draw a new adjacency list for the equivalent undirected, unweighted graph.
E
T
G
M
F
H
S
P
4. (a) The following Python code represents a graph as a dictionary, with the key being a
node and the value being a list of adjacent nodes. Draw the graph that it represents.
GRAPH = {
'A':['B','D','E'],
'B':['A','C','D'],
'C':['B','G'],
'D':['A','B','E','F'],
'E':['A','D'],
'F':['D'],
'G':['C']
}
(b) Show how a weighted graph could be represented as a dictionary of dictionaries. (Give
some weights to the edges in your graph)
Extension task
4
Worksheet 5 Graphs
Unit 7 Data structures
5. Do some Internet research to find out more about the Page Rank algorithm invented by
Larry Page and Sergei Brin.
6. Draw the adjacency matrix for this graph. [5]
A B C D E
A 1 1 1 1
B 1 1
C 1 1 1
D 1 1 1
E 1 1
A {E:2, C:6}
B {A:10}
C {E:1,D:7,B:3}
D {D:4}
E {B:3}
8. Contrast the use of an adjacency matrix with that of an adjacency list [5]