In [2]:
require("Graphs")
using Graphs
function getParents(g, vertex)
res = Set()
for edge in g.edges
s = edge.source
t = edge.target
if (t.label == vertex) push!(res, s)
end
end
return res
end
function getChildren(g, vertex)
res = Set()
for edge in g.edges
s = edge.source
t = edge.target
if (s.label == vertex) push!(res, t)
end
end
return res
end
Out[2]:
In [3]:
pairs = [(1,3), (2,3), (3,4), (3,5)]
gd = graph(ExVertex[], ExEdge{ExVertex}[], is_directed=false)
map((x) -> add_vertex!(gd, string(x)), 1:5)
V = vertices(gd)
map((edg) -> add_edge!(gd, V[edg[1]], V[edg[2]]), pairs)
# GenericIncidenceList is best for the graph
priority = topological_sort_by_dfs(gd)
for vertex in priority
idx = vertex.index
# can now access in V[idx]
end
# So you just go through it topologically
#eds = Edge{Int}[Edge(i,p[1],p[2]) for (i,p) in enumerate(pairs)]
#gd = simple_edgelist(5, eds)
#gd = simple_inclist(length(pairs))
#for i = 1 : length(pairs)
# a = pairs[i]
# add_edge!(gd, a[1], a[2]) # add edge
#end
In [4]:
type FactorGraph
Graph::GenericGraph{ExVertex,ExEdge{ExVertex},Array{ExVertex,1},Array{ExEdge{ExVertex},1},Array{Array{ExEdge{ExVertex},1},1}}
Variables #is a list of hashmaps: Each one contains the
Factors #
end
In [9]:
In [168]:
edges(gd)
Out[168]:
In [186]:
In [187]:
gd.edges
Out[187]:
In [189]:
typeof(gd)
Out[189]:
In [164]:
getChildren(g2, "3")
Out[164]:
In [150]:
haskey(u, "label")