section 10.5 euler and hamilton paths · 2017-04-19 · hamilton circuits and paths [def] a...

90
1 Today we will discuss two sections: Section 10.5 Euler and Hamilton Paths Section 10.6 Shortest-Paths problems

Upload: others

Post on 24-Jun-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

1

Today we will discuss two sections:

Section 10.5 Euler and Hamilton Paths

Section 10.6 Shortest-Paths problems

Page 2: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

2

At the previous meeting we posed to questions:

Can we travel long the edges of a graph starting at a vertex and returning to the same vertex by traversing each edge of the graph exactly once?

Can we travel long the edges of a graph starting at a vertex and returning to the same vertex by visiting each vertex in the graph exactly once?

10.5 Euler and Hamilton Paths

Page 3: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

3

At the previous meeting we posed to questions:

Can we travel long the edges of a graph starting at a vertex and returning to the same vertex by traversing each edge of the graph exactly once?- we have answer to this one for both undirected and directed graphs

Can we travel long the edges of a graph starting at a vertex and returning to the same vertex by visiting each vertex in the graph exactly once?- let's get the answer to this one

10.5 Euler and Hamilton Paths

Page 4: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

4

Hamilton circuits and paths

[Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G exactly once.

[Def] A Hamilton path in a graph G is a simple path that passes through every vertex in G exactly once.

Examples:

10.5 Euler and Hamilton Paths

a b

cd

e

a

e

b

d cG H

Page 5: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

5

Hamilton circuits and paths

[Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G exactly once.

[Def] A Hamilton path in a graph G is a simple path that passes through every vertex in G exactly once.

Examples:

10.5 Euler and Hamilton Paths

a b

cd

e

a

e

b

d cG H

Page 6: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

6

Hamilton circuits and paths

[Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G exactly once.

[Def] A Hamilton path in a graph G is a simple path that passes through every vertex in G exactly once.

Examples:

10.5 Euler and Hamilton Paths

a b

cd

e

a

e

b

d cG H

a Hamilton path

Page 7: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

7

Hamilton circuits and paths

[Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G exactly once.

[Def] A Hamilton path in a graph G is a simple path that passes through every vertex in G exactly once.

Examples:

10.5 Euler and Hamilton Paths

a b

cd

e

Ga Hamilton path

a

e

b

d cH

Page 8: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

8

Hamilton circuits and paths

[Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G exactly once.

[Def] A Hamilton path in a graph G is a simple path that passes through every vertex in G exactly once.

Examples:

10.5 Euler and Hamilton Paths

a b

cd

e

Ga Hamilton path

a

e

b

d cH

a Hamilton circuit

Page 9: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

9

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

Visit http://puzzlemuseum.com/month/picm02/200207icosian.htm to find more about it.

It consisted of a wooden dodecahedron with a peg at each vertex, and a string. The 20 vertices were labeled with 20 cities. The goal was to start in one city and travel along the edges of dodecahedron to visit each of the other 19 cities once, and end back at the first city.

Page 10: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

10

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

Visit http://puzzlemuseum.com/month/picm02/200207icosian.htm to find more about it.

A dodecahedron is a polyhedron with 12 regular pentagons as faces.

Is there a circuit in the graph that passes each vertex exactly once?

Page 11: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

11

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

Visit http://puzzlemuseum.com/month/picm02/200207icosian.htm to find more about it.

A dodecahedron is a polyhedron with 12 regular pentagons as faces.

Is there a circuit in the graph that passes each vertex exactly once?

Yes, there is such a circuit.

Page 12: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

12

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

The graph on the left is isomorphic to the graph consisting all the edges and vertices of a dodecahedron.

We will show the circuit on it.

Yes, there is such a circuit.

Page 13: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

13

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

The graph on the left is isomorphic to the graph consisting all the edges and vertices of a dodecahedron.

We will show the circuit on it.

Yes, there is such a circuit.

Page 14: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

14

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

The graph on the left is isomorphic to the graph consisting all the edges and vertices of a dodecahedron.

We will show the circuit on it.

Yes, there is such a circuit.

Page 15: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

15

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

The graph on the left is isomorphic to the graph consisting all the edges and vertices of a dodecahedron.

We will show the circuit on it.

Yes, there is such a circuit.

Page 16: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

16

Hamilton circuits and paths

The terminology comes from a game Icozian puzzle, invented in 1875 by Irish mathematician Sir William Rowan Hamilton.

10.5 Euler and Hamilton Paths

The graph on the left is isomorphic to the graph consisting all the edges and vertices of a dodecahedron.

We will show the circuit on it.

Yes, there is such a circuit.

Page 17: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

17

Hamilton circuits

There are no simple necessary and sufficient criteria for the existence of Hamilton circuits.

However, many theorems are known that give sufficient conditions for the existence of Hamilton circuits.

Also, certain properties can be used to show that a graph has no Hamilton circuit.

10.5 Euler and Hamilton Paths

Page 18: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

18

Hamilton circuits

[Property 1] A graph with a vertex of degree 1 cannot have a Hamilton circuit.Reason: in Hamilton circuit each vertex is incident with two edges.

10.5 Euler and Hamilton Paths

Page 19: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

19

Hamilton circuits

[Property 1] A graph with a vertex of degree 1 cannot have a Hamilton circuit.Reason: in Hamilton circuit each vertex is incident with two edges.

[Property 2] If a vertex in the graph has degree 2, then both edges that are incident with this vertex must be part of the Hamilton circuit.

10.5 Euler and Hamilton Paths

Page 20: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

20

Hamilton circuits

[Property 1] A graph with a vertex of degree 1 cannot have a Hamilton circuit.Reason: in Hamilton circuit each vertex is incident with two edges.

[Property 2] If a vertex in the graph has degree 2, then both edges that are incident with this vertex must be part of the Hamilton circuit.

[Property 3] When a Hamilton circuit is being constructed and it passed through a vertex, then all remaining edges incident with this vertex, other than the two used in the circuit, can be removed from consideration.

10.5 Euler and Hamilton Paths

Page 21: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

21

Hamilton circuits

[Property 4] A Hamilton circuit cannot contain a smaller circuit within it.

10.5 Euler and Hamilton Paths

Page 22: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

22

Hamilton circuits

Example: show that Kn has a Hamilton circuit if n 3.

10.5 Euler and Hamilton Paths

Page 23: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

23

Hamilton circuits

Example: show that Kn has a Hamilton circuit if n 3.

10.5 Euler and Hamilton Paths

Page 24: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

24

Hamilton circuits

Example: show that Kn has a Hamilton circuit if n 3.

10.5 Euler and Hamilton Paths

Note that the more edges a graph has, the more likely it has a Hamilton circuit.

Page 25: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

25

Hamilton circuits

[Theorem] Dirac's Theorem If G is a simple graph with n vertices with n 3 such that the degree of every vertex in G is at least n/2, then G has a Hamilton circuit.

10.5 Euler and Hamilton Paths

Page 26: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

26

Hamilton circuits

[Theorem] Dirac's Theorem If G is a simple graph with n vertices with n 3 such that the degree of every vertex in G is at least n/2, then G has a Hamilton circuit.

[Theorem] Ore's TheoremIf G is a simple graph with n vertices with n 3 such that deg(u)+deg(v) n for every pair of non-adjacent vertices u and v in G, then G has a Hamilton circuit.

10.5 Euler and Hamilton Paths

Page 27: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

27

Hamilton circuits

[Theorem] Dirac's Theorem If G is a simple graph with n vertices with n 3 such that the degree of every vertex in G is at least n/2, then G has a Hamilton circuit.

[Theorem] Ore's TheoremIf G is a simple graph with n vertices with n 3 such that deg(u)+deg(v) n for every pair of non-adjacent vertices u and v in G, then G has a Hamilton circuit.

Both theorems provide sufficient conditions for a connected simple graph to have a Hamilton circuit.

10.5 Euler and Hamilton Paths

Page 28: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

28

Hamilton circuits

[Theorem] Dirac's Theorem If G is a simple graph with n vertices with n 3 such that the degree of every vertex in G is at least n/2, then G has a Hamilton circuit.

[Theorem] Ore's TheoremIf G is a simple graph with n vertices with n 3 such that deg(u)+deg(v) n for every pair of non-adjacent vertices u and v in G, then G has a Hamilton circuit.

Example: graph C5 has a Hamilton circuit, but doesn't

satisfy the hypotheses of either Ore's theorem or Dirac's theorem.

10.5 Euler and Hamilton Paths

Page 29: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

29

Hamilton circuits

The best algorithm known for finding a Hamilton circuit in a graph or determining that no such circuit exists have exponential worst-case time complexity (in the number of vertices of the graph)

10.5 Euler and Hamilton Paths

Page 30: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

30

Applications of Hamilton circuits and paths

Traveling salesperson problem (TSP):Asks for a shortest route a traveling salesperson should take to visit a set of cities.

10.5 Euler and Hamilton Paths

Page 31: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

31

Applications of Hamilton circuits and paths

Traveling salesperson problem (TSP):Asks for a shortest route a traveling salesperson should take to visit a set of cities.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

10.5 Euler and Hamilton Paths

Page 32: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

32

Applications of Hamilton circuits and paths

Gray Codes:Read about in the bookExample 8, page 702

10.5 Euler and Hamilton Paths

Page 33: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

33

Many problems can be modeled using graphs with weights assigned to their edges.

Graphs that have weights assigned to their edges are called weighted graphs.

10.6 Shortest-paths Problems

Page 34: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

34

Many problems can be modeled using graphs with weights assigned to their edges.

Graphs that have weights assigned to their edges are called weighted graphs.

Example: flight times between some cities.

10.6 Shortest-paths Problems

Page 35: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

35

Many problems can be modeled using graphs with weights assigned to their edges.

Graphs that have weights assigned to their edges are called weighted graphs.

The length of a path in a weighted graph is the sum of the weights of the edges in this graph.

10.6 Shortest-paths Problems

Page 36: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

36

Many problems can be modeled using graphs with weights assigned to their edges.

Graphs that have weights assigned to their edges are called weighted graphs.

The length of a path in a weighted graph is the sum of the weights of the edges in this graph.

10.6 Shortest-paths Problems

A question:What is the shortest path between Austin and Atlanta?

Page 37: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

37

There are several different algorithms that find a shortest path between two vertices in a weighted graph.

Dijkstra's algorithm given in the Rosen book allows to find a shortest path between two vertices in an undirected weighted graph.

In CSI 33 you will study Dijkstra's algorithm that works on directed graphs as well.

10.6 Shortest-paths Problems

Page 38: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

38

Dijkstra's algorithm procedure Dijkstra(G: weighted connected simple graph, with all weights positive){G=(V,E) has vertices a = v

0, …, v

n = z, and weights w(v

i,v

j),

where w(vi,v

j) = if {v

i,v

j} E}

for i:= 1 to n // set all the labels to inftyL(v

i) :=

L(a) := 0, S := // set a's label to 0 and set S is empty infty

while z Su := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

Page 39: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

39

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

Goal: find a shortest path from a to f

2

Page 40: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

40

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

2

Page 41: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

41

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0

S :=

2

Page 42: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

42

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0

S := u := a, because L(a) = 02

Page 43: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

43

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0

S := {a}

u := a, because L(a) = 02

Page 44: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

44

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)

2

Page 45: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

45

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 2 <

2

Page 46: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

46

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 2 <

2

Page 47: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

47

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 1 <

2

Page 48: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

48

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

1

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 1 <

2

Page 49: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

49

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

1

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 5 <

2

Page 50: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

50

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 5 <

2

Page 51: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

51

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 21 <

2

Page 52: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

52

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a}

u := a, because L(a) = 0v's: b,d,e,f (adjacent to a)0 + 21 <

2

Page 53: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

53

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a}

2

Page 54: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

54

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a}

u := d, because L(d) = 12

Page 55: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

55

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a, d}

u := d, because L(d) = 12

Page 56: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

56

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a, d}

u := d, because L(d) = 1v's: b, c, e

2

Page 57: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

57

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a, d}

u := d, because L(d) = 1v's: b, c, e1+2 < 2 False

2

Page 58: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

58

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

15

21

S := {a, d}

u := d, because L(d) = 1v's: b, c, e1+1 <

2

Page 59: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

59

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

215

21

S := {a, d}

u := d, because L(d) = 1v's: b, c, e1+1 <

2

Page 60: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

60

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

215

21

S := {a, d}

u := d, because L(d) = 1v's: b, c, e1+3 < 5

2

Page 61: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

61

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d}

u := d, because L(d) = 1v's: b, c, e1+3 < 5

2

Page 62: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

62

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d}

2

Page 63: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

63

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d}

2u := b, because L(b) = 2 and b comes before c

Page 64: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

64

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b}

2u := b, because L(b) = 2 and b comes before c

Page 65: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

65

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b}

2u := b, because L(b) = 2 and b comes before cv's: c

Page 66: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

66

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b}

2u := b, because L(b) = 2 and b comes before cv's: c2+1 < 2 False

Page 67: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

67

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b}

2

Page 68: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

68

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b}

2 u := c, because L(c) = 2

Page 69: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

69

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c}

2 u := c, because L(c) = 2

Page 70: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

70

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c}

2 u := c, because L(c) = 2v's:

Page 71: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

71

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c}

2

Page 72: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

72

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c}

2 u := e, because L(e) = 4

Page 73: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

73

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c, e}

2 u := e, because L(e) = 4

Page 74: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

74

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c, e}

2 u := e, because L(e) = 4v's: f

Page 75: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

75

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c, e}

2 u := e, because L(e) = 4v's: f

Page 76: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

76

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

21

S := {a, d, b, c, e}

2 u := e, because L(e) = 4v's: f4+4 < 21

Page 77: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

77

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e}

2 u := e, because L(e) = 4v's: f4+4 < 21

Page 78: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

78

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e}

2

Page 79: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

79

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e}

2 u := f

Page 80: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

80

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e, f}

2 u := f

Page 81: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

81

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e, f}

2 u := fv's:

Page 82: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

82

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S STOP!

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e, f}

2

Page 83: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

83

procedure Dijkstra(G)for i:= 1 to n // set all the labels to infty

L(vi) :=

L(a) := 0, S := // set a's label to 0 and set S is empty inftywhile z S f S STOP!

u := a vertex not in S with smallest L(u)S := S {u} // adding a vertex to Sfor all vertices v not in S // update the labels

if L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)return L(z) // return the length of a shortest path from a to z

10.6 Shortest-paths Problems

a

e

b

d cf

21

215 1

134

0 2

214

8

S := {a, d, b, c, e, f}

return 82

Page 84: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

84

[Theorem] Dijkstra's algorithm finds the length of a shortest path between two vertices in a connected simple undirected weighted graph.

[Theorem] Dijkstra's algorithm uses O(n2) operations (additions and comparisons) to find the length of a shortest path between two vertices in a connected undirected simple weighted graph with n vertices.

10.6 Shortest-paths Problems

Page 85: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

85

Traveling salesperson problem (TSP):Find a shortest route a traveling salesperson should take to visit a set of cities, assuming that the edges have weights.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

10.5 Euler and Hamilton Paths

Page 86: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

86

Traveling salesperson problem (TSP):Find a shortest route a traveling salesperson should take to visit a set of cities, assuming that the edges have weights.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

Straightforward approach: examine all possible Hamilton circuits and select one of minimum length.

10.5 Euler and Hamilton Paths

Page 87: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

87

Traveling salesperson problem (TSP):Find a shortest route a traveling salesperson should take to visit a set of cities, assuming that the edges have weights.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

Straightforward approach: examine all possible Hamilton circuits and select one of minimum length.

It is too expensive!Once a starting point is chosen, there are (n-1)! different Hamilton circuits to examine (2nd vertex: n-1 choices, 3rd vertex: n-2 choices, ...)

10.5 Euler and Hamilton Paths

Page 88: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

88

Traveling salesperson problem (TSP):Find a shortest route a traveling salesperson should take to visit a set of cities, assuming that the edges have weights.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

Straightforward approach: examine all possible Hamilton circuits and select one of minimum length.

It is too expensive!We can reduce it to (n-1)! / 2 different Hamilton circuits to examine to explore because Hamilton circuits can be reversed, but it is still too expensive

10.5 Euler and Hamilton Paths

Page 89: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

89

Traveling salesperson problem (TSP):Find a shortest route a traveling salesperson should take to visit a set of cities, assuming that the edges have weights.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

A great deal of effort was devoted to designing efficient algorithms to solve it, but still no polynomial worst-case time complexity algorithm is found so far.

10.5 Euler and Hamilton Paths

Page 90: Section 10.5 Euler and Hamilton Paths · 2017-04-19 · Hamilton circuits and paths [Def] A Hamilton circuit in a graph G is a simple circuit that passes through every vertex in G

90

Traveling salesperson problem (TSP):Find a shortest route a traveling salesperson should take to visit a set of cities, assuming that the edges have weights.

This problem can be reduced to finding a Hamilton circuit in a complete graph such that the total weight of its edges as small as possible.

A practical approach is to use approximation algorithm. These are algorithms that do not necessary produce the exact solution, but instead are guaranteed to produce a solution close to an exact solution.i.e. they will produce a circuit with total length/weight W' such that W W' cW, c is a constant.

10.5 Euler and Hamilton Paths