-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #856 from AlgebraicJulia/export-named-graphs
Export named graphs from `Catlab.Graphs`
- Loading branch information
Showing
10 changed files
with
140 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Searching | ||
module GraphSearching | ||
export bfs_parents, bfs_tree, dfs_parents, dfs_tree | ||
|
||
using ACSets | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" Extends the basic graph types with vertex and/or edge names. | ||
Naming vertices and edges and looking them up by name is a common requirement. | ||
This module provides a simple interface and default graph types for named | ||
graphs. Names are understood to be unique within the graph but are *not* assumed | ||
to be strings or symbols. | ||
""" | ||
module NamedGraphs | ||
export vertex_name, edge_name, vertex_named, edge_named, | ||
AbstractNamedGraph, NamedGraph | ||
|
||
using ACSets | ||
using ...GATs, ..BasicGraphs | ||
|
||
# Names interface | ||
################# | ||
|
||
""" Name of a vertex in a graph. | ||
By default, the name of a vertex is its ID. | ||
""" | ||
vertex_name(g::HasVertices, v) = v | ||
|
||
""" Name of an edge in a graph. | ||
By default, the name of an edge is its ID. | ||
""" | ||
edge_name(g::HasGraph, e) = e | ||
|
||
""" Get vertex in graph with given name. | ||
""" | ||
function vertex_named(g::HasVertices, name) | ||
@assert has_vertex(g, name) | ||
name | ||
end | ||
|
||
""" Get edge in graph with given name. | ||
""" | ||
function edge_named(g::HasGraph, name) | ||
@assert has_edge(g, name) | ||
name | ||
end | ||
|
||
# Named graphs | ||
############## | ||
|
||
@present SchNamedGraph <: SchGraph begin | ||
VName::AttrType | ||
EName::AttrType | ||
vname::Attr(V, VName) | ||
ename::Attr(E, EName) | ||
end | ||
|
||
""" Abstract type for graph with named vertices and edges. | ||
""" | ||
@abstract_acset_type AbstractNamedGraph <: AbstractGraph | ||
|
||
""" Graph with named vertices and edges. | ||
""" | ||
@acset_type NamedGraph(SchNamedGraph, index=[:src,:tgt,:ename], | ||
unique_index=[:vname]) <: AbstractNamedGraph | ||
|
||
vertex_name(g::AbstractNamedGraph, args...) = subpart(g, args..., :vname) | ||
edge_name(g::AbstractNamedGraph, args...) = subpart(g, args..., :ename) | ||
|
||
vertex_named(g::AbstractNamedGraph, name) = only(incident(g, name, :vname)) | ||
edge_named(g::AbstractNamedGraph, name)= only(incident(g, name, :ename)) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module TestNamedGraphs | ||
using Test | ||
|
||
using Catlab.Graphs | ||
|
||
# Basic graphs | ||
############## | ||
|
||
g = path_graph(Graph, 3) | ||
@test vertex_name(g, 2) == 2 | ||
@test vertex_named(g, 2) == 2 | ||
@test edge_name(g, 1) == 1 | ||
@test edge_named(g, 1) == 1 | ||
|
||
# Named graphs | ||
############## | ||
|
||
g = path_graph(NamedGraph{Symbol,Symbol}, 3, | ||
V=(vname=[:x,:y,:z],), E=(ename=[:f,:g],)) | ||
@test vertex_name(g, 2) == :y | ||
@test vertex_named(g, :y) == 2 | ||
@test edge_name(g, 1) == :f | ||
@test edge_named(g, :f) == 1 | ||
|
||
end |