how to win coding competitions: secrets of champions week 4: … · 2016. 11. 19. · 2 2 4 5 1 6 2...

145
How to Win Coding Competitions: Secrets of Champions Week 4: Algorithms on Graphs Lecture 9: Single Source Shortest Paths Maxim Buzdalov Saint Petersburg 2016

Upload: others

Post on 01-Feb-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

  • How to Win Coding Competitions: Secrets of Champions

    Week 4: Algorithms on GraphsLecture 9: Single Source Shortest Paths

    Maxim BuzdalovSaint Petersburg 2016

  • Single Source Shortest Path problem

    Problem: for every vertex determine a shortest path from v0

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    4

    73

    42

    51 1

    0

    3

    22

    1

    1

    2

    1

    6

    2

    16

    9

    2 / 8

  • Single Source Shortest Path problem

    Problem: for every vertex determine a shortest path from v0

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    4

    73

    42

    51 1

    0

    3

    22

    1

    1

    2

    1

    6

    2

    16

    9

    2 / 8

  • Single Source Shortest Path problem

    Problem: for every vertex determine a shortest path from v0

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    4

    73

    42

    51 1

    0

    3

    22

    1

    1

    2

    1

    6

    2

    16

    9

    2 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example.

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)

    Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example.

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example.

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example.

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    ∞ ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    9

    ∞ ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    9

    ∞ ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    9

    ∞ ∞

    5 7

    13

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    9

    ∞ ∞

    5 7

    13

    11

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    9

    ∞ ∞

    5 7

    13

    11

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 1

    0

    4

    3

    9

    ∞ ∞

    5 7

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    9

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    9

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    9

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    9

    13

    11

    20

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    ∞ ∞

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    5 ∞

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    5 ∞

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    9

    5 6

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    11

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    10

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    45

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    10

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    45

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    10

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    45

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    10

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    45

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 2

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm

    Bellman-Ford algorithm:I Do |V | − 1 times:

    I Visit all edgesI For (u, v), update

    shortest distance to vI If nothing updated,

    terminate

    I Running time: O(|V | · |E |)Why does it work?

    I First k iterations buildfirst k segments in everyshortest path

    Example. Iteration 3. Nothing changed. We may stop

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    3 / 8

  • Bellman-Ford algorithm and negative cycles

    A negative cycle is a cycle of total negative length

    I If any vertex of the negative cycle is reachable, then there is no shortest distanceto any vertex of this cycle, and any vertex reachable from it

    The Bellman-Ford algorithm can detect negative cycles. How?

    I Update shortest distances along all edges once more

    I If a shortest distance to v changes, then v is reachable from a negative cycle

    4 / 8

  • Bellman-Ford algorithm and negative cycles

    A negative cycle is a cycle of total negative length

    I If any vertex of the negative cycle is reachable, then there is no shortest distanceto any vertex of this cycle, and any vertex reachable from it

    The Bellman-Ford algorithm can detect negative cycles. How?

    I Update shortest distances along all edges once more

    I If a shortest distance to v changes, then v is reachable from a negative cycle

    4 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]

    I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]I It should go through other v ′ /∈ SI But edge lengths are non-negative → contradiction

    I How to update S :I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]

    I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]I It should go through other v ′ /∈ SI But edge lengths are non-negative → contradiction

    I How to update S :I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]

    I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]I It should go through other v ′ /∈ SI But edge lengths are non-negative → contradiction

    I How to update S :I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]

    I It should go through other v ′ /∈ SI But edge lengths are non-negative → contradiction

    I How to update S :I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]I It should go through other v ′ /∈ S

    I But edge lengths are non-negative → contradictionI How to update S :

    I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]I It should go through other v ′ /∈ SI But edge lengths are non-negative → contradiction

    I How to update S :I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm

    An efficient algorithm for non-negative edge lengths

    I Idea: Maintain a set of vertices S with determined shortest distance from v0I Initially, S = {v0}I For all v /∈ S , maintain shortest distance estimation: D ′[v ] = minu∈S D[u] + L(u, v)

    I Lemma: For a v /∈ S with the smallest D ′[v ], the shortest distance is D ′[v ]I Assume it is not trueI There is another path which yields a distance strictly smaller than D ′[v ]I It should go through other v ′ /∈ SI But edge lengths are non-negative → contradiction

    I How to update S :I Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    5 / 8

  • Dijkstra algorithm. Example run

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    ∞ ∞

    ∞ ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    7 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    ∞ ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    5 ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 ∞

    5 ∞

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 ∞

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 6

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 6

    8

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 6

    8

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 6

    8

    5 7

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    9

    5 6

    8

    5 7

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    10

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    10

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    10

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    10

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    17

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    15

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    15

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    15

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    15

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Example run

    0

    4

    3

    6

    5 6

    8

    5 7

    9

    13

    8

    14

    A

    B

    C

    D

    E

    F

    G

    H J

    K

    L

    M

    N11

    7

    23

    12 1

    1

    4

    1

    0

    63

    22

    4

    5

    1

    6

    2

    1

    9

    6 / 8

  • Dijkstra algorithm. Implementation choices

    I Recall: How to update SI Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    I How to choose vertices with smallest distance?1. Näıve way. Iterate each time over vertices

    I Running time of one iteration: O(|V |)I |V | iterations, O(|V |2) total time. Good for dense graphs, bad for sparse ones

    2. Using binary heapI Choosing – “extract minimum”: O(log |V |), at most |V | operationsI Updating – “decrease key”: O(log |V |), at most |E | operationsI Total running time: O(|E | log |V |). Good for sparse graphs, bad for dense ones

    3. Using Fibonacci heap: “decrease key” in amortized O(1) timeI Total running time: O(|V | log |V | + |E |). However, impractical :(

    7 / 8

  • Dijkstra algorithm. Implementation choices

    I Recall: How to update SI Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    I How to choose vertices with smallest distance?

    1. Näıve way. Iterate each time over verticesI Running time of one iteration: O(|V |)I |V | iterations, O(|V |2) total time. Good for dense graphs, bad for sparse ones

    2. Using binary heapI Choosing – “extract minimum”: O(log |V |), at most |V | operationsI Updating – “decrease key”: O(log |V |), at most |E | operationsI Total running time: O(|E | log |V |). Good for sparse graphs, bad for dense ones

    3. Using Fibonacci heap: “decrease key” in amortized O(1) timeI Total running time: O(|V | log |V | + |E |). However, impractical :(

    7 / 8

  • Dijkstra algorithm. Implementation choices

    I Recall: How to update SI Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    I How to choose vertices with smallest distance?1. Näıve way. Iterate each time over vertices

    I Running time of one iteration: O(|V |)I |V | iterations, O(|V |2) total time. Good for dense graphs, bad for sparse ones

    2. Using binary heapI Choosing – “extract minimum”: O(log |V |), at most |V | operationsI Updating – “decrease key”: O(log |V |), at most |E | operationsI Total running time: O(|E | log |V |). Good for sparse graphs, bad for dense ones

    3. Using Fibonacci heap: “decrease key” in amortized O(1) timeI Total running time: O(|V | log |V | + |E |). However, impractical :(

    7 / 8

  • Dijkstra algorithm. Implementation choices

    I Recall: How to update SI Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    I How to choose vertices with smallest distance?1. Näıve way. Iterate each time over vertices

    I Running time of one iteration: O(|V |)I |V | iterations, O(|V |2) total time. Good for dense graphs, bad for sparse ones

    2. Using binary heapI Choosing – “extract minimum”: O(log |V |), at most |V | operationsI Updating – “decrease key”: O(log |V |), at most |E | operationsI Total running time: O(|E | log |V |). Good for sparse graphs, bad for dense ones

    3. Using Fibonacci heap: “decrease key” in amortized O(1) timeI Total running time: O(|V | log |V | + |E |). However, impractical :(

    7 / 8

  • Dijkstra algorithm. Implementation choices

    I Recall: How to update SI Choose v /∈ S with the smallest D ′[v ]I Set D[v ] = D ′[v ]I S ← S ∪ {v}I For all edges (v , v ′) ∈ E , update D ′[v ′]← min(D ′[v ′],D[v ] + L(v , v ′))

    I How to choose vertices with smallest distance?1. Näıve way. Iterate each time over vertices

    I Running time of one iteration: O(|V |)I |V | iterations, O(|V |2) total time. Good for dense graphs, bad for sparse ones

    2. Using binary heapI Choosing – “extract minimum”: O(log |V |), at most |V | operationsI Updating – “decrease key”: O(log |V |), at most |E | operationsI Total running time: O(|E | log |V |). Good for sparse graphs, bad for dense ones

    3. Using Fibonacci heap: “decrease key” in amortized O(1) timeI Total running time: O(|V | log |V | + |E |). However, impractical :(

    7 / 8

  • Dijkstra with heap vs. Standard libraries

    Contest trick: How to implement Dijkstra with heap using standard libraries?

    I Libraries of C++ and Java do not support heaps with “decrease key” operation

    I Basically two choices:1. Simulate binary heap with a binary tree of vertices ordered by distance

    I “Extract minimum”: remove the minimum (leftmost) vertexI “Decrease key”: remove vertex, change distance, insert vertexI O(|E | log |V |) but quite slow due to tree’s hidden constant

    2. Simulate binary heap with a priority queue of vertex-distance pairsI “Extract minimum”: remove the minimum record,

    discard and continue if distance in pair differs from current distance to that vertexI “Decrease key”: just insert a vertex-distance pair with the new distanceI O(|E | log |E |) but rather fast, generally better than tree

    8 / 8

  • Dijkstra with heap vs. Standard libraries

    Contest trick: How to implement Dijkstra with heap using standard libraries?

    I Libraries of C++ and Java do not support heaps with “decrease key” operationI Basically two choices:

    1. Simulate binary heap with a binary tree of vertices ordered by distanceI “Extract minimum”: remove the minimum (leftmost) vertexI “Decrease key”: remove vertex, change distance, insert vertexI O(|E | log |V |) but quite slow due to tree’s hidden constant

    2. Simulate binary heap with a priority queue of vertex-distance pairsI “Extract minimum”: remove the minimum record,

    discard and continue if distance in pair differs from current distance to that vertexI “Decrease key”: just insert a vertex-distance pair with the new distanceI O(|E | log |E |) but rather fast, generally better than tree

    8 / 8

  • Dijkstra with heap vs. Standard libraries

    Contest trick: How to implement Dijkstra with heap using standard libraries?

    I Libraries of C++ and Java do not support heaps with “decrease key” operationI Basically two choices:

    1. Simulate binary heap with a binary tree of vertices ordered by distanceI “Extract minimum”: remove the minimum (leftmost) vertexI “Decrease key”: remove vertex, change distance, insert vertex

    I O(|E | log |V |) but quite slow due to tree’s hidden constant2. Simulate binary heap with a priority queue of vertex-distance pairs

    I “Extract minimum”: remove the minimum record,discard and continue if distance in pair differs from current distance to that vertex

    I “Decrease key”: just insert a vertex-distance pair with the new distanceI O(|E | log |E |) but rather fast, generally better than tree

    8 / 8

  • Dijkstra with heap vs. Standard libraries

    Contest trick: How to implement Dijkstra with heap using standard libraries?

    I Libraries of C++ and Java do not support heaps with “decrease key” operationI Basically two choices:

    1. Simulate binary heap with a binary tree of vertices ordered by distanceI “Extract minimum”: remove the minimum (leftmost) vertexI “Decrease key”: remove vertex, change distance, insert vertexI O(|E | log |V |) but quite slow due to tree’s hidden constant

    2. Simulate binary heap with a priority queue of vertex-distance pairsI “Extract minimum”: remove the minimum record,

    discard and continue if distance in pair differs from current distance to that vertexI “Decrease key”: just insert a vertex-distance pair with the new distanceI O(|E | log |E |) but rather fast, generally better than tree

    8 / 8

  • Dijkstra with heap vs. Standard libraries

    Contest trick: How to implement Dijkstra with heap using standard libraries?

    I Libraries of C++ and Java do not support heaps with “decrease key” operationI Basically two choices:

    1. Simulate binary heap with a binary tree of vertices ordered by distanceI “Extract minimum”: remove the minimum (leftmost) vertexI “Decrease key”: remove vertex, change distance, insert vertexI O(|E | log |V |) but quite slow due to tree’s hidden constant

    2. Simulate binary heap with a priority queue of vertex-distance pairsI “Extract minimum”: remove the minimum record,

    discard and continue if distance in pair differs from current distance to that vertexI “Decrease key”: just insert a vertex-distance pair with the new distance

    I O(|E | log |E |) but rather fast, generally better than tree

    8 / 8

  • Dijkstra with heap vs. Standard libraries

    Contest trick: How to implement Dijkstra with heap using standard libraries?

    I Libraries of C++ and Java do not support heaps with “decrease key” operationI Basically two choices:

    1. Simulate binary heap with a binary tree of vertices ordered by distanceI “Extract minimum”: remove the minimum (leftmost) vertexI “Decrease key”: remove vertex, change distance, insert vertexI O(|E | log |V |) but quite slow due to tree’s hidden constant

    2. Simulate binary heap with a priority queue of vertex-distance pairsI “Extract minimum”: remove the minimum record,

    discard and continue if distance in pair differs from current distance to that vertexI “Decrease key”: just insert a vertex-distance pair with the new distanceI O(|E | log |E |) but rather fast, generally better than tree

    8 / 8