Upload
gareth
View
52
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Graph Searching. Overview. Graph Notation and Implementation BFS (Breadth First Search) DFS (Depth First Search) Topology SCC ( Strongly Connected Component ) Centre, Radius, Diameter Multi-state BFS. What is Graph?. 1. NT. Q. 4. vertex. WA. SA. NSW. 2. edge. V. 3. T. Graph. - PowerPoint PPT Presentation
Citation preview
Graph Graph SearchingSearching
OverviewOverview
GraphGraph– Notation and ImplementationNotation and Implementation
BFS (Breadth First Search)BFS (Breadth First Search) DFS (Depth First Search)DFS (Depth First Search) TopologyTopology SCC (Strongly Connected SCC (Strongly Connected
Component)Component) Centre, Radius, DiameterCentre, Radius, Diameter Multi-state BFSMulti-state BFS
What is What is Graph?Graph?
GraphGraph
A A graphgraph is defined as G=(V,E), is defined as G=(V,E),
i.e a set of vertices and edges, i.e a set of vertices and edges, wherewhere– V is the set of vertices (singular: V is the set of vertices (singular:
vertex)vertex)– E is the set of edges that connect E is the set of edges that connect
some of the verticessome of the vertices
1
2
4
3
vertex
edge
WA
NT
SA
Q
NSW
V
T
GraphGraph
Directed/Undirected GraphDirected/Undirected Graph Weighted/Unweighted GraphWeighted/Unweighted Graph ConnectivityConnectivity
Representation of Representation of GraphGraph Adjacency MatrixAdjacency Matrix Adjacency listAdjacency list Edge listEdge list
Representation of Representation of GraphGraph
AdjacencAdjacency Matrixy Matrix
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 vertices of a vertex a 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
GraphGraph
root
siblings
descendents children
ancestors
parent
TreeTree
Depth-First Search Depth-First Search (DFS)(DFS) Strategy: Go as far as you can (if Strategy: Go as far as you can (if
you have not visit there), you have not visit there), otherwise, go back and try otherwise, go back and try another wayanother way
ImplementationImplementation
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
Topological SortTopological Sort
Topological order: Topological order: A numbering of the vertices of a A numbering of the vertices of a directed acyclic graph such that directed acyclic graph such that every edge from a vertex numbered every edge from a vertex numbered i to a vertex numbered j satisfies i<ji to a vertex numbered j satisfies i<j
Topological Sort: Topological Sort: Finding the topological order of a Finding the topological order of a directed acyclic graphdirected acyclic graph
Tsort AlgorithmTsort Algorithm
If the graph has more then one vertex If the graph has more then one vertex that has indegree 0, add a vertice to that has indegree 0, add a vertice to connect to all indegree-0 verticesconnect to all 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 death time of the vertices represent
the reverse of topological orderthe reverse of topological order
Example: Assembly Example: Assembly LineLine1.1. In a factory, there is several process. In a factory, there is several process.
Some need to be done before others. Some need to be done before others. Can you order those processes so that Can you order those processes so that they can be done smoothly?they can be done smoothly?
2.2. Chris is now studying OI materials. Chris is now studying OI materials. There are many topics. He needs to There are many topics. He needs to master some basic topics before master some basic topics before understanding those advanced one. understanding those advanced one. Can you help him to plan a smooth Can you help him to plan a smooth study plan?study plan?
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 , one can
go from go from uu to to vv and from and from vv to to uu.. Informally speaking, an SCC of a Informally speaking, an SCC of a
graph is a subset of vertices thatgraph is 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)
Finding Shortest PathFinding Shortest Path
How can we use DFS to find the How can we use DFS to find the shortest distance from a vertex to shortest distance from a vertex to another? The distance of the first another? The distance of the first path found? path found?
Breadth-First Search Breadth-First Search (BFS)(BFS) BFS tries to find the target from BFS tries to find the target from
nearest vertices first.nearest vertices first. BFS makes use of a queue to BFS makes use of a queue to
store visited (but not dead) store visited (but not dead) vertices, expanding the path from vertices, expanding the path from the earliest visited vertices.the earliest visited vertices.
1
4
3
25
6
Simulation of BFSSimulation of BFS
Queue:Queue: 1 4 3 5 2 6
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 Initially all vertices except the start vertex are marked as start vertex are marked as unvisitedunvisited and the queue contains and the queue contains the start vertex onlythe start vertex only
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 functions – Avoiding stack overflowoverflow
Flood FillFlood Fill
An algorithm that determines the An algorithm that determines the area connected to a given node in area connected to a given node in a multi-dimensional arraya multi-dimensional array
Start BFS/DFS from the given Start BFS/DFS from the given node, counting the total number node, counting the total number of nodes visitedof nodes visited
Example: Squareland (HKOI 2006)Example: Squareland (HKOI 2006)
Variations of BFS and Variations of BFS and DFSDFS Bidirectional Search (BDS)Bidirectional Search (BDS) Iterative Deepening Search(IDS)Iterative Deepening Search(IDS)
Bidirectional search Bidirectional search (BDS)(BDS) Searches Searches simultaneouslysimultaneously from from
both the start vertex and goal both the start vertex and goal vertexvertex
Commonly implemented as Commonly implemented as bidirectional BFSbidirectional BFS
start goal
BDS Example: Bomber Man (1 BDS Example: Bomber Man (1 Bomb)Bomb)
find the shortest path from the upper-left find the shortest path from the upper-left corner to the lower-right corner in a maze using corner to the lower-right corner in a maze using a bomb. The bomb can destroy a wall.a bomb. The bomb can destroy a wall.
SS
EE
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?
SS 11 22 33 44 55 66 77 88 99
1100
2211
1111
2200
1122
1199
1133
1188
1144
1177
1177
1166
1155
1111
1122
1133
1144
1155
1166
1100
99 88 77 66 55 44 33 22 11 EE
ExampleExample
Iterative deepening Iterative deepening search (IDS)search (IDS)
Iteratively performs DFS with Iteratively performs DFS with increasing depth boundincreasing depth 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 The complexity of IDS is the same as DFSas DFS
Summary of DFS, BFSSummary of DFS, BFS
We learned some variations of We learned some variations of DFS and BFS DFS and BFS – Bidirectional search (BDS) Bidirectional search (BDS) – Iterative deepening search (IDS)Iterative deepening search (IDS)
EquationEquation
Question: Find the number of solution of Question: Find the number of solution of xxii, given k, given 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 , ,
kk44xx44pp4 4 , k, k44xx44
pp4 4 + k+ k55xx55pp5 5 , k, k44xx44
pp4 4 + k+ k55xx55pp5 5 + k+ k66xx66
pp6 6