Math236 Discrete Maths with Applications P. Ittmann UKZN, - - PowerPoint PPT Presentation

math236 discrete maths with applications
SMART_READER_LITE
LIVE PREVIEW

Math236 Discrete Maths with Applications P. Ittmann UKZN, - - PowerPoint PPT Presentation

Math236 Discrete Maths with Applications P. Ittmann UKZN, Pietermaritzburg Semester 1, 2012 Ittmann (UKZN PMB) Math236 2012 1 / 24 Edge weights Sometimes it is desirable to have graphs in which edges have different lengths For example,


slide-1
SLIDE 1

Math236 Discrete Maths with Applications

  • P. Ittmann

UKZN, Pietermaritzburg

Semester 1, 2012

Ittmann (UKZN PMB) Math236 2012 1 / 24

slide-2
SLIDE 2

Edge weights

Sometimes it is desirable to have graphs in which edges have different lengths For example, in a map with cities as vertices and highways as edges Consider the following problem A traveller wishes to drive from Soweto in Gauteng to Graskop in the Eastern Transvaal Given that the traveller has a map of South Africa that shows the distance between particular pairs of cities or towns How does the traveller determine the shortest possible?

Ittmann (UKZN PMB) Math236 2012 2 / 24

slide-3
SLIDE 3

Edge weights (cont.)

Definition A weighted graph is a graph in which every edge e has a positive, real number w(e) assigned to it We call w(e) the weight of e or the length of e Example Consider the following graph

G:

v1 v2 v3 v4 v5 1 5 3 7 1

In this case, w(v1v4) = 5, w(v3v5) = 7

Ittmann (UKZN PMB) Math236 2012 3 / 24

slide-4
SLIDE 4

Edge weights (cont.)

Definition Let G be a graph with vertex set {v1, v2, . . . , vn} The weight matrix of G is the n × n matrix W = (wi j) where wi j =      w(vivj) if vi and vj are adjacent ∞ if vi and vj are not adjacent if i = j

Ittmann (UKZN PMB) Math236 2012 4 / 24

slide-5
SLIDE 5

Edge weights (cont.)

Applying this definition of W to the example above, we get W =       ∞ 1 5 ∞ ∞ ∞ ∞ ∞1 1 ∞ 3 7 5 ∞ 3 ∞ ∞ 1 7 ∞       Let G be a weighted graph and let P be a path in G The length of P is the sum of the weights of the edges in P We denote this by l(p)

Ittmann (UKZN PMB) Math236 2012 5 / 24

slide-6
SLIDE 6

Edge weights (cont.)

Example Revisiting this example:

G:

v1 v2 v3 v4 v5 1 5 3 7 1

Let P = v1v4v3v5v2 Then, l(P) = 5 + 3 + 7 + 1 = 16

Ittmann (UKZN PMB) Math236 2012 6 / 24

slide-7
SLIDE 7

Distance and shortest paths

Definition Let G be a connected weighted graph and let a, b ∈ V (G) The distance between a and b is the length of the shortest a − b path in G We use the notation dG(a, b)

Ittmann (UKZN PMB) Math236 2012 7 / 24

slide-8
SLIDE 8

Distance and shortest paths (cont.)

Example From the example:

G:

v1 v2 v3 v4 v5 1 5 3 7 1

So, dG(v1, v3) = 1 dG(v1, v4) = 4 dG(v3, v4) = 1

Ittmann (UKZN PMB) Math236 2012 8 / 24

slide-9
SLIDE 9

Distance and shortest paths (cont.)

Theorem Let P = v1v2 . . . vk be a shortest path from v1 to vk Then for each i ∈ {2, 3, . . . , k − 1}

1

The section v1v2 . . . vi is a shortest v1 − vi path in G, and

2

v1 is closer to vi than vk (that is, dG(v1, vi) < dG(v1, vk)

Ittmann (UKZN PMB) Math236 2012 9 / 24

slide-10
SLIDE 10

Distance and shortest paths (cont.)

Proof. We prove (1.) first Suppose to the contrary that there exists a shorted v1 − vi path P′ v1 vi vk

P

We can replace the section v1v2 . . . vi of P by P′ This yields a shorter path from v1 to vk This contradicts the choice of P as the shortest v1 − vk path

Ittmann (UKZN PMB) Math236 2012 10 / 24

slide-11
SLIDE 11

Distance and shortest paths (cont.)

Proof. We now prove (2.) We show that d(v1, vi) < d(v1, vk) By the proof of 1. above, d(v1, vi) = l(v1v2 . . . vi) < l(v1v2 . . . vk) = d(v1, vk)

Ittmann (UKZN PMB) Math236 2012 11 / 24

slide-12
SLIDE 12

Dijkstra’s Algorithm

Consider the following problem Suppose we are given a connected graph G and a vertex u in G How do we find for each vertex v ∈ V (G) − {u},

The distance between u and v A shortest u − v path in G

One way to solve this problem is to use Djikstra’s Algorithm

Ittmann (UKZN PMB) Math236 2012 12 / 24

slide-13
SLIDE 13

Dijkstra’s Algorithm (cont.)

Djikstra’s algorithm works on the following idea In the first step, we find a vertex closest to u, call it u1 A shortest u − u1 path can only consist of a single edge

u u1

To find u1 and compute d(u, u1) we do the following

Look up the weight w(uv) for all v ∈ V (G) − {u} Choose u1 such that w(uu1) is minimum Set d(u, u1) = w(uu1)

Ittmann (UKZN PMB) Math236 2012 13 / 24

slide-14
SLIDE 14

Dijkstra’s Algorithm (cont.)

Example Consider the following graph

u v x y t r 3 1 9 1 4 2 2

Here w(uv) = 3, w(ux) = 1 and w(uy) = 9 The minimum weight is w(ux) Hence u1 = x is the vertex closest to u We conclude that d(u, u1) = d(u, x) = 1

Ittmann (UKZN PMB) Math236 2012 14 / 24

slide-15
SLIDE 15

Dijkstra’s Algorithm (cont.)

In the second step, we find a vertex second closest to u and call it u2 A shortest u − u2 path is of the form uu2

  • r

uu1u2

u u1 u2 u u1 u2

Ittmann (UKZN PMB) Math236 2012 15 / 24

slide-16
SLIDE 16

Dijkstra’s Algorithm (cont.)

To find u2 and compute d(u, u2) we do the following for all v ∈ V (G) − {u, u1}:

Look up w(uv) and d(u, u1) + w(u1v) Choose u2 such that min {w(uu2), d(u, u1) + w(u1u2)} is minimum Set d(u, u2) = min {w(uu2), d(u, u1) + w(u1u2)}

Ittmann (UKZN PMB) Math236 2012 16 / 24

slide-17
SLIDE 17

Dijkstra’s Algorithm (cont.)

Example Revisiting the following graph

u v x y t r 3 1 9 1 4 2 2

Here w(uv) = 3, w(uy) = 9 Also, d(u, u1) + w(u1y) = 2 The minimum is d(u, u1) + w(u1y) = 2 Hence u2 = y is the vertex second closest to u and d(u, u2) = d(u, u1) + w(u1y) = 2

Ittmann (UKZN PMB) Math236 2012 17 / 24

slide-18
SLIDE 18

Dijkstra’s Algorithm (cont.)

In the third step, we find a vertex third closest to u and call it u3 A shortest u − u3 path is of the form uu3

  • r

uu1u3

  • r

u · · · u2u3

u u1 u2 u u1 u3 u3 u u2 u3

Ittmann (UKZN PMB) Math236 2012 18 / 24

slide-19
SLIDE 19

Dijkstra’s Algorithm (cont.)

To find u3 and compute d(u, u3) we do the following for all v ∈ V (G) − {u, u1, u2}:

Look up w(uv) and d(u, u1) + w(u1v) and d(u, u2) + w(u2v) Choose u3 such that min {w(uu3), d(u, u1) + w(u1u3), d(u, u2) + w(u2u3)} is minimum Set d(u, u2) = min {w(uu3), d(u, u1) + w(u1u3), d(u, u2) + w(u2u3)}

Ittmann (UKZN PMB) Math236 2012 19 / 24

slide-20
SLIDE 20

Dijkstra’s Algorithm (cont.)

Example

u v x y t r 3 1 9 1 4 2 2

Here w(uv) = 3 Also, d(u, u2) + w(u2r) = 4 The minimum is w(uv) = 3 Hence u3 = v is the vertex third closest to u and d(u, u3) = 3

Ittmann (UKZN PMB) Math236 2012 20 / 24

slide-21
SLIDE 21

Dijkstra’s Algorithm (cont.)

In the kth step, we find a vertex kth closest to u and call it uk A shortest u − uk path is of the form uuk

  • r

uu1uk

  • r

u · · · u2uk u · · · u3uk · · · u · · · uk−1uk

u uk u u1 uk u u2 uk u uk-1 uk

Ittmann (UKZN PMB) Math236 2012 21 / 24

slide-22
SLIDE 22

Dijkstra’s Algorithm (cont.)

To find uk and compute d(u, uk) we do the following for all v ∈ V (G) − {u, u1, u2, u3, . . . , uk−1}:

Look up

w(uv) and d(u, u1) + w(u1v) and d(u, u2) + w(u2v) and ... d(u, uk−1) + w(uk−1v)

Choose uk such that min {w(uu3), d(u, u1) + w(u1u3), . . . , d(u, uk−1) + w(uk−1uk)} is minimum Set d(u, u2) = min {w(uu3), d(u, u1) + w(u1u3), . . . , d(u, uk−1) + w(uk−1uk)}

Ittmann (UKZN PMB) Math236 2012 22 / 24

slide-23
SLIDE 23

Dijkstra’s Algorithm (cont.)

Example

u v x y t r 3 1 9 1 4 2 2

To find u4, note the following d(u, u2) + w(u2r) = 4 and d(u, u3) + w(v, t) = 7 The minimum is d(u, u2) + w(u2r) = 4 Hence u4 = r is the vertex fourth closest to u and d(u, u4) = 4

Ittmann (UKZN PMB) Math236 2012 23 / 24

slide-24
SLIDE 24

Dijkstra’s Algorithm (cont.)

Example

u v x y t r 3 1 9 1 4 2 2

To find u5, note the following d(u, u3) + w(u3t) = 7 and d(u, u4) + w(u4, t) = 6 The minimum is d(u, u4) + w(u4, t) = 6 Hence u5 = t is the vertex fifth closest to u and d(u, u5) = 6

Ittmann (UKZN PMB) Math236 2012 24 / 24