From baee83266afc28d0b47af3f67bff17f0adc1fbbe Mon Sep 17 00:00:00 2001 From: Antti H S Laaksonen Date: Mon, 17 Apr 2017 14:38:33 +0300 Subject: [PATCH] Use a constant for infinite values --- chapter07.tex | 14 +++++++------- chapter13.tex | 15 +++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/chapter07.tex b/chapter07.tex index 45de57e..2ab4339 100644 --- a/chapter07.tex +++ b/chapter07.tex @@ -142,9 +142,9 @@ we can directly implement a solution in C++: \begin{lstlisting} int f(int x) { - if (x < 0) return 1e9; + if (x < 0) return INF; if (x == 0) return 0; - int u = 1e9; + int u = INF; for (int i = 1; i <= k; i++) { u = min(u, f(x-c[i])+1); } @@ -154,7 +154,7 @@ int f(int x) { The code assumes that the available coins are $\texttt{c}[1], \texttt{c}[2], \ldots, \texttt{c}[k]$, -and $10^9$ denotes infinity. +and the constant \texttt{INF} denotes infinity. This function works but it is not efficient yet, because it goes through a large number of ways to construct the sum. @@ -191,10 +191,10 @@ implemented as follows: \begin{lstlisting} int f(int x) { - if (x < 0) return 1e9; + if (x < 0) return INF; if (x == 0) return 0; if (d[x]) return d[x]; - int u = 1e9; + int u = INF; for (int i = 1; i <= k; i++) { u = min(u, f(x-c[i])+1); } @@ -233,7 +233,7 @@ instead of a recursive function: \begin{lstlisting} d[0] = 0; for (int i = 1; i <= x; i++) { - int u = 1e9; + int u = INF; for (int j = 1; j <= k; j++) { if (i-c[j] < 0) continue; u = min(u, d[i-c[j]]+1); @@ -269,7 +269,7 @@ is used for this: \begin{lstlisting} d[0] = 0; for (int i = 1; i <= x; i++) { - d[i] = 1e9; + d[i] = INF; for (int j = 1; j <= k; j++) { if (i-c[j] < 0) continue; int u = d[i-c[j]]+1; diff --git a/chapter13.tex b/chapter13.tex index 8b5c1fd..60fbfcb 100644 --- a/chapter13.tex +++ b/chapter13.tex @@ -207,10 +207,10 @@ reduce the distances. The algorithm constructs an array \texttt{distance} that will contain the distance from $x$ to all nodes in the graph. -The initial value $10^9$ means infinity. +The constant \texttt{INF} denotes an infinite distance. \begin{lstlisting} -for (int i = 1; i <= n; i++) distance[i] = 1e9; +for (int i = 1; i <= n; i++) distance[i] = INF; distance[x] = 0; for (int i = 1; i <= n-1; i++) { for (int a = 1; a <= n; a++) { @@ -307,7 +307,7 @@ in which case the algorithm does not add the node to the queue again. \begin{lstlisting} -for (int i = 1; i <= n; i++) distance[i] = 1e9; +for (int i = 1; i <= n; i++) distance[i] = INF; distance[x] = 0; q.push(x); while (!q.empty()) { @@ -571,10 +571,10 @@ The code keeps track of processed nodes in an array \texttt{ready}, and maintains the distances in an array \texttt{distance}. Initially, the distance to the starting node is 0, -and the distance to all other nodes is $10^9$ (infinite). +and the distance to all other nodes is infinite. \begin{lstlisting} -for (int i = 1; i <= n; i++) distance[i] = 1e9; +for (int i = 1; i <= n; i++) distance[i] = INF; distance[x] = 0; q.push({0,x}); while (!q.empty()) { @@ -764,15 +764,14 @@ The following code constructs a distance matrix \texttt{d} where $\texttt{d}[a][b]$ is the shortest distance between nodes $a$ and $b$. First, the algorithm initializes \texttt{d} -using the adjacency matrix \texttt{v} of the graph -($10^9$ means infinity): +using the adjacency matrix \texttt{v} of the graph: \begin{lstlisting} for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) d[i][j] = 0; else if (v[i][j]) d[i][j] = v[i][j]; - else d[i][j] = 1e9; + else d[i][j] = INF; } } \end{lstlisting}