Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections.abc import Set, Mapping
- from functools import reduce
- graph = {
- "a": {"b", "c"},
- "b": set(),
- "c": set()
- }
- def get_nodes(graph):
- return list(graph.keys())
- def get_edges(graph: Mapping[Set]):
- edges = set()
- def red_graph(acc, elem):
- key, val = elem
- def red_node(acc2, elem2):
- edges.add((key, elem2))
- reduce(red_node, val, None)
- reduce(red_graph, graph.items(), None)
- return edges
- def add_node(graph: Mapping[Set], node):
- if not node in graph:
- graph[node] = set()
- return graph
- def add_edge(graph: Mapping[Set], edge):
- if edge[0] in graph:
- graph[edge[0]].add(edge[1])
- else:
- graph[edge[0]] = {edge[1]}
- if not edge[1] in graph:
- graph[edge[1]] = set()
- return graph
- # graph = add_node(graph, "ab")
- graph = add_edge(graph, ('b', 'c'))
- print(get_nodes(graph))
- print(get_edges(graph))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement