Advanced Graph Modelling and Searching

Download Report

Transcript Advanced Graph Modelling and Searching

Advanced Graph Modelling and
Searching
HKOI Training 2010
Graph
•
•
•
•
•
A graph is a set of vertices and a set of edges
G = (V, E)
Number of vertices = |V|
Number of edges = |E|
We assume simple graph, so |E| = O(|V|2)
Trees in graph theory
• In graph theory, a tree is an acyclic, connected
graph
– Acyclic means “without cycles”
Properties of trees
• |E| = |V| - 1
– |E| = (|V|)
• Between any pair of vertices, there is a unique path
• Adding an edge between a pair of non-adjacent
vertices creates exactly one cycle
• Removing an edge from the tree breaks the tree into
two smaller trees
Definition?
• The following four conditions are equivalent:
– G is connected and acyclic
– G is connected and |E| = |V| - 1
– G is acyclic and |E| = |V| - 1
– Between any pair of vertices in G, there exists a
unique path
• G is a tree if at least one of the above
conditions is satisfied
Trees and related terms
ancestors
root
parent
siblings
descendants
children
Representation of Graph
• Adjacency Matrix
• Adjacency list
• Edge list
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
adjacent
vertices of a
vertex u
O(V)
O(deg(u))
O(deg(u))
deg(u): the number of edges connecting vertex u
Graph Traversal
• Given: a graph
• Goal: visit all (or some) vertices and edges of
the graph using some strategy (the order of visit
is systematic)
• DFS, BFS are examples of graph traversal
algorithms
• Some shortest path algorithms and spanning
tree algorithms have specific visit order
Idea of DFS and BFS
• This is a brief idea of DFS and BFS
• DFS: continue visiting next vertex whenever there is a
road, go back if no road (ie. visit to the depth of current
path)
– Example: a human want to visit a place, but do not know the
path
• BFS: go through all the adjacent vertices before going
further (ie. spread among next vertices)
– Example: set a house on fire, the fire will spread through the
house
DFS (pseudo code)
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
DFS (Demonstration)
F
E
unvisited
A
visited
D
B
C
“Advanced” DFS
• Apart from just visiting the vertices, DFS
can also provide us with valuable
information
• DFS can be enhanced by introducing:
– birth time and death time of a vertex
• birth time: when the vertex is first visited
• death time: when we retreat from the vertex
– DFS tree
– parent of a vertex
DFS spanning tree / forest
• A rooted tree
• The root is the start vertex
• If v is first visited from u, then u is the parent of v in the
DFS tree
• Edges are those in forward direction of DFS, ie. when
visiting vertices that are not visited before
• If some vertices are not reachable from the start vertex,
those vertices will form other spanning trees (1 or more)
• The collection of the trees are called forest
DFS (pseudo code)
DFS (vertex u) {
mark u as visited
time  time+1; birth[u]=time;
for each vertex v directly reachable from u
if v is unvisited
parent[v]=u
DFS (v)
time  time+1; death[u]=time;
}
DFS forest (Demonstration)
A
B
C
D
E
F
G
H
birth
1
2
3
13
10
4
14
6
death
12
9
8
16
11
5
15
7
parent
-
A
B
-
A
C
D
C
A
D
B
E
F
E
G
C
A
D
F
H
unvisited
B
visited
C
H
G
visited (dead)
Classification of edges
•
•
•
•
Tree edge
Forward edge
Back edge
Cross edge
A
D
B
E
C
• Question: which type of edges is
F always
H
absent in an undirected graph?
G
Determination of edge types
• How to determine the type of an arbitrary
edge (u, v) after DFS?
• Tree edge
– parent [v] = u
• Forward edge
– not a tree edge; and
– birth [v] > birth [u]; and
– death [v] < death [u]
• How about back edge and cross edge?
Determination of edge types
Tree
edge
Forward Edge
Back Edge
Cross Edge
parent [v] not a tree edge
birth[v] < birth[u]
birth[v] < birth[u]
=u
birth[v] > birth[u]
death[v] > death[u] death[v] < death[u]
death[v] < death[u]
Applications of DFS Forests
• Topological sorting (Tsort)
• Strongly-connected components (SCC)
• Some more “advanced” algorithms
Example: Tsort
• Topological order: A numbering of the vertices
of a directed acyclic graph such that every edge
from a vertex numbered i to a vertex numbered
j satisfies i<j
• Tsort: Number the vertices in topological order
3
1
6
7
2
4
5
Tsort Algorithm
• If the graph has more then one vertex that has
indegree 0, add a vertice to connect to all
indegree-0 vertices
• Let the indegree 0 vertice be s
• Use s as start vertice, and compute the DFS
forest
• The death time of the vertices represent the
reverse of topological order
Tsort (Demonstration)
S
A
B
C
D
birth
1
2
3
4
12
death
16
11
10
7
A
E
15
F
G
13
8
5
14
9
6
B
C
G
S
D
E
GCFBAED
DEABFCG
F
Example: SCC
• A graph is strongly-connected if
– for any pair of vertices u and v, one can go
from u to v and from v to u.
• Informally speaking, an SCC of a graph is a
subset of vertices that
– forms a strongly-connected subgraph
– does not form a strongly-connected
subgraph with the addition of any new vertex
SCC (Illustration)
SCC (Algorithm)
• Compute the DFS forest of the graph G to
get the death time of the vertices
• Reverse all edges in G to form G’
• Compute a DFS forest of G’, but always
choose the vertex with the latest death time
when choosing the root for a new tree
• The SCCs of G are the DFS trees in the DFS
forest of G’
SCC (Demonstration)
A
B
C
D
E
F
G
H
birth
1
2
3
13
10
4
14
6
death
12
9
8
16
11
5
15
7
parent
-
A
B
-
A
C
D
C
E
F
A
E
B
D
A
D
G
F
B
C
C
H
G
H
SCC (Demonstration)
A
F
D
G
F
C
B
C
H
G
B
D
E
A
E
H
DFS Summary
• DFS spanning tree / forest
• We can use birth time and death time in DFS spanning
tree to do varies things, such as Tsort, SCC
• Notice that in the previous slides, we related birth time
and death time. But in the discussed applications, birth
time and death time can be independent, ie. birth time
and death time can use different time counter
Breadth-first search (BFS)



Revised:
DFS: continue visiting next vertex
whenever there is a road, go
back if no road (ie. visit to the
depth of current path)
BFS: go through all the adjacent
vertices before going further (ie.
spread among next vertices)
• In order to “spread”,
we need to makes
use of a data
structure, queue ,to
remember just
visited vertices
BFS (Pseudo code)
while queue not empty
dequeue the first vertex u from queue
for each vertex v directly reachable from u
if v is unvisited
enqueue v to queue
mark v as visited
• Initially all vertices except the start vertex are
marked as unvisited and the queue contains
the start vertex only
BFS (Demonstration)
Queue:
A B C
D E
H
G J
I
I
G
D
F
C
unvisited
H
visited
A
E
J
F
B
visited (dequeued)
Applications of BFS
• Shortest paths finding
• Flood-fill (can also be handled by DFS)
Comparisons of DFS and BFS
DFS
BFS
Depth-first
Breadth-first
Stack
Queue
Does not guarantee
shortest paths
Guarantees shortest
paths
What is graph modeling?
• Conversion of a problem into a graph problem
• Sometimes a problem can be easily solved
once its underlying graph model is recognized
• Graph modeling appears almost every year in
NOI or IOI
Basics of graph modeling
• A few steps:
– identify the vertices and the edges
– identify the objective of the problem
– state the objective in graph terms
– implementation:
• construct the graph from the input instance
• run the suitable graph algorithms on the graph
• convert the output to the required format
Simple examples (1)
• Given a grid maze with obstacles, find a
shortest path between two given points
start
goal
Simple examples (2)
• A student has the phone numbers of some
other students
• Suppose you know all pairs (A, B) such that A
has B’s number
• Now you want to know Alan’s number, what is
the minimum number of calls you need to
make?
Simple examples (2)
• Vertex: student
• Edge: whether A has B’s number
– Add an edge from A to B if A has B’s number
• Problem: find a shortest path from your vertex
to Alan’s vertex
Complex examples (1)
• Same settings as simple example 1
• You know a trick – walking through an
obstacle! However, it can be used for only
once
• What should a vertex represent?
– your position only?
– your position + whether you have used the
trick
Complex examples (1)
• A vertex is in the form (position, used)
• The vertices are divided into two groups
– trick used
– trick not used
Complex examples (1)
unused
start
goal
used
start
goal
goal
Complex examples (1)
• How about you can walk through obstacles for
k times?
Complex examples (1)
k
start
goal
k-1
k-2
Complex examples (1)
k
start
goal
k-1
k-4
k-3
k-2
Complex examples (2)
• The famous 8-puzzle
• Given a state, find the moves that bring it
to the goal state
1
2
3
4
5
6
7
8
Complex examples (2)
• What does a vertex represent?
– the position of the empty square?
– the number of tiles that are in wrong
positions?
– the state (the positions of the eight tiles)
• What are the edges?
• What is the equivalent graph problem?
Complex examples (2)
1 2 3
4 5 6
7 8
1 2 3
4 5 6
7
8
1 2 3
4
6
7 5 8
1 2 3
4 5
7 8 6
1 2
4 5 3
7 8 6
1 2 3
4
5
7 8 6
1 2 3
4 5 6
7 8
Complex examples (3)
• Theseus and Minotaur
– http://www.logicmazes.com/theseus.html
– Extract:
• Theseus must escape from a maze. There is also a
mechanical Minotaur in the maze. For every turn that
Theseus takes, the Minotaur takes two turns. The Minotaur
follows this program for each of his two turns:
• First he tests if he can move horizontally and get closer to
Theseus. If he can, he will move one square horizontally. If
he can’t, he will test if he could move vertically and get
closer to Theseus. If he can, he will move one square
vertically. If he can’t move either horizontally or vertically,
then he just skips that turn.
Complex examples (3)
• What does a vertex represent?
– Theseus’ position
– Minotaur’s position
– Both
Some more examples
• How can the followings be modeled?
– Tilt maze (Single-goal mazes only)
• http://www.clickmazes.com/newtilt/ixtilt2d.htm
– Double tilt maze
• http://www.clickmazes.com/newtilt/ixtilt.htm
– No-left-turn maze
• http://www.clickmazes.com/noleft/ixnoleft.htm
– Same as complex example 1, but you can use
the trick for k times
Teacher’s Problem
• Question: A teacher wants to distribute sweets to
students in an order such that, if student u tease
student v, u should not get the sweet before v
• Vertex: student
• Edge: directed, (v,u) is a directed edge if student v
tease u
• Algorithm: Tsort
OI Man
• Question: OIMan (O) has 2 kinds of form: Hand S-form. He can transform to S-form for m
minutes by battery. He can only kill monsters
(M) and virus (V) in S-form. Given the number
of battery and m, find the minimum time
needed to kill the virus.
• Vertex: position, form, time left for S-form,
number of batteries left
• Edge: directed
– Move to P in H-form (position)
– Move to P/M/V in S-form (position, S-form time)
– Use battery and move (position, form, S-form time,
number of batteries)
PWWP
PPPP
PWWP
OMMV
What you have learnt:
Graph Modeling
Variations of BFS and DFS
• Bidirectional Search (BDS)
• Iterative Deepening Search(IDS)
BDS(BI-DIRECTIONAL SEARCH)
•
•
•
•
BFS eats up memory
Let it waste more
Start BFS at both start and goal
Searching becomes faster
start
goal
BDS(BI-DIRECTIONAL SEARCH)
BDS Example: Bomber Man (1 Bomb)
• find the shortest path from the upper-left corner to
the lower-right corner in a maze using a bomb. The
bomb can destroy a wall.
S
E
Bomber Man (1 Bomb)
S 1 2
3 4
13 12 11 12
4
8
7
6
5
10
6
9
5
10 11 12 13
4
7
8
9
3
2
1 E
Shortest Path length = 8
What will happen if we stop once we find a
path?
Example
S
1
2
3
4
5
6
21
20
19
18
17
11 12 13 14 15 16
10
9 8 7 6 5 4 3
7
8
9
10
11
12
13
14
17 16 15
2
1
E
Iterative deepening search (IDS)
• Iteratively performs DFS with increasing depth
bound
• Shortest paths are guaranteed
IDS
IDS (pseudo code)
DFS (vertex u, depth d) {
mark u as visited
if (d>0)
for each vertex v directly reachable from u
if v is unvisited
DFS (v,d-1)
}
i=0
Do {
DFS(start vertex,i)
Increment i
}While (target is not found)
IDS
• The complexity of IDS is the same as DFS if
the search tree is balanced
Summary of DFS, BFS
• We learned some variations of DFS and BFS
– Bidirectional search (BDS)
– Iterative deepening search (IDS)
Other topics
• Euler circuit
• Articulation Point
• Bridge