Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | imogene-dixon |
View: | 62 times |
Download: | 3 times |
Introduction to Graph TheoryIntroduction to Graph Theory
HKOI Training (Intermediate)HKOI Training (Intermediate)Kelly ChoiKelly Choi
28 Mar 200928 Mar 2009
OverviewOverview
Introduction to GraphsIntroduction to Graphs Graph RepresentationGraph Representation Visiting a graphVisiting a graph
BFS (Breadth First Search)BFS (Breadth First Search) DFS (Depth First Search)DFS (Depth First Search)
Topological SortTopological Sort Flood FillFlood Fill Other TopicsOther Topics
A Classical ProblemA Classical Problem
MazeMaze Find a path Find a path
from S to Efrom S to E
S
E
RumoursRumours
In a group of people, a rumour start In a group of people, a rumour start from from SamSam to others. to others.
Each person can spread the rumour Each person can spread the rumour to his/her friends. to his/her friends.
Through how many persons will the Through how many persons will the rumour reach rumour reach EmilyEmily??
SamSam
EmilyEmily
TimTim
KateKate
JohnJohn
Aren’t the two problems similar?Aren’t the two problems similar?
Essentially we have some Essentially we have some verticesvertices which are linked together by some which are linked together by some edgesedges..
In both problems, we want to find a In both problems, we want to find a path to get from one vertex to path to get from one vertex to another.another. In other cases, it could be some other In other cases, it could be some other
problems. problems.
WHAT ARE GRAPHS?
GraphGraph
In a graph, we have some In a graph, we have some verticesvertices and and edgesedges. An edge links two . An edge links two vertices together, with or without a vertices together, with or without a direction. direction.
1
2
4
3
vertex
edge
GraphGraph
Mathematically, a Mathematically, a graphgraph is defined is defined as G=(V,E), as G=(V,E), V is the set of vertices (singular: vertex)V is the set of vertices (singular: vertex) E is the set of edges that connect some E is the set of edges that connect some
of the verticesof the vertices For convenience,For convenience,
Label vertices with 1, 2, 3, …Label vertices with 1, 2, 3, … Edges can be represented by their two Edges can be represented by their two
endpoints.endpoints.
GraphGraph
Directed/Undirected GraphDirected/Undirected Graph Weighted/Unweighted GraphWeighted/Unweighted Graph Simple GraphSimple Graph ConnectivityConnectivity
Graph Modelling
S
E
SS
EE
GRAPH REPRESENTATION
Representation of GraphRepresentation of Graph
How do we store a graph in a How do we store a graph in a program?program? Adjacency MatrixAdjacency Matrix Adjacency linked listAdjacency linked list Edge listEdge list
Adjacency Matrix
1 2 3 4 5 6
1 0 0 1 0 1 1
2 0 0 0 0 0 0
3 1 0 0 0 0 1
4 0 0 0 0 0 0
5 0 1 0 0 0 0
6 0 0 0 1 0 0
11
2233
44
66
55
Adjacency Linked List
1 3 5 6
2
3 1 6
4
5 2
6 4
11
2233
44
66
55
Edge List
i e[i][1] e[i][2]
1 1 3
2 1 5
3 1 6
4 3 1
5 3 6
6 5 2
7 6 4
11
2233
44
66
55
i s[i]
1 1
2 4
3 4
4 6
5 6
6 7
Representation of GraphRepresentation of Graph
Adjacency Adjacency MatrixMatrix
Adjacency Adjacency Linked ListLinked List
Edge ListEdge List
Memory Memory StorageStorage
O(VO(V22)) O(V+E)O(V+E) O(V+E)O(V+E)
Check Check whether whether ((uu,,vv) is an ) is an edgeedge
O(1)O(1) O(deg(u))O(deg(u)) O(deg(u))O(deg(u))
Find all Find all adjacent adjacent vertices of a vertices of a vertex vertex uu
O(V)O(V) O(deg(u))O(deg(u)) O(deg(u))O(deg(u))
deg(u): the number of edges connecting vertex deg(u): the number of edges connecting vertex uu
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 RecursionUsing Recursion
Strategy: Go as far as you can (if Strategy: Go as far as you can (if you have not visit there), otherwise, you have not visit there), otherwise, go back and try another waygo back and try another way
ImplementationImplementation
This is known as Depth-First Search This is known as Depth-First Search (DFS).(DFS).
DFS (vertex DFS (vertex uu) {) {
mark mark uu as as visitedvisited
for each vertex for each vertex vv directly reachable from directly reachable from uu
if if vv is is unvisitedunvisited
DFS (DFS (vv))
}}
Initially all vertices are marked as Initially all vertices are marked as unvisitedunvisited
08-07-2006 21
Note the color of the vertices
1. Vertices fall into 3 categories: Unvisited (White) Discovered (Grey) Dead (All reachable vertices from
these vertices are discovered) (Black)
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)Breadth-First Search (BFS)
BFS tries to find the target from BFS tries to find the target from nearestnearest vertices first. vertices first.
BFSBFS uses a queue to store discovered uses a queue to store discovered
verticesvertices expand the path from the earliest expand the path from the earliest
discovered vertices.discovered vertices.
1
4
3
25
6
Simulation of BFSSimulation of BFS
Queue:Queue: 1 4 3 5 2 6
Question
Why does BFS work to find the shortest path?
ImplementationImplementation
while queue Q not emptywhile queue Q not emptydequeue the first vertex dequeue the first vertex uu from Q from Qfor each vertex for each vertex vv directly reachable from directly reachable from uu
if if vv is is unvisitedunvisitedenqueue enqueue vv to Q to Qmark mark vv as as visitedvisited
Initially all vertices except the start Initially all vertices except the start vertex are marked as vertex are marked as unvisitedunvisited and and the queue contains the start vertex the queue contains the start vertex onlyonly
AdvantagesAdvantages
Guarantee shortest paths for Guarantee shortest paths for unweighted graphsunweighted graphs
Use queue instead of recursive Use queue instead of recursive functions – Avoiding stack overflowfunctions – Avoiding stack overflow
MORE GRAPH PROBLEMS
Finding the shortest paths isn’t the only graph problem…
Teacher’s Problem (HKOI 2004 Senior)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 SortTopological Sort
In short, in a In short, in a directeddirected graph, graph, We want to give a label to the verticesWe want to give a label to the vertices So that if there is an edge from u to v, So that if there is an edge from u to v,
then u<vthen u<v Is it always possible?Is it always possible?
Topological Sort: to find such orderTopological Sort: to find such order
ObservationObservation
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 FillFlood Fill
Starting from one vertex, visit (fill) Starting from one vertex, visit (fill) all vertices that are connected, in all vertices that are connected, in order to get some information, e.g. order to get some information, e.g. areaarea
We can use BFS/DFSWe can use BFS/DFS
Example: Largest Continuous Example: Largest Continuous Region (HKOI2003 Senior Q4)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 TopicsMiscellaneous Topics
Euler Path / CircuitEuler Path / Circuit A path/circuit that goes through every A path/circuit that goes through every
edge edge exactly onceexactly once Diameter and RadiusDiameter and Radius TreesTrees More advanced topicsMore advanced topics
Finding Strongly Connected Finding Strongly Connected Components (SCC)Components (SCC)
Preview of Other Advanced Problems
More on DFS and BFSMore on DFS and BFS Shortest path in a weighted graphShortest path in a weighted graph
Dijkstra’s Algorithm: Using priority Dijkstra’s Algorithm: Using priority queuequeue
Disjoint set and minimum spanning Disjoint set and minimum spanning treestrees
Preview of Other Advanced Problems
Searching techniques: Searching techniques: Bidirectional search (BDS) Bidirectional search (BDS) Iterative deepening search (IDS)Iterative deepening search (IDS)
Network Flow (not required in IOI)Network Flow (not required in IOI)
1067 Maze1067 Maze 2045 Teacher’s Problem2045 Teacher’s Problem 2037 Largest Continuous Region2037 Largest Continuous Region
(HKOI 2003 Senior Q4)(HKOI 2003 Senior Q4) 2066 Squareland2066 Squareland 3021 Bomber Man3021 Bomber Man
Practice Problems