introduction to algorithms

74
Introduction to Algorithms Jiafen Liu Sept. 2013

Upload: belicia-romero

Post on 03-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Algorithms. Jiafen Liu. Sept. 2013. Today’s Tasks. Shortest Paths Properties of shortest paths Dijkstra’s algorithm Correctness Analysis Breadth-first search. Paths in graphs. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Algorithms

Introduction to Algorithms

Jiafen Liu

Sept. 2013

Page 2: Introduction to Algorithms

Today’s Tasks

• Shortest Paths – Properties of shortest paths

– Dijkstra’s algorithm

– Correctness

– Analysis

– Breadth-first search

Page 3: Introduction to Algorithms

Paths in graphs

• Consider a digraph G= (V, E) with edge-weight function w: E→R. The weight of path p = v1→v2→…→vk is defined to be

• Example:

w(p)=?

Page 4: Introduction to Algorithms

Shortest paths

• A shortest path from u to v is a path of minimum weight from u to v.

• The shortest path weight from u to v is defined as

δ(u, v) = min{w(p): p is a path from u to v}.

• In what case there is no shortest path?– Note: δ(u, v) = ∞ if no path from u to v exists.– There is a negative cycle.

• In other cases, usually a finite set.

Page 5: Introduction to Algorithms

Well-definedness of shortest paths

• If a graph G contains a negative-weight cycle, then some shortest paths may not exist.

• Example:

Page 6: Introduction to Algorithms

Thought of solving

• Dynamic Programming

• Greedy Algorithms

• Characteristics:– Optimal Substructure– Overlapping Subproblems– Greedy choice property

Page 7: Introduction to Algorithms

Optimal substructure

• What is the optimal substructure for this problem?

• Theorem. A subpath of a shortest path is a shortest path.

• How to prove?– Cut and paste

Page 8: Introduction to Algorithms

What’s the subproblems?

• Shortest path going from u to v, maybe involves computing shortest path from u to some intermediate point x, and then from x to v.

• Different choosing of x1,x2…xn consists overlapping subproblems.

• How many subproblems are there?

• |v|2

Page 9: Introduction to Algorithms

Triangle inequality

• Important for us to make greedy choice

• Theorem:

For all u, v, x V, we have∈

• Figure Illustration:

Page 10: Introduction to Algorithms

Single-source shortest paths

• Problem. From a given source vertex s V∈ , find the shortest-path weights δ(s, v) for all v V∈ .

• If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist.

• IDEA: Greedy.

1.Maintain a set S of vertices whose shortest-path distances from s are known. (s S∈ )

2.At each step add the vertex v V–S ∈ whose distance estimate from s is minimal to S .

3.Update the distance estimates of vertices adjacent to v.

Page 11: Introduction to Algorithms

Dijkstra’s algorithm

Page 12: Introduction to Algorithms

Example

Page 13: Introduction to Algorithms

Example

Page 14: Introduction to Algorithms

Example

Page 15: Introduction to Algorithms

Example

Page 16: Introduction to Algorithms

Example

Page 17: Introduction to Algorithms

Example

Page 18: Introduction to Algorithms

Example

Page 19: Introduction to Algorithms

Example

Page 20: Introduction to Algorithms

Example

Page 21: Introduction to Algorithms

Example

Page 22: Introduction to Algorithms

Example

Page 23: Introduction to Algorithms

Correctness —Part I

• Lemma. Initializing d[s]←0 and d[v]←∞ for all v V–{s} ∈ establishes d[v] ≥ δ(s,v) for all v V∈ , and this invariant is maintained over any sequence of relaxation steps.

• Any suggestions about how to prove?– Induction

• Proof.

• Base case: check the initializing term

• Induction: suppose that this fails to hold at some point.

Page 24: Introduction to Algorithms

Correctness —Part I

• Suppose not. Let v be the first vertex for which d[v] < δ(s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then how to induct a contradition?

d[v]< δ(s, v)

≤ δ(s, u) + δ(u, v)

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

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

• Contradiction.

supposition

triangle inequality

sh. path ≤ specific path

v is first violation

Page 25: Introduction to Algorithms

Correctness —Part II

• Lemma. Let u be v’s predecessor on a shortest path from s to v: s→…u→v.

if d[u]=δ(s, u) and if we relax edge(u, v),

Then, we have d[v] = δ(s, v) after the relaxation.

• Proof.

• s→…u→v is the shortest path from s to v, so δ(s, v) = w(s→… →u)+ w(u, v)

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

Page 26: Introduction to Algorithms

Correctness —Part II

• Suppose that d[v] > δ(s, v) before the relaxation. – By Correctness I, d[v] ≥ δ(s,v) – Otherwise d[v] = δ(s,v) , we’re done.

• Then test d[v] > d[u] + w(u, v) succeeds – because d[v] > δ(s, v)

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

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

• Then the algorithm sets d[v] = d[u]+ w(u,v)

= δ(s, v).

Page 27: Introduction to Algorithms

Correctness —Part III

• Theorem. Dijkstra’s algorithm terminates with d[v] = δ(s, v) for all v V∈ .

• Something to be addressed:– Once we add a vertex to S, we never change

its weight

• Proof.

• It suffices to show that d[v] = δ(s, v) for every v V ∈ when v is added to S.

Page 28: Introduction to Algorithms

Correctness —Part III

• Suppose for contradiction: Suppose u is the first vertex added to S for which d[u] ≠ δ(s, u). – d[u] > δ(s, u).

• Let p be a shortest path from s to u.

• Now look at the first place here where the path p exits S

Page 29: Introduction to Algorithms

Correctness —Part III

• Let y be the first vertex in V–S along a shortest path from s to u, and let x be its predecessor.

• Since u is the first vertex violating the claimed invariant, we have d[x] = δ(s, x) .

Page 30: Introduction to Algorithms

Correctness —Part III

• When x was added to S, the edge (x, y) was relaxed, which implies that

d[y] = δ(s, y) ≤ δ(s, u) < d[u]

• But, d[u] ≤ d[y] by our choice of u. Contradiction.

Page 31: Introduction to Algorithms

Analysis of Dijkstra

Page 32: Introduction to Algorithms

Analysis of Dijkstra

• Note: Same formula as in the analysis of Prim’s minimum spanning tree algorithm.

Page 33: Introduction to Algorithms

Unweighted graphs

• Suppose that w(u, v) =1 for all (u, v) E ∈ .

Can Dijkstra’s algorithm be improved?– Use a simple FIFO queue instead of a priority

queue.

– Analysis: Time = O(V+ E).

Page 34: Introduction to Algorithms

Example

Page 35: Introduction to Algorithms

Example

Page 36: Introduction to Algorithms

Example

Page 37: Introduction to Algorithms

Example

Page 38: Introduction to Algorithms

Example

Page 39: Introduction to Algorithms

Example

Page 40: Introduction to Algorithms

Example

Page 41: Introduction to Algorithms

Example

Page 42: Introduction to Algorithms

Example

Page 43: Introduction to Algorithms

Example

Page 44: Introduction to Algorithms

Example

Page 45: Introduction to Algorithms

Example

Page 46: Introduction to Algorithms

Correctness of BFS

• IDEA: The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra.

• Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] + 1.

Page 47: Introduction to Algorithms

Step Further I

Page 48: Introduction to Algorithms

Bellman-Ford algorithm

Page 49: Introduction to Algorithms

Example of Bellman-Ford

Page 50: Introduction to Algorithms

Example of Bellman-Ford

Page 51: Introduction to Algorithms

Example of Bellman-Ford

Page 52: Introduction to Algorithms

Example of Bellman-Ford

Page 53: Introduction to Algorithms

Example of Bellman-Ford

Page 54: Introduction to Algorithms

Example of Bellman-Ford

Page 55: Introduction to Algorithms

Example of Bellman-Ford

Page 56: Introduction to Algorithms

Example of Bellman-Ford

Page 57: Introduction to Algorithms

Example of Bellman-Ford

Page 58: Introduction to Algorithms

Example of Bellman-Ford

Page 59: Introduction to Algorithms

Example of Bellman-Ford

Page 60: Introduction to Algorithms

Example of Bellman-Ford

Page 61: Introduction to Algorithms

Example of Bellman-Ford

Page 62: Introduction to Algorithms

Example of Bellman-Ford

Page 63: Introduction to Algorithms

Example of Bellman-Ford

Page 64: Introduction to Algorithms

Example of Bellman-Ford

Page 65: Introduction to Algorithms

Example of Bellman-Ford

Page 66: Introduction to Algorithms

Example of Bellman-Ford

Page 67: Introduction to Algorithms

Example of Bellman-Ford

Page 68: Introduction to Algorithms

Example of Bellman-Ford

Page 69: Introduction to Algorithms

Example of Bellman-Ford

Page 70: Introduction to Algorithms

Step Further II

• All-pairs shortest paths

• Input: Digraph G= (V, E), where V= {1, 2, …, n}, with edge-weight function w: E→R.

• Output: n ×nmatrix of shortest-path lengths δ(i, j)for all i, j∈V.

• Floyd alglrithm

Page 71: Introduction to Algorithms

Any idea of this problem?

• Dynamic programming again!

• Consider the n×n adjacency matrix A= (aij) of the digraph, and define dij

(m)=weight of a shortest path from i to j that uses at most m edges

• Claim: We have

and for m= 1, 2, …, n–1,

Page 72: Introduction to Algorithms

Proof of Claim

Page 73: Introduction to Algorithms

Matrix multiplication

• Compute C= A·B, where C, A, and B are n×n matrices

• Time = Θ(n3) using the standard algorithm.

• What if we map “+”→“min” and “·”→“+”?

Page 74: Introduction to Algorithms