Challenge
Given a graph (a structure consisting of nodes and vertices), classify it according to a few categories.
Specifically, you will be given an unweighted directed graph, which is a set of nodes and edges from one node to another (but not necessarily both ways). You can take this in any reasonable and consistent format, like an adjacency list/matrix. You may take a list of nodes, but it is unnecessary. No edge will appear twice exactly (however, \$A\rightarrow B\$ and \$B\rightarrow A\$ might both appear). Every node will be a part of at least one edge, so you can gather all needed information from the edges and don't need a list of nodes if you don't want it.
Then, you must classify it according to the following categories:
- If every pair of connected nodes is connected both ways, the graph is undirected. Otherwise, it is directed.
- A cycle is a path in the graph from one node to another where the first and last nodes are the same, no other nodes appear multiple times in the path, and there are at least three distinct nodes in the path. If a such cycle exists, the graph is cyclic, and otherwise, it is acyclic.
- If, for every pair of nodes \$A, B\$, there exists either a path from \$A\$ to \$B\$, the graph is strongly connected. If replacing every edge with an undirected edge (that is, substituting each edge \$A\rightarrow B\$ with \$A\leftrightarrow B\$) makes the aforementioned condition true but the graph isn't strongly connected, then the graph is weakly connected. Otherwise, the graph is disconnected.
Note that it is impossible to construct a strongly connected acyclic directed graph; if every pair of nodes has a path in both directions, either they are the same, and it is undirected, or they are different, and it is cyclic. It is impossible to construct a weakly connected undirected graph by definition (replacing the edges with undirected edges doesn't mean anything on an undirected graph, so the graph would've been strongly connected already).
Formal specification
If the first part made sense and you understand the graph theory, this part is probably not worth reading. It is just here to be exceedingly clear about the definitions.
To be extremely formal, a graph \$G\$ is a set of nodes \$N\$ and edges \$E\$ where each edge connects a node \$A\$ to a node \$B\$. The first condition asks if \$(A\rightarrow B)\in E\Leftrightarrow(B\rightarrow A)\in E\$ for all \$A,B\in N\$. If so, the graph is undirected, and otherwise, it is directed. Then, we define a path as the following:
There exists a path from \$A\$ to \$B\$ if and only if \$(A\rightarrow B)\in E\$ or there exists some node \$C\$ such that there is a path from \$A\$ to \$C\$ and a path from \$C\$ to \$B\$.
The second condition asks the following: does there exist a node \$A\$ such that there is a path \$A\rightarrow B\rightarrow C\rightarrow\dots\rightarrow A\$ where no node appears more than once except for \$A\$ and there are at least three distinct nodes in the path? If so, the graph is cyclic, and otherwise, it is acyclic. (Note that the "at least three distinct nodes" condition prevents \$A\rightarrow B\rightarrow A\$ from being treated as a cycle in both undirected and directed graphs.)
The final condition asks the following: for every \$A,B\in N\$, does there exist a path from \$A\$ to \$B\$? If so, the graph is strongly connected. If not, if we add \$(B\rightarrow A)\$ to \$E\$ for every \$(A\rightarrow B)\in E\$, does this condition become true? If so, the graph is weakly connected. Otherwise, it is disconnected.
Formatting
As mentioned above, you can take the graph in any reasonable format. You should output one of 9 distinct values for each possible combination of (undirected, directed) × (cyclic, acyclic) × (strongly connected, weakly connected, disconnected); these can be any 9 consistent and distinct outputs, including erroring. For example, you may choose to output 7 various values for 7 of the categories, output nothing for the 8th, and crash on the 9th. You cannot error for more than one category (this is to prevent needing to formally define distinct classes of errors). Outputting the same value to STDOUT and STDERR count as two different outputs.
There will be at least one edge. This means that a graph containing a single node is also invalid.
Test Cases
You can find an image representation of these test cases here, which includes an example for each of the categories in a table format.
These test cases are given as a list of edges in the format [[src, dest], [src, dest], ...]
. The output is descriptive, but you don't need to output the full name, of course.
[[1, 2], [2, 3], [3, 4], [4, 1]] -> Directed, Cyclic, Strongly Connected
[[1, 2], [2, 1], [2, 3], [3, 2]] -> Undirected, Acyclic, Strongly Connected
[[1, 2], [2, 1], [2, 3], [3, 2], [3, 1], [1, 3]] -> Undirected, Cyclic, Strongly Connected
[[1, 2], [2, 3]] -> Directed, Acyclic, Weakly Connected
[[1, 2], [2, 3], [3, 4], [4, 2]] -> Directed, Cyclic, Weakly Connected
[[1, 2], [3, 4]] -> Directed, Acyclic, Disconnected
[[1, 2], [3, 4], [4, 5], [5, 3]] -> Directed, Cyclic, Disconnected
[[1, 2], [2, 1], [3, 4], [4, 3]] -> Undirected, Acyclic, Disconnected
[[1, 2], [2, 1], [3, 4], [4, 3], [4, 5], [5, 4], [5, 3], [3, 5]] -> Undirected, Cyclic, Disconnected
Related
- Is the graph acyclic? - determines if a directed graph is acyclic (input format is required to be an adjacency list)
- Is this figure connected? - determines if an undirected grid graph is connected (input format is required to be a list of grid coordinates)
This is code-golf, so the shortest solution in bytes wins per language category.
[1, 2]
and[2, 1]
a single edge in an undirected graph (so they cannot be traversed back and forth when testing for cyclic-ness), but it's unclear if such a cycle is allowed in the graph[[1, 2], [2, 1], [2, 3]]
(which is not undirected). \$\endgroup\$