Skip to content

Commit 70a04eb

Browse files
authored
Add pre_order_dfs_vertices
2 parents 35be64d + f407ac5 commit 70a04eb

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/Graphs/abstractgraph.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ end
261261
return post_order_dfs_vertices(dfs_tree_graph, root_vertex)
262262
end
263263

264+
@traitfn function pre_order_dfs_vertices(graph::::(!IsDirected), root_vertex)
265+
dfs_tree_graph = dfs_tree(graph, root_vertex)
266+
return pre_order_dfs_vertices(dfs_tree_graph, root_vertex)
267+
end
268+
264269
@traitfn function post_order_dfs_edges(graph::::(!IsDirected), root_vertex)
265270
dfs_tree_graph = dfs_tree(graph, root_vertex)
266271
return post_order_dfs_edges(dfs_tree_graph, root_vertex)
@@ -336,6 +341,10 @@ end
336341
return [node.vertex for node in PostOrderDFS(TreeGraph(graph, root_vertex))]
337342
end
338343

344+
@traitfn function pre_order_dfs_vertices(graph::::IsDirected, root_vertex)
345+
return [node.vertex for node in PreOrderDFS(TreeGraph(graph, root_vertex))]
346+
end
347+
339348
# Traverse the tree using a [post-order depth-first search](https://en.wikipedia.org/wiki/Tree_traversal#Depth-first_search), returning the edges where the source is the current vertex and the destination is the parent vertex.
340349
# Assumes the graph is a [rooted directed tree](https://en.wikipedia.org/wiki/Tree_(graph_theory)#Rooted_tree).
341350
# Returns a list of edges directed **towards the root vertex**!

src/NamedGraphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export NamedGraph,
168168
path_digraph,
169169
path_graph,
170170
periphery,
171+
pre_order_dfs_vertices,
171172
post_order_dfs_vertices,
172173
post_order_dfs_edges,
173174
radius,

test/test_abstractgraph.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ using NamedGraphs
3939
@test vertex_path(ng2, (1, 1, 2), (1, 1, 1)) == [(1, 1, 2), (1, 1), (1, 1, 1)]
4040
@test edge_path(ng2, (1, 1, 2), (1, 1, 1)) ==
4141
[net2((1, 1, 2), (1, 1)), net2((1, 1), (1, 1, 1))]
42+
# Test DFS traversals
43+
@test post_order_dfs_vertices(ng2, (1,)) ==
44+
[(1, 1, 1), (1, 1, 2), (1, 1), (1, 2, 1), (1, 2, 2), (1, 2), (1,)]
45+
@test pre_order_dfs_vertices(ng2, (1,)) ==
46+
[(1,), (1, 1), (1, 1, 1), (1, 1, 2), (1, 2), (1, 2, 1), (1, 2, 2)]
4247

4348
# directed trees
4449
dg1 = dfs_tree(g1, 5)

0 commit comments

Comments
 (0)