Graph Searching

Introduction to Graph Theory
HKOI Training
(Intermediate)
Kelly Choi
28 Mar 2009
Overview



Introduction to Graphs
Graph Representation
Visiting a graph





BFS (Breadth First Search)
DFS (Depth First Search)
Topological Sort
Flood Fill
Other Topics
A Classical Problem

Maze

S
Find a path
from S to E
E
Rumours



In a group of people, a rumour start
from Sam to others.
Each person can spread the rumour
to his/her friends.
Through how many persons will the
rumour reach Emily?
Sam
Tim
Kate
John
Emily
Aren’t the two problems similar?


Essentially we have some vertices
which are linked together by some
edges.
In both problems, we want to find a
path to get from one vertex to
another.

In other cases, it could be some other
problems.
WHAT ARE GRAPHS?
Graph

In a graph, we have some vertices
and edges. An edge links two
vertices together, with or without a
direction.
1
4
2
vertex
edge
3
Graph

Mathematically, a graph is defined
as G=(V,E),



V is the set of vertices (singular: vertex)
E is the set of edges that connect some
of the vertices
For convenience,


Label vertices with 1, 2, 3, …
Edges can be represented by their two
endpoints.
Graph




Directed/Undirected Graph
Weighted/Unweighted Graph
Simple Graph
Connectivity
Graph Modelling
S
S
E
E
GRAPH REPRESENTATION
Representation of Graph

How do we store a graph in a
program?



Adjacency Matrix
Adjacency linked list
Edge list
Adjacency Matrix
1
2
3
4
5
6
1
1
2
3
0
0
1
0
0
0
1
0
0
0
0
0
1
0
0
5
1
6
0
1
4
0
0
0
0
0
0
5
0
1
0
0
0
0
6
0
0
0
1
0
0
2
3
4
Adjacency Linked List
1

3

5

6
1
2
3
5
6

1

6
2
3
4
4
5

2
6

4
Edge List
i
e[i][1]
e[i][2]
i
s[i]
1
1
3
1
1
2
1
5
2
4
3
1
6
3
4
4
3
1
4
6
5
3
6
5
6
6
5
2
6
7
7
6
4
1
5
6
2
3
4
Representation of Graph
Adjacency
Matrix
Adjacency
Linked List
Edge List
Memory
Storage
O(V2)
O(V+E)
O(V+E)
Check
whether
(u,v) is an
edge
O(1)
O(deg(u))
O(deg(u))
Find all
O(V)
adjacent
vertices of a
vertex u
O(deg(u))
O(deg(u))
deg(u): the number of edges connecting vertex u
VISITING A GRAPH
Visiting a Graph


To scan an array: use iterations
(for-loops)
How to scan the vertices of a
graph?



e.g. to find a path from S to E
Usually we visit a vertex only after
discovering an edge to it from its
neighbour
one of the ways: recursion
Using Recursion

Strategy: Go as far as you can (if
you have not visit there), otherwise,
go back and try another way
Implementation

This is known as Depth-First Search
(DFS).
DFS (vertex u) {
mark u as visited
for each vertex v directly reachable from u
if v is unvisited
DFS (v)
}

Initially all vertices are marked as
unvisited
Note the color of the vertices
1.
Vertices fall into 3 categories:



08-07-2006
Unvisited (White)
Discovered (Grey)
Dead (All reachable vertices from
these vertices are discovered) (Black)
21
Use of DFS

DFS is very useful



When you have to do something with
every vertices
When you want to know whether one
vertex is connected to another vertices
Drawbacks


Stack overflow
Finding a shortest path is difficult
Breadth-First Search (BFS)


BFS tries to find the target from
nearest vertices first.
BFS


uses a queue to store discovered
vertices
expand the path from the earliest
discovered vertices.
Simulation of BFS

Queue:
1
4
3
5
2
4
1
3
2
5
6
6
Question

Why does BFS work to find the
shortest path?
Implementation
while queue Q not empty
dequeue the first vertex u from Q
for each vertex v directly reachable from u
if v is unvisited
enqueue v to Q
mark v as visited

Initially all vertices except the start
vertex are marked as unvisited and
the queue contains the start vertex
only
Advantages


Guarantee shortest paths for
unweighted graphs
Use queue instead of recursive
functions – Avoiding stack overflow
MORE GRAPH PROBLEMS
Finding the shortest paths isn’t the only graph
problem…
Teacher’s Problem (HKOI 2004 Senior)


Emily wants to distribute candies to
N students one by one, with a rule
that if student A is teased by B, A
can receive candy before B.
Given lists of students teased by
each students, find a possible
sequence to give the candies
Topological Sort

In short, in a directed graph,




We want to give a label to the vertices
So that if there is an edge from u to v,
then u<v
Is it always possible?
Topological Sort: to find such order
Observation



The vertex numbered 1 must have
no incoming edge.
The vertex numbered 2 must have
no incoming edges other than
(possibly) one from vertex 1.
And so on…
How to solve the problem?



Find a vertex with no incoming
edges. Number it and remove all
outgoing edges from it.
Repeat the process.
Problem: How to implement the
above process?


Iteration
Recursion
Finding area

Find the area
that is
reachable from
A.
A
Flood Fill



Starting from one vertex, visit (fill)
all vertices that are connected, in
order to get some information, e.g.
area
We can use BFS/DFS
Example: Largest Continuous
Region (HKOI2003 Senior Q4)
Remark on representation of graphs

In BFS/DFS, we perform operations
on all neighbours of some vertices.


Use adjacency linked list / edge list
In some other applications, we may
check whether there is an edge
between two vertices.

Adjacency matrix may be better in
some cases.
Summary

You should know




What a graph is
What it means to model a problem with
a graph
Basic ideas and implementation of
DFS/BFS to find shortest paths / scan
all vertices
Some ideas of Topological Sort and
Floodfilling
Miscellaneous Topics

Euler Path / Circuit




A path/circuit that goes through every
edge exactly once
Diameter and Radius
Trees
More advanced topics

Finding Strongly Connected
Components (SCC)
Preview of Other Advanced Problems


More on DFS and BFS
Shortest path in a weighted graph


Dijkstra’s Algorithm: Using priority
queue
Disjoint set and minimum spanning
trees
Preview of Other Advanced
Problems

Searching techniques:



Bidirectional search (BDS)
Iterative deepening search (IDS)
Network Flow (not required in IOI)
Practice Problems





1067 Maze
2045 Teacher’s Problem
2037 Largest Continuous Region
(HKOI 2003 Senior Q4)
2066 Squareland
3021 Bomber Man