Graph Traversal Breadth First Search Breadth First Search (BFS) - - PowerPoint PPT Presentation

graph traversal breadth first search breadth first search
SMART_READER_LITE
LIVE PREVIEW

Graph Traversal Breadth First Search Breadth First Search (BFS) - - PowerPoint PPT Presentation

Graph Traversal Breadth First Search Breadth First Search (BFS) Idea Explore graph layer by layer from a start vertex s . s . . . Layer 0 Layer 1 Layer 2 3 / 16 BFS Applications Finding a shortest path. Determine distances.


slide-1
SLIDE 1

Graph Traversal

slide-2
SLIDE 2

Breadth First Search

slide-3
SLIDE 3

Breadth First Search (BFS)

Idea

◮ Explore graph layer by layer from a start vertex s.

. . .

Layer 0 Layer 1 Layer 2

s

3 / 16

slide-4
SLIDE 4

BFS

Applications

◮ Finding a shortest path. ◮ Determine distances. ◮ Garbage collection (checking for connected components) ◮ Solving Puzzles ◮ Web crawling ◮ Base for other algorithms.

4 / 16

slide-5
SLIDE 5

BFS – Algorithm

Preparation

◮ For every vertex v, set the distance dist(v) := ∞ and the

parent par(v) := null. For the start vertex a, set dist(a) := 0.

◮ Add a to an empty queue Q.

a b c d e f g h i

Q a ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

5 / 16

slide-6
SLIDE 6

BFS – Algorithm

Iteration

◮ Select and remove first vertex v from Q.

a b c d e f g h i

Q ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

5 / 16

slide-7
SLIDE 7

BFS – Algorithm

Iteration

◮ For each u ∈ N(v) with dist(u) = ∞,

◮ set dist(u) := dist(v) + 1,

a b c d e f g h i

Q ∞ 1 ∞ 1 ∞ ∞ ∞ ∞

5 / 16

slide-8
SLIDE 8

BFS – Algorithm

Iteration

◮ For each u ∈ N(v) with dist(u) = ∞,

◮ set dist(u) := dist(v) + 1, ◮ set par(u) := v, and

a b c d e f g h i

Q ∞ 1 ∞ 1 ∞ ∞ ∞ ∞

5 / 16

slide-9
SLIDE 9

BFS – Algorithm

Iteration

◮ For each u ∈ N(v) with dist(u) = ∞,

◮ set dist(u) := dist(v) + 1, ◮ set par(u) := v, and ◮ add u to Q.

a b c d e f g h i

Q c e ∞ 1 ∞ 1 ∞ ∞ ∞ ∞

5 / 16

slide-10
SLIDE 10

BFS – Algorithm

Iteration

◮ Repeat until Q is empty.

a b c d e f g h i

Q e b g 2 1 ∞ 1 ∞ 2 ∞ ∞

5 / 16

slide-11
SLIDE 11

BFS – Algorithm

Iteration

◮ Repeat until Q is empty.

a b c d e f g h i

Q b g h 2 1 ∞ 1 ∞ 2 2 ∞

5 / 16

slide-12
SLIDE 12

BFS – Algorithm

Iteration

◮ Repeat until Q is empty.

a b c d e f g h i

Q g h d f 2 1 3 1 3 2 2 ∞

5 / 16

slide-13
SLIDE 13

BFS – Algorithm

Iteration

◮ Repeat until Q is empty.

a b c d e f g h i

Q h d f 2 1 3 1 3 2 2 3

5 / 16

slide-14
SLIDE 14

BFS

Input: A graph G = (V, E) and a start vertex s ∈ V.

1 For Each v ∈ V 2

Set dist(v) := ∞ and par(v) := null.

3 Create a new empty queue Q. 4 Set dist(s) := 0 and add s to Q. 5 While Q is not empty 6

v := Q.deque()

7

For Each u ∈ N(v) with dist(u) = ∞

8

Set dist(u) := dist(v) + 1 and set par(u) = v.

9

Add u to Q.

6 / 16

slide-15
SLIDE 15

BFS – Runtime

Preparation

◮ Each vertex is accessed once. No edge is accessed. ◮ O(|V|) time

Iteration

◮ Each vertex is added to the queue at most once. ◮ For each vertex removed from the queue, each neighbour is accessed

  • nce.

◮ Thus, runtime is

  • v∈V

|N(v)| = 2|E| Total runtime: O(|V| + |E|)

7 / 16

slide-16
SLIDE 16

Depth First Search

slide-17
SLIDE 17

Depth First Search

Idea

◮ Follow path until you get stuck. ◮ If got stuck, backtrack path until reach unexplored neighbour.

Continue on unexplored neighbour. Applications

◮ Tree-traversal ◮ Cycle detection ◮ Finding blocks and strongly connected components ◮ Mace generation ◮ Base for other algorithm, e. g. testing for planarity

9 / 16

slide-18
SLIDE 18

DFS – Algorithm (using recurrence)

Preparation

◮ For every vertex v, set it as unvisited (vis(v) := False) and set the

parent par(v) := null.

◮ Call DFS(s) for the start vertex s.

DFS(v)

◮ Set vis(v) = True. ◮ For each u ∈ N(v) with vis(u) = False

◮ Set par(u) := v. ◮ Call DFS(u). 10 / 16

slide-19
SLIDE 19

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack:

11 / 16

slide-20
SLIDE 20

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a

11 / 16

slide-21
SLIDE 21

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c

11 / 16

slide-22
SLIDE 22

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c b

11 / 16

slide-23
SLIDE 23

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c b

11 / 16

slide-24
SLIDE 24

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c b f

11 / 16

slide-25
SLIDE 25

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c b f g

11 / 16

slide-26
SLIDE 26

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c b f g

11 / 16

slide-27
SLIDE 27

DFS – Algorithm Example

Run a DFS with start vertex s.

a b c d e f g h i

Stack: a c b f

11 / 16

slide-28
SLIDE 28

DFS – Runtime

Runtime

◮ A single DFS(v) call (without recurrence) accesses v and all its

  • neighbours. Thus, runtime (for a single vertex v is O(|N[v]|).

◮ For each vertex v, DFS(v) is called only once. ◮ Total runtime: O(|V| + |E|)

12 / 16

slide-29
SLIDE 29

DFS – Edges

A DFS partitions the edges in four groups.

  • 1. Tree edges.

Determined by parent pointers par(·).

13 / 16

slide-30
SLIDE 30

DFS – Edges

A DFS partitions the edges in four groups.

  • 1. Tree edges.

Determined by parent pointers par(·).

  • 2. Forward edges.

From an ancestor to a descented

13 / 16

slide-31
SLIDE 31

DFS – Edges

A DFS partitions the edges in four groups.

  • 1. Tree edges.

Determined by parent pointers par(·).

  • 2. Forward edges.

From an ancestor to a descented

  • 3. Back edges.

From a descented to an ancestor

13 / 16

slide-32
SLIDE 32

DFS – Edges

A DFS partitions the edges in four groups.

  • 1. Tree edges.

Determined by parent pointers par(·).

  • 2. Forward edges.

From an ancestor to a descented

  • 3. Back edges.

From a descented to an ancestor

  • 4. Cross edges. (only in directed graphs)

Remaining edges

13 / 16

slide-33
SLIDE 33

DFS – Recognising Edges

On the DFS-tree

◮ Make a preorder and a postorder traversal. ◮ For each vertex, store a vector (i, j) where i is the index of v in the

preorder and j is the index of v in the postorder. For an edge uv

◮ Let (i, j) be the indices for u and (x, y) be the indices for v. ◮ Forward edge: i < x ◮ Backward edge: i > x and j < y ◮ Cross edge: i > x and j > y

14 / 16

slide-34
SLIDE 34

DFS – Detecting Cycles

Theorem A graph G has a cycle if and only if any DFS has a back edge. Proof (⇒)

◮ Assume G has a cycle {v1, v2, . . . , vk}. W. l. o. g., let v1 be the first

visited by the DFS.

v1 v2 v3 v4 vk

◮ Then, vk is an ancestor of v1 in the DFS tree, i. e., vkv1 is a back

edge.

  • 15 / 16
slide-35
SLIDE 35

DFS – Detecting Cycles

Theorem A graph G has a cycle if and only if any DFS has a back edge. Proof (⇐)

◮ Assume a DFS produces a back edge uv.

v u

◮ Thus, v is an ancestor of u, i. e., the path from v to u using tree

edges plus the edge uv form a cycle.

  • 16 / 16