-
Notifications
You must be signed in to change notification settings - Fork 112
/
functions.jl
1286 lines (983 loc) · 31.1 KB
/
functions.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Oscar: Polyhedron, Polymake, pm_object
import Oscar.Polymake: Directed, Undirected
function pm_object(G::Graph{T}) where {T <: Union{Directed, Undirected}}
return G.pm_graph
end
################################################################################
################################################################################
## Constructing and modifying
################################################################################
################################################################################
@doc raw"""
Graph{T}(nverts::Int64) where {T <: Union{Directed, Undirected}}
Construct a graph on `nverts` vertices and no edges. `T` indicates whether the
graph should be `Directed` or `Undirected`.
# Examples
Make a directed graph with 5 vertices and print the number of nodes and edges.
```jldoctest
julia> g = Graph{Directed}(5);
julia> n_vertices(g)
5
julia> n_edges(g)
0
```
"""
function Graph{T}(nverts::Int64) where {T <: Union{Directed, Undirected}}
pmg = Polymake.Graph{T}(nverts)
return Graph{T}(pmg)
end
@doc raw"""
graph_from_adjacency_matrix(::Type{T}, G) where {T <:Union{Directed, Undirected}}
Return the graph with adjacency matrix `G`.
This means that the nodes ``i, j`` are connected by an edge
if and only if ``G_{i,j}`` is one.
In the undirected case, it is assumed that ``i > j`` i.e. the upper triangular
part of ``G`` is ignored.
# Examples
```jldoctest
julia> G = ZZ[0 0; 1 0]
[0 0]
[1 0]
julia> graph_from_adjacency_matrix(Directed, G)
Directed graph with 2 nodes and the following edges:
(2, 1)
julia> graph_from_adjacency_matrix(Undirected, G)
Undirected graph with 2 nodes and the following edges:
(2, 1)
```
"""
graph_from_adjacency_matrix(::Type, G::Union{MatElem, Matrix})
function graph_from_adjacency_matrix(::Type{T}, G::Union{MatElem, Matrix}) where {T <: Union{Directed, Undirected}}
n = nrows(G)
@req nrows(G)==ncols(G) "not a square matrix"
g = Graph{T}(n)
for i in 1:n
for j in 1:(T==Undirected ? i-1 : n)
if isone(G[i,j])
add_edge!(g, i, j)
else
iszero(G[i,j]) || error("not an adjacency matrix")
end
end
end
return g
end
_has_node(G::Graph, node::Int64) = 0 < node <= n_vertices(G)
@doc raw"""
add_edge!(g::Graph{T}, s::Int64, t::Int64) where {T <: Union{Directed, Undirected}}
Add edge `(s,t)` to the graph `g`.
Return `true` if a new edge `(s,t)` was added, `false` otherwise.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 1, 2)
false
julia> n_edges(g)
1
```
"""
function add_edge!(g::Graph{T}, source::Int64, target::Int64) where {T <: Union{Directed, Undirected}}
_has_node(g, source) && _has_node(g, target) || return false
old_nedges = n_edges(g)
Polymake._add_edge(pm_object(g), source-1, target-1)
return n_edges(g) == old_nedges + 1
end
@doc raw"""
rem_edge!(g::Graph{T}, s::Int64, t::Int64) where {T <: Union{Directed, Undirected}}
rem_edge!(g::Graph{T}, e::Edge) where {T <: Union{Directed, Undirected}}
Remove edge `(s,t)` from the graph `g`.
Return `true` if there was an edge from `s` to `t` and it got removed, `false`
otherwise.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2)
true
julia> n_edges(g)
1
julia> rem_edge!(g, 1, 2)
true
julia> n_edges(g)
0
```
"""
function rem_edge!(g::Graph{T}, s::Int64, t::Int64) where {T <: Union{Directed, Undirected}}
has_edge(g, s, t) || return false
old_nedges = n_edges(g)
Polymake._rem_edge(pm_object(g), s-1, t-1)
return n_edges(g) == old_nedges - 1
end
@doc raw"""
add_vertex!(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Add a vertex to the graph `g`. Return `true` if there a new vertex was actually
added.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> n_vertices(g)
2
julia> add_vertex!(g)
true
julia> n_vertices(g)
3
```
"""
function add_vertex!(g::Graph{T}) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
old_nvertices = n_vertices(g)
Polymake._add_vertex(pmg)
return n_vertices(g) - 1 == old_nvertices
end
@doc raw"""
rem_vertex!(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Remove the vertex `v` from the graph `g`. Return `true` if node `v` existed and
was actually removed, `false` otherwise.
Please note that this will shift the indices of the vertices with index larger than `v`,
but it will preserve the vertex ordering.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> n_vertices(g)
2
julia> rem_vertex!(g, 1)
true
julia> n_vertices(g)
1
```
"""
function rem_vertex!(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
_has_node(g, v) || return false
pmg = pm_object(g)
old_nvertices = n_vertices(g)
result = Polymake._rem_vertex(pmg, v-1)
Polymake._squeeze(pmg)
return n_vertices(g) + 1 == old_nvertices
end
@doc raw"""
rem_vertices!(g::Graph{T}, a::AbstractArray{Int64}) where {T <: Union{Directed, Undirected}}
Remove the vertices in `a` from the graph `g`. Return `true` if at least one vertex was removed.
Please note that this will shift the indices of some of the remaining vertices, but it will preserve the vertex ordering.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> n_vertices(g)
2
julia> rem_vertices!(g, [1, 2])
true
julia> n_vertices(g)
0
```
"""
function rem_vertices!(g::Graph{T}, a::AbstractVector{Int64}) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
old_nvertices = n_vertices(g)
for v in a
0 < v <= old_nvertices && Polymake._rem_vertex(pmg, v-1)
end
Polymake._squeeze(pmg)
return n_vertices(g) < old_nvertices
end
@doc raw"""
add_vertices!(g::Graph{T}, n::Int64) where {T <: Union{Directed, Undirected}}
Add a `n` new vertices to the graph `g`. Return the number of vertices that
were actually added to the graph `g`.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> n_vertices(g)
2
julia> add_vertices!(g, 5);
julia> n_vertices(g)
7
```
"""
function add_vertices!(g::Graph{T}, n::Int64) where {T <: Union{Directed, Undirected}}
return count(_->add_vertex!(g), 1:n)
end
################################################################################
################################################################################
## Edges
################################################################################
################################################################################
struct Edge
source::Int64
target::Int64
end
@doc raw"""
src(e::Edge)
Return the source of an edge.
# Examples
```jldoctest
julia> g = complete_graph(2);
julia> E = collect(edges(g));
julia> e = E[1]
Edge(2, 1)
julia> src(e)
2
```
"""
function src(e::Edge)
return e.source
end
@doc raw"""
dst(e::Edge)
Return the destination of an edge.
# Examples
```jldoctest
julia> g = complete_graph(2);
julia> E = collect(edges(g));
julia> e = E[1]
Edge(2, 1)
julia> dst(e)
1
```
"""
function dst(e::Edge)
return e.target
end
Vector{Int}(e::Edge) = [src(e), dst(e)]
Base.isless(a::Edge, b::Edge) = Base.isless(Vector{Int}(a), Vector{Int}(b))
rem_edge!(g::Graph{T}, e::Edge) where {T <: Union{Directed, Undirected}} =
rem_edge!(g, src(e), dst(e))
@doc raw"""
reverse(e::Edge)
Return the edge in the opposite direction of the edge `e`.
# Examples
```jldoctest
julia> g = complete_graph(2);
julia> E = collect(edges(g));
julia> e = E[1]
Edge(2, 1)
julia> reverse(e)
Edge(1, 2)
```
"""
function reverse(e::Edge)
return Edge(dst(e), src(e))
end
struct EdgeIterator
pm_itr::Polymake.GraphEdgeIterator{T} where {T <: Union{Directed, Undirected}}
l::Int64
end
Base.length(eitr::EdgeIterator) = eitr.l
Base.eltype(::Type{EdgeIterator}) = Edge
function Base.iterate(eitr::EdgeIterator, index = 1)
if index > eitr.l
return nothing
else
e = Polymake.get_element(eitr.pm_itr)
s = Polymake.first(e)
t = Polymake.last(e)
edge = Edge(s+1, t+1)
Polymake.increment(eitr.pm_itr)
return (edge, index+1)
end
end
################################################################################
################################################################################
## Accessing properties
################################################################################
################################################################################
@doc raw"""
n_vertices(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return the number of vertices of a graph.
# Examples
The edge graph of the cube has eight vertices, just like the cube itself.
```jldoctest
julia> c = cube(3);
julia> g = vertex_edge_graph(c);
julia> n_vertices(g)
8
```
"""
function n_vertices(g::Graph{T}) where {T <: Union{Directed, Undirected}}
return Polymake.nv(pm_object(g))
end
@doc raw"""
n_edges(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return the number of edges of a graph.
# Examples
The edge graph of the cube has 12 edges just like the cube itself.
```jldoctest
julia> c = cube(3);
julia> g = vertex_edge_graph(c);
julia> n_edges(g)
12
```
"""
function n_edges(g::Graph{T}) where {T <: Union{Directed, Undirected}}
return Polymake.ne(pm_object(g))
end
@doc raw"""
edges(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return an iterator over the edges of the graph `g`.
# Examples
A triangle has three edges.
```jldoctest
julia> triangle = simplex(2);
julia> g = vertex_edge_graph(triangle);
julia> collect(edges(g))
3-element Vector{Edge}:
Edge(2, 1)
Edge(3, 1)
Edge(3, 2)
```
"""
function edges(g::Graph{T}) where {T <: Union{Directed, Undirected}}
return EdgeIterator(Polymake.edgeiterator(pm_object(g)), n_edges(g))
end
@doc raw"""
has_edge(g::Graph{T}, source::Int64, target::Int64) where {T <: Union{Directed, Undirected}}
Check for an edge in a graph.
# Examples
Check for the edge $1\to 2$ in the edge graph of a triangle.
```jldoctest
julia> triangle = simplex(2);
julia> g = vertex_edge_graph(triangle);
julia> has_edge(g, 1, 2)
true
```
"""
function has_edge(g::Graph{T}, source::Int64, target::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
return Polymake._has_edge(pmg, source-1, target-1)
end
function has_edge(g::Graph{T}, e::Edge) where {T <: Union{Directed, Undirected}}
return has_edge(g, e.source, e.target)
end
@doc raw"""
has_vertex(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Check for a vertex in a graph.
# Examples
The edge graph of a triangle only has 3 vertices.
```jldoctest
julia> triangle = simplex(2);
julia> g = vertex_edge_graph(triangle);
julia> has_vertex(g, 1)
true
julia> has_vertex(g, 4)
false
```
"""
function has_vertex(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
return Polymake._has_vertex(pmg, v-1)
end
@doc raw"""
neighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Return the neighboring vertices of a vertex `v` in a graph `g`. If the graph is
directed, the neighbors reachable via outgoing edges are returned.
# Examples
```jldoctest
julia> g = Graph{Directed}(5);
julia> add_edge!(g, 1, 3);
julia> add_edge!(g, 3, 4);
julia> neighbors(g, 3)
1-element Vector{Int64}:
4
```
"""
function neighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g);
result = Polymake._outneighbors(pmg, v-1)
return [x+1 for x in result]
end
@doc raw"""
degree(g::Graph{T} [, v::Int64]) where {T <: Union{Directed, Undirected}}
Return the degree of the vertex `v` in the graph `g`.
If `v` is missing, return the list of degrees of all vertices.
# Examples
```jldoctest
julia> g = vertex_edge_graph(icosahedron());
julia> degree(g, 1)
5
```
"""
degree(g::Graph, v::Int64) = length(neighbors(g, v))
degree(g::Graph) = [ length(neighbors(g, v)) for v in 1:n_vertices(g) ]
@doc raw"""
inneighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Return the vertices of a graph `g` that have an edge going towards `v`. For an
undirected graph, all neighboring vertices are returned.
# Examples
```jldoctest
julia> g = Graph{Directed}(5);
julia> add_edge!(g, 1, 3);
julia> add_edge!(g, 3, 4);
julia> inneighbors(g, 3)
1-element Vector{Int64}:
1
julia> inneighbors(g, 1)
Int64[]
```
"""
function inneighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g);
result = Polymake._inneighbors(pmg, v-1)
return [x+1 for x in result]
end
@doc raw"""
outneighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Return the vertices of a graph `g` that are target of an edge coming from `v`.
For an undirected graph, all neighboring vertices are returned.
# Examples
```jldoctest
julia> g = Graph{Directed}(5);
julia> add_edge!(g, 1, 3);
julia> add_edge!(g, 3, 4);
julia> outneighbors(g, 3)
1-element Vector{Int64}:
4
julia> outneighbors(g, 4)
Int64[]
```
"""
function outneighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g);
result = Polymake._outneighbors(pmg, v-1)
return [x+1 for x in result]
end
@doc raw"""
all_neighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Return all vertices of a graph `g` that are connected to the vertex `v` via an
edge, independent of the edge direction.
# Examples
```jldoctest
julia> g = Graph{Directed}(5);
julia> add_edge!(g, 1, 3);
julia> add_edge!(g, 3, 4);
julia> all_neighbors(g, 3)
2-element Vector{Int64}:
1
4
julia> all_neighbors(g, 4)
1-element Vector{Int64}:
3
```
"""
function all_neighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g);
result = union(Polymake._inneighbors(pmg, v-1), Polymake._outneighbors(pmg, v-1))
return [x+1 for x in result]
end
@doc raw"""
incidence_matrix(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return an unsigned (boolean) incidence matrix representing a graph `g`.
# Examples
```jldoctest
julia> g = Graph{Directed}(5);
julia> add_edge!(g, 1, 3);
julia> add_edge!(g, 3, 4);
julia> incidence_matrix(g)
5×2 IncidenceMatrix
[1]
[]
[1, 2]
[2]
[]
```
"""
function incidence_matrix(g::Graph{T}) where {T <: Union{Directed, Undirected}}
IncidenceMatrix(Polymake.graph.incidence_matrix(pm_object(g)))
end
@doc raw"""
signed_incidence_matrix(g::Graph{Directed})
Return a signed incidence matrix representing a directed graph `g`.
# Examples
```jldoctest
julia> g = Graph{Directed}(5);
julia> add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,3,4); add_edge!(g,4,5); add_edge!(g,5,1);
julia> signed_incidence_matrix(g)
5×5 Matrix{Int64}:
-1 0 0 0 1
1 -1 0 0 0
0 1 -1 0 0
0 0 1 -1 0
0 0 0 1 -1
```
"""
signed_incidence_matrix(g::Graph{Directed}) = convert(Matrix{Int}, Polymake.graph.signed_incidence_matrix(pm_object(g)))
################################################################################
################################################################################
## Higher order algorithms
################################################################################
################################################################################
@doc raw"""
automorphism_group_generators(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return generators of the automorphism group of the graph `g`.
# Examples
```jldoctest
julia> g = complete_graph(4);
julia> automorphism_group_generators(g)
3-element Vector{PermGroupElem}:
(3,4)
(2,3)
(1,2)
```
"""
function automorphism_group_generators(g::Graph{T}) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g);
result = Polymake.graph.automorphisms(pmg)
return _pm_arr_arr_to_group_generators(result, n_vertices(g))
end
@doc raw"""
automorphism_group(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return the automorphism group of the graph `g`.
# Examples
```jldoctest
julia> g = complete_graph(4);
julia> automorphism_group(g)
Permutation group of degree 4
```
"""
function automorphism_group(g::Graph{T}) where {T <: Union{Directed, Undirected}}
return _gens_to_group(automorphism_group_generators(g))
end
@doc raw"""
shortest_path_dijkstra(g::Graph{T}, s::Int64, t::Int64; reverse::Bool=false) where {T <: Union{Directed, Undirected}}
Compute the shortest path between two vertices in a graph using Dijkstra's
algorithm. All edges are set to have a length of 1. The optional parameter
indicates whether the edges should be considered reversed.
# Examples
```jldoctest
julia> g = Graph{Directed}(3);
julia> add_edge!(g, 1, 2);
julia> add_edge!(g, 2, 3);
julia> add_edge!(g, 3, 1);
julia> shortest_path_dijkstra(g, 3, 1)
2-element Vector{Int64}:
3
1
julia> shortest_path_dijkstra(g, 1, 3)
3-element Vector{Int64}:
1
2
3
julia> shortest_path_dijkstra(g, 3, 1; reverse=true)
3-element Vector{Int64}:
3
2
1
```
"""
function shortest_path_dijkstra(g::Graph{T}, s::Int64, t::Int64; reverse::Bool=false) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
em = Polymake.EdgeMap{T, Int64}(pmg)
for e in edges(g)
Polymake._set_entry(em, src(e)-1, dst(e)-1, 1)
end
result = Polymake._shortest_path_dijkstra(pmg, em, s-1, t-1, !reverse)
return Polymake.to_one_based_indexing(result)
end
@doc raw"""
is_connected(g::Graph{Undirected})
Checks if the undirected graph `g` is connected.
# Examples
```jldoctest
julia> g = Graph{Undirected}(3);
julia> is_connected(g)
false
julia> add_edge!(g, 1, 2);
julia> add_edge!(g, 2, 3);
julia> is_connected(g)
true
```
"""
is_connected(g::Graph{Undirected}) = Polymake.call_function(:graph, :is_connected, pm_object(g))::Bool
function connected_components(g::Graph{Undirected})
im = Polymake.call_function(:graph, :connected_components, pm_object(g))::IncidenceMatrix
return [Vector(Polymake.row(im,i)) for i in 1:Polymake.nrows(im)]
end
@doc raw"""
is_strongly_connected(g::Graph{Directed})
Checks if the directed graph `g` is strongly connected.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> is_strongly_connected(g)
false
julia> add_edge!(g, 2, 1);
julia> is_strongly_connected(g)
true
```
"""
is_strongly_connected(g::Graph{Directed}) = Polymake.call_function(:graph, :is_strongly_connected, pm_object(g))::Bool
@doc raw"""
strongly_connected_components(g::Graph{Directed})
Return the strongly connected components of a directed graph `g`.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> length(strongly_connected_components(g))
2
julia> add_edge!(g, 2, 1);
julia> strongly_connected_components(g)
1-element Vector{Vector{Int64}}:
[1, 2]
```
"""
function strongly_connected_components(g::Graph{Directed})
im = Polymake.call_function(:graph, :strong_components, pm_object(g))::IncidenceMatrix
return [Vector(Polymake.row(im,i)) for i in 1:Polymake.nrows(im)]
end
@doc raw"""
is_weakly_connected(g::Graph{Directed})
Checks if the directed graph `g` is weakly connected.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> is_weakly_connected(g)
true
```
"""
is_weakly_connected(g::Graph{Directed}) = Polymake.call_function(:graph, :is_weakly_connected, pm_object(g))::Bool
@doc raw"""
weakly_connected_components(g::Graph{Directed})
Return the weakly connected components of a directed graph `g`.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> weakly_connected_components(g)
1-element Vector{Vector{Int64}}:
[1, 2]
```
"""
function weakly_connected_components(g::Graph{Directed})
im = Polymake.call_function(:graph, :weakly_connected_components, pm_object(g))::IncidenceMatrix
return [Vector(Polymake.row(im,i)) for i in 1:Polymake.nrows(im)]
end
@doc raw"""
diameter(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Return the diameter of a (strongly) connected (di-)graph `g`.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> weakly_connected_components(g)
1-element Vector{Vector{Int64}}:
[1, 2]
```
"""
function diameter(g::Graph{T}) where {T <: Union{Directed, Undirected}}
if T == Directed && !is_strongly_connected(g) ||
T == Undirected && !is_connected(g)
throw(ArgumentError("The (di-)graph must be (strongly) connected!"))
end
return Polymake.call_function(:graph, :diameter, pm_object(g))::Int
end
@doc raw"""
is_isomorphic(g1::Graph{T}, g2::Graph{T}) where {T <: Union{Directed, Undirected}}
Checks if the graph `g1` is isomorphic to the graph `g2`.
# Examples
```jldoctest
julia> is_isomorphic(vertex_edge_graph(simplex(3)), dual_graph(simplex(3)))
true
julia> is_isomorphic(vertex_edge_graph(cube(3)), dual_graph(cube(3)))
false
```
"""
is_isomorphic(g1::Graph{T}, g2::Graph{T}) where {T <: Union{Directed, Undirected}} = Polymake.graph.isomorphic(pm_object(g1), pm_object(g2))::Bool
@doc raw"""
is_isomorphic_with_permutation(G1::Graph, G2::Graph) -> Bool, Vector{Int}
Return whether `G1` is isomorphic to `G2` as well as a permutation
of the nodes of `G1` such that both graphs agree.
# Examples
```jldoctest
julia> is_isomorphic_with_permutation(vertex_edge_graph(simplex(3)), dual_graph(simplex(3)))
(true, [1, 2, 3, 4])
```
"""
function is_isomorphic_with_permutation(G1::Graph, G2::Graph)
f12 = Polymake.graph.find_node_permutation(G1.pm_graph, G2.pm_graph)
if isnothing(f12)
return false, Vector{Int}()
end
return true, Polymake.to_one_based_indexing(f12)
end
@doc raw"""
_is_equal_up_to_permutation_with_permutation(A1::MatElem, A2::MatElem) -> Bool, Vector{Int}
Return a permutation `I` such that `A1[I,I] == A2` and whether it exists.
The method assumes that both matrices are symmetric, their diagonal entries
are all equal (and so irrelevant) and the off-diagonal entries are either ``0``
or ``1``. It is assumed that `A1` and `A2` are symmetric and
their upper triangular part is ignored.
"""
function _is_equal_up_to_permutation_with_permutation(A1::MatElem, A2::MatElem)
g1 = graph_from_adjacency_matrix(Undirected, A1)
g2 = graph_from_adjacency_matrix(Undirected, A2)
b, T = is_isomorphic_with_permutation(g1, g2)
if b
@assert A1[T, T] == A2
end
return b, T
end
################################################################################
################################################################################
## Standard constructions
################################################################################
################################################################################
@doc raw"""
vertex_edge_graph(p::Polyhedron)
Return the edge graph of a `Polyhedron`, vertices of the graph correspond to
vertices of the polyhedron, there is an edge between two vertices if the
polyhedron has an edge between the corresponding vertices. The resulting graph
is `Undirected`.
If the polyhedron has lineality, then it has no vertices or bounded edges, so the `vertex_edge_graph` will be the empty graph.
In this case, the keyword argument can be used to consider the polyhedron modulo its lineality space.
# Examples
Construct the edge graph of the cube. Like the cube it has 8 vertices and 12
edges.
```jldoctest
julia> c = cube(3);
julia> g = vertex_edge_graph(c);
julia> n_vertices(g)
8
julia> n_edges(g)
12
```
"""
function vertex_edge_graph(p::Polyhedron; modulo_lineality=false)
lineality_dim(p) != 0 && !modulo_lineality && return Graph{Undirected}(0)
og = Graph{Undirected}(pm_object(p).GRAPH.ADJACENCY)
is_bounded(p) || rem_vertices!(og, _ray_indices(pm_object(p)))
return og
end
@doc raw"""
dual_graph(p::Polyhedron)
Return the dual graph of a `Polyhedron`, vertices of the graph correspond to
facets of the polyhedron and there is an edge between two vertices if the
corresponding facets are neighboring, meaning their intersection is a
codimension 2 face of the polyhedron.
For bounded polyhedra containing 0 in the interior this is the same as the edge
graph the polar dual polyhedron.
# Examples
Construct the dual graph of the cube. This is the same as the edge graph of the
octahedron, so it has 6 vertices and 12 edges.