Skip to content

Commit b18125d

Browse files
committed
feat(clrs): add 1.2-1 exercise and 1.2-2 draft
1 parent dfa0dbf commit b18125d

1 file changed

Lines changed: 64 additions & 8 deletions

File tree

docs/CLRS/chapter-1.md

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,88 @@ Some real-world examples of sorting are listing reviews on a website by score, o
1010
Also, examples for computing convex hulls are calculating the total area of a forest, discovering minimum safe distance from an endemic strike area or minimizing costs when fencing livestock.
1111

1212
### 1.1-2
13-
> Other than speed, what other measures of efficiency might one use in a real-world
14-
setting?
13+
> Other than speed, what other measures of efficiency might one use in a real-world setting?
1514
1615
In the real world, memory is also a limited resource that should be accounted for.
1716

1817
Outside of computing specifically, other measures of efficiency are number of steps to execute a task, energy used, or even money spent for a project.
1918

2019
### 1.1-3
21-
> Select a data structure that you have seen previously, and discuss its strengths and
22-
limitations.
20+
> Select a data structure that you have seen previously, and discuss its strengths and limitations.
2321
2422
Doubly-linked lists are useful for accessing and modifying elements at the start (head) or end (tail) of the list. However, it's not very performant while accessing an element in the middle of it, since it would require traversing all elements up to that specific element.
2523

2624
### 1.1-4
27-
> How are the shortest-path and traveling-salesman problems given above similar?
28-
How are they different?
25+
> How are the shortest-path and traveling-salesman problems given above similar? How are they different?
2926
3027
Both the shortest-path and traveling-salesman problems can be modeled as graphs with weighted edges which aims to minimize the path traversal cost in a graph.
3128

3229
The shortest-path problem, however, comprises finding the shortest path between a source and a destination node, while the traveling-salesman consists of traversing all graph nodes, where the source and destination nodes are the same.
3330

3431
### 1.1-5
35-
> Come up with a real-world problem in which only the best solution will do. Then
36-
come up with one in which a solution that is “approximately” the best is good
32+
> Come up with a real-world problem in which only the best solution will do. Then come up with one in which a solution that is “approximately” the best is good
3733
enough.
3834

3935
Finding out whether a specific book is in a library or not, without accounting for its ordering would require, in the worst case, every single book to be checked out. Depending on the size of the library, it would take several hours to complete. Hence, only checking off shelves at a time solves this problem in a relatively short amount of time, even for libraries with millions of books.
4036

4137
While buying food, on the other hand, simply requires someone to go to one of the nearest supermarkets, rather than comparing prices with all of the markets in the city in order to find the absolute minimum price.
38+
39+
### 1.2-1
40+
> Give an example of an application that requires algorithmic content at the application level, and discuss the function of the algorithms involved.
41+
42+
Modern-day GPS devices and applications rely on shortest-path algorithms to fulfill their purpose.
43+
44+
The beginning of the travel is modeled as the source node and the destination place as the target node. The algorithm has to provide de most suitable path according to the route limitations and preferences specified by the user, such as the chosen mean of transport, time of the trip, intermediate places and traffic.
45+
46+
### 1.2-2
47+
> Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size $n$, insertion sort runs in $8n^2$ steps, while merge sort runs in $64n \lg n$ steps. For which values of n does insertion sort beat merge sort?
48+
49+
Let $f_x(n)$ be a function for the number of steps obtained through the implementation of an arbitrary algorithm $x$. We define $f_i(n)$ for insertion sort and $f_m(n)$ for merge sort. Since the number of steps for an algorithm is a positive integer, we round up to the nearest integer.
50+
51+
$$
52+
\begin{align}
53+
f_i:\mathbb{N} &\to \mathbb{N}&
54+
f_m:\mathbb{N} &\to \mathbb{N}\\
55+
n &\mapsto \lceil 8n^2 \rceil&
56+
x &\mapsto \lceil 64n\lg n \rceil\\
57+
\end{align}
58+
$$
59+
60+
For insertion sort to beat merge sort, must hold the following:
61+
62+
$$
63+
\begin{align}
64+
f_i(n) &< f_m(n)\\
65+
\lceil 8n^2 \rceil &< \lceil 64n\lg n \rceil
66+
\end{align}
67+
$$
68+
69+
Since $n \in \mathbb{N}$, $8n^2 \in \mathbb{N}$ under the closure of naturals. For all $a \in \mathbb{N}$, it is true that $\lceil a \rceil = a$. Hence, we can simplify $\lceil 8n^2 \rceil$ as $8n^2$.
70+
71+
$$
72+
\begin{align}
73+
8n^2 &< \lceil 64n \lg n \rceil
74+
\end{align}
75+
$$
76+
77+
Also, for all $b \in \mathbb{N}$, exists $c \in \mathbb{R^+}$ such that if $b \le \lceil c \rceil$, then $b \le c$.
78+
79+
$$
80+
\begin{align} 8n^2 &< 64n \lg n
81+
\end{align} $$
82+
83+
From that, we can derive that:
84+
85+
$$
86+
\begin{align}
87+
\frac{8n^2}{8n} &< \frac{64n \lg n}{8n}\\
88+
n &< 8 \lg n
89+
\end{align}
90+
$$
91+
92+
This turns out to be a transcendental inequality, therefore we can't solve for n algebrically.
93+
94+
For that, we will use a numerical analysis method known as the Newton-Raphson method.
95+
96+
### 1.2-3
97+
> What is the smallest value of n such that an algorithm whose running time is $100n^2$ runs faster than an algorithm whose running time is $2^n$ on the same machine?

0 commit comments

Comments
 (0)