A-level Computing Teacher guide Unit guide: COMP3 - Graph

A-level Computing
COMP3 Graph Traversals
COMP3: Section 3.3.2 Programming Concepts: Standard Algorithms
Graphs traversals
Traversing a Graph
Perhaps the most fundamental graph problem is to visit every edge and vertex in a graph in a
systematic way. This is what is meant by traversing a graph.
Mazes are naturally represented by graphs, where each graph vertex denotes a junction of the
maze, e.g. B and each graph edge denotes a hallway in the maze, e.g. A — D as shown in Figure
1.
Figure 1 Maze and its equivalent graph
Start
A
J
C
Finish
Start
A
C
B
J
Finish
H
B
G
I
D
E
F
I
G
E
H
D
F
To escape the maze a graph traversal algorithm must avoid getting trapped in the maze visiting the
same place repeatedly, e.g. ADEFEFEF….
Also the maze must be traversed in a systematic way to ensure that it is possible to escape the
maze.
To meet these two requirements the algorithm must
1. Mark each vertex when it is first visited
2. Keep track of what hasn’t been completely explored
Although unravelled threads (Ariadne’s thread) have been used to mark visited places in a maze in
mythology, we will rely on Boolean flags as follows:
-
Undiscovered - the vertex is in its initial, virgin state
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
A-level Computing – COMP3 graph traversals
-
Discovered - the vertex has been found, but we have not yet checked out all its incident
edges, e.g. E has been visited but edges E — F and E — G have not yet been explored.
Processed or completely explored- the vertex after we have visited all its incident edges.
Clearly, a vertex cannot be processed until after we discover it, so the state of each vertex
progresses during the traversal from undiscovered to discovered, then to processed or completely
explored.
A structure must be maintained containing the vertices that have been discovered but not yet
completely processed. Initially only the start vertex is considered to be discovered. To completely
explore a vertex v, we must evaluate each edge leaving v. If an edge goes to an undiscovered
vertex u, we mark u discovered and add it to the list of work to do. We ignore an edge that goes to
a processed vertex because further consideration adds nothing to our knowledge of the graph. We
can also ignore any edge going to a discovered but not processed vertex because the destination
already resides on the list of vertices to process.
Breadth-First Search (BFS)
At some point during the course of a traversal, every node in the graph changes state from
undiscovered to discovered. In a breadth-first search of an undirected graph, we assign a direction
to each edge, from the discoverer u to the discovered v. Thus u is denoted the parent of v. With
this scheme each node has exactly one parent, except for the root. This defines a tree on the
vertices of the graph as shown in Figure 3.
Figure 2 Graph representing the maze of Figure 1
Root
A
C
B
J
I
D
E
G
H
F
Figure 3 Breadth-first search tree
A
B
D
C
E
G
F
H
I
J
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
2 of 7
A-level Computing – COMP3 graph traversals
The traversal proceeds as follows:
Starting at node A
Explore all the neighbours of A:
- Explore D, B
Then explore all the unvisited neighbours of the neighbours of A:
- E, C
Then visit unvisited neighbours of these:
- F, G
Then visit unvisited neighbour of these:
- H, I
Continue until no more unvisited nodes remain:
- J
The vertices are visited in the following order:
ADBECFGHIJ
The breadth-first search defines a shortest path from the root to every other vertex in the tree. This
property makes breadth-first search very useful in shortest path problems. In the algorithm below,
once a vertex is discovered it is placed in a queue. Since the vertices are processed in first-in, firstout order, the oldest vertices are expanded first which are exactly those closest to the root.
BFS algorithm:
For each vertex u in graph except start vertex, s
Do
State[u] ← ′undiscovered′
Parent[u] ← nil
State[s] ← ‘discovered’
Parent[s] ← nil
EnQueue(Q, s)
While Not Queue empty
Do
u = DeQueue(Q)
Process vertex u
For each v adjacent to u
Do
Process edge(u, v)
If State[v] = ′undiscovered′
Then
State[v] ← ′discovered′
Parent[v] ← u
Enqueue(Q, v)
State[u] ← ′processed/completely explored′
Figure 3 shows a screenshot taken from GraphDraw (www.educational-computing.co.uk) part way
through a breadth-first traversal of the graph from Figure 2. Note that vertices A, B and D have
been completely explored, i.e. all incident edges visited.
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
3 of 7
A-level Computing – COMP3 graph traversals
Figure 3 GraphDraw screenshot showing a partly completed BFS
Applications of Breadth-First Search
Connected Components
A friendship graph is one in which the vertices are people and there is an edge between two
people if and only if they are friends. Such graphs are called social networks. In 1967, the social
psychologist Stanley Milgram performed a remarkable experiment. Milgram was interested in an
unresolved hypothesis in the sociological community of the day. The hypothesis was that the
world, viewed as an enormous network of social acquaintances was in a certain sense ″small″.
Small meant that anyone person in the world could be reached through a network of friends in only
a few steps. It was called the small-world problem. In fact, Milgram’s work was predated by that of
Karinthy in 1929. In a series of short stories, Karinthy’s asserted that people are linked by at most
five links. This was the first published appearance of the concept we know today as ″six degrees of
separation″. The theory supposes that there is always a short path linking every two people in the
world. A graph is said to be connected if there is a path between every pair of vertices. If the theory
is true, it means that the friendship graph must be connected. Connected components can be
found using bread-first search. We start from the first vertex. Anything that we discover during the
search must be part of the same connected component.
The vertex-colouring problem seeks to assign a colour to each vertex of a graph such that no edge
links any two vertices of the same colour. This is achieved easily by assigning each vertex a
unique colour. However, the goal is to use as few colours as possible.
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
4 of 7
A-level Computing – COMP3 graph traversals
Two-colouring Graphs
A graph is bipartite if it can be coloured without conflicts while using only two colours. Bipartite
graphs are important because they arise naturally in many applications. Suppose that in a group of
n single women and n single men who desire to get married, each participant indicates who among
the opposite sex would be acceptable as a potential spouse. A bipartite graph can be used to
represent this with two sets of vertices, one to represent the set of n women and the other the set
of n men as shown in Figure 4. A woman w is joined by an edge to a man m if they like each other.
For example we could have the women Sue, Mary, Ann, Pam, Jennie, Val and Wendy, and the
men Dave, Brian, Kevin, Tom, Alex, Steve and Neil. If Sue liked Dave and Brian, (and vice-versa),
Mary liked Dave and Kevin, Ann liked Tom, Alex and Steve, Pam liked Brian, Jennie liked Dave
and Tom, Val liked Steve, we would have the graph shown in Figure 4. In this situation could we
marry everybody to someone they liked? This is one version of the Marriage Problem.
Figure 4 Potential for marriage graph
Sue
Mary
Ann
Pam
Jennie
Val
Dave
Brian
Kevin
Tom
Alex
Steve
We add to the breadth-first search algorithm so that whenever a new vertex is discovered, it is
coloured the opposite of its parent. By checking whether any non-discovered edge links two
vertices of the same colour conflict can be detected meaning that the graph cannot be twocoloured. When the amended algorithm is applied to the potential for marriage graph we end up
with a colouring which confirms that the graph is bipartite as shown in Figure 5.
Figure 5 also indicates how all n men and n woman can be married so that each marriage is
between a man and a woman who like each other.
Figure 5 Bipartite graph
Sue
Mary
Ann
Pam
Jennie
Val
Dave
Brian
Kevin
Tom
Alex
Steve
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
5 of 7
A-level Computing – COMP3 graph traversals
Depth-First Search (DFS)
The difference between BFS and DFS is in the order in which they explore vertices. This order
depends completely upon the container data structure used to store the discovered but not
processed/completely explored vertices.


Queue - By storing the vertices in a first-in, first-out (FIFO) queue, the oldest unexplored
vertices are explored first.
Stack - By storing vertices in a last-in, first-out (LIFO) stack, vertices are explored by visiting a
new neighbour a new neighbour if one is available and backing up only when surrounded by
previously discovered vertices.
Depth-first search has a recursive solution which eliminates the need to explicitly use a stack.
The algorithm maintains a notion of traversal time for each vertex. Each time a vertex is entered or
exited Time is incremented.
DFS algorithm:
DFS(G, u)
State[u] ← ′discovered′
Process vertex u
Entry[u] ← Time
Time ← Time + 1
For each v adjacent to u
Do
Process edge(u, v)
If State[v] = ′undiscovered′
Then
Parent[v] ← u
DFS(G,v)
State[u] ← ′processed/completely explored′
Exit[u] ← Time
Time ← Time + 1
The difference between the exit and entry times for v gives how many descendents v has in the
DFS tree. Half the time difference denotes the number of descendents of v. Figure 6 shows a
depth first traversal on the graph from Figure 1. Note that the algorithm descends as far as it can
and when it can go no further it backtracks until it can find another route to descend as shown in
Figure 7.
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
6 of 7
A-level Computing – COMP3 graph traversals
Figure 6 Partially-completed depth-first search of graph from Figure 1
Figure 7 Partially-completed depth-first search of graph from Figure 1 showing
backtracking having taken place
Applications of Depth-First Search
DFS can be used to solve puzzles with only one solution, such as mazes.
Reproduced by permission of Dr K Bond. All rights reserved.
AQA Education (AQA) is a registered charity (number 1073334) and a company limited by guarantee registered in
England and Wales (number 3644723). Our registered address is AQA, Devas Street, Manchester M15 6EX.
7 of 7