-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathabstract_multigraph.jl
163 lines (116 loc) · 3.99 KB
/
abstract_multigraph.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
using Graphs
using SparseArrays
import Base: show, eltype, copy
import Graphs: nv, has_edge, has_vertex, add_edge!, rem_edge!, rem_vertex!,
rem_vertices!, add_vertex!, add_vertices!, outneighbors, inneighbors, vertices, edges,
adjacency_matrix, src, dst, nv, edgetype
export AbstractMultigraph
# export multype
"""
AbstractMultigraph{T}<:AbstractGraph{T}
An abstract type representing a multigraph.
"""
abstract type AbstractMultigraph{T<:Integer} <:AbstractGraph{T} end
function copy(mg::AbstractMultigraph{T}) where {T} end
function show(io::IO, mg::AbstractMultigraph{T}) where {T}
dir = is_directed(mg) ? "directed" : "undirected"
print(io, "{$(nv(mg)), $(ne(mg))} $(dir) $(T) multigraph")
end
eltype(mg::AbstractMultigraph{T}) where {T<:Integer} = T
multype(mg::AbstractMultigraph) = Int
edgetype(mg::AbstractMultigraph) = MultipleEdge{eltype(mg), multype(mg)}
function nv(mg::AbstractMultigraph) end
function vertices(mg::AbstractMultigraph) end
function adjacency_matrix(mg::AbstractMultigraph) end
function has_edge(mg::AbstractMultigraph, e::AbstractMultipleEdge)
s = src(e)
d = dst(e)
if has_vertex(mg, s) && has_vertex(mg, d)
return mul(mg, s, d) >= mul(e)
end
return false
end
has_edge(mg::AbstractMultigraph, t) = has_edge(mg, MultipleEdge(t))
add_edge!(mg::AbstractMultigraph, t) = add_edge!(mg, MultipleEdge(t))
rem_edge!(mg::AbstractMultigraph, t) = rem_edge!(mg, MultipleEdge(t))
has_edge(mg::AbstractMultigraph, x, y) = has_edge(mg, MultipleEdge(x, y))
add_edge!(mg::AbstractMultigraph, x, y) = add_edge!(mg, MultipleEdge(x, y))
rem_edge!(mg::AbstractMultigraph, x, y) = rem_edge!(mg, MultipleEdge(x, y))
"""
has_edge(mg::AbstractMultigraph, s, d, mul)
Return `true` if `mg` has a multiple edge from `s` to `d` whose multiplicity
is not less than `mul`.
## Examples
```julia
julia> using Graphs, Multigraphs
julia> mg = Multigraph(3);
julia> add_edge!(mg, 1, 2, 2);
julia> has_edge(mg, 1, 2, 3)
false
julia> has_edge(mg, 1, 2, 2)
true
```
"""
has_edge(mg::AbstractMultigraph, x, y, z) = has_edge(mg, MultipleEdge(x, y, z))
"""
add_edge!(mg::AbstractMultigraph, s, d, mul)
Add a multiple edge from `s` to `d` multiplicity `mul`. If there is a multiple
edge from `s` to `d`, it will increase its multiplicity by `mul`.
Return `true` multiple edge was added successfully, otherwise return `false`.
## Examples
```julia
julia> using Graphs, Multigraphs
julia> mg = Multigraph(3);
julia> e = MultipleEdge(1, 2, 1);
julia> add_edge!(mg, e);
julia> ne(mg, true)
1
julia> add_edge!(mg, e);
julia> ne(mg, true)
2
```
"""
add_edge!(mg::AbstractMultigraph, x, y, z) = add_edge!(mg, MultipleEdge(x, y, z))
"""
rem_edge!(mg::AbstractMultigraph, s, d, mul)
Remove the multiplicity of edge from `s` to `d` by `mul` in `mg`, if `mg` has such
a multiple edge.
## Examples
```julia
julia> using Graphs, Multigraphs
julia> mg = Multigraph(3);
julia> add_edge!(mg, 1, 2, 2);
julia> rem_edge!(mg, 1, 2, 3)
false
julia> rem_edge!(mg, 1, 2, 2)
true
```
"""
rem_edge!(mg::AbstractMultigraph, x, y, z) = rem_edge!(mg, MultipleEdge(x, y, z))
has_vertex(mg::AbstractMultigraph, v::Integer) = v in vertices(mg)
rem_vertex!(mg::AbstractMultigraph{T}, v::T) where {T<:Integer} = rem_vertices!(mg, [v])
add_vertex!(mg::AbstractMultigraph{T}) where {T<:Integer} = add_vertices!(mg, one(T))
function outneighbors(mg::AbstractMultigraph, v) end
function inneighbors(mg::AbstractMultigraph, v) end
"""
edges(mg::AbstractMultigraph)
Return a `MultipleEdgeIter` for `mg`.
## Examples
```jltestdoc
julia>
julia> using Graphs, Multigraphs
julia> mg = Multigraph(path_graph(4));
julia> add_edge!(mg, 1, 3, 2);
julia> collect(edges(mg))
4-element Array{Any,1}:
Multiple edge 1 => 2 with multiplicity 1
Multiple edge 1 => 3 with multiplicity 2
Multiple edge 2 => 3 with multiplicity 1
Multiple edge 3 => 4 with multiplicity 1
```
"""
edges(mg::AbstractMultigraph) = MultipleEdgeIter(mg)
"""
mul(mg::AbstractMultigraph, src, dst)
Return the multiplicity of the edge from `src` to `dst`.
"""