advanced graph modelling and searching
DESCRIPTION
Advanced Graph Modelling and Searching. HKOI Training 2008. 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. - PowerPoint PPT PresentationTRANSCRIPT
Advanced Graph Advanced Graph Modelling and SearchingModelling and Searching
HKOI Training 2008HKOI Training 2008
GraphGraph
A graph is a set of vertices and a set of A graph is a set of vertices and a set of edgesedges
G = (V, E)G = (V, E) Number of vertices = |V|Number of vertices = |V| Number of edges = |E|Number of edges = |E| We assume We assume simplesimple graph, so |E| = O(|V| graph, so |E| = O(|V|22))
Trees in graph theoryTrees in graph theory
In graph theory, a tree is an acyclic, In graph theory, a tree is an acyclic, connected graphconnected graph AcyclicAcyclic means means ““without cycleswithout cycles””
Properties of treesProperties of trees
|E| = |V| - 1|E| = |V| - 1 |E| = |E| = (|V|)(|V|)
Between any pair of vertices, there is a unique Between any pair of vertices, there is a unique pathpath
Adding an edge between a pair of non-adjacent Adding an edge between a pair of non-adjacent vertices creates vertices creates exactly oneexactly one cycle cycle
Removing an edge from the tree breaks the tree Removing an edge from the tree breaks the tree into two smaller treesinto two smaller trees
Definition?Definition?
The following four conditions are The following four conditions are equivalent:equivalent: G is connected and acyclicG is connected and acyclic G is connected and |E| = |V| - 1 G is connected and |E| = |V| - 1 G is acyclic and |E| = |V| - 1G is acyclic and |E| = |V| - 1 Between any pair of vertices in G, there exists Between any pair of vertices in G, there exists
a unique patha unique path G is a tree if at least one of the above G is a tree if at least one of the above
conditions is satisfiedconditions is satisfied
Trees and related termsTrees and related terms
root
siblings
descendants children
ancestors
parent
Representation of GraphRepresentation of Graph
Adjacency MatrixAdjacency Matrix Adjacency listAdjacency list Edge listEdge list
Representation of GraphRepresentation 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 TraversalGraph Traversal
Given: a graphGiven: a graph Goal: visit all (or some) vertices and edges Goal: visit all (or some) vertices and edges
of the graph using some of the graph using some strategy strategy (the order (the order of visit is of visit is systematicsystematic))
DFS, BFS are examples of graph traversal DFS, BFS are examples of graph traversal algorithmsalgorithms
Some shortest path algorithms and Some shortest path algorithms and spanning tree algorithms have specific visit spanning tree algorithms have specific visit orderorder
Idea of DFS and BFSIdea of DFS and BFS
This is a brief idea of DFS and BFSThis is a brief idea of DFS and BFS
DFS: continue visiting next vertex whenever there DFS: continue visiting next vertex whenever there is a road, go back if no road (ie. visit to the depth of is a road, go back if no road (ie. visit to the depth of current path)current path)
Example: a human want to visit a place, but do not know Example: a human want to visit a place, but do not know the paththe path
BFS: go through all the adjacent vertices before BFS: go through all the adjacent vertices before going further (ie. spread among next vertices)going further (ie. spread among next vertices)
Example: set a house on fire, the fire will spread through Example: set a house on fire, the fire will spread through the housethe house
DFS (pseudo code)DFS (pseudo code)
DFS (vertex DFS (vertex uu) {) {mark mark uu as as visitedvisitedfor each vertex for each vertex vv directly reachable from directly reachable from uu
if if vv is is unvisitedunvisitedDFS (DFS (vv))
}}
Initially all vertices are marked as Initially all vertices are marked as unvisitedunvisited
F
A
BC
D
E
DFS (Demonstration)DFS (Demonstration)
unvisited
visited
““Advanced” DFSAdvanced” DFS
Apart from just visiting the vertices, Apart from just visiting the vertices, DFS can also provide us with valuable DFS can also provide us with valuable informationinformation
DFS can be enhanced by introducing:DFS can be enhanced by introducing: birthbirth time and time and deathdeath time of a vertex time of a vertex
• birth time: when the vertex is first visitedbirth time: when the vertex is first visited• death time: when we retreat from the vertexdeath time: when we retreat from the vertex
DFS treeDFS tree parentparent of a vertex of a vertex
DFS spanning tree / forestDFS spanning tree / forest
A A rootedrooted tree tree The root is the start vertexThe root is the start vertex If If vv is first visited from is first visited from uu, then , then uu is the parent of is the parent of vv in in
the DFS treethe DFS tree Edges are those in forward direction of DFS, ie. when Edges are those in forward direction of DFS, ie. when
visiting vertices that are not visited beforevisiting vertices that are not visited before
If some vertices are not reachable from the start If some vertices are not reachable from the start vertex, those vertices will form other spanning trees vertex, those vertices will form other spanning trees (1 or more)(1 or more)
The collection of the trees are called forestThe collection of the trees are called forest
DFS (pseudo code)DFS (pseudo code)
DFS (vertex DFS (vertex uu) {) {mark mark uu as as visitedvisited
time time time+1; birth[u]=time; time+1; birth[u]=time;
for each vertex for each vertex vv directly reachable from directly reachable from uuif if vv is is unvisitedunvisited
parent[v]=uparent[v]=u
DFS (DFS (vv)) time time time+1; death[u]=time; time+1; death[u]=time;
}}
A
F
B
C
D
E
GH
DFS forest (Demonstration)DFS forest (Demonstration)
unvisited
visited
visited (dead)
A B C D E F G H
birth
death
parent
A
B
C
F
E
D
G
1 2 3 13 10 4 14
12 9 8 16 11 5 15
H
6
7
- A B - A C D C
Classification of edgesClassification of edges
Tree edgeTree edge Forward edgeForward edge Back edgeBack edge Cross edgeCross edge
Question: which type of edges is always Question: which type of edges is always absent in an absent in an undirectedundirected graph? graph?
A
B
C
F
E
D
G
H
Determination of edge typesDetermination of edge types
How to determine the type of an arbitrary How to determine the type of an arbitrary edge (edge (uu, , vv) after DFS?) after DFS?
Tree edgeTree edge parentparent [ [vv] = ] = uu
Forward edgeForward edge not a tree edge; andnot a tree edge; and birthbirth [ [vv] > ] > birthbirth [ [uu]; and]; and deathdeath [ [vv] < ] < deathdeath [ [uu]]
How about back edge and cross edge?How about back edge and cross edge?
Determination of edge typesDetermination of edge types
Tree edge
Forward Edge Back Edge Cross Edge
parent [v] = u
not a tree edgebirth[v] > birth[u]death[v] < death[u]
birth[v] < birth[u]death[v] > death[u]
birth[v] < birth[u]death[v] < death[u]
Applications of DFS ForestsApplications of DFS Forests
Topological sorting (Tsort)Topological sorting (Tsort) Strongly-connected components (SCC)Strongly-connected components (SCC) Some more “advanced” algorithmsSome more “advanced” algorithms
Example: TsortExample: Tsort
Topological order: A numbering of the Topological order: A numbering of the vertices of a directed acyclic graph such vertices of a directed acyclic graph such that every edge from a vertex numbered i to that every edge from a vertex numbered i to a vertex numbered j satisfies i<ja vertex numbered j satisfies i<j
Tsort: Number the vertices in topological Tsort: Number the vertices in topological orderorder
1
2
3
4 5
6
7
Tsort AlgorithmTsort Algorithm
If the graph has more then one vertex that If the graph has more then one vertex that has indegree 0, add a vertice to connect to has indegree 0, add a vertice to connect to all indegree-0 verticesall indegree-0 vertices
Let the indegree 0 vertice be Let the indegree 0 vertice be ss Use Use ss as start vertice, and compute the as start vertice, and compute the
DFS forestDFS forest The death time of the vertices represent the The death time of the vertices represent the
reverse of topological orderreverse of topological order
Tsort (Demonstration)Tsort (Demonstration)
S
D
B
E F
C
G
A
S A B C D E F G
birth
death
G C F B A E D
1 2 3 4 5
67
8
91011
12 13
141516
D E A B F C G
Example: SCCExample: SCC
A graph is strongly-connected ifA graph is strongly-connected if for any pair of vertices for any pair of vertices uu and and vv, one can go , one can go
from from uu to to vv and from and from vv to to uu.. Informally speaking, an SCC of a graph Informally speaking, an SCC of a graph
is a subset of vertices thatis a subset of vertices that forms a strongly-connected subgraphforms a strongly-connected subgraph does not form a strongly-connected does not form a strongly-connected
subgraph with the addition of any new subgraph with the addition of any new vertexvertex
SCC (Illustration)SCC (Illustration)
SCC (Algorithm)SCC (Algorithm)
Compute the DFS forest of the graph G to Compute the DFS forest of the graph G to get the get the death timedeath time of the vertices of the vertices
ReverseReverse all edges in G to form G’ all edges in G to form G’ Compute a DFS forest of G’, but always Compute a DFS forest of G’, but always
choose the vertex with the choose the vertex with the latest death latest death timetime when choosing the root for a new tree when choosing the root for a new tree
The SCCs of G are the DFS trees in the The SCCs of G are the DFS trees in the DFS forest of G’DFS forest of G’
A
F
B
C
D
GH
SCC (Demonstration)SCC (Demonstration)
A
F
B
C
D
E
GH
A B C D E F G H
birth
death
parent
1 2 3 13 10 4 14
12 9 8 16 11 5 15
6
7
- A B - A C D C
D
G
A E B
F
C
H
SCC (Demonstration)SCC (Demonstration)
D
G
A E B
F
C
H
A
F
B
C
D
GH
E
DFS SummaryDFS Summary
DFS spanning tree / forestDFS spanning tree / forest
We can use birth time and death time in DFS We can use birth time and death time in DFS spanning tree to do varies things, such as Tsort, SCCspanning tree to do varies things, such as Tsort, SCC
Notice that in the previous slides, we Notice that in the previous slides, we relatedrelated birth birth time and death time. But in the discussed time and death time. But in the discussed applications, birth time and death time can be applications, birth time and death time can be independentindependent, ie. birth time and death time can use , ie. birth time and death time can use different time counterdifferent time counter
Breadth-first search (BFS)Breadth-first search (BFS)
In order to In order to “spread”, we need “spread”, we need to makes use of a to makes use of a data structure, data structure, queue queue ,to ,to remember just remember just visited verticesvisited vertices
Revised:Revised: DFS: continue visiting next DFS: continue visiting next
vertex whenever there is a road, vertex whenever there is a road, go back if no road (ie. visit to the go back if no road (ie. visit to the depth of current path)depth of current path)
BFS: go through all the adjacent BFS: go through all the adjacent vertices before going further (ie. vertices before going further (ie. spread among next vertices)spread among next vertices)
BFS (Pseudo code)BFS (Pseudo code)while queue not emptywhile queue not empty
dequeue the first vertex dequeue the first vertex uu from queue from queue
for each vertex for each vertex vv directly reachable from directly reachable from uu
if if vv is is unvisitedunvisited
enqueue enqueue vv to queue to queue
mark mark vv as as visitedvisited
Initially all vertices except the start vertex are Initially all vertices except the start vertex are marked as marked as unvisitedunvisited and the queue contains the and the queue contains the start vertex onlystart vertex only
A
B
C
D
E
F
G
H
I
J
BFS (Demonstration)BFS (Demonstration)
unvisited
visited
visited (dequeued)
Queue: A B C F D E H G J I
Applications of BFSApplications of BFS
Shortest paths findingShortest paths finding Flood-fill (can also be handled by DFS)Flood-fill (can also be handled by DFS)
Comparisons of DFS and BFSComparisons of DFS and BFS
DFSDFS BFSBFS
Depth-firstDepth-first Breadth-firstBreadth-first
StackStack QueueQueue
Does not guarantee Does not guarantee shortest pathsshortest paths
Guarantees shortest Guarantees shortest pathspaths
What is graph modeling?What is graph modeling?
Conversion of a problem into a graph Conversion of a problem into a graph problemproblem
Sometimes a problem can be easily Sometimes a problem can be easily solved once its underlying graph model is solved once its underlying graph model is recognizedrecognized
Graph modeling appears almost every Graph modeling appears almost every year in NOI or IOIyear in NOI or IOI
Basics of graph modelingBasics of graph modeling
A few steps:A few steps: identify the vertices and the edgesidentify the vertices and the edges identify the objective of the problemidentify the objective of the problem state the objective in graph termsstate the objective in graph terms implementation:implementation:
• construct the graph from the input instanceconstruct the graph from the input instance• run the suitable graph algorithms on the graphrun the suitable graph algorithms on the graph• convert the output to the required formatconvert the output to the required format
Simple examples (1)Simple examples (1)
Given a grid maze with obstacles, find Given a grid maze with obstacles, find a shortest path between two given a shortest path between two given pointspoints
start
goal
Simple examples (2)Simple examples (2)
A student has the phone numbers of some A student has the phone numbers of some other studentsother students
Suppose you know all pairs (A, B) such Suppose you know all pairs (A, B) such that A has B’s numberthat A has B’s number
Now you want to know Alan’s number, Now you want to know Alan’s number, what is the minimum number of calls you what is the minimum number of calls you need to make?need to make?
Simple examples (2)Simple examples (2)
Vertex: studentVertex: student Edge: whether A has B’s numberEdge: whether A has B’s number
Add an edge from A to B if A has B’s numberAdd an edge from A to B if A has B’s number Problem: find a shortest path from your Problem: find a shortest path from your
vertex to Alan’s vertexvertex to Alan’s vertex
Complex examples (1)Complex examples (1) Same settings as simple example 1Same settings as simple example 1 You know a trick – walking through an You know a trick – walking through an
obstacle! However, it can be used for obstacle! However, it can be used for only onceonly once
What should a vertex represent?What should a vertex represent? your position only?your position only? your position + whether you have used the your position + whether you have used the
tricktrick
Complex examples (1)Complex examples (1)
A vertex is in the form (A vertex is in the form (positionposition, , usedused)) The vertices are divided into two groupsThe vertices are divided into two groups
trick usedtrick used trick not usedtrick not used
Complex examples (1)Complex examples (1)
start
goal
unused
used
start goal
goal
Complex examples (1)Complex examples (1)
How about you can walk through How about you can walk through obstacles for k times?obstacles for k times?
Complex examples (1)Complex examples (1)
k
k-1
start goal
k-2
Complex examples (1)Complex examples (1)
k
k-1
start goal
k-4 k-2k-3
Complex examples (2)Complex examples (2)
The famous 8-puzzleThe famous 8-puzzle Given a state, find the moves that Given a state, find the moves that
bring it to the goal statebring it to the goal state
1 2 3
4 5 6
7 8
Complex examples (2)Complex examples (2) What does a vertex represent?What does a vertex represent?
the position of the empty square?the position of the empty square? the number of tiles that are in wrong the number of tiles that are in wrong
positions?positions? the state (the positions of the eight tiles)the state (the positions of the eight tiles)
What are the edges?What are the edges? What is the equivalent graph problem?What is the equivalent graph problem?
Complex examples (2)Complex examples (2)1 2 34 5 67 8
1 2 34 5 67 8
1 2 34 57 8 6
1 2 34 67 5 8
1 2 34 5 6
7 8
1 24 5 37 8 6
1 2 34 57 8 6
Complex examples (3)Complex examples (3)
Theseus and MinotaurTheseus and Minotaur http://www.logicmazes.com/theseus.html Extract:Extract:
• Theseus must escape from a maze. There is also a Theseus must escape from a maze. There is also a mechanical Minotaur in the maze. For every turn that mechanical Minotaur in the maze. For every turn that Theseus takes, the Minotaur takes two turns. The Theseus takes, the Minotaur takes two turns. The Minotaur follows this program for each of his two Minotaur follows this program for each of his two turns:turns:
• First he tests if he can move horizontally and get First he tests if he can move horizontally and get closer to Theseus. If he can, he will move one square closer to Theseus. If he can, he will move one square horizontally. If he can’t, he will test if he could move horizontally. If he can’t, he will test if he could move vertically and get closer to Theseus. If he can, he will vertically and get closer to Theseus. If he can, he will move one square vertically. If he can’t move either move one square vertically. If he can’t move either horizontally or vertically, then he just skips that turn.horizontally or vertically, then he just skips that turn.
Complex examples (3)Complex examples (3) What does a vertex represent?What does a vertex represent?
Theseus’ positionTheseus’ position Minotaur’s positionMinotaur’s position BothBoth
Some more examplesSome more examples
How can the followings be modeled?How can the followings be modeled? Tilt maze (Single-goal mazes only)Tilt maze (Single-goal mazes only)
• http://www.clickmazes.com/newtilt/ixtilt2d.htm Double title mazeDouble title maze
• http://www.clickmazes.com/newtilt/ixtilt.htm No-left-turn mazeNo-left-turn maze
• http://www.clickmazes.com/noleft/ixnoleft.htm Same as complex example 1, but you Same as complex example 1, but you
can use the trick for can use the trick for kk times times
Teacher’s ProblemTeacher’s Problem Question: A teacher wants to distribute sweets Question: A teacher wants to distribute sweets
to students in an order such that, if student to students in an order such that, if student uu tease student tease student vv, , uu should not get the sweet should not get the sweet before before vv
Vertex: studentVertex: student Edge: directed, (v,u) is a directed edge if student Edge: directed, (v,u) is a directed edge if student
v tease uv tease u Algorithm: TsortAlgorithm: Tsort
OI ManOI Man
Question: OIMan (Question: OIMan (OO) has 2 kinds of form: ) has 2 kinds of form: H- and S-form. He can transform to S-form H- and S-form. He can transform to S-form for for mm minutes by battery. He can only kill minutes by battery. He can only kill monsters (monsters (MM) and virus () and virus (VV) in S-form. Given ) in S-form. Given the number of battery and the number of battery and mm, find the , find the minimum time needed to kill the virus.minimum time needed to kill the virus.
Vertex: position, form, time left for S-form, Vertex: position, form, time left for S-form, number of batteries leftnumber of batteries left
Edge: directedEdge: directed Move to Move to PP in H-form (position) in H-form (position) Move to Move to P/M/VP/M/V in S-form (position, S-form time) in S-form (position, S-form time) Use battery and move (position, form, S-form Use battery and move (position, form, S-form
time, number of batteries)time, number of batteries)
PWWPPPPPPWWPOMMV
What you have learnt:What you have learnt:
Graph ModelingGraph Modeling
Variations of BFS and DFSVariations of BFS and DFS
Bidirectional Search (BDS)Bidirectional Search (BDS) Iterative Deepening Search(IDS)Iterative Deepening Search(IDS)
BDS(BI-DIRECTIONAL BDS(BI-DIRECTIONAL SEARCH)SEARCH)
BFS eats up memoryBFS eats up memoryLet it waste moreLet it waste moreStart BFS at both start and goalStart BFS at both start and goalSearching becomes fasterSearching becomes faster
start goal
BDS(BI-DIRECTIONAL BDS(BI-DIRECTIONAL SEARCH)SEARCH)
BDS Example: Bomber Man (1 Bomb)BDS Example: Bomber Man (1 Bomb)
find the shortest path from the upper-left corner find the shortest path from the upper-left corner to the lower-right corner in a maze using a to the lower-right corner in a maze using a bomb. The bomb can destroy a wall.bomb. The bomb can destroy a wall.
S
E
Bomber Man (1 Bomb)Bomber Man (1 Bomb)
SS
EE
1 2 3
4
1234
5
4
Shortest Path length = 8
5
6 67 78 8
9
9
10
10
11
11
12
12 12
13
13
What will happen if we stop once we find a path?
S 1 2 3 4 5 6 7 8 9
10
21 11
20 12
19 13
18 14
17 17 16 15
11 12 13 14 15 16
10
9 8 7 6 5 4 3 2 1 E
ExampleExample
Iterative deepening search Iterative deepening search (IDS)(IDS)
Iteratively performs DFS with increasing Iteratively performs DFS with increasing depth bounddepth bound
Shortest paths are guaranteedShortest paths are guaranteed
IDSIDS
IDS (pseudo code)IDS (pseudo code)
DFS (vertex DFS (vertex u, u, depth depth dd) {) {mark mark uu as as visitedvisitedif (d>0)if (d>0)
for each vertex for each vertex vv directly reachable from directly reachable from uuif if vv is is unvisitedunvisited
DFS (DFS (v,v,d-1d-1))}}
i=0i=0Do {Do {
DFS(start vertex,i)DFS(start vertex,i)Increment iIncrement i
}While (target is not found)}While (target is not found)
IDSIDS
The complexity of IDS is the same as The complexity of IDS is the same as DFSDFS
Summary of DFS, BFSSummary of DFS, BFS
We learned some variations of DFS and We learned some variations of DFS and BFS BFS Bidirectional search (BDS) Bidirectional search (BDS) Iterative deepening search (IDS)Iterative deepening search (IDS)
EquationEquation
Question: Find the number of solution of xQuestion: Find the number of solution of x ii, ,
given kgiven kii,p,pii. 1<=n<=6, 1<=x. 1<=n<=6, 1<=xii<=150<=150
Vertex: possible values of Vertex: possible values of kk11xx11
pp1 1 , k, k11xx11pp1 1 + k+ k22xx22
pp2 2 , k, k11xx11pp1 1 + k+ k22xx22
pp2 2 + k+ k33xx33pp3 3 , k, k44xx44
pp4 4 , ,
kk44xx44pp4 4 + k+ k55xx55
pp5 5 , k, k44xx44pp4 4 + k+ k55xx55
pp5 5 + k+ k66xx66pp6 6