1
1
"""
2
2
MetaGraph{
3
3
Code<:Integer,
4
+ Graph<:AbstractGraph{Code},
4
5
Label,
5
- Graph,
6
6
VertexData,
7
7
EdgeData,
8
8
GraphData,
9
9
WeightFunction,
10
- Weight<:Real
10
+ Weight
11
11
} <: AbstractGraph{Code}
12
12
13
13
A graph type with custom vertex labels containing vertex-, edge- and graph-level metadata.
@@ -19,13 +19,20 @@ It is recommended not to set `Label` to an integer type, so as to avoid confusio
19
19
- `graph::Graph`: underlying, data-less graph with vertex indices of type `Code`
20
20
- `vertex_labels::Dict{Code,Label}`: dictionary mapping vertex codes to vertex labels
21
21
- `vertex_properties::Dict{Label,Tuple{Code,VertexData}}`: dictionary mapping vertex labels to vertex codes & data
22
- - `edge_data::Dict{Tuple{Label,Label},EdgeData}`: dictionary mapping edge labels such as `(label_u, label_v)` to edge metadata
23
- - `graph_data::GraphData`: graph metadata
24
- - `weight_function::WeightFunction`: function defining edge weight from edge metadata
25
- - `default_weight::Weight`: default weight for the edges
22
+ - `edge_data::Dict{Tuple{Label,Label},EdgeData}`: dictionary mapping edge labels such as `(label_u, label_v)` to edge data
23
+ - `graph_data::GraphData`: global data for the graph object
24
+ - `weight_function::WeightFunction`: function computing edge weight from edge data, its output must have the same type as `default_weight`
25
+ - `default_weight::Weight`: default weight used when an edge doesn't exist
26
26
"""
27
27
struct MetaGraph{
28
- Code<: Integer ,Label,Graph,VertexData,EdgeData,GraphData,WeightFunction,Weight<: Real
28
+ Code<: Integer ,
29
+ Graph<: AbstractGraph{Code} ,
30
+ Label,
31
+ VertexData,
32
+ EdgeData,
33
+ GraphData,
34
+ WeightFunction,
35
+ Weight,
29
36
} <: AbstractGraph{Code}
30
37
graph:: Graph
31
38
vertex_labels:: Dict{Code,Label}
@@ -36,68 +43,107 @@ struct MetaGraph{
36
43
default_weight:: Weight
37
44
end
38
45
46
+ """
47
+ MetaGraph(
48
+ graph,
49
+ vertices_description,
50
+ edges_description,
51
+ graph_data=nothing,
52
+ weight_function=edge_data -> 1.0,
53
+ default_weight=1.0,
54
+ )
55
+
56
+ Construct a non-empty `MetaGraph` based on lists of vertices and edges with their labels and data.
57
+
58
+ These lists must be constructed as follows:
59
+ - `vertices_description` is a vector of pairs `label => data` (the code of a vertex will correspond to its rank in the list)
60
+ - `edges_description` is a vector of pairs `(label1, label2) => data`
61
+
62
+ Furthermore, they must be coherent with the `graph` argument, i.e. describe the same set of vertices and edges.
63
+ """
64
+ function MetaGraph (
65
+ graph:: AbstractGraph{Code} ,
66
+ vertices_description:: Vector{Pair{Label,VertexData}} ,
67
+ edges_description:: Vector{Pair{Tuple{Label,Label},EdgeData}} ,
68
+ graph_data= nothing ,
69
+ weight_function= edge_data -> 1.0 ,
70
+ default_weight= 1.0 ,
71
+ ) where {Code,Label,VertexData,EdgeData}
72
+ # Construct vertex data
73
+ @assert length (vertices_description) == nv (graph)
74
+ vertex_labels = Dict {Code,Label} ()
75
+ vertex_properties = Dict {Label,Tuple{Code,VertexData}} ()
76
+ for (code, (label, data)) in enumerate (vertices_description)
77
+ vertex_labels[code] = label
78
+ vertex_properties[label] = (code, data)
79
+ end
80
+ # Construct edge data
81
+ @assert length (edges_description) == ne (graph)
82
+ for ((label_1, label_2), _) in edges_description
83
+ code_1 = vertex_properties[label_1][1 ]
84
+ code_2 = vertex_properties[label_2][1 ]
85
+ @assert has_edge (graph, code_1, code_2)
86
+ end
87
+ edge_data = Dict {Tuple{Label,Label},EdgeData} ()
88
+ for ((label_1, label_2), data) in edges_description
89
+ edge_data[label_1, label_2] = data
90
+ end
91
+ return MetaGraph (
92
+ graph,
93
+ vertex_labels,
94
+ vertex_properties,
95
+ edge_data,
96
+ graph_data,
97
+ weight_function,
98
+ default_weight,
99
+ )
100
+ end
101
+
39
102
"""
40
103
MetaGraph(
41
104
graph;
42
- Label = Symbol,
43
- VertexData = Nothing,
44
- EdgeData = Nothing,
45
- graph_data = nothing,
46
- weight_function = edge_data -> 1.0,
47
- default_weight = 1.0
105
+ Label= Symbol,
106
+ VertexData= Nothing,
107
+ EdgeData= Nothing,
108
+ graph_data= nothing,
109
+ weight_function= edge_data -> 1.0,
110
+ default_weight= 1.0
48
111
)
49
112
50
113
Construct an empty `MetaGraph` with the given metadata types and weights.
114
+
115
+ !!! danger "Warning"
116
+ This constructor is not type-stable, it is only there for convenience.
51
117
"""
52
118
function MetaGraph (
53
119
graph:: AbstractGraph{Code} ;
54
120
Label= Symbol,
55
121
VertexData= Nothing,
56
122
EdgeData= Nothing,
57
123
graph_data= nothing ,
58
- weight_function= edata -> 1.0 ,
124
+ weight_function= edge_data -> 1.0 ,
59
125
default_weight= 1.0 ,
60
126
) where {Code}
127
+ @assert nv (graph) == 0
61
128
if Label <: Integer
62
129
@warn " Constructing a MetaGraph with integer labels is not advised."
63
- elseif nv (graph) > 0
64
- @warn " Constructing a MetaGraph with a nonempty underlying graph is not advised."
65
130
end
131
+ vertex_labels = Dict {Code,Label} ()
132
+ vertex_properties = Dict {Label,Tuple{Code,VertexData}} ()
133
+ edge_data = Dict {Tuple{Label,Label},EdgeData} ()
66
134
return MetaGraph (
67
135
graph,
68
- Dict {Code,Label} () ,
69
- Dict {Label,Tuple{Code,VertexData}} () ,
70
- Dict {Tuple{Label,Label},EdgeData} () ,
136
+ vertex_labels ,
137
+ vertex_properties ,
138
+ edge_data ,
71
139
graph_data,
72
140
weight_function,
73
141
default_weight,
74
142
)
75
143
end
76
144
77
- function Base. zero (
78
- meta_graph:: MetaGraph{Code,Label,Graph,VertexData,EdgeData}
79
- ) where {Code,Label,Graph,VertexData,EdgeData}
80
- return MetaGraph (
81
- Graph ();
82
- Label= Label,
83
- VertexData= VertexData,
84
- EdgeData= EdgeData,
85
- graph_data= meta_graph. graph_data,
86
- weight_function= meta_graph. weight_function,
87
- default_weight= meta_graph. default_weight,
88
- )
89
- end
90
-
91
- function Base.:(== )(meta_graph_1:: MetaGraph , meta_graph_2:: MetaGraph )
92
- return meta_graph_1. graph == meta_graph_2. graph
93
- end
94
-
95
- function Base. copy (meta_graph:: MetaGraph )
96
- return deepcopy (meta_graph)
97
- end
98
-
99
145
function Base. show (
100
- io:: IO , meta_graph:: MetaGraph{<:Any,Label, <:Any,VertexData,EdgeData}
146
+ io:: IO , meta_graph:: MetaGraph{<:Any,<:Any,Label ,VertexData,EdgeData}
101
147
) where {Label,VertexData,EdgeData}
102
148
print (
103
149
io,
0 commit comments