Title: GRAPHS Graph Search Methods
1GRAPHSGraph Search Methods
2Graph Search Methods
- A vertex u is reachable from vertex viff there
is a path from v to u. - A search method starts at a given vertex v
andvisits/labels/marks every vertex that is
reachablefrom v.
3Graph Search Methods
- Many graph problems solved by a search method
- Finding path from one vertex to another.
- Determining whether a graph connected
- Find a spanning tree
- Finding a minimum-cost path/tree
- Commonly used search methods
- Breadth-first search
- Depth-first search
4Breadth-First Search Algorithm
- Visit the starting vertex and put into a FIFO
queue - Repeatedly remove a vertex from the queue,visit
its unvisited adjacent vertices,put newly
visited vertices into the queue - All vertices reachable from the start vertex
(including the start vertex) are visited - When the queue becomes empty, the search is
terminated
5Breadth-First Search Example
- Start search at vertex 1.
6Breadth-First Search Example
FIFO queue 1
- Mark/label start vertex and put in a FIFO queue.
- Remove 1 from QueueVisit adjacent unvisited
verticesPut in Queue.
7Breadth-First Search Example
FIFO queue 2 4
8Breadth-First Search Example
FIFO queue 2 4
- Remove 2 from QueueVisit adjacent unvisited
verticesPut in Queue.
9Breadth-First Search Example
FIFO queue 4 5 3 6
10Breadth-First Search Example
FIFO queue 4 5 3 6
- Remove 4 from QueueVisit adjacent unvisited
verticesPut in Queue.
11Breadth-First Search Example
FIFO queue 5 3 6
12Breadth-First Search Example
FIFO queue 5 3 6
- Remove 5 from QueueVisit adjacent unvisited
verticesPut in Queue.
13Breadth-First Search Example
FIFO queue 3 6 9 7
14Breadth-First Search Example
FIFO queue 3 6 9 7
- Remove 3 from QueueVisit adjacent unvisited
verticesPut in Queue.
15Breadth-First Search Example
FIFO queue 6 9 7
16Breadth-First Search Example
FIFO queue 6 9 7
- Remove 6 from QueueVisit adjacent unvisited
verticesPut in Queue.
17Breadth-First Search Example
FIFO queue 9 7
18Breadth-First Search Example
FIFO queue 9 7
- Remove 9 from QueueVisit adjacent unvisited
verticesPut in Queue.
19Breadth-First Search Example
FIFO queue 7 8
20Breadth-First Search Example
FIFO queue 7 8
- Remove 7 from QueueVisit adjacent unvisited
verticesPut in Queue.
21Breadth-First Search Example
FIFO queue 8
22Breadth-First Search Example
FIFO queue 8
- Remove 8 from QueueVisit adjacent unvisited
verticesPut in Queue.
23Breadth-First Search Example
FIFO queue
- Queue is empty. Search terminates.
24Time Complexity
- Each visited vertex is put on the queue exactly
once. - When a vertex is removed from the queue,we
examine its adjacent vertices. - O(n) if adjacency matrix used.
- O(vertex degree) if adjacency lists used.
- Total time
- O(n2) if adjacency matrix used
- O(ne) if adjacency lists usedwhere e is the sum
of vertex degrees andtherefore e is also the
number of edges.
25Path from Vertex u to Vertex v
- Start a breadth-first search at vertex u.
- Terminatewhen vertex v is visited orwhen queue
becomes empty(whichever occurs first) - Time
- O(n2) when adjacency matrix used
- O(ne) when adjacency lists used(e is number of
edges)
26Is a Graph Connected?
- Start a breadth-first search at any vertex of the
graph. - A graph is connected iff all n vertices are
visited. - Time
- O(n2) when adjacency matrix used
- O(ne) when adjacency lists used(e is number of
edges)
27Connected Components
- Start a breadth-first search at any unvisited
vertex of the graph. - Newly visited vertices (plus edges between
them)define a component. - Repeat until all vertices are visited.
- Time
- O(n2) when adjacency matrix used
- O(ne) when adjacency lists used(e is number of
edges)
28Connected Components
2
1
3
8
4
5
9
10
6
11
7
29Breath-First Spanning Tree
- Start a breadth-first search at any vertex of the
graph. - If a graph is connected,the n-1 edges used to
get to unvisited verticesdefine a spanning tree
? breadth-first spanning tree - Time
- O(n2) when adjacency matrix used
- O(ne) when adjacency lists used(e is number of
edges)
30Spanning Tree
31Depth-First Search Algorithm
- Visit the starting vertex v and mark it as
reached - Select an unreached vertex u adjacent from v
- If such a vertex does not exist, the search
terminate, otherwise a depth-first search from u
is initiated - When this search is completed, we select another
unreached vertex adjacent to from v - If such a vertex does not exist, then the search
terminates, otherwise a depth-first search from
this vertex - and so on..
32Depth-First Search
depthFirstSearch(v) Label vertex v as
reached. for (each unreached vertex u adjacent
from v) depthFirstSearch(u)
33Depth-First Search Example
- Start search at vertex 1.
- Label vertex 1 and do a depth first searchfrom
either 2 or 4.
34Depth-First Search Example
- Label vertex 2 and do a depth first searchfrom
either 3, 5 or 6.
35Depth-First Search Example
- Label vertex 5 and do a depth first searchfrom
either 3, 7 or 9.
36Depth-First Search Example
- Label vertex 9 and do a depth first searchfrom
either 6 or 8.
37Depth-First Search Example
- Label vertex 8 and return to vertex 9.
- From vertex 9 do a DFS(6).
38Depth-First Search Example
- Label vertex 6 and do a depth first searchfrom
either 4 or 7.
39Depth-First Search Example
- Label vertex 4 and return to 6.
- From vertex 6 do a DFS(7).
40Depth-First Search Example
- Label vertex 7 and return to 6.
- Return to 9.
41Depth-First Search Example
42Depth-First Search Example
43Depth-First Search Example
- Label 3 and return to 5.
- Return to 2.
44Depth-First Search Example
45Depth-First Search Example
- Return to invoking method.
46Depth-First Search Properties
- Sample complexity as BFS.
- Same properties with respect to path
finding,connected components, and spanning
trees. - Edges used to reach unlabeled verticesdefine a
depth-first spanning treewhen the graph is
connected. - There are problems for which BFS is better than
DFS and vice versa.
47READING