cs.uni-paderborn.de€¦ · ss 2019 chapter 5 3 basic notation a directed graph g is a pair (v,e),...

159
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms Christian Scheideler SS 2019

Upload: others

Post on 18-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

  • Premaster Course Algorithms 1

    Chapter 5: Basic Graph Algorithms

    Christian ScheidelerSS 2019

  • Basic Graph Algorithms

    Overview:• Basic notation• Graph representations• Breadth-First-Search• Depth-First-Search• Minimum spanning trees

    SS 2019 Chapter 5 2

  • SS 2019 Chapter 5 3

    Basic Notation

    A directed graph G is a pair (V,E), where V is a finite setand E⊆V×V.

    Elements in V are called nodes or vertices, elements in Eare called edges. Correspondingly, V is the node set andE the edge set of G.

    Edges are ordered pairs of nodes. Edges of the form (u,u), u∈V, are allowed and called loops.

    If (u,v)∈E, we say that the edge leads from u to v. Wealso say that u and v are adjacent.

    The degree of a node v is the number of edges (v,w) in E.

  • SS 2019 Chapter 5 4

    Directed Graph

    1 2

    5

    3

    4 6

    { }

    ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ){ }3,6,4,5,5,4,1,4,5,2,4,2,2,2,2,1E

    6,5,4,3,2,1V

    =

    =

  • SS 2019 Chapter 5 5

    Basic Notation

    An undirected graph G is a pair (V,E), where V is a finite set and E is a set of subsets of V of size two.

    Elements in V are called nodes or vertices, elements in Eare called edges. Correspondingly, V is the node set andE the edge set of G.

    Edges have the form {u,v}. Edges of the form {u,u} areusually not allowed.

    If {u,v}∈E, then we say that u and v are adjacent.

    The degree of u is the number of edges {u,v} in E.

  • SS 2019 Chapter 5 6

    Undirected Graph

    1 2

    5

    3

    4 6

    V={1,2,3,4,5,6}

    E={ {1,2}, {1,5}, {2,5}, {3,6} }

  • SS 2019 Chapter 5 7

    • n: number of nodes, m: number of edges• δ(v,w): distance of w to v in G

    - directed graph: number of edges of a shortest directed pathfrom v to w

    - undirected draph: number of edges of a shortest path from vto w

    • D=maxv,w δ(v,w): diameter of G

    ADB C

    D=4E

    Graph Theory

  • SS 2019 Chapter 5 8

    G undirected: • G is connected: diameter D is finite (i.e., there is a path

    from every node to every other node in G)Example: graph that is not connected

    ADB C

    E

    Graph Theory

  • SS 2019 Chapter 5 9

    G directed: • G is weakly connected: diameter D is finite, if all edges

    are seen as undirected• G is strongly connected: D is finite

    ADB C

    E

    Graph Theory

  • SS 2019 Chapter 5 10

    Linear list:

    • degree 2• large diameter (n-1 for n nodes)→ bad for data structures

    Graph Theory

  • SS 2019 Chapter 5 11

    Complete binary tree:

    • n=2d+1-1 nodes, degree 3• Diameter is 2d ~ 2 log2 n→ good for data structures

    0

    d

    depth d

    Graph Theory

  • SS 2019 Chapter 5 12

    2-dimensional grid:

    • n = k2 nodes, maximum degree 4• diameter is 2(k-1) ~ 2 n

    1

    k

    side length k

    Graph Theory

  • SS 2019 Chapter 5 13

    1: Sequence of edges

    1 2

    4 3

    (1,2)/ (4,1)(3,4)(2,3)

    Graph Representations

    Dummy

  • SS 2019 Chapter 5 14

    2: Adjacency list

    1 2

    4 3 2

    3

    V

    3

    4

    4 1

    1 n

    Graph Representations

  • SS 2019 Chapter 5 15

    3: Adjacency matrix

    • A[i,j]∈{0,1}• A[i,j]=1 if and only if (i,j)∈E

    1 2

    4 3

    0 1 1 00 0 1 10 0 0 11 0 0 0

    A =

    Graphrepräsentationen

  • SS 2019 Chapter 5 16

    Central question: How can we traverse thenodes of the graph so that every node isvisited at least once?

    Graph Traversal

  • SS 2019 Chapter 5 17

    Central question: How can we traverse thenodes of the graph so that every node isvisited at least once?

    Basic strategies:• Breadth-first search• Depth-first search

    Graph Traversal

  • SS 2019 Chapter 5 18

    • Start at some node s• Explore graph distance by distance from s

    s

    Breadth-First Search

  • SS 2019 Chapter 5 19

    • Start from some node s• Explorate graph in depth-first manner

    ( : current, : still active, : done)

    s

    Depth-First Search

  • SS 2019 Chapter 5 20

    Breadth-First Search

    Breadth-first search algorithm forms the base of manyother graph algorithms.

    Given a graph G=(V,E) and a source s∈V, the goal ofbreadth-first search is to find all nodes v∈V that arereachable from s. A node v is reachable from s if there isa (directed) path in G from s to v.

    The breadth-first search algorithm can also be used tocompute the distance δ(s,v) of v from s.

  • SS 2019 Chapter 5 21

    Breadth-First Search

    If a new node v is discovered while searching for new nodesin Adj[u], then u is called the predecessor of v.(Adj[u]: set of neighbors or adjacency list of u, i.e., the nodesv∈V with {u,v}∈E resp. (u,v)∈E)

    In the algorithm: nodes are white, gray, or black.

    White nodes are nodes that have not been discovered yet.

    Gray nodes are nodes that have already been discovered but whose adjacency list has not completely been explored yet.

    Black nodes are nodes that have already been discoveredand fully processed.

  • SS 2019 Chapter 5 22

    Pseudo-code for Breadth-First Search

    BFS uses queue for gray nodes. [ ] =:ucolor stores color of v. Initially

    WHITE. [ ] =:ud field for distance to s. Initially ∞.

    [ ] =π :u field for predecessor. Initially NIL.

    BFS(G,S)1 for each node u∈V\{s} do2 color[u]←WHITE3 d[u] ← ∞4 p[u] ← NIL5 color[s] ← GRAY6 d[s] ← 07 π[s] ← NIL8 Q ← { }9 Enqueue(Q,s)10 while Q ≠ { } do11 u ← Dequeue(Q)12 for each v∈Adj[u] do13 if color[v]=WHITE then14 color[v] ← GRAY15 d[v] ← d[u]+116 π[v] ← u17 Enqueue(Q,v)18 color[u] ← BLACK

    · BFS uses queue for gray nodes.

    ·

    [

    ]

    =

    :

    u

    color

    stores color of v. Initially WHITE.

    ·

    [

    ]

    =

    :

    u

    d

    field for distance to s. Initially

    ¥

    .

    ·

    [

    ]

    =

    p

    :

    u

    field for predecessor. Initially NIL.

    _1180339330.unknown

    _1180339381.unknown

    _1180339402.unknown

    _1180339283.unknown

  • SS 2019 Chapter 5 23

    Pseudo-code for Breadth-First Search

    initialize BFS

    BFS(G,S)1 for each node u∈V\{s} do2 color[u]←WHITE3 d[u] ← ∞4 p[u] ← NIL5 color[s] ← GRAY6 d[s] ← 07 π[s] ← NIL8 Q ← { }9 Enqueue(Q,s)10 while Q ≠ { } do11 u ← Dequeue(Q)12 for each v∈Adj[u] do13 if color[v]=WHITE then14 color[v] ← GRAY15 d[v] ← d[u]+116 π[v] ← u17 Enqueue(Q,v)18 color[u] ← BLACK

    BFS uses queue for gray nodes. [ ] =:ucolor stores color of v. Initially

    WHITE. [ ] =:ud field for distance to s. Initially ∞.

    [ ] =π :u field for predecessor. Initially NIL.

    · BFS uses queue for gray nodes.

    ·

    [

    ]

    =

    :

    u

    color

    stores color of v. Initially WHITE.

    ·

    [

    ]

    =

    :

    u

    d

    field for distance to s. Initially

    ¥

    .

    ·

    [

    ]

    =

    p

    :

    u

    field for predecessor. Initially NIL.

    _1180339330.unknown

    _1180339381.unknown

    _1180339402.unknown

    _1180339283.unknown

  • 24

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: s

    0

    i

    h

    g

    f

    ed

    c

    b

    a

    SS 2019 Chapter 5 24

    Breadth-First Search Example

  • 25

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: s

    0

    i

    h

    g

    f

    ed

    c

    b

    a

    SS 2019 Chapter 5 25

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    26

    s

    Q:

    u0

    i

    h

    g

    f

    ed

    c

    b

    a

    SS 2019 Chapter 5 26

    Breadth-First Search Example

  • 27

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q:

    u0

    i

    h

    g

    f

    ed

    c

    b

    v=a

    SS 2019 Chapter 5 27

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    28

    s

    Q:

    u0

    i

    h

    g

    f

    ed

    c

    b

    v=a

    SS 2019 Chapter 5 28

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    29

    s

    Q:

    u0

    i

    h

    g

    f

    ed

    c

    b

    v=a

    SS 2019 Chapter 5 29

    Breadth-First Search Example

  • 30

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q:

    u0

    1

    i

    h

    g

    f

    ed

    c

    b

    v=a

    SS 2019 Chapter 5 30

    Breadth-First Search Example

  • 31

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: a

    u0

    1

    i

    h

    g

    f

    e

    c

    b

    v=a

    SS 2019 Chapter 5 31

    Breadth-First Search Example

  • 32

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: a

    u0

    1

    i

    h

    g

    f

    ed

    c

    v=b

    a

    SS 2019 Chapter 5 32

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    33

    s

    Q: a

    u0

    1

    i

    h

    g

    f

    ed

    c

    v=b

    a

    SS 2019 Chapter 5 33

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    34

    s

    Q: a

    u0

    1

    i

    h

    g

    f

    ed

    c

    v=b

    a

    SS 2019 Chapter 5 34

    Breadth-First Search Example

  • 35

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: a

    u0

    1

    1

    i

    h

    g

    f

    ed

    c

    v=b

    a

    SS 2019 Chapter 5 35

    Breadth-First Search Example

  • 36

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: a, b

    u0

    1

    1

    i

    h

    g

    f

    ed

    c

    v=b

    a

    SS 2019 Chapter 5 36

    Breadth-First Search Example

  • 37

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: a, b

    u0

    1

    1

    i

    h

    g

    f

    ed

    c

    b

    a

    SS 2019 Chapter 5 37

    Breadth-First Search Example

  • 38

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: a, b

    u0

    1

    1

    i

    h

    g

    f

    ed

    c

    b

    a

    SS 2019 Chapter 5 38

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    39

    s

    Q: b

    0

    1

    1

    i

    h

    g

    f

    ed

    c

    b

    u=a

    SS 2019 Chapter 5 39

    Breadth-First Search Example

  • 40

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b

    0

    1

    1

    i

    h

    g

    f

    ev=d

    c

    b

    u=a

    SS 2019 Chapter 5 40

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    41

    s

    Q: b

    0

    1

    1

    i

    h

    g

    f

    ev=d

    c

    b

    u=a

    SS 2019 Chapter 5 41

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    42

    s

    Q: b

    0

    1

    1

    i

    h

    g

    f

    ev=d

    c

    b

    u=a

    SS 2019 Chapter 5 42

    Breadth-First Search Example

  • 43

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b

    0

    1

    1

    i

    h

    g

    f

    ev=d

    c

    b

    u=a 2

    SS 2019 Chapter 5 43

    Breadth-First Search Example

  • 44

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b, d

    0

    1

    1

    i

    h

    g

    f

    ev=d

    c

    b

    u=a 2

    SS 2019 Chapter 5 44

    Breadth-First Search Example

  • 45

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b, d

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    SS 2019 Chapter 5 45

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    46

    s

    Q: b, d

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    SS 2019 Chapter 5 46

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    47

    s

    Q: b, d

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    SS 2019 Chapter 5 47

    Breadth-First Search Example

  • 48

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b, d

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    2

    SS 2019 Chapter 5 48

    Breadth-First Search Example

  • 49

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b, d, c

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    2

    SS 2019 Chapter 5 49

    Breadth-First Search Example

  • 50

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b, d, c

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    2

    SS 2019 Chapter 5 50

    Breadth-First Search Example

  • 51

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: b, d, c

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    b

    u=a 2

    2

    SS 2019 Chapter 5 51

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s 0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    u=b

    a 2

    2

    52

    Q: d, c

    SS 2019 Chapter 5 52

    Breadth-First Search Example

  • 53

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: d, c

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    u=b

    a 2

    2

    SS 2019 Chapter 5 53

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    54

    s

    Q: d, c

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    u=b

    a 2

    2

    SS 2019 Chapter 5 54

    Breadth-First Search Example

  • 55

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: d, c

    0

    1

    1

    i

    h

    g

    f

    ed

    v=c

    u=b

    a 2

    2

    SS 2019 Chapter 5 55

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    56

    s

    Q: c

    0

    1

    1

    i

    h

    g

    f

    eu=d

    v=c

    b

    a 2

    2

    SS 2019 Chapter 5 56

    Breadth-First Search Example

  • 57

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: c

    0

    1

    1

    i

    h

    g

    f

    v=eu=d

    c

    b

    a 2

    2

    SS 2019 Chapter 5 57

    Breadth-First Search Example

  • 58

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: c, e

    0

    1

    1

    i

    h

    g

    f

    v=eu=d

    c

    b

    a 2

    2

    3

    SS 2019 Chapter 5 58

    Breadth-First Search Example

  • 59

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: c, e

    0

    1

    1

    i

    h

    g

    v=f

    eu=d

    c

    b

    a 2

    2

    3

    SS 2019 Chapter 5 59

    Breadth-First Search Example

  • 60

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    s

    Q: c, e, f

    0

    1

    1

    i

    h

    g

    v=f

    eu=d

    c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 60

    Breadth-First Search Example

  • 61

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: c, e, f

    0

    1

    1

    i

    h

    g

    f

    eu=d

    c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 61

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    62

    v=s

    Q: c, e, f

    0

    1

    1

    i

    h

    g

    f

    eu=d

    c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 62

    Breadth-First Search Example

  • 63

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: c, e, f

    0

    1

    1

    i

    h

    g

    f

    eu=d

    c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 63

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    64

    v=s

    Q: e, f

    0

    1

    1

    i

    h

    g

    f

    ed

    u=c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 64

    Breadth-First Search Example

  • 65

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: e, f

    0

    1

    1

    i

    h

    g

    f

    ed

    u=c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 65

    Breadth-First Search Example

  • 66

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: e, f

    0

    1

    1

    i

    h

    g

    f

    ed

    u=c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 66

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    67

    v=s

    Q: f

    0

    1

    1

    i

    h

    g

    f

    u=ed

    c

    b

    a 2

    2

    3

    3

    SS 2019 Chapter 5 67

    Breadth-First Search Example

  • 68

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: f, g

    0

    1

    1

    i

    h

    g

    f

    u=ed

    c

    b

    a 2

    2

    3

    3

    4

    SS 2019 Chapter 5 68

    Breadth-First Search Example

  • 69

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: f, g

    0

    1

    1

    i

    h

    g

    f

    u=ed

    c

    b

    a 2

    2

    3

    3

    4

    SS 2019 Chapter 5 69

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    70

    v=s

    Q: g

    0

    1

    1

    i

    h

    g

    u=f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    SS 2019 Chapter 5 70

    Breadth-First Search Example

  • 71

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: g, i

    0

    1

    1

    i

    h

    g

    u=f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    SS 2019 Chapter 5 71

    Breadth-First Search Example

  • 72

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: g, i

    0

    1

    1

    i

    h

    g

    u=f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    SS 2019 Chapter 5 72

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    73

    v=s

    Q: i

    0

    1

    1

    i

    h

    u=g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    SS 2019 Chapter 5 73

    Breadth-First Search Example

  • 74

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: i, h

    0

    1

    1

    i

    h

    u=g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 74

    Breadth-First Search Example

  • 75

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: i, h

    0

    1

    1

    i

    h

    u=g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 75

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    76

    v=s

    Q: h

    0

    1

    1

    u=i

    h

    g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 76

    Breadth-First Search Example

  • 77

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: h

    0

    1

    1

    u=i

    h

    g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 77

    Breadth-First Search Example

  • 78

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q: h

    0

    1

    1

    u=i

    h

    g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 78

    Breadth-First Search Example

  • BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    79

    v=s

    Q:

    0

    1

    1

    i

    u=h

    g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 79

    Breadth-First Search Example

  • 80

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q:

    0

    1

    1

    i

    u=h

    g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 80

    Breadth-First Search Example

  • 81

    BFS(G,s)1. „initialize BFS“2. while Q≠∅ do3. u ← Dequeue(Q)4. for v∈Adj[u] do5. if color[v]=WHITE then6. color[v] ← GRAY7. d[v] ← d[u]+1; π[v] ← u8. Enqueue(Q,v)9. color[u] ← BLACK

    v=s

    Q:

    0

    1

    1

    i

    u=h

    g

    f

    ed

    c

    b

    a 2

    2

    3

    3

    4

    4

    5

    SS 2019 Chapter 5 81

    Breadth-First Search Example

  • SS 2019 Chapter 5 82

    Breadth-First Search Runtime

    Theorem 5.1: Given a graph G=(V,E) and a sources, algorithm BFS has a runtime of

    ( )EV +O

  • SS 2019 Chapter 5 83

    Depth-First Search

    BFS(G,S)1 for each node u∈V\{s} do2 color[u]←WHITE3 p[u] ← NIL4 color[s] ← GRAY5 π[s] ← NIL6 Q ← { }7 Enqueue(Q,s)8 while Q ≠ { } do9 u ← Dequeue(Q)10 for each v∈Adj[u] do11 if color[v]=WHITE then12 color[v] ← GRAY13 π[v] ← u14 Enqueue(Q,v)15 color[u] ← BLACK

    DFS(G,S)1 for each node u∈V\{s} do2 color[u]←WHITE3 p[u] ← NIL4 color[s] ← GRAY5 π[s] ← NIL6 Q ← { }7 Push(Q,s)8 while Q ≠ { } do9 u ← Pop(Q)10 for each v∈Adj[u] do11 if color[v]=WHITE then12 color[v] ← GRAY13 π[v] ← u14 Push(Q,v)15 color[u] ← BLACK

  • SS 2019 Chapter 5 84

    Instead of an iterative approach we will look at a recursiveapproach of realizing depth-first search.

    Recursive approach:• Initially: all nodes are white• Discovered nodes become gray• Fully processed nodes become blackTwo time stamps: d[v] and f[v] (lie between 1 and 2|V|)• d[v]: time at which v is discovered• f[v]: time at which v has been processed

    Depth-First Search

  • 85

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

    SS 2019 Chapter 5 85

    Depth-First Search

  • 86

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

    SS 2019 Chapter 5 86

    Depth-First Search

  • 87SS 2019 Chapter 5 87

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 88

    time=0

    SS 2019 Chapter 5 88

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 89

    u

    time=0

    SS 2019 Chapter 5 89

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 90

    u

    time=0

    SS 2019 Chapter 5 90

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 91

    u

    time=0

    SS 2019 Chapter 5 91

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 92

    u

    time=1

    1

    SS 2019 Chapter 5 92

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 93

    u

    time=1

    1

    v

    SS 2019 Chapter 5 93

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 94

    u

    time=1

    1

    v

    SS 2019 Chapter 5 94

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 95

    time=1

    1

    u

    SS 2019 Chapter 5 95

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 96

    time=2

    1

    u

    2

    SS 2019 Chapter 5 96

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 97

    time=2

    1

    u

    2

    v

    SS 2019 Chapter 5 97

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 98

    time=2

    1

    u

    2

    v

    SS 2019 Chapter 5 98

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 99

    time=2

    1

    2

    u

    SS 2019 Chapter 5 99

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 100

    time=3

    1

    2

    u

    3

    SS 2019 Chapter 5 100

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 101

    time=3

    1

    2

    u

    3

    SS 2019 Chapter 5 101

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 102

    time=3

    1

    2

    u

    3

    SS 2019 Chapter 5 102

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 103

    time=4

    1

    23

    4

    SS 2019 Chapter 5 103

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 104

    time=4

    1

    23

    4u

    v

    104SS 2019 Chapter 5 104

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 105

    time=4

    1

    23

    4u

    v

    105SS 2019 Chapter 5 105

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 106

    time=4

    1

    23

    4

    u

    106SS 2019 Chapter 5 106

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 107

    time=5

    1

    23

    4

    u

    5

    107SS 2019 Chapter 5 107

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 108

    time=5

    1

    23

    4

    u

    5

    v

    108SS 2019 Chapter 5 108

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 109

    time=5

    1

    23

    4

    u

    5

    v

    109SS 2019 Chapter 5 109

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 110

    time=5

    1

    23

    4

    u

    5

    v

    110SS 2019 Chapter 5 110

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 111

    time=6

    1

    23

    4

    5

    u

    6

    111SS 2019 Chapter 5 111

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 112

    time=6

    1

    23

    4

    5

    u

    6

    v

    112SS 2019 Chapter 5 112

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 113

    time=6

    1

    23

    4

    5

    u

    6

    v

    113SS 2019 Chapter 5 113

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 114

    time=6

    1

    23

    4

    5

    u

    6v

    114SS 2019 Chapter 5 114

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 115

    time=6

    1

    23

    4

    5

    u

    6v

    115SS 2019 Chapter 5 115

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 116

    time=6

    1

    23

    4

    5

    u

    6v

    116SS 2019 Chapter 5 116

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 117

    time=7

    1

    23

    4

    5

    7

    6v

    117SS 2019 Chapter 5 117

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 118

    time=7

    1

    23

    4

    5

    7

    6

    u

    118SS 2019 Chapter 5 118

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 119

    time=8

    1

    23

    4

    5

    7

    6

    8

    119SS 2019 Chapter 5 119

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 120

    time=8

    1

    23

    4

    5

    7

    6

    8

    u

    120SS 2019 Chapter 5 120

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 121

    time=9

    1

    23

    4

    5

    7

    6

    8

    9

    121SS 2019 Chapter 5 121

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 122

    time=9

    1

    23

    4

    5

    7

    6

    8

    9

    u

    v

    122SS 2019 Chapter 5 122

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 123

    time=9

    1

    23

    4

    5

    7

    6

    8

    9

    u

    v

    123SS 2019 Chapter 5 123

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 124

    time=9

    1

    23

    4

    5

    7

    6

    8

    9

    u

    124SS 2019 Chapter 5 124

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 125

    time=10

    1

    23

    4

    5

    7

    6

    8

    9

    u

    10

    125SS 2019 Chapter 5 125

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 126

    time=10

    1

    23

    4

    5

    7

    6

    8

    9

    u

    10

    v

    126SS 2019 Chapter 5 126

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 127

    time=10

    1

    23

    4

    5

    7

    6

    8

    9

    u

    10

    v

    127SS 2019 Chapter 5 127

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 128

    time=10

    1

    23

    4

    5

    7

    6

    8

    9

    u

    10v

    128SS 2019 Chapter 5 128

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 129

    time=10

    1

    23

    4

    5

    7

    6

    8

    9

    u

    10v

    129SS 2019 Chapter 5 129

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 130

    time=10

    1

    23

    4

    5

    7

    6

    8

    9

    u

    10v

    130SS 2019 Chapter 5 130

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 131

    time=11

    1

    23

    4

    5

    7

    6

    8

    9

    10v

    11

    131SS 2019 Chapter 5 131

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 132

    time=11

    1

    23

    4

    5

    7

    6

    8

    9

    10

    11

    132SS 2019 Chapter 5 132

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 133

    time=12

    1

    23

    4

    5

    7

    6

    8

    9

    10

    11

    12

    133SS 2019 Chapter 5 133

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • 134

    time=12

    1

    23

    4

    5

    7

    6

    8

    9

    10

    11

    12

    runtime:O(|V|+|E|)

    134SS 2019 Chapter 5 134

    Depth-First Search

    DFS(G)1. for each vertex u∈V do color[u] ← WHITE ; π[u] ← nil2. time ← 03. for each vertex u∈V do4. if color[u]=WHITE then DFS-Visit(u)

    DFS-Visit(u)1. color[u] ← GRAY2. time ← time +1; d[u] ← time4. for each v∈Adj[u] do5. if color[v] = WHITE then π[v] ← u ; DFS-Visit(v)6. color[u] ← BLACK7. time ← time+1; f[u] ← time

  • SS 2019 Chapter 5 135

    Theorem 5.2: Given a graph G=(V,E), algorithm DFS has a runtime of ( )EV +O .

    Depth-First Search Runtime

    Theorem 5.2: Given a graph G=(V,E), algorithm DFS has a runtime of

    (

    )

    E

    V

    +

    O

    .

    _1180524334.unknown

  • Classification of edges:• Tree edges are edges of the DFS-forest in G• Back edges are edges (u,v) that connect node u with

    one of its ancestors in the DFS-tree• Forward edges are non-tree edges (u,v) that connect

    node u with one of its descendents in the DFS-tree• Cross edges are the other edges

    1

    23

    5

    7

    6

    8

    9

    10

    11

    12a

    b

    c d

    e4

    SS 2019 Chapter 5 136

    Depth-First Search

  • Classification of edges:• Tree edges are edges of the DFS-forest in G• Back edges are edges (u,v) that connect node u with

    one of its ancestors in the DFS-tree• Forward edges are non-tree edges (u,v) that connect

    node u with one of its descendents in the DFS-tree• Cross edges are the other edges

    1

    23

    5

    7

    6

    8

    9

    10

    11

    12a

    b

    c d

    e4

    back edge

    SS 2019 Chapter 5 137

    Depth-First Search

  • Classification of edges:• Tree edges are edges of the DFS-forest in G• Back edges are edges (u,v) that connect node u with

    one of its ancestors in the DFS-tree• Forward edges are non-tree edges (u,v) that connect

    node u with one of its descendents in the DFS-tree• Cross edges are the other edges

    1

    23

    5

    7

    6

    8

    9

    10

    11

    12a

    b

    c d

    e4forward edge

    SS 2019 Chapter 5 138

    Depth-First Search

  • Classification of edges:• Tree edges are edges of the DFS-forest in G• Back edges are edges (u,v) that connect node u with

    one of its ancestors in the DFS-tree• Forward edges are non-tree edges (u,v) that connect

    node u with one of its descendents in the DFS-tree• Cross edges are the other edges

    1

    23

    5

    7

    6

    8

    9

    10

    11

    12a

    b

    c d

    e4

    cross edge

    SS 2019 Chapter 5 139

    Depth-First Search

  • SS 2019 Chapter 5 140

    Classification of edges (v,w):

    Edge type d[v]< d[w] f[v]>f[w]

    tree & forward yes yes

    back no no

    cross no yes

    Depth-First Search

  • SS 2019 Chapter 5 141

    Application:• Recognition of acyclic directed graphs

    (DAG)

    Property: no directed cycles

    Depth-First Search

  • SS 2019 Chapter 5 142

    Theorem 5.3: The following statements areequivalent:1. G is a DAG2. The DFS-tree does not contain a back edge3. ∀(v,w)∈E: f[v]>f[w]

    Topological sort: Assign numbers s(v) to the nodes vso that for all edges (v,w)∈E, s(v)

  • SS 2019 Chapter 5 143

    Minimum Spanning Trees

    Definition 5.4:

    1. A weighted undirected graph (G,w) is an undirected graph G=(V,E) with a weightfunction w:E→ℝ

    2. For any subgraph H=(U,F) (i.e., U⊆V, F⊆E) ofG, the weight w(H) of H is defined as

    w(H) = Σ w(e)e∈F

  • SS 2019 Chapter 5 144

    Definition 5.5:

    1. A subgraph H of an undirected graph G iscalled a spanning tree of G=(V,E) if H is a treeover all nodes of G.

    2. A spanning tree S of a weighted undirectedgraph G is called a minimum spanning tree(MST) of G if the weight of S is minimal among all spanning trees of G.

    Minimum Spanning Trees

  • SS 2019 Chapter 5 145

    dc

    ba

    1

    4

    3

    2

    2

    Graph G=(V,E)

    dc

    ba

    13

    2

    Spanning tree of G

    dc

    ba

    13 2

    Spanning tree of G

    3

    dc

    ba2

    2

    Spanning tree of G

    (minimal)

    (minimal) (not minimal)

    Minimum Spanning Trees

  • SS 2019 Chapter 5 146

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    Minimum Spanning Trees

  • SS 2019 Chapter 5 147

    Goal: Given a weighted undirected graph (G,w) withG=(V,E), find a minimum spanning tree of (G,w).

    Approach: We successively extend the edge setA⊆E to a minimum spanning tree.

    1. Initially, A={ }.2. In each step, we extend A to A∪{ {u,v} }, where

    {u,v} is an A-safe edge, until |A|=|V|-1.

    Definition 5.6: {u,v} is called A-safe if under theassumption that A can be extended to an MST, also A∪{ {u,v} } can be extended to an MST.

    Minimum Spanning Trees

  • SS 2019 Chapter 5 148

    Generic MST-Algorithm

    Generic-MST(G,w)1. A ← { }2. while A is not a spanning tree do3. find an A-safe edge {u,v}4. A ← A ∪ { {u,v} }5. return A

    Correctness: results from the definition of A-safe edges

  • SS 2019 Chapter 5 149

    Cuts in GraphsDefinition 5.7: 1. A cut (C,V\C) in a graph G=(V,E) is a partition of the node

    set V of the graph. 2. An edge of G is crossing a cut (V,V\C) if one node of it is in

    C and the other node in V\C.3. A cut (C,V\C) respects a subset A⊆E if no edge in A crosses

    the cut.4. An edge crossing the cut (C,V\C) is called light if it has a

    minimal weight among all edges crossing (C,V\C).

    CV\C

    A

  • SS 2019 Chapter 5 150

    4

    Cuts in Graphs

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411C

    V-C

    C

    V-C

    crossing edges

    light

  • SS 2019 Chapter 5 151

    Characterization of Safe Edges

    Theorem 5.8: Let (G,w) with G=(V,E) be a weightedundirected graph, and let A⊆E be contained in a minimum spanning tree of (G,w). Moreover, let(C,V\C) be a cut respecting A and {u,v} be a light edge crossing (C,V\C). Then {u,v} is an A-safe edge.

    Corollary 5.9: Let (G,w) with G=(V,E) be a weightedundirected graph, and let A⊆E be contained in a minimum spanning tree of (G,w). If {u,v} is an edge of mimimal weight that connects a connectedcomponent C of GA=(V,A) with the rest of thegraph GA, then {u,v} is an A-safe edge.

  • SS 2019 Chapter 5 152

    Prim‘s Algorithm

    • At any time of the algorithm, GA=(V,A) consists of a treeTA and a set of isolated nodes IA.

    • A edge of minimal weight that connects IA with TA isadded to A.

    TANode v with minimum w(u,v) over all edges crossing the cut

    u

  • SS 2019 Chapter 5 153

    • At any time of the algorithm, GA=(V,A) consists of a treeTA and a set of isolated nodes IA.

    • A edge of minimal weight that connects IA with TA isadded to A.

    • The nodes in IA are organized in a Min-Heap. The keyd[v] of a node v∈IA is given by the minimal weight of an edge that connects v with TA.

    Prim‘s Algorithm

  • SS 2019 Chapter 5 154

    Prim(G,w,s)1. for each vertex v∈V do d[v]←∞; π[v]←nil2. d[s]←0; Q←BuildHeap(V)3. while Q≠∅ do4. u←deleteMin(Q)5. for each vertex v∈Adj[u] do6. if v∈Q and d[v]>w(u,v) then7. DecreaseKey(Q,v,w(u,v)); π[v]←u

    Prim‘s Algorithm

    DecreaseKey(Q,v,d) operation:Reduces d[v] of v in Q to d and performs heapifyUpfrom the current position of v to repair heap property.

  • SS 2019 Chapter 5 155

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    Prim‘s Algorithm

  • SS 2019 Chapter 5 156

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    Prim‘s Algorithm

  • SS 2019 Chapter 5 157

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    Prim‘s Algorithm

  • SS 2019 Chapter 5 158

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    Prim‘s Algorithm

  • SS 2019 Chapter 5 159

    4

    d

    e

    g

    c

    f

    b

    h

    a i

    8 79

    10

    21

    8

    4 2

    76

    1411

    Correctness of Prim‘s algorithm for general instances(G,w): results from correctness of Generic-MST andCorollary 5.9.

    Prim‘s Algorithm

    Premaster Course Algorithms 1��Chapter 5: Basic Graph AlgorithmsBasic Graph AlgorithmsBasic NotationDirected GraphBasic NotationUndirected GraphFoliennummer 7Foliennummer 8Foliennummer 9Foliennummer 10Foliennummer 11Foliennummer 12Foliennummer 13Foliennummer 14Foliennummer 15Foliennummer 16Foliennummer 17Foliennummer 18Foliennummer 19Breadth-First SearchBreadth-First SearchPseudo-code for Breadth-First SearchPseudo-code for Breadth-First SearchFoliennummer 24Foliennummer 25Foliennummer 26Foliennummer 27Foliennummer 28Foliennummer 29Foliennummer 30Foliennummer 31Foliennummer 32Foliennummer 33Foliennummer 34Foliennummer 35Foliennummer 36Foliennummer 37Foliennummer 38Foliennummer 39Foliennummer 40Foliennummer 41Foliennummer 42Foliennummer 43Foliennummer 44Foliennummer 45Foliennummer 46Foliennummer 47Foliennummer 48Foliennummer 49Foliennummer 50Foliennummer 51Foliennummer 52Foliennummer 53Foliennummer 54Foliennummer 55Foliennummer 56Foliennummer 57Foliennummer 58Foliennummer 59Foliennummer 60Foliennummer 61Foliennummer 62Foliennummer 63Foliennummer 64Foliennummer 65Foliennummer 66Foliennummer 67Foliennummer 68Foliennummer 69Foliennummer 70Foliennummer 71Foliennummer 72Foliennummer 73Foliennummer 74Foliennummer 75Foliennummer 76Foliennummer 77Foliennummer 78Foliennummer 79Foliennummer 80Foliennummer 81Breadth-First Search RuntimeDepth-First SearchFoliennummer 84Foliennummer 85Foliennummer 86Foliennummer 87Foliennummer 88Foliennummer 89Foliennummer 90Foliennummer 91Foliennummer 92Foliennummer 93Foliennummer 94Foliennummer 95Foliennummer 96Foliennummer 97Foliennummer 98Foliennummer 99Foliennummer 100Foliennummer 101Foliennummer 102Foliennummer 103Foliennummer 104Foliennummer 105Foliennummer 106Foliennummer 107Foliennummer 108Foliennummer 109Foliennummer 110Foliennummer 111Foliennummer 112Foliennummer 113Foliennummer 114Foliennummer 115Foliennummer 116Foliennummer 117Foliennummer 118Foliennummer 119Foliennummer 120Foliennummer 121Foliennummer 122Foliennummer 123Foliennummer 124Foliennummer 125Foliennummer 126Foliennummer 127Foliennummer 128Foliennummer 129Foliennummer 130Foliennummer 131Foliennummer 132Foliennummer 133Foliennummer 134Foliennummer 135Foliennummer 136Foliennummer 137Foliennummer 138Foliennummer 139Foliennummer 140Foliennummer 141Foliennummer 142Minimum Spanning TreesMinimum Spanning TreesMinimum Spanning TreesMinimum Spanning TreesMinimum Spanning TreesGeneric MST-AlgorithmCuts in GraphsCuts in GraphsCharacterization of Safe EdgesPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm