• No se han encontrado resultados

LIBRE ARBITRIO Y LIBERTAD (74)

In document LA NOCIÓN DE LIBERTAD EN (página 24-56)

Basic Form and Application

Motivating Problem: Given a connected, weighted graph G with V ≤ 100 and two vertices s1 and s2, find a vertex v in G that represents the best meeting point, i.e. dist[s1][v] + dist[s2][v] is the minimum over all possible v. What is the best solution?

This problem requires the shortest path information from two sources s1 and s2 to all vertices in G. This can be easily done with two calls of Dijkstra’s algorithm. One from s1 to produce shortest distance array dist1 from s1, and one from s2 to produce dist2. Then iterates through all possible vertices in graph to find v such that dist1[v] + dist2[v] is minimized. Can we do better?

If the given graph is known to have V ≤ 100, then there is an even ‘simpler’ algorithm – in terms of implementation – to solve this problem as quickly as possible!

4.7. FLOYD WARSHALL’S  Steven & Felix, NUSc

Load the small graph into an Adjacency Matrix and then run the following short code with 3 nested loops. When it terminates, AdjMatrix[i][j] will contain the shortest path distance between two pair of vertices i and j in G. The original problem now become easy.

REP (k, 0, V - 1) // recall that #define REP(i, a, b) for (int i = int(a); i <= int(b); i++) REP (i, 0, V - 1)

REP (j, 0, V - 1)

AdjMatrix[i][j] = min(AdjMatrix[i][j], AdjMatrix[i][k] + AdjMatrix[k][j]);

This algorithm is called Floyd Warshall’s algorithm, invented by Robert W Floyd and Stephen Warshall. Floyd Warshall’s is a DP algorithm that clearly runs in O(V3) due to its 3 nested loops4, but since |V | ≤ 100 for the given problem, this is do-able. In general, Floyd Warshall’s solves another classical graph problem: the All-Pairs Shortest Paths (APSP) problem. It is an alternative algorithm (for small graphs) compared to calling SSSP algorithms multiple times:

1. V calls of O((V + E) logV ) Dijkstra’s = O(V3log V ) if E = O(V2).

2. V calls of O(V E) Bellman Ford’s = O(V4) if E = O(V2).

In a programming contest setting, Floyd Warshall’s main attractiveness is basically its implemen- tation speed – 4 short lines only. If the given graph is small, do not hesitate using this algorithm – even if you only need a solution for the SSSP problem.

Explanation of Floyd Warshall’s DP Solution

We provide this section for the benefit of readers who are interested to know why Floyd Warshall’s works. This section can be skipped if you just want to use this algorithm per se. However, examining this section can further strengthen your DP skill. Note that there are graph problems that have no classical algorithm and must be solved with DP techniques.

The basic idea behind Floyd Warshall’s is to gradually allow the usage of intermediate vertices to form the shortest paths. Let the vertices be labeled from 0 to ‘V -1’. We start with direct edges only, i.e. shortest path of vertex i to vertex j, denoted as sp(i,j) = weight of edge (i,j). We then find shortest paths between any two vertices with help of restricted intermediate vertices from vertex [0 ... k]. First, we only allow k = 0, then k = 1, ..., up to k = V -1.

Figure 4.18: Floyd Warshall’s Explanation

In Figure 4.18, we want to find sp(3,4). The shortest possible path is 3-0-2-4 with cost 3. But how to reach this solution? We know that with direct edges only, sp(3,4) = 5, as in Figure 4.18.A. The solution for sp(3,4) will eventually be reached from sp(3,2)+sp(2,4). But with only direct edges, sp(3,2)+sp(2,4) = 3+1 is still > 3.

When we allow k = 0, i.e. vertex 0 can now be used as an intermediate vertex, then sp(3,4) is reduced as sp(3,4) = sp(3,0)+sp(0,4) = 1+3 = 4, as in Figure 4.18.B. Note that with k = 0, sp(3,2) – which we will need later – also drop from 3 to sp(3,0)+sp(0,2) = 1+1 = 2. Floyd Warshall’s will process sp(i,j) for all pairs considering only vertex 0 as the intermediate vertex.

When we allow k = 1, i.e. vertex 0 and 1 can now be used as the intermediate vertices, then it happens that there is no change to sp(3,4), sp(3,2), nor to sp(2,4).

When we allow k = 2, i.e. vertices 0, 1, and 2 now can be used as the intermediate vertices, then sp(3,4) is reduced again as sp(3,4) = sp(3,2)+sp(2,4) = 2+1 = 3. Floyd Warshall’s repeats this process for k = 3 and finally k = 4 but sp(3,4) remains at 3.

We define Dki,j to be the shortest distance from i to j with only [0..k] as intermediate vertices. Then, Floyd Warshall’s recurrence is as follows:

Di,j−1= weight(i, j). This is the base case when we do not use any intermediate vertices.

Di,jk = min(Dk−1i,j , Dk−1i,k + Dk,jk−1) = min(not using vertex k, using k), for k≥ 0, see Figure 4.19.

Figure 4.19: Using Intermediate Vertex to (Possibly) Shorten Path

This DP formulation requires us to fill the entries layer by layer. To fill out an entry in the table k, we make use of entries in the table k-1. For example, to calculate D23,4, (row 3, column 4, in table k = 2, index start from 0), we look at the minimum of D3,41 or the sum of D13,2 + D12,4. See Figure 4.20 for illustration.

Figure 4.20: Floyd Warshall’s DP Table

The na¨ıve implementation is to use 3-dimensional matrix D[k][i][j] of size O(V3). However, we can utilize a space-saving trick by dropping dimension k and computing D[i][j] ‘on-the-fly’. Thus, the Floyd Warshall’s algorithm just need O(V2) space although it still runs in O(V3).

Other Applications

The basic purpose of Floyd Warshall’s algorithm is to solve the APSP problem. However, it can also be applied to other graph problems.

4.7. FLOYD WARSHALL’S  Steven & Felix, NUSc

Transitive Closure (Warshall’s algorithm)

Stephen Warshall developed algorithm for finding solution for Transitive Closure problem: Given a graph, determine if vertex i is connected to j. This variant uses logical bitwise operators which is much faster than arithmetic operators. Initially, d[i][j] contains 1 (true) if vertex i is directly connected to vertex j, 0 (false) otherwise. After running O(V3) Warshall’s algorithm below, we can check if any two vertices i and j are directly or indirectly connected by checking d[i][j].

REP (k, 0, V - 1) REP (i, 0, V - 1)

REP (j, 0, V - 1)

d[i][j] = d[i][j] | (d[i][k] & d[k][j]);

Minimax and Maximin

The Minimax path problem is a problem of finding the minimum of maximum edge weight among all possible paths from i to j. There can be many paths from i to j. For a single path from i to j, we pick the maximum edge weight along this path. Then for all possible paths from i to j, pick the one with the minimum max-edge-weight. The reverse problem of Maximin is defined similarly. The solution using Floyd Warshall’s is shown below. First, initialize d[i][j] to be the weight of edge (i,j). This is the default minimax cost for two vertices that are directly connected. For pair i-j without any direct edge, set d[i][j] = INF. Then, we try all possible intermediate vertex k. The minimax cost d[i][j] is min of either (itself) or (the max between d[i][k] or d[k][j]).

REP (k, 0, V - 1) REP (i, 0, V - 1)

REP (j, 0, V - 1)

d[i][j] = min(d[i][j], max(d[i][k], d[k][j]));

Exercise: In this section, we have shown you how to solve Minimax (and Maximin) with Floyd Warshall’s algorithm. However, this problem can also be modeled as an MST problem and solved using Kruskal’s algorithm. Find out the way!

Programming Exercises for Floyd Warshall’s algorithm:

• Floyd Warshall’s Standard Application (for APSP or SSSP on small graph)

1. UVa 186 - Trip Routing (graph is small) 2. UVa 341 - Non-Stop Travel (graph is small) 3. UVa 423 - MPI Maelstrom (graph is small)

4. UVa 821 - Page Hopping (one of the ‘easiest’ ICPC World Finals problem) 5. UVa 10075 - Airlines (with special great-circle distances, see Section 7.2) 6. UVa 10171 - Meeting Prof. Miguel (solution is easy with APSP information) 7. UVa 11015 - 05-32 Rendezvous (graph is small)

8. UVa 10246 - Asterix and Obelix

9. UVa 10724 - Road Construction (adding one edge will only change ‘few things’) 10. UVa 10793 - The Orc Attack (Floyd Warshall’s simplifies this problem)

11. UVa 10803 - Thunder Mountain (graph is small)

• Variants

1. UVa 334 - Identifying Concurrent Events (transitive closure is only the sub-problem) 2. UVa 534 - Frogger (Minimax)

3. UVa 544 - Heavy Cargo (Maximin)

4. UVa 869 - Airline Comparison (run Warshall’s twice, then compare the AdjMatrices) 5. UVa 925 - No more prerequisites, please!

6. UVa 10048 - Audiophobia (Minimax) 7. UVa 10099 - Tourist Guide (Maximin)

In document LA NOCIÓN DE LIBERTAD EN (página 24-56)

Documento similar