GRAPHS Graph Search Methods - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

GRAPHS Graph Search Methods

Description:

Select an unreached vertex u adjacent from v ... When this search is completed, we select another unreached vertex adjacent to from v ... – PowerPoint PPT presentation

Number of Views:116
Avg rating:3.0/5.0
Slides: 48
Provided by: poste74
Category:

less

Transcript and Presenter's Notes

Title: GRAPHS Graph Search Methods


1
GRAPHSGraph Search Methods
  • Fall 2003
  • CSE, POSTECH

2
Graph 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.

3
Graph 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

4
Breadth-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

5
Breadth-First Search Example
  • Start search at vertex 1.

6
Breadth-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.

7
Breadth-First Search Example
FIFO queue 2 4
8
Breadth-First Search Example
FIFO queue 2 4
  • Remove 2 from QueueVisit adjacent unvisited
    verticesPut in Queue.

9
Breadth-First Search Example
FIFO queue 4 5 3 6
10
Breadth-First Search Example
FIFO queue 4 5 3 6
  • Remove 4 from QueueVisit adjacent unvisited
    verticesPut in Queue.

11
Breadth-First Search Example
FIFO queue 5 3 6
12
Breadth-First Search Example
FIFO queue 5 3 6
  • Remove 5 from QueueVisit adjacent unvisited
    verticesPut in Queue.

13
Breadth-First Search Example
FIFO queue 3 6 9 7
14
Breadth-First Search Example
FIFO queue 3 6 9 7
  • Remove 3 from QueueVisit adjacent unvisited
    verticesPut in Queue.

15
Breadth-First Search Example
FIFO queue 6 9 7
16
Breadth-First Search Example
FIFO queue 6 9 7
  • Remove 6 from QueueVisit adjacent unvisited
    verticesPut in Queue.

17
Breadth-First Search Example
FIFO queue 9 7
18
Breadth-First Search Example
FIFO queue 9 7
  • Remove 9 from QueueVisit adjacent unvisited
    verticesPut in Queue.

19
Breadth-First Search Example
FIFO queue 7 8
20
Breadth-First Search Example
FIFO queue 7 8
  • Remove 7 from QueueVisit adjacent unvisited
    verticesPut in Queue.

21
Breadth-First Search Example
FIFO queue 8
22
Breadth-First Search Example
FIFO queue 8
  • Remove 8 from QueueVisit adjacent unvisited
    verticesPut in Queue.

23
Breadth-First Search Example
FIFO queue
  • Queue is empty. Search terminates.

24
Time 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.

25
Path 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)

26
Is 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)

27
Connected 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)

28
Connected Components
2
1
3
8
4
5
9
10
6
11
7
29
Breath-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)

30
Spanning Tree
31
Depth-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..

32
Depth-First Search
depthFirstSearch(v) Label vertex v as
reached. for (each unreached vertex u adjacent
from v) depthFirstSearch(u)
33
Depth-First Search Example
  • Start search at vertex 1.
  • Label vertex 1 and do a depth first searchfrom
    either 2 or 4.

34
Depth-First Search Example
  • Label vertex 2 and do a depth first searchfrom
    either 3, 5 or 6.

35
Depth-First Search Example
  • Label vertex 5 and do a depth first searchfrom
    either 3, 7 or 9.

36
Depth-First Search Example
  • Label vertex 9 and do a depth first searchfrom
    either 6 or 8.

37
Depth-First Search Example
  • Label vertex 8 and return to vertex 9.
  • From vertex 9 do a DFS(6).

38
Depth-First Search Example
  • Label vertex 6 and do a depth first searchfrom
    either 4 or 7.

39
Depth-First Search Example
  • Label vertex 4 and return to 6.
  • From vertex 6 do a DFS(7).

40
Depth-First Search Example
  • Label vertex 7 and return to 6.
  • Return to 9.

41
Depth-First Search Example
  • Return to 5.

42
Depth-First Search Example
  • Do a DFS(3).

43
Depth-First Search Example
  • Label 3 and return to 5.
  • Return to 2.

44
Depth-First Search Example
  • Return to 1.

45
Depth-First Search Example
  • Return to invoking method.

46
Depth-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.

47
READING
  • Read Chapter 12
Write a Comment
User Comments (0)
About PowerShow.com