graph theory and optimization weighted graphs shortest paths … · computing distances and...

81
Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm Graph Theory and Optimization Weighted Graphs Shortest Paths & Spanning Trees Nicolas Nisse Université Côte d’Azur, Inria, CNRS, I3S, France October 2018 N. Nisse Graph Theory and applications 1/16

Upload: others

Post on 23-Sep-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Graph Theory and OptimizationWeighted Graphs

Shortest Paths & Spanning Trees

Nicolas Nisse

Université Côte d’Azur, Inria, CNRS, I3S, France

October 2018

N. Nisse Graph Theory and applications 1/16

Page 2: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Outline

1 Weighted Graphs, distance

2 Shortest paths and Spanning trees

3 Breadth First Search (BFS)

4 Dijkstra Algorithm

5 Kruskal Algorithm

N. Nisse Graph Theory and applications 2/16

Page 3: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs (length/capacity/cost/distance)

Let G = (V ,E) be a graph, we can assign a weight to the edgesw : E → R+

w may represent

• length

• capacity

• cost

• ...

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

N. Nisse Graph Theory and applications 3/16

Page 4: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs (length/capacity/cost/distance)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

• weigth of subgraph H: w(H) = ∑e∈E(H)

w(e) ex: w(H) = 72

• length of path P = (v1, · · · ,v`): w(P) = ∑e∈E(P)

w(e) = ∑1≤i<`

w({vi ,vi+1})

sum of weights of edges of P

• distance dist(x ,y): minimum length of a path from x ∈ V to y ∈ V .

N. Nisse Graph Theory and applications 3/16

Page 5: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs (length/capacity/cost/distance)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

• weigth of subgraph H: w(H) = ∑e∈E(H)

w(e)

• length of path P = (v1, · · · ,v`): w(P) = ∑e∈E(P)∗

w(e) = ∑1≤i<`

w({vi ,vi+1})

sum of weights of edges of Pex: w(P) = 125

∗ a path P = (v1, · · · ,v`) is seen as the subgraph P = ({v1 , · · · ,v`},{{vi ,vi+1} | 1≤ i < `})

• distance dist(x ,y): minimum length of a path from x ∈ V to y ∈ V .

N. Nisse Graph Theory and applications 3/16

Page 6: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs (length/capacity/cost/distance)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

• weigth of subgraph H: w(H) = ∑e∈E(H)

w(e)

• length of path P = (v1, · · · ,v`): w(P) = ∑e∈E(P)

w(e) = ∑1≤i<`

w({vi ,vi+1})

sum of weights of edges of P

• distance dist(x ,y): minimum length of a path from x ∈ V to y ∈ V .ex: dist(a, j) = 16

N. Nisse Graph Theory and applications 3/16

Page 7: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Outline

1 Weighted Graphs, distance

2 Shortest paths and Spanning trees

3 Breadth First Search (BFS)

4 Dijkstra Algorithm

5 Kruskal Algorithm

N. Nisse Graph Theory and applications 4/16

Page 8: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs: two important questions• Computing distances and shortest paths

• Breadth First Search (BFS) (unweighted graph, i.e., weights= 1)• Dijkstra’s algorithm (1956)• Bellman-Ford algorithm (1958) handle negative weights

Applications: GPS, routing in the Internet, basis of manyalgorithms...

You think it is easy?

[AS network in 2000, Burch, Cheswick]

N. Nisse Graph Theory and applications 5/16

Page 9: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs: two important questions• Computing distances and shortest paths

• Breadth First Search (BFS) (unweighted graph, i.e., weights= 1)• Dijkstra’s algorithm (1956)• Bellman-Ford algorithm (1958) handle negative weights

Applications: GPS, routing in the Internet, basis of manyalgorithms...

You think it is easy? ...really?.... Algorithms needed!!

[AS network in 2000, Burch, Cheswick]

N. Nisse Graph Theory and applications 5/16

Page 10: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Weighted graphs: two important questions

• Computing distances and shortest paths• Breadth First Search (BFS) (unweighted graph, i.e., weights= 1)• Dijkstra’s algorithm (1956)• Bellman-Ford algorithm (1958) handle negative weights

Applications: GPS, routing in the Internet, basis of manyalgorithms...

• Computing minimum spanning treesGoal: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

• Boruvska (1926), Kruskal (1956), Prim (1957)

Applications:Minimum (cheapest) substructure (subgraph) preserving connectivity.ex: “first published by Boruvska as a method of constructing an efficientelectricity network" (Wikipedia)

N. Nisse Graph Theory and applications 5/16

Page 11: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Outline

1 Weighted Graphs, distance

2 Shortest paths and Spanning trees

3 Breadth First Search (BFS)

4 Dijkstra Algorithm

5 Kruskal Algorithm

N. Nisse Graph Theory and applications 6/16

Page 12: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 13: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

ToBeExplored=(a)

0

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 14: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

ToBeExplored=(a,b)

10

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 15: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

ToBeExplored=(a,b,c)

1

1

0

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 16: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

ToBeExplored=(a,b,c,f)

1

1

1

0

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 17: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(b,c,f)

1

1

1

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 18: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(b,c,f)

1

1

1

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 19: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(b,c,f,g)

1

1

12

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 20: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(b,c,f,g,h)

1

1

12

2

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 21: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(c,f,g,h)

1

1

12

2

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 22: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(c,f,g,h,d)

1

1

12

2

2

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 23: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(f,g,h,d)

1

1

12

2

2

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 24: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(f,g,h,d,i)

1

1

12

2

2

2

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 25: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(h,d,i)

1

1

12

2

2

2

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 26: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(h,d,i,j)

1

1

12

2

2

2 3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 27: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j)

1

1

12

2

2

2 3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 28: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j,e)

1

1

12

2

2

2 3

3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 29: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphs

In unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j,e)

1

1

12

2

2

2 3

3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

N. Nisse Graph Theory and applications 7/16

Page 30: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphsIn unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j,e)

1

1

12

2

2

2 3

3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

Output: for any v ∈ V , d(v) = dist(r ,v).T is a shortest path tree of G rooted in r : i.e., T spanning subtree of G s.t.

for any v ∈ V , the path from r to v in T is a shortest path from r to v in G.N. Nisse Graph Theory and applications 7/16

Page 31: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphsIn unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j,e)

1

1

12

2

2

2 3

3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

Time-Complexity: # operations = O(|E |) each edge is considered

Exercise: Give an algorithm that decides if a graph is connectedN. Nisse Graph Theory and applications 7/16

Page 32: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphsIn unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j,e)

1

1

12

2

2

2 3

3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

Time-Complexity: # operations = O(|E |) each edge is consideredRmk1: allows to decide whether G is connected

G connected iff dist(r ,v)< ∞ defined for all v ∈ VN. Nisse Graph Theory and applications 7/16

Page 33: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphsIn unweighted graph, length of path P = # of edges of P = |E(P)|

a

b

d

e

h

j

g

c

f

i

0

ToBeExplored=(d,i,j,e)

1

1

12

2

2

2 3

3

Breadth First Searchinput: unweighted graph G = (V ,E) and r ∈ V

Initially: d(r) = 0, ToBeExplored = (r)Done = /0 and T = (V (T ),E(T )) = ({r}, /0)

While ToBeExplored 6= /0 do

Let v = head(ToBeExplored)

for u ∈ N(v)\ (ToBeExplored ∪Done) do

d(u)← d(v)+1add u in V (T ) and {v ,u} in E(T )add u at the end of ToBeExplored

remove v from ToBeExplored , add v to Done

Time-Complexity: # operations = O(|E |) each edge is consideredRmk2: gives only one shortest path tree, may be more...

depends on the ordering in which vertices are consideredN. Nisse Graph Theory and applications 7/16

Page 34: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphsDiameter of a graph G: maximum distance between two vertices of G.

diam(G) = maxu,v∈V(G)

dist(u,v)

Exercise: Give an algorithm that computes the diameter of a graph.What is the number of operations?

Exercise: What does this algorithm computes??

input: unweighted tree T = (V ,E) and r ∈ V

1 Execute a BFS rooted in r

2 Let u be a node maximizing the distance from r

3 Execute a BFS rooted in u

4 Let w be a node maximizing the distance from u

return dist(u,w)

What is the number of operations?

N. Nisse Graph Theory and applications 8/16

Page 35: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

BFS: Connectivity and distances in unweighted graphsDiameter of a graph G: maximum distance between two vertices of G.

diam(G) = maxu,v∈V(G)

dist(u,v)

Exercise: Give an algorithm that computes the diameter of a graph.What is the number of operations?

Exercise: What does this algorithm computes??

input: unweighted tree T = (V ,E) and r ∈ V

1 Execute a BFS rooted in r

2 Let u be a node maximizing the distance from r

3 Execute a BFS rooted in u

4 Let w be a node maximizing the distance from u

return dist(u,w)

What is the number of operations?

N. Nisse Graph Theory and applications 8/16

Page 36: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Diameter of treesTheorem: Previous algorithm computes the diameter of T

Termination: two executions of BFSCorrectness: u is a leaf (otherwise, there would be a vertex further from r ) Similarly, wis a leaf For contradiction, assume that diam(T ) = dist(x ,y)> dist(u,w)

(x and y must be leaves)Several Cases:

r

w u

z

r

w u

z

r

w u

x y x y

w

x y

z

a

b c

d

e

f

As an example, consider the second one (from the left)f +e+ c ≥max{f +a; f +e+b; f +d} (u further from r )b ≥max{e+a;e+ f ;e+d} (w further from u)

So dist(u,w) = b+ c ≥ a+d = dist(x ,y), a contradiction

N. Nisse Graph Theory and applications 9/16

Page 37: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Outline

1 Weighted Graphs, distance

2 Shortest paths and Spanning trees

3 Breadth First Search (BFS)

4 Dijkstra Algorithm

5 Kruskal Algorithm

N. Nisse Graph Theory and applications 10/16

Page 38: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

BFS algorithm does not work in weighted graphs Exercise: Example?

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

N. Nisse Graph Theory and applications 11/16

Page 39: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = 0

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 40: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a}

10

5

1

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 41: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f}

10

5

1

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 42: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f}

10

1

1

43

3

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 43: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c}

10

1

143

3

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 44: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c}

10

1

143

3

8

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 45: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i}

10

1

143

3

8

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 46: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i}

10

1

143

3

8

9

99

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 47: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d}

10

1

143

3

8

9

99

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 48: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d}

10

1

19

3

8

9

99

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 49: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h}

10

1

19

3

8

9

99

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 50: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g}

10

1

19

3

8

9

99

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 51: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b}

10

1

19

3

8

9

99

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 52: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b,e}

10

1

19

3

8

9

99

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 53: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b,e}

10

1

19

3

8

9

16

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 54: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b,e,j}=V

10

1

19

3

8

9

16

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞,

parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

For all v ∈ Done, d(v) = dist(r ,v). Otherwise dist(r ,v)≤ d(v).

N. Nisse Graph Theory and applications 11/16

Page 55: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b,e,j}=V

10

1

19

3

8

9

16

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞, parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

N. Nisse Graph Theory and applications 11/16

Page 56: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b,e,j}=V

10

1

19

3

8

9

16

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞, parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

Output: ∀v ∈ V , d(v) = dist(r ,v), T is a shortest path tree of G rooted in rTime-complexity: O(|E |+ |V | log |V |) (requires sorting ∗)

N. Nisse Graph Theory and applications 11/16

Page 57: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm (required positive weights)

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

0

Done = {a,f,c,i,d,h,g,b,e,j}=V

10

1

19

3

8

9

16

15

Dijkstrainput: graph G = (V ,E), weight w , and r ∈ V

Initially: d(r) = 0, (VT ,ET ) = ( /0, /0), Done = /0,and ∀v ∈ V \{r} d(v) = ∞, parent(v) = /0While Done 6= V do

Let v ∈ V \Done with d(v) minimum ∗

Add v in VT and {v ,parent(v)} in ET

Add v in Donefor u ∈ N(v)\Done do

if d(u)> d(v)+w({u,v}) thend(u)← d(v)+w({u,v})parent(u)← v

Output: ∀v ∈ V , d(v) = dist(r ,v), T is a shortest path tree of G rooted in rproof: since w positive⇒ a subpath of a shortest path is a a shortest path

N. Nisse Graph Theory and applications 11/16

Page 58: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Dijkstra’s algorithm Proof of correctness

Termination: After i th iteration of while loop, |Done|= i , then the algorithmterminates in |V | iterations of while loopCorrectness: By induction on 1≤ i < |V |, after the i th iteration of while loop,|Done|= i , and ∀v ∈ Done, d(v) = dist(r ,v). ok for i = 0

Assume the hypothesis holds ater the i th iteration.Let v ∈ V \Done be chosen at the (i +1)th iteration.

• By minimality of d(v) (in V \Done),

if G connected, then d(v)< ∞ and Done∩N(v) 6= /0, and

d(v) = minu∈Done∩N(v) d(u)+w({u,v}) by induction: d(v)≥ dist(v , r)

• For contradiction, assume that d(v)< dist(v , r): there is a shortestpath P = (r , · · · ,x ,v) of length < d .

• x ∈ Done: otherwise it would contradict minimality of d(v)• dist(v , r) = dist(x , r)+w({x ,v}) = d(x)+w({x ,v})< d(v) =

minu∈Done∩N(v) d(u)+w({u,v})≤ d(x)+w({x ,v})a contradiction

N. Nisse Graph Theory and applications 12/16

Page 59: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Outline

1 Weighted Graphs, distance

2 Shortest paths and Spanning trees

3 Breadth First Search (BFS)

4 Dijkstra Algorithm

5 Kruskal Algorithm

N. Nisse Graph Theory and applications 13/16

Page 60: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 61: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 62: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 63: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 64: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 65: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 66: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 67: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 68: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 69: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 70: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 71: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 72: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 73: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 74: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 75: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 76: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 77: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

N. Nisse Graph Theory and applications 14/16

Page 78: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning Tree

Reminder: given G = (V ,E) with weight w : E → RCompute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

Time-Complexity: # operations = O(|E | log |E |) sorting

N. Nisse Graph Theory and applications 14/16

Page 79: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Minimum Spanning TreeReminder: given G = (V ,E) with weight w : E → R

Compute a spanning tree T of G with w(T ) minimum

a

b

d

e

h

j

g

c

f

i

10

5

1

0

7

42

22

1

2

6

96

4

30

7

1

108

Kruskalinput: connected graph G = (V ,E), weight w

Initially: Let (e1, · · · ,em) be an ordering of E innon decreasing ordering of w , and T = ( /0, /0)For i ≤m do

Add ei in T if it does not create a cycle.

Exercise: Prove that, T returned by the Alg. is a minimum spanning treeIdea of proof: by contradiction

N. Nisse Graph Theory and applications 14/16

Page 80: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Kruskal’s algorithm Proof of correctness

Terminaison: obviousCorrectness:(Sketch) Clearly, T is a spanning tree (it is acyclic by definition,and if it is not connected, some edges connecting the components shouldhave been added)Assume it is not minimum and let (e1, · · · ,en−1) be its edges in nondecreasing ordering of their weights.Among the min. spanning tree of G, let T ∗ with edges (f1, · · · , fn−1) such thatthe minimum index i with ei 6= fi is maximized.T ∗∪ei contains a cycle C and, there is j > i such that fj ∈ E(C)\E(T ) andw(fj)≤ w(ei) (otw, T ∗ is not minimum).

• if w(fj)< w(ei) then the algorithm should have chosen fj instead of ei

• if w(fj) = w(ei), T ′ obtained from T ∗by replacing fj by ei is a minimumspanning tree, contradicting the maximality of i .

N. Nisse Graph Theory and applications 15/16

Page 81: Graph Theory and Optimization Weighted Graphs Shortest Paths … · Computing distances and shortest paths Breadth First Search (BFS )(unweightedgraph, i.e., weights=1 Dijkstra’s

Weighted Graphs, distance Shortest paths and Spanning trees Breadth First Search (BFS) Dijkstra Algorithm Kruskal Algorithm

Summary: To be remembered

• weighted graph, distances

• Deciding connectivityShortest path tree in undirected graph O(|E |), BFS

• Computing Shortest path tree O(|E |+ |V | log |V |), Dijkstra

• Computing Min. spanning tree O(|E | log |E |), Kruskal

N. Nisse Graph Theory and applications 16/16