Skip to content

Commit edea689

Browse files
authored
Merge pull request #1307 from el-sambal/master
Rewrite article on Strongly Connected Components, and add graph pictures
2 parents cd50071 + 15b6f5b commit edea689

File tree

9 files changed

+529
-101
lines changed

9 files changed

+529
-101
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Compiled pages are published at [https://cp-algorithms.com/](https://cp-algorith
1616

1717
## Changelog
1818

19+
- July 16, 2024: Major overhaul of the [Finding strongly connected components / Building condensation graph](https://cp-algorithms.com/graph/strongly-connected-components.html) article.
1920
- June 26, 2023: Added automatic RSS feeds for [new articles](https://cp-algorithms.com/feed_rss_created.xml) and [updates in articles](https://cp-algorithms.com/feed_rss_updated.xml).
2021
- December 20, 2022: The repository name and the owning organizations were renamed! Now the repo is located at [https://github.com/cp-algorithms/cp-algorithms](https://github.com/cp-algorithms/cp-algorithms). It is recommended to update the upstream link in your local repositories, if you have any.
2122
- October 31, 2022: It is now possible to select and copy $\LaTeX$ source code of formulas within the articles.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.log
2+
*.aux
3+
*.pdf

src/graph/strongly-connected-components-tikzpicture/cond_graph.svg

Lines changed: 138 additions & 0 deletions
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
\documentclass[tikz]{standalone}
2+
\usepackage{xcolor}
3+
\usetikzlibrary{arrows,positioning,quotes,backgrounds,arrows.meta,bending,positioning,shapes,shapes.geometric}
4+
\begin{document}
5+
\begin{tikzpicture}
6+
[scale=2.5,very thick,every node/.style={draw,inner sep=0.18cm,outer sep=1.5mm}, every path/.style={->}]
7+
8+
%\draw[help lines] (-1,-2) grid (7,2);
9+
\node[rectangle,green,fill=green!20] (0) at (0,0) {\color{black}\textbf{\{0,7\}}};
10+
\node[rectangle,red,fill=red!20] (1) at (1,0.5) {\color{black}\textbf{\{1,2,3,5,6\}}};
11+
\node[rectangle,blue,fill=blue!20] (4) at (2,0) {\color{black}\textbf{\{4,9\}}};
12+
\node[rectangle,yellow,fill=yellow!20] (8) at (1,-0.3) {\color{black}\textbf{\{8\}}};
13+
14+
\path (0) edge (1);
15+
\path (0) edge (8);
16+
\path (1) edge (4);
17+
\path (8) edge (1);
18+
\path (8) edge (4);
19+
20+
\end{tikzpicture}
21+
22+
\end{document}

src/graph/strongly-connected-components-tikzpicture/graph.svg

Lines changed: 166 additions & 0 deletions
Loading
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
\documentclass[tikz]{standalone}
2+
\usepackage{xcolor}
3+
\usetikzlibrary{arrows,positioning,quotes,backgrounds,arrows.meta,bending,positioning,shapes,shapes.geometric}
4+
\begin{document}
5+
\pgfdeclarelayer{background}
6+
\pgfsetlayers{background,main}
7+
\begin{tikzpicture}
8+
[scale=1.3,very thick,every circle node/.style={draw,inner sep=0.18cm,outer sep=1.1mm,fill=brown!30}, every path/.style={->}]
9+
10+
%\draw[help lines] (-1,-2) grid (7,2);
11+
\node[circle] (0) at (0,0) {\textbf0};
12+
\node[circle] (1) at (1,1) {\textbf1};
13+
\node[circle] (2) at (3,1) {\textbf2};
14+
\node[circle] (3) at (5,1) {\textbf3};
15+
\node[circle] (4) at (6,0) {\textbf4};
16+
\node[circle] (5) at (4,0) {\textbf5};
17+
\node[circle] (6) at (2,0) {\textbf6};
18+
\node[circle] (7) at (1,-1) {\textbf7};
19+
\node[circle] (8) at (3,-1) {\textbf8};
20+
\node[circle] (9) at (5,-1) {\textbf9};
21+
22+
\path (0) edge (1);
23+
\path (0) edge[bend left=20] (7);
24+
\path (1) edge[loop below] (1);
25+
\path (1) edge[bend left=20] (2);
26+
\path (2) edge[bend left=20] (1);
27+
\path (2) edge (5);
28+
\path (3) edge (2);
29+
\path (3) edge (4);
30+
\path (4) edge[bend left=20] (9);
31+
\path (5) edge (3);
32+
\path (5) edge (6);
33+
\path (5) edge (9);
34+
\path (6) edge (2);
35+
\path (7) edge[bend left=20] (0);
36+
\path (7) edge (6);
37+
\path (7) edge (8);
38+
\path (8) edge (6);
39+
\path (8) edge (9);
40+
\path (9) edge[bend left=20] (4);
41+
42+
\begin{pgfonlayer}{background}
43+
\draw[thick, red, fill=red!20!white] plot [smooth cycle,tension=0.65] coordinates{(0.65,1.3)(4.7,1.52)(5.4,0.7)(4.25,-.4)(3,-.33)(1.5,-.4)};
44+
\draw[thick, green, fill=green!20!white] plot [smooth cycle,tension=0.7] coordinates{(0.2,0.4)(1.5,-0.9)(.9,-1.5)(-.45,-.1)};
45+
\draw[thick, blue, fill=blue!20!white] plot [smooth cycle,tension=0.7] coordinates{(5.8,0.5)(4.45,-0.8)(5.1,-1.5)(6.6,-.1)};
46+
\draw[thick, yellow, fill=yellow!20!white] plot [smooth cycle,tension=0.9] coordinates{(3,-0.5)(3.6,-1)(3,-1.5)(2.4,-1)};
47+
\end{pgfonlayer}
48+
\end{tikzpicture}
49+
50+
\end{document}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
These are the pictures (graphs) used in the article about Strongly Connected Components and the Condensation graph.
2+
3+
Compile the .tex file with pdflatex, and then use a command line tool like pdf2svg to convert the compiled pdf into an svg file. We want svg because it is scalable, i.e. still looks good when the user zooms in far.
4+
5+
This svg can than be directly included in the markdown.

src/graph/strongly-connected-components.md

Lines changed: 94 additions & 101 deletions
Large diffs are not rendered by default.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#include "strongly_connected_components.h"
5+
6+
int main() {
7+
// we only have a single test case for now
8+
int n = 10;
9+
vector<vector<int>> adj(n);
10+
adj[0].push_back(1);
11+
adj[0].push_back(7);
12+
adj[1].push_back(1);
13+
adj[1].push_back(2);
14+
adj[2].push_back(1);
15+
adj[2].push_back(5);
16+
adj[3].push_back(2);
17+
adj[3].push_back(4);
18+
adj[4].push_back(9);
19+
adj[5].push_back(3);
20+
adj[5].push_back(6);
21+
adj[5].push_back(9);
22+
adj[6].push_back(2);
23+
adj[7].push_back(0);
24+
adj[7].push_back(6);
25+
adj[7].push_back(8);
26+
adj[8].push_back(6);
27+
adj[8].push_back(9);
28+
adj[9].push_back(4);
29+
30+
vector<vector<int>> components, adj_scc;
31+
strongy_connected_components(adj, components, adj_scc);
32+
33+
// sort things to make it easier to verify
34+
sort(components.begin(), components.end(),
35+
[](auto &l, auto &r) { return l[0] < r[0]; });
36+
for (vector<int> a : adj_scc)
37+
sort(a.begin(), a.end());
38+
39+
assert(components.size() == 4);
40+
assert(components[0] == std::vector<int>({0, 7}));
41+
assert(components[1] == std::vector<int>({1, 2, 3, 5, 6}));
42+
assert(components[2] == std::vector<int>({4, 9}));
43+
assert(components[3] == std::vector<int>({8}));
44+
45+
assert(adj_scc[0] == std::vector<int>({1, 1, 8}));
46+
assert(adj_scc[1] == std::vector<int>({4, 4}));
47+
assert(adj_scc[8] == std::vector<int>({1, 4}));
48+
49+
return 0;
50+
}

0 commit comments

Comments
 (0)