@@ -21,6 +21,21 @@ defmodule BitGraph do
21
21
Map . put ( graph , :adjacency , Adjacency . copy ( adjacency , graph . edges ) )
22
22
end
23
23
24
+ @ doc """
25
+ Creates a subgraph on `subgraph_vertices`
26
+ """
27
+ def subgraph ( graph , subgraph_vertices ) do
28
+ Enum . reduce ( BitGraph . vertices ( graph ) , copy ( graph ) ,
29
+ fn existing_vertex , acc ->
30
+ if existing_vertex not in subgraph_vertices do
31
+ BitGraph . delete_vertex ( acc , existing_vertex )
32
+ else
33
+ acc
34
+ end
35
+ end
36
+ )
37
+ end
38
+
24
39
def default_opts ( ) do
25
40
[ max_vertices: 1024 ]
26
41
end
@@ -97,10 +112,8 @@ defmodule BitGraph do
97
112
from_index = V . get_vertex_index ( graph , from )
98
113
to_index = V . get_vertex_index ( graph , to )
99
114
from_index && to_index &&
100
- (
101
115
E . delete_edge ( graph , from_index , to_index )
102
- Map . update ( graph , :edges , % { } , fn edges -> Map . delete ( edges , { from_index , to_index } ) end )
103
- ) || graph
116
+ || graph
104
117
end
105
118
106
119
def delete_edges ( graph , edges ) do
@@ -140,6 +153,10 @@ defmodule BitGraph do
140
153
out_edges ( graph , vertex , fn _from , to , graph -> V . get_vertex ( graph , to ) end )
141
154
end
142
155
156
+ def neighbors ( graph , vertex ) do
157
+ MapSet . union ( out_neighbors ( graph , vertex ) , in_neighbors ( graph , vertex ) )
158
+ end
159
+
143
160
def out_edges ( graph , vertex , edge_fun \\ & default_edge_info / 3 ) do
144
161
edges_impl ( graph , V . get_vertex_index ( graph , vertex ) , edge_fun , :out_edges )
145
162
end
@@ -148,6 +165,10 @@ defmodule BitGraph do
148
165
in_neighbors ( graph , vertex ) |> MapSet . size ( )
149
166
end
150
167
168
+ def degree ( graph , vertex ) do
169
+ in_degree ( graph , vertex ) + out_degree ( graph , vertex )
170
+ end
171
+
151
172
def out_degree ( graph , vertex ) do
152
173
out_neighbors ( graph , vertex ) |> MapSet . size ( )
153
174
end
@@ -156,7 +177,10 @@ defmodule BitGraph do
156
177
neighbor_indices = neighbors_impl ( graph , vertex_index , direction )
157
178
Enum . reduce ( neighbor_indices , MapSet . new ( ) , fn neighbor_index , acc ->
158
179
{ from_vertex , to_vertex } = edge_vertices ( vertex_index , neighbor_index , direction )
159
- MapSet . put ( acc , edge_info_fun . ( from_vertex , to_vertex , graph ) )
180
+ case edge_info_fun . ( from_vertex , to_vertex , graph ) do
181
+ nil -> acc
182
+ edge_info -> MapSet . put ( acc , edge_info )
183
+ end
160
184
end )
161
185
end
162
186
0 commit comments