3
I’m studying about graph theory, more specifically about vertices which are units representing a given node of a graph. However, there are still some doubts regarding the vertices.
See this illustration graph in Python:
graph = {
'A': ['B', 'C', 'D'],
'B': ['A'],
'C': ['A', 'D'],
'D': ['A', 'C']
}
Note that it is an undirected graph, the dictionary holds the path to each vertex.
My doubt is related to the vertex, if it is possible to make it represent a object or a class. And the object that I would like my vertex to represent would be the kind of class Ambiente
:
class Ambiente:
def __init__(self, titulo, descricao, id):
self.titulo = titulo
self.descricao = descricao
self.id = id
I tried to associate the content in the dictionary:
ambientes = {
'A': Ambiente('Sala de maquinas',
"A sala contém várias maquinas industriais, todas parecem não funcionar por anos. O som que é emitido dela é fantasmagórico e causa um estado de panico e alerta, como se a qualquer momento alguma entidade fosse emergir daquela pilha de metal amaldiçoada. Há poucos ruídos, no entanto, não há sinal de vida alguma.",
'A'
)
}
But it does not seem to be effective in this way, especially when I need to navigate the paths of the graph. Therefore, I would like my question below to be answered.
Question
How could I associate objects to the vertices of the graph?
The
for
that builds thestring
is basically ajoin
and can therefore be replaced bystring += ', '.join(map(lambda x: x.id, value))
. Personally I replace everything inside the firstfor
by a singleprint
, thus:print(" {}: [{}]".format(key.id, ', '.join(map(lambda x: x.id, value))))
. In addition to thestr
which are also not necessary becausekey.id
is already astring
– Isac
@Isac, thanks for the suggestion :) . I even edited my reply.
– AlexCiuffa