I'm not sure if this is what you are looking for but here is how I though about it.
some_graph = DiGraph()
some_graph.add_edge("a","b")
some_graph.is_directed()
This returns True since there is only an edge in one direction.
some_graph = DiGraph()
some_graph.add_edge("a","b")
some_graph.add_edge("b","a")
some_graph.is_directed()
This returns False since there are edges in both directions with the same weight.
some_graph = DiGraph()
some_graph.add_edge("a","b")
some_graph.add_edge("b","a",2)
some_graph.is_directed()
This returns True since even though there are edges in both directions they have different weights.
Full code:
class DiGraph :
def __init__ ( self ) :
self._adj = {}
def add_node ( self , u ) :
if u not in self._adj :
self._adj [ u ] = []
def add_edge ( self , u , v , weight =1) :
self.add_node ( u )
self.add_node ( v )
self._adj [ u ].append (( v , weight ) )
def is_directed ( self ) :
for u, neigh in self._adj.items():
for n in neigh:
print("u", u, "neigh:", neigh)
if n[0] != u and not ([(node,w) for node,w in self._adj[n[0]] if (node==u and w==n[1])]):
return True
return False