You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/geometry/circle-line-intersection.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ Had we solved the original system of equations using algebraic methods, we would
41
41
42
42
As indicated at the outset, we assume that the circle is centered at the origin, and therefore the input to the program is the radius $r$ of the circle and the parameters $A$, $B$ and $C$ of the equation of the line.
43
43
44
-
~~~~~
44
+
```cpp
45
45
double r, a, b, c; // given as input
46
46
double x0 = -a*c/(a*a+b*b), y0 = -b*c/(a*a+b*b);
47
47
if (c*c > r*r*(a*a+b*b)+EPS)
@@ -61,7 +61,7 @@ else {
61
61
puts ("2 points");
62
62
cout << ax << ' ' << ay << '\n' << bx << ' ' << by << '\n';
Copy file name to clipboardExpand all lines: src/graph/all-pair-shortest-path-floyd-warshall.md
+31-21Lines changed: 31 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,15 +24,17 @@ Before $k^{th}$ phase $( k = 1 ... n )$, the $d[i][j]$ for any vertices $i$ and
24
24
In other words, before $k^{th}$ phase the value of $d[i][j]$ is equal to the length of the shortest path from vertex $i$ to the vertex $j$, if this path is allowed to enter only the vertex with numbers smaller $k$ (the beginning and end of the path are not restricted by this property).
25
25
26
26
It is easy to make sure that this property holds for the first phase. For $k = 0$, we can fill matrix as:
27
-
28
-
/* Assuming weight(i, j) is the cost of the direct edge from the vertex i to the vertex j
29
-
weight(i ,j) = 0 if no edge between vertex i and vertex j exits and
30
-
weight(i, j) is non-zero if the edge between vertex i and vertex j exists */
31
-
32
-
if ( weight(i, j) != 0) // direct edge between i and j exists
33
-
d[i][j] = weight(i, j);
34
-
else // direct edge between i and j does not exist
35
-
d[i][j] = INF;
27
+
28
+
```cpp
29
+
/* Assuming weight(i, j) is the cost of the direct edge from the vertex i to the vertex j
30
+
weight(i ,j) = 0 if no edge between vertex i and vertex j exits and
31
+
weight(i, j) is non-zero if the edge between vertex i and vertex j exists */
32
+
33
+
if ( weight(i, j) != 0) // direct edge between i and j exists
34
+
d[i][j] = weight(i, j);
35
+
else// direct edge between i and j does not exist
36
+
d[i][j] = INF;
37
+
```
36
38
37
39
At the same time, if there is no edge from $i$ to $j$, $d[i][j]$ should be $\infty$ (some high positive value denoting infinity). As we shall see later, it is a requirement for the algorithm.
38
40
@@ -50,7 +52,9 @@ Suppose now that we are in the $k^{th}$ phase, and we want to compute the matrix
50
52
51
53
Combining these two cases, we find that $k^{th}$ phase is required to recalculate the length of the shortest paths between all pairs of vertices $i$ and $j$ in the following way:
Thus, all the work that is required to produce $k^{th}$ phase is to iterate over all pairs of vertices and recalculate the length of the shortest path between them. As a result, after the $n^{th}$ phase, in the Distance Matrix, the value of $d[i][j]$ is the length of the shortest path between pair of vertices $i$ and $j$, or, is $\infty$ if the path between the vertices $i$ and $j$ does not exist.
56
60
@@ -64,20 +68,24 @@ Let $d[][]$ is a $2-D$ array of size n x n, which is filled according to the $0^
64
68
65
69
It is required to have $d[i][i] = 0$ for any $i$ at the $0^{th}$ phase.
66
70
67
-
for ( int k = 0 ; k < n ; ++k )
68
-
for ( int i = 0 ; i < n ; ++i )
69
-
for ( int j = 0 ; j < n ; ++j )
70
-
d[i][j] = min ( d[i][j] , d[i][k] + d[k][j] ) ;
71
+
```cpp
72
+
for ( int k = 0 ; k < n ; ++k )
73
+
for ( int i = 0 ; i < n ; ++i )
74
+
for ( int j = 0 ; j < n ; ++j )
75
+
d[i][j] = min ( d[i][j] , d[i][k] + d[k][j] ) ;
76
+
```
71
77
72
78
It is assumed that if there is no edge between two any vertices $i$ and $j$, the adjacency matrix at $d[i][j]$ contains a large number (large enough so that it is greater than the length of any path in this graph), then this edge will always be unprofitable to take, and the algorithm will work correctly.
73
79
74
80
However, in the presence of negative weight edges in the graph, special measures are not taken, the resulting values in matrix may appear of the form $\infty - 1$, $\infty - 2$, etc., which, of course, still indicates that between the respective vertices no path at all exists. Therefore, if the graph is having negative weight edges, it is better to write Floyd-Warshall algorithm in the following way, so that it does not perform transitions from states, which already are "non-existence of the path":
75
81
76
-
for ( int k = 0 ; k < n ; ++k )
77
-
for ( int i = 0 ; i < n ; ++i )
78
-
for ( int j = 0 ; j < n ; ++j )
79
-
if ( d [i][k] < INF && d [k][j] < INF )
80
-
d[i][j] = min ( d[i][j] , d[i][k] + d[k][j] ) ;
82
+
```cpp
83
+
for ( int k = 0 ; k < n ; ++k )
84
+
for ( int i = 0 ; i < n ; ++i )
85
+
for ( int j = 0 ; j < n ; ++j )
86
+
if ( d [i][k] < INF && d [k][j] < INF )
87
+
d[i][j] = min ( d[i][j] , d[i][k] + d[k][j] ) ;
88
+
```
81
89
82
90
## Retrieving the sequence of vertices in the shortest path
83
91
@@ -93,8 +101,10 @@ With respect to the Floyd-Warshall Algorithm, the unpleasant effects of the real
93
101
94
102
To avoid this, the can be modified to take the error(EPS = $\Delta$) into account, by using following type of comparison:
0 commit comments