class base graph[readonly data vertice type, readonly data edge type]
extends debuggable
implements graph[vertice type, edge type]
private class edge[readonly data vertice type, readonly data edge type]
implements immutable data
edge(vertice type from, vertice type to, edge type the source)
this • from = from
this • to = to
this • the source = the source
protected equivalence relation[vertice type] equivalence
private dictionary[vertice type, set[edge[vertice type, edge type]]] all edges
overload base graph()
this(runtime utildefault equivalence !> readonly value !> equivalence relation[vertice type])
override readonly set[vertice type] vertices()
return all edgeskeys()
override void add edge(vertice type from, vertice type to, edge type the source)
new edge : edge[vertice type, edge type] • new(from, to, the source)
if outgoing edges is_not null
outgoing edgesadd(new edge)
else
new outgoing edgesadd(new edge)
all edgesput(from, new outgoing edges)
override immutable set[vertice type] adjacent(vertice type from) pure
edge set : all edgesget(from)
if edge set is null
return empty[vertice type] • new()
for (edge : edge setelements)
adjacent verticesadd(edgeto)
return adjacent verticesfrozen copy()
override boolean introduces cycle(vertice type from, vertice type to) pure
if equivalence(from, to)
return true
visitedadd(from)
visitedadd(to)
for (considered vertice : considered)
for (target vertice : adjacent(considered vertice) • elements)
if visitedcontains(target vertice)
if equivalence(target vertice, from)
return true
else
consideredappend(target vertice)
visitedadd(target vertice)
return false