1. Trang chủ
  2. » Công Nghệ Thông Tin

Introduction to Algorithms Second Edition Instructor’s Manual 2nd phần 9 pdf

43 335 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Minimum Spanning Trees
Trường học University of Example
Chuyên ngành Computer Science
Thể loại Lecture notes
Năm xuất bản 2023
Thành phố Example City
Định dạng
Số trang 43
Dung lượng 300,52 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-5KRUSKALV, E, w A← ∅ for each vertexv ∈ V do MAKE-SETv sort E into nondecreasing order by weight w for eachu, v taken from the sor

Trang 1

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-5

KRUSKAL(V, E, w)

A← ∅

for each vertexv ∈ V

do MAKE-SET(v)

sort E into nondecreasing order by weight w

for each(u, v) taken from the sorted list

do if FIND-SET(u) = FIND-SET(v)

Get the shaded edges shown in the Þgure

Suppose we had examined(c, e) before (e, f ) Then would have found (c, e) safe

and would have rejected(e, f ).

Analysis

Initialize A: O (1)

First for loop: |V | MAKE-SETs

Second for loop: O (E) FIND-SETs and UNIONs

• Assuming the implementation of disjoint-set data structure, already seen inChapter 21, that uses union by rank and path compression:

O ((V + E) α(V )) + O(E lg E)

Since G is connected, |E| ≥ |V | − 1 ⇒ O(E α(V )) + O(E lg E).

α(|V |) = O(lg V ) = O(lg E).

Therefore, total time is O (E lg E).

|E| ≤ |V |2⇒ lg |E| = O(2 lg V ) = O(lg V ).

Trang 2

23-6 Lecture Notes for Chapter 23: Minimum Spanning Trees

Therefore, O (E lg V ) time (If edges are already sorted, O(E α(V )), which is

almost linear.)

Prim’s algorithm

Builds one tree, so A is always a tree.

Starts from an arbitrary “root” r.

• At each step, Þnd a light edge crossing cut(V A , V − V A ), where V A= vertices

that A is incident on Add this edge to A.

light edge

V A

[Edges of A are shaded.]

How to Þnd the light edge quickly?

Use a priority queue Q:

Each object is a vertex in V − V A

• Key ofv is minimum weight of any edge (u, v), where u ∈ V A

• Then the vertex returned by EXTRACT-MINisv such that there exists u ∈ V A

and(u, v) is light edge crossing (V A , V − V A ).

• Key ofv is ∞ if v is not adjacent to any vertices in V A

The edges of A will form a rooted tree with root r:

r is given as an input to the algorithm, but it can be any vertex.

• Each vertex knows its parent in the tree by the attribute π[v] = parent of v π[v] = NILifv = r or v has no parent.

As algorithm progresses, A = {(v, π[v]) : v ∈ V − {r} − Q}.

At termination, V A = V ⇒ Q = ∅, so MST is A = {(v, π[v]) : v ∈ V − {r}}.

Trang 3

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-7

for eachv ∈ Adj[u]

do ifv ∈ Q and w(u, v) < key[v]

thenπ[v] ← u

DECREASE-KEY(Q, v, w(u, v))

Do example from previous graph.[Let a student pick the root.]

Analysis

Depends on how the priority queue is implemented:

Suppose Q is a binary heap.

Initialize Q and Þrst for loop: O (V lg V )

Decrease key of r: O(lg V )

while loop: |V | EXTRACT-MINcalls⇒ O(V lg V )

≤ |E| DECREASE-KEYcalls⇒ O(E lg V )

• Suppose we could do DECREASE-KEYin O (1) amortized time.

Then ≤ |E| DECREASE-KEY calls take O (E) time altogether ⇒ total time becomes O (V lg V + E).

In fact, there is a way to do DECREASE-KEY in O (1) amortized time:

Fi-bonacci heaps, in Chapter 20

Trang 4

Solutions for Chapter 23:

Minimum Spanning Trees

Solution to Exercise 23.1-1

Theorem 23.1 shows this

Let A be the empty set and S be any set containing u but not v.

Solution to Exercise 23.1-4

A triangle whose edge weights are all equal is a graph in which every edge is a lightedge crossing some cut But the triangle is cyclic, so it is not a minimum spanningtree

Solution to Exercise 23.1-6

Suppose that for every cut of G, there is a unique light edge crossing the cut Let

us consider two minimum spanning trees, T and T, of G We will show that every edge of T is also in T, which means that T and T are the same tree and hencethere is a unique minimum spanning tree

Consider any edge (u, v) ∈ T If we remove (u, v) from T , then T becomes

disconnected, resulting in a cut(S, V − S) The edge (u, v) is a light edge crossing

the cut(S, V − S) (by Exercise 23.1-3) Now consider the edge (x, y) ∈ T thatcrosses (S, V − S) It, too, is a light edge crossing this cut Since the light edge

crossing(S, V − S) is unique, the edges (u, v) and (x, y) are the same edge Thus, (u, v) ∈ T Since we chose(u, v) arbitrarily, every edge in T is also in T.Here’s a counterexample for the converse:

Trang 5

Solutions for Chapter 23: Minimum Spanning Trees 23-9

Here, the graph is its own minimum spanning tree, and so the minimum spanningtree is unique Consider the cut({x} , {y, z}) Both of the edges (x, y) and (x, z)

are light edges crossing the cut, and they are both light edges

If we knew that all of the edge weights in the graph were integers in the rangefrom 1 to |V |, then we could sort the edges in O(V + E) time using counting sort Since the graph is connected, V = O(E), and so the sorting time is re- duced to O (E) This would yield a total running time of O(V + E + E α(V )) =

O (E α(V )), again since V = O(E), and since E = O(E α(V )) The time to

process the edges, not the time to sort them, is now the dominant term edge about the weights won’t help speed up any other part of the algorithm, sincenothing besides the sort uses the weight values

Knowl-If the edge weights were integers in the range from 1 to W for some constant W ,

then we could again use counting sort to sort the edges more quickly This time,

sorting would take O (E + W) = O(E) time, since W is a constant As in the Þrst part, we get a total running time of O (E α(V )).

Solution to Exercise 23.2-5

The time taken by Prim’s algorithm is determined by the speed of the queue

oper-ations With the queue implemented as a Fibonacci heap, it takes O (E + V lg V )

time

Since the keys in the priority queue are edge weights, it might be possible to plement the queue even more efÞciently when there are restrictions on the possibleedge weights

im-We can improve the running time of Prim’s algorithm if W is a constant by menting the queue as an array Q[0 W + 1] (using the W + 1 slot for key= ∞),

Trang 6

imple-23-10 Solutions for Chapter 23: Minimum Spanning Trees

where each slot holds a doubly linked list of vertices with that weight as theirkey Then EXTRACT-MINtakes only O (W) = O(1) time (just scan for the Þrst

nonempty slot), and DECREASE-KEY takes only O(1) time (just remove the

ver-tex from the list it’s in and insert it at the front of the list indexed by the new key)

This gives a total running time of O (E), which is the best possible asymptotic time

(since(E) edges must be processed).

However, if the range of edge weights is 1 to|V |, then EXTRACT-MIN takes

(V ) time with this data structure So the total time spent doing EXTRACT-MIN

is(V2), slowing the algorithm to (E + V2) = (V2) In this case, it is better

to keep the Fibonacci-heap priority queue, which gave the(E + V lg V ) time.

There are data structures not in the text that yield better running times:

• The van Emde Boas data structure (mentioned in the chapter notes for Chapter 6

and the introduction to Part V) gives an upper bound of O (E + V lg lg V ) time

for Prim’s algorithm

• A redistributive heap (used in the single-source shortest-paths algorithm ofAhuja, Mehlhorn, Orlin, and Tarjan, and mentioned in the chapter notes for

Chapter 24) gives an upper bound of O E + V lg V

for Prim’s algorithm

Solution to Exercise 23.2-7

We start with the following lemma

Lemma

Let T be a minimum spanning tree of G = (V, E), and consider a graph G =

(V, E) for which G is a subgraph, i.e., V ⊆ Vand E ⊆ E Let T = E − T be the edges of G that are not in T Then there is a minimum spanning tree of Gthat

includes no edges in T

Proof By Exercise 23.2-1, there is a way to order the edges of E so that Kruskal’s

algorithm, when run on G, produces the minimum spanning tree T We will show that Kruskal’s algorithm, run on G, produces a minimum spanning tree T that

includes no edges in T We assume that the edges in E are considered in the same relative order when Kruskal’s algorithm is run on G and on G We Þrst state andprove the following claim

Claim

For any pair of vertices u , v ∈ V , if these vertices are in the same set after Kruskal’s algorithm run on G considers any edge (x, y) ∈ E, then they are in the same set after Kruskal’s algorithm run on Gconsiders(x, y).

Proof of claim Let us order the edges of E by nondecreasing weight as (x1, y1), (x2, y2), , (x k , y k ), where k = |E| This sequence gives the order in which the edges of E are considered by Kruskal’s algorithm, whether it is run on G or on G

We will use induction, with the inductive hypothesis that if u and v are in the same set after Kruskal’s algorithm run on G considers an edge (x i , y i ), then they are in

Trang 7

Solutions for Chapter 23: Minimum Spanning Trees 23-11

the same set after Kruskal’s algorithm run on Gconsiders the same edge We use

induction on i.

Basis: For the basis, i = 0 Kruskal’s algorithm run on G has not considered

any edges, and so all vertices are in different sets The inductive hypothesis holdstrivially

Inductive step: We assume that any vertices that are in the same set after Kruskal’s

algorithm run on G has considered edges (x1, y1), (x2, y2), , (x i−1, y i−1) are in the same set after Kruskal’s algorithm run on G has considered the same

edges When Kruskal’s algorithm runs on G, after it considers(x i−1, y i−1), it may consider some edges in E−E before considering (x i , y i ) The edges in E−E may

cause UNIONoperations to occur, but sets are never divided Hence, any vertices

that are in the same set after Kruskal’s algorithm run on G considers(x i−1 , y i−1 )

are still in the same set when(x i , y i ) is considered.

When Kruskal’s algorithm run on G considers (x i , y i ), either x i and y i are found

to be in the same set or they are not

If Kruskal’s algorithm run on G Þnds x i and y i to be in the same set, then

no UNIONoperation occurs The sets of vertices remain the same, and so theinductive hypothesis continues to hold after considering(x i , y i ).

If Kruskal’s algorithm run on G Þnds x i and y i to be in different sets, then theoperation UNION(x i , y i ) will occur Kruskal’s algorithm run on G will Þnd

that either x i and y iare in the same set or they are not By the inductive esis, when edge (x i , y i ) is considered, all vertices in x i’s set when Kruskal’s

hypoth-algorithm runs on G are in x i ’s set when Kruskal’s algorithm runs on G, and

the same holds for y i Regardless of whether Kruskal’s algorithm run on GÞnds x i and y i to already be in the same set, their sets are united after consider-ing(x i , y i ), and so the inductive hypothesis continues to hold after considering

With the claim in hand, we suppose that some edge(u, v) ∈ T is placed into T

That means that Kruskal’s algorithm run on G found u and v to be in the same

set (since(u, v) ∈ T ) but Kruskal’s algorithm run on G found u and v to be in

different sets (since(u, v) is placed into T) This fact contradicts the claim, and we

conclude that no edge in T is placed into T Thus, by running Kruskal’s algorithm

on G and G, we demonstrate that there exists a minimum spanning tree of Gthat

We use this lemma as follows Let G = (V, E) be the graph G = (V, E) with

the one new vertex and its incident edges added Suppose that we have a minimum

spanning tree T for G We compute a minimum spanning tree for Gby creating

the graph G = (V, E), where E consists of the edges of T and the edges in

E− E (i.e., the edges added to G that made G), and then Þnding a minimum

spanning tree T for G By the lemma, there is a minimum spanning tree for Gthat includes no edges of E − T In other words, Ghas a minimum spanning tree

that includes only edges in T and E− E; these edges comprise exactly the set E

Thus, the the minimum spanning tree T of G is also a minimum spanning tree

of G

Trang 8

23-12 Solutions for Chapter 23: Minimum Spanning Trees

Even though the proof of the lemma uses Kruskal’s algorithm, we are not required

to use this algorithm to Þnd T We can Þnd a minimum spanning tree by anymeans we choose Let us use Prim’s algorithm with a Fibonacci-heap priorityqueue Since|V| = |V | + 1 and |E| ≤ 2 |V | − 1 (E contains the |V | − 1 edges of T and at most |V | edges in E− E), it takes O(V ) time to construct G,and the run of Prim’s algorithm with a Fibonacci-heap priority queue takes time

O (E+ Vlg V) = O(V lg V ) Thus, if we are given a minimum spanning tree

of G, we can compute a minimum spanning tree of Gin O(V lg V ) time.

Solution to Problem 23-1

a To see that the minimum spanning tree is unique, observe that since the graph

is connected and all edge weights are distinct, then there is a unique light edgecrossing every cut By Exercise 23.1-6, the minimum spanning tree is unique

To see that the second-best minimum spanning tree need not be unique, here is

a weighted, undirected graph with a unique minimum spanning tree of weight 7and two second-best minimum spanning trees of weight 8:

1

minimum spanning tree

1

second-best minimum spanning tree

1

second-best minimum spanning tree

b Since any spanning tree has exactly |V | − 1 edges, any second-best minimum

spanning tree must have at least one edge that is not in the (best) minimumspanning tree If a second-best minimum spanning tree has exactly one edge,say(x, y), that is not in the minimum spanning tree, then it has the same set of

edges as the minimum spanning tree, except that(x, y) replaces some edge, say (u, v), of the minimum spanning tree In this case, T= T −{(u, v)}∪{(x, y)},

T − T with minimum weight If we were to add(u, v) to T, we would get

a cycle c This cycle contains some edge (x, y) in T− T (since otherwise, T

would contain a cycle)

We claim that w(x, y) > w(u, v) We prove this claim by contradiction,

so let us assume that w(x, y) < w(u, v) (Recall the assumption that

edge weights are distinct, so that we do not have to concern ourselves with

w(x, y) = w(u, v).) If we add (x, y) to T , we get a cycle c, which contains

Trang 9

Solutions for Chapter 23: Minimum Spanning Trees 23-13

some edge(u, v) in T −T(since otherwise, Twould contain a cycle)

There-fore, the set of edges T= T − {(u, v)} ∪ {(x, y)} forms a spanning tree, and

we must also havew(u, v) < w(x, y), since otherwise T would be a ning tree with weight less than w(T ) Thus, w(u, v) < w(x, y) < w(u, v),

span-which contradicts our choice of(u, v) as the edge in T −Tof minimum weight.Since the edges (u, v) and (x, y) would be on a common cycle c if we were

to add(u, v) to T, the set of edges T− {(x, y)} ∪ {(u, v)} is a spanning tree,

and its weight is less thanw(T) Moreover, it differs from T (because it differs from Tby only one edge) Thus, we have formed a spanning tree whose weight

is less than w(T) but is not T Hence, T was not a second-best minimumspanning tree

c We can Þll in max[u , v] for all u, v ∈ V in O(V2) time by simply doing a search from each vertex u, having restricted the edges visited to those of the spanning tree T It doesn’t matter what kind of search we do: breadth-Þrst,

depth-Þrst, or any other kind

We’ll give pseudocode for both breadth-Þrst and depth-Þrst approaches Eachapproach differs from the pseudocode given in Chapter 22 in that we don’t need

to compute d or f values, and we’ll use the max table itself to record whether a vertex has been visited in a given search In particular, max[u , v] =NILif and

only if u = v or we have not yet visited vertex v in a search from vertex u Note

also that since we’re visiting via edges in a spanning tree of an undirected graph,

we are guaranteed that the search from each vertex u—whether breadth-Þrst or

depth-Þrst—will visit all vertices There will be no need to “restart” the search

as is done in the DFS procedure of Section 22.3 Our pseudocode assumes that

the adjacency list of each vertex consists only of edges in the spanning tree T

Here’s the breadth-Þrst search approach:

BFS-FILL-MAX(T, w)

for each vertex u ∈ V

do for each vertexv ∈ V

for eachv ∈ Adj[x]

do if max[u , v] =NILandv = u

then if x = u or w(x, v) > max[u, x]

then max[u, v] ← (x, v) else max[u , v] ← max[u, x]

ENQUEUE(Q, v)

return max

Here’s the depth-Þrst search approach:

Trang 10

23-14 Solutions for Chapter 23: Minimum Spanning Trees

DFS-FILL-MAX(T, w)

for each vertex u ∈ V

do for each vertexv ∈ V

do max[u , v] ←NIL

DFS-FILL-MAX-VISIT(u, u, max)

return max

DFS-FILL-MAX-VISIT(u, x, max)

for each vertexv ∈ Adj[x]

do if max[u , v] =NILandv = u

then if x = u or w(x, v) > max[u, x]

then max[u, v] ← (x, v) else max[u , v] ← max[u, x]

DFS-FILL-MAX-VISIT(u, v, max)

For either approach, we are Þlling in |V | rows of the max table Since the

number of edges in the spanning tree is|V | − 1, each row takes O(V ) time to Þll in Thus, the total time to Þll in the max table is O (V2).

d In part (b), we established that we can Þnd a second-best minimum spanning

tree by replacing just one edge of the minimum spanning tree T by some

edge (u, v) not in T As we know, if we create spanning tree T by replacingedge(x, y) ∈ T by edge (u, v) ∈ T , then w(T) = w(T ) − w(x, y) + w(u, v).

For a given edge(u, v), the edge (x, y) ∈ T that minimizes w(T) is the edge of maximum weight on the unique path between u and v in T If we have already computed the max table from part (c) based on T , then the identity of this edge

is precisely what is stored in max[u , v] All we have to do is determine an edge (u, v) ∈ T for which w(max[u, v]) − w(u, v) is minimum.

Thus, our algorithm to Þnd a second-best minimum spanning tree goes as lows:

fol-1 Compute the minimum spanning tree T Time: O (E + V lg V ), using Prim’s

algorithm with a Fibonacci-heap implementation of the priority queue Since

|E| < |V |2, this running time is O(V2).

2 Given the minimum spanning tree T , compute the max table, as in part (c) Time: O(V2).

3 Find an edge (u, v) ∈ T that minimizes w(max[u, v]) − w(u, v) Time:

O (E), which is O(V2).

4 Having found an edge(u, v) in step 3, return T= T −{max[u, v]}∪{(u, v)}

as a second-best minimum spanning tree

The total time is O (V2).

Trang 11

Lecture Notes for Chapter 24:

Single-Source Shortest Paths

w(p) : u ; v p ( if there exists a path u ; v ,

Shortest path u to v is any path p such that w(p) = δ(u, v).

Example: shortest paths from s

[d values appear inside vertices Shaded edges show shortest paths.]

6 5

6

6 5

6

This example shows that the shortest path might not be unique

It also shows that when we look at shortest paths from one vertex to all othervertices, the shortest paths are organized as a tree

Can think of weights as representing any measure that

Trang 12

24-2 Lecture Notes for Chapter 24: Single-Source Shortest Paths

• accumulates linearly along a path,

• we want to minimize

Examples: time, cost, penalties, loss

Generalization of breadth-Þrst search to weighted graphs

Variants

Single-source: Find shortest paths from a given source vertex s ∈ V to every

vertexv ∈ V

Single-destination: Find shortest paths to a given destination vertex.

worst case than solving single-source

All-pairs: Find shortest path from u to v for all u, v ∈ V We’ll see algorithms

for all-pairs in the next chapter

Negative-weight edges

OK, as long as no negative-weight cycles are reachable from the source

• If we have a negative-weight cycle, we can just keep going around it, and get

w(s, v) = −∞ for all v on the cycle.

• But OK if the negative-weight cycle is not reachable from the source

• Some algorithms work only if there are no negative-weight edges in the graph.We’ll be clear when they’re allowed and not allowed

Trang 13

Lecture Notes for Chapter 24: Single-Source Shortest Paths 24-3

Shortest paths can’t contain cycles:

• Already ruled out negative-weight cycles

• Positive-weight⇒ we can get a shorter path by omitting the cycle

• Zero-weight: no reason to use them ⇒ assume that our solutions won’t usethem

Output of single-source shortest-path algorithm

For each vertexv ∈ V :

d[v] = δ(s, v).

Initially, d[v] = ∞.

Reduces as algorithms progress But always maintain d[ v] ≥ δ(s, v).

Call d[ v] a shortest-path estimate.

π[v] = predecessor of v on a shortest path from s.

• If no predecessor, π[v] =NIL

π induces a tree—shortest-path tree.

• We won’t prove properties ofπ in lecture—see text.

Initialization

All the shortest-paths algorithms start with INIT-SINGLE-SOURCE

INIT-SINGLE-SOURCE(V, s)

for eachv ∈ V

do d[ v] ← ∞

π[v] ←NIL

d[s]← 0

Relaxing an edge(u, v)

Can we improve the shortest-path estimate for v by going through u and taking (u, v)?

Trang 14

24-4 Lecture Notes for Chapter 24: Single-Source Shortest Paths

For all the single-source shortest-paths algorithms we’ll look at,

• start by calling INIT-SINGLE-SOURCE,

• then relax edges

The algorithms differ in the order and how many times they relax each edge

Shortest-paths properties

Based on calling INIT-SINGLE-SOURCE once and then calling RELAX zero ormore times

Triangle inequality

For all(u, v) ∈ E, we have δ(s, v) ≤ δ(s, u) + w(u, v).

s ; u → v is a path s ; v, and if we use a shortest path s ; u, its weight is δ(s, u) + w(u, v).

Upper-bound property

Always have d[ v] ≥ δ(s, v) for all v Once d[v] = δ(s, v), it never changes.

Proof Initially true.

Suppose there exists a vertex such that d[ v] < δ(s, v).

Without loss of generality,v is Þrst vertex for which this happens.

Let u be the vertex that causes d[v] to change.

Then d[v] = d[u] + w(u, v).

So,

d[v] < δ(s, v)

≤ δ(s, u) + w(u, v) (triangle inequality)

≤ d[u] + w(u, v) (v is Þrst violation)

⇒ d[v] < d[u] + w(u, v)

Trang 15

Lecture Notes for Chapter 24: Single-Source Shortest Paths 24-5

Contradicts d[v] = d[u] + w(u, v).

Once d[ v] reaches δ(s, v), it never goes lower It never goes up, since relaxations

only lower shortest-path estimates

Proof After relaxation:

d[ v] ≤ d[u] + w(u, v) (RELAXcode)

= δ(s, u) + w(u, v)

= δ(s, v) (lemma—optimal substructure)

Since d[v] ≥ δ(s, v), must have d[v] = δ(s, v).

Path relaxation property

Let p = v0, v1, , v k  be a shortest path from s = v0 to v k If we relax,

in order, (v0, v1), (v1, v2), , (v k−1, v k ), even intermixed with other relaxations, then d[ v k]= δ(s, v k ).

Proof Induction to show that d[ v i]= δ(s, v i ) after (v i−1, v i ) is relaxed.

Basis: i = 0 Initially, d[v0]= 0 = δ(s, v0) = δ(s, s).

Inductive step: Assume d[ v i−1] = δ(s, v i−1 ) Relax (v i−1 , v i ) By convergence property, d[ v i]= δ(s, v i ) afterward and d[v i] never changes

The Bellman-Ford algorithm

• Allows negative-weight edges

Computes d[ v] and π[v] for all v ∈ V

• ReturnsTRUEif no negative-weight cycles reachable from s,FALSEotherwise

Trang 16

24-6 Lecture Notes for Chapter 24: Single-Source Shortest Paths

5

2

–3

2 1

Values you get on each pass and how quickly it converges depends on order ofrelaxation

But guaranteed to converge after|V | − 1 passes, assuming no negative-weight

cycles

Proof Use path-relaxation property.

Letv be reachable from s, and let p = v0, v1, , v k  be a shortest path from s

tov, where v0 = s and v k = v Since p is acyclic, it has ≤ |V | − 1 edges, so

k ≤ |V | − 1.

Each iteration of the for loop relaxes all edges:

• First iteration relaxes(v0, v1).

• Second iteration relaxes(v1, v2).

kth iteration relaxes (v k−1, v k ).

By the path-relaxation property, d[ v] = d[v k]= δ(s, v k ) = δ(s, v).

How about theTRUE/FALSEreturn value?

Suppose there is no negative-weight cycle reachable from s.

At termination, for all(u, v) ∈ E, d[ v] = δ(s, v)

≤ δ(s, u) + w(u, v) (triangle inequality)

= d[u] + w(u, v)

So BELLMAN-FORDreturnsTRUE

Trang 17

Lecture Notes for Chapter 24: Single-Source Shortest Paths 24-7

Now suppose there exists negative-weight cycle c = v0, v1, , v k, where

Single-source shortest paths in a directed acyclic graph

Since a dag, we’re guaranteed no negative-weight cycles

DAG-SHORTEST-PATHS(V, E, w, s)

topologically sort the vertices

INIT-SINGLE-SOURCE(V, s)

for each vertex u, taken in topologically sorted order

do for each vertexv ∈ Adj[u]

do RELAX(u, v, w)

Example:

2 6

2

–2 –1

Correctness: Because we process vertices in topologically sorted order, edges of

any path must be relaxed in order of appearance in the path.

⇒ Edges on any shortest path are relaxed in order

⇒ By path-relaxation property, correct

Trang 18

24-8 Lecture Notes for Chapter 24: Single-Source Shortest Paths

Dijkstra’s algorithm

No negative-weight edges.

Essentially a weighted version of breadth-Þrst search

• Instead of a FIFO queue, uses a priority queue

Keys are shortest-path weights (d[v]).

Have two sets of vertices:

S = vertices whose Þnal shortest-path weights are determined,

• Dijkstra’s algorithm can be viewed as greedy, since it always chooses the

“light-est” (“clos“light-est”?) vertex in V − S to add to S.

8

5

6 5

Order of adding to S: s , y, z, x.

Correctness:

Loop invariant: At the start of each iteration of the while loop, d[v] =

δ(s, v) for all v ∈ S.

Initialization: Initially, S= ∅, so trivially true

Termination: At end, Q = ∅ ⇒ S = V ⇒ d[v] = δ(s, v) for all v ∈ V

Trang 19

Lecture Notes for Chapter 24: Single-Source Shortest Paths 24-9

Maintenance: Need to show that d[u] = δ(s, u) when u is added to S in each

So, there’s a path s ; u.

This means there’s a shortest path s ; u p

Just before u is added to S, path p connects a vertex in S (i.e., s) to a vertex in

; u (Could have x = s or y = u, so that p1

or p2may have no edges.)

Claim

d[y] = δ(s, y) when u is added to S.

Proof x ∈ S and u is the Þrst vertex such that d[u] = δ(s, u) when u is added

to S ⇒ d[x] = δ(s, x) when x is added to S Relaxed (x, y) at that time, so by

Now can get a contradiction to d[u] = δ(s, u):

y is on shortest path s ; u, and all edge weights are nonnegative

⇒ δ(s, y) ≤ δ(s, u) ⇒

d[y] = δ(s, y)

≤ δ(s, u)

≤ d[u] (upper-bound property)

Also, both y and u were in Q when we chose u, so

d[u] ≤ d[y] ⇒ d[u] = d[y]

Therefore, d[y] = δ(s, y) = δ(s, u) = d[u].

Contradicts assumption that d[u] = δ(s, u) Hence, Dijkstra’s algorithm is

correct

Trang 20

24-10 Lecture Notes for Chapter 24: Single-Source Shortest Paths

Analysis: Like Prim’s algorithm, depends on implementation of priority queue.

If binary heap, each operation takes O (lg V ) time ⇒ O(E lg V ).

• If a Fibonacci heap:

• Each EXTRACT-MINtakes O (1) amortized time.

There are O(V ) other operations, taking O(lg V ) amortized time each.

Therefore, time is O (V lg V + E).

Difference constraints

Given a set of inequalities of the form x j − x i ≤ b k

x’s are variables, 1 ≤ i, j ≤ n,

b’s are constants, 1 ≤ k ≤ m.

Want to Þnd a set of values for the x’s that satisfy all m inequalities, or determine

that no such values exist Call such a set of values a feasible solution.

If x is a feasible solution, then so is x + d for any constant d.

Proof x is a feasible solution ⇒ x j − x i ≤ b k for all i , j, k

Constraint graph

G = (V, E), weighted, directed.

V = {v0, v1, v2, , v n }: one vertex per variable + v0

E = {(v i , v j ) : x j − x i ≤ b k is a constraint} ∪ {(v0, v1), (v0, v2), , (v0, v n )}

w(v0, v j ) = 0 for all j

w(v i , v j ) = b k if x j − x i ≤ b k

Trang 21

Lecture Notes for Chapter 24: Single-Source Shortest Paths 24-11

5 –1

1 Show no negative-weight cycles⇒ feasible solution

Need to show that x j − x i ≤ b kfor all constraints Use

2 Show negative-weight cycles⇒ no feasible solution

Without loss of generality, let a negative-weight cycle be c = v1, v2, , v k,wherev1= v k (v0can’t be on c, since v0has no entering edges.) c corresponds

If x is a solution satisfying these inequalities, it must satisfy their sum.

So add them up

... row takes O(V ) time to Þll in Thus, the total time to Þll in the max table is O (V2).

d In part (b), we established that we can Þnd a second- best minimum... precisely what is stored in max[u , v] All we have to is determine an edge (u, v) ∈ T for which w(max[u, v]) − w(u, v) is minimum.

Thus, our algorithm to Þnd a second- best minimum... class="text_page_counter">Trang 19< /span>

Lecture Notes for Chapter 24: Single-Source Shortest Paths 24 -9< /small>

Maintenance: Need to show that

Ngày đăng: 13/08/2014, 18:20

TỪ KHÓA LIÊN QUAN