Graph Search + DAGs Breadth First Search Breadth First Search (BFS) - - PowerPoint PPT Presentation

graph search dags breadth first search breadth first
SMART_READER_LITE
LIVE PREVIEW

Graph Search + DAGs Breadth First Search Breadth First Search (BFS) - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Graph Search + DAGs

slide-2
SLIDE 2

Breadth First Search

slide-3
SLIDE 3

Breadth First Search (BFS)

Idea

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

. . .

Layer 0 Layer 1 Layer 2

s

3 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 ◮ Mace generation ◮ Base for other algorithm

9 / 29

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. 1 Procedure DFS(v) 2

Set vis(v) = True.

3

For Each u ∈ N(v) with vis(u) = False

4

Set par(u) := v.

5

Call DFS(u). (For large graphs, do not use a recursive implementation.)

10 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

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 / 29

slide-28
SLIDE 28

DFS – Runtime

Runtime

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

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

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

12 / 29

slide-29
SLIDE 29

DFS – Edges

A DFS partitions the edges in four groups.

  • 1. Tree edges.

Determined by parent pointers par(·).

13 / 29

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 / 29

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 / 29

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 / 29

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 / 29

slide-34
SLIDE 34

DFS – Detecting Cycles

Theorem A graph G has a cycle if and only if any DFS has a back edge.

15 / 29

slide-35
SLIDE 35

DFS – Detecting Cycles

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 descendent of v1 in the DFS tree, i. e., vkv1 is a back

edge.

  • 16 / 29
slide-36
SLIDE 36

DFS – Detecting Cycles

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.

  • 17 / 29
slide-37
SLIDE 37

Directed Acyclic Graphs

slide-38
SLIDE 38

Directed Acyclic Graphs

Directed Acyclic Graphs (DAG) A directed acyclic graph (or DAG for short) is a directed graph that contains no cycles.

19 / 29

slide-39
SLIDE 39

Directed Acyclic Graphs

Cycles + DFS

◮ A graph contains a cycle if and only if a DFS produces a back edge. ◮ Thus, if a graph is acyclic, a DFS on this graph produces no back edges.

Lemma Determining if a given directed graph is a DAG can be done in linear time. Partial Orders

◮ Relation which is transitive, reflexive, and antisymmetric. ◮ For each partial order there is a corresponding DAG and vice versa.

20 / 29

slide-40
SLIDE 40

Sources and Sinks

Source and Sink In a DAG, a source is a vertex without incoming edges; a sink is a vertex without outgoing edges. Source Sink

21 / 29

slide-41
SLIDE 41

Sources and Sinks

Lemma Each DAG contains at least one source and one sink. Proof

◮ Pick arbitrary vertex v. ◮ If there is a vertex u with (v, u) ∈ E, go to u. ◮ Repeat this for u. ◮ Since V is finite and there are no cycles, u will be a sink eventually. ◮ By symmetry, the same for sources.

  • 22 / 29
slide-42
SLIDE 42

Topological Order

Topological Order For a directed graph, a vertex order v1, v2, . . . , vn (vi = vj ↔ i = j) is a topological order if (vi, vj ) ∈ E implies i < j. . . .

23 / 29

slide-43
SLIDE 43

Topological Order

Lemma A graph is a DAG if and only if admits a topological order. Proof (⇐)

◮ If a graph admits a topological order, it cannot contain cycles.

  • Proof (⇒)

◮ After removing or adding a source or a sink from or to a DAG, the

resulting graph is still a DAG.

◮ The first vertex of a topological order is a source, the last is a sink. ◮ Thus, by induction, each DAG admits a topological order.

  • Finding a Topological Order

◮ A post-order on a DFS-tree gives a topological order.

24 / 29

slide-44
SLIDE 44

Exercises

slide-45
SLIDE 45

Exercises

In an undirected graph G = (V, E), the eccentricity ecc(v) of a vertex v, the diameter diam(G), and the radius rad(G) of G are defined as follows:

◮ ecc(v) = max u∈V d(u, v) ◮ diam(G) = max v∈V ecc(v) ◮ rad(G) = min v∈V ecc(v)

Give an algorithm that determines the diameter and radius of a given

  • graph. State the runtime of your algorithm.

26 / 29

slide-46
SLIDE 46

Exercises

Let G = (V, E) be an undirected graph. For a vertex v ∈ V and a set S ⊆ V, the projection Pr(v, S) is the set of vertices in S with minimal distance to v. Formally, Pr(v, S) = { u ∈ S | d(u, v) = d(v, S) }. Give an algorithm that determines the projection Pr(v, S) in linear time for a graph G, vertex v and vertex set S.

27 / 29

slide-47
SLIDE 47

Exercises

In a DAG G, an edge (u, v) is transitive if, after removing it, there is still a path from u to v. Give an algorithm that determines all transitive edges in a given DAG. Give an O(|V| + |E|) time algorithm to remove all the cycles in a directed graph G = (V, E). Removing a cycle means removing an edge of the cycle. If there are k cycles in G, the algorithm should only remove at most O(k) edges. Give an algorithm that determines if a given undirected graph is a tree in

O(|V|) time, i. e., the runtime should not depend on the number of edges.

28 / 29

slide-48
SLIDE 48

Exercises

You are given an undirected graph G and an integer k. Give a linear time algorithm that finds the maximum induced subgraph H of G such that each vertex in H has degree at least k, or proves that no such graph exists. Consider a graph G = (V, E) where V = {1, . . . , n}. We say that an adjacency list representation of G is monotone if, for every vertex v, the vertices adjacent to i are listed in increasing order. Given an adjacency list representation of G, produce a monotone adjacency list representation

  • f G in linear time.

29 / 29