Skip to content

Commit a40c8c7

Browse files
muenchnerkindllemmy
authored andcommitted
Add Graphs!ConnectedIn(G)
#70 [Feature]
1 parent 07ef2bc commit a40c8c7

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The Modules
1313
| :--: | ---- | ---- | ---- |
1414
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/Functions.tla">Functions.tla</a> | Notions about functions including injection, surjection, and bijection. |[&#10004;](https://github.com/tlaplus/CommunityModules/blob/master/modules/tlc2/overrides/Functions.java) | [@muenchnerkindl](https://github.com/muenchnerkindl), [@quicquid](https://github.com/quicquid),[@lemmy](https://github.com/lemmy) |
1515
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/Folds.tla">Folds.tla</a> | Basic Fold operator. | | [@quicquid](https://github.com/quicquid), [@muenchnerkindl](https://github.com/muenchnerkindl) |
16-
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/Graphs.tla">Graphs.tla</a> | Common operators on directed and undirected graphs. | | Leslie Lamport |
16+
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/Graphs.tla">Graphs.tla</a> | Common operators on directed and undirected graphs. | | Leslie Lamport, [@lemmy](https://github.com/lemmy), [@muenchnerkindl](https://github.com/muenchnerkindl) |
1717
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/SequencesExt.tla">SequencesExt.tla</a> | Various operators to manipulate sequences. | [&#10004;](https://github.com/tlaplus/CommunityModules/blob/master/modules/tlc2/overrides/SequencesExt.java)| [@muenchnerkindl](https://github.com/muenchnerkindl),[@lemmy](https://github.com/lemmy), [@hwayne](https://github.com/hwayne), [@quicquid](https://github.com/quicquid) |
1818
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/Relation.tla">Relation.tla</a> | Basic operations on relations, represented as binary Boolean functions over some set S.| | [@muenchnerkindl](https://github.com/muenchnerkindl) |
1919
| <a href="https://github.com/tlaplus/CommunityModules/blob/master/modules/FiniteSetsExt.tla">FiniteSetsExt.tla</a> | An Operator to do folds without having to use RECURSIVE. | &#10004;| [@hwayne](https://github.com/hwayne),[@lemmy](https://github.com/lemmy), [@quicquid](https://github.com/quicquid) |

modules/Graphs.tla

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ SimplePath(G) ==
3838
AreConnectedIn(m, n, G) ==
3939
\E p \in SimplePath(G) : (p[1] = m) /\ (p[Len(p)] = n)
4040

41+
ConnectionsIn(G) ==
42+
\* Compute a Boolean matrix that indicates, for each pair of nodes,
43+
\* if there exists a path that links the two nodes. The computation,
44+
\* based on Warshall's algorithm, is much more efficient than the
45+
\* definition used in AreConnectedIn, and the result can be cached
46+
\* by TLC, avoiding recomputation.
47+
LET C[N \in SUBSET G.node] ==
48+
\* Matrix representing the existence of paths whose inner nodes
49+
\* (i.e., except for the source and the target) are all in the
50+
\* set of nodes N.
51+
IF N = {}
52+
THEN [m,n \in G.node |-> m = n \/ <<m,n>> \in G.edge]
53+
ELSE LET u == CHOOSE u \in N : TRUE
54+
Cu == C[N \ {u}]
55+
IN [m,n \in G.node |-> \/ Cu[m,n]
56+
\/ Cu[m,u] /\ Cu[u,n]]
57+
IN C[G.node]
58+
4159
IsStronglyConnected(G) ==
4260
\A m, n \in G.node : AreConnectedIn(m, n, G)
4361
-----------------------------------------------------------------------------
@@ -48,5 +66,6 @@ IsTreeWithRoot(G, r) ==
4866
/\ \A n \in G.node : AreConnectedIn(n, r, G)
4967
=============================================================================
5068
\* Modification History
69+
\* Last modified Sun Mar 06 18:10:34 CET 2022 by Stephan Merz
5170
\* Last modified Tue Dec 21 15:55:45 PST 2021 by Markus Kuppe
5271
\* Created Tue Jun 18 11:44:08 PST 2002 by Leslie Lamport

tests/GraphsTests.tla

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ Graphs(nodes) ==
1414
ASSUME \A g \in Graphs({"A", "B", "C"}):
1515
\A u,v \in g.node :
1616
AreConnectedIn(u, v, g) \in BOOLEAN
17+
18+
ASSUME LET G == [node |-> {1,2,3,4,5,6},
19+
edge |-> {<<1,2>>, <<2,3>>, <<2,4>>, <<3,2>>, <<3,4>>, <<3,5>>,
20+
<<4,2>>, <<5,6>>, <<6,5>>}]
21+
IN \A m,n \in G.node : AreConnectedIn(m,n,G) <=> ConnectionsIn(G)[m,n]
22+
1723
=====================================================================
1824
\* Modification History
25+
\* Last modified Sun Mar 06 18:15:49 CET 2022 by Stephan Merz
1926
\* Last modified Tue Dec 21 15:55:45 PST 2021 by Markus Kuppe
2027
\* Created Mon Dec 20 20:55:45 PST 2021 by Markus Kuppe

0 commit comments

Comments
 (0)