Advertisement
STANAANDREY

add and del from graph

Jan 17th, 2023
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import functools
  2.  
  3. g = {1:{2,3,4}, 2:{1,3}, 3:{1,4}, 4:{1,3}}
  4. lst = [(2,4), (3,2)]
  5.  
  6. def addToGraph(graph, lst):
  7.  
  8.     def reducer(acc, elem):
  9.         graph[elem[0]].add(elem[1])
  10.         #graph[elem[1]].add(elem[0])
  11.  
  12.     functools.reduce(reducer, lst, None)
  13.     return graph
  14.  
  15. def delFromGraph(graph, lst):
  16.     def reducer(acc, elem):
  17.         graph[elem[0]].discard(elem[1])
  18.         #graph[elem[1]].discard(elem[0])
  19.     functools.reduce(reducer, lst, None)
  20.     return graph
  21.  
  22. print(addToGraph(g, lst))
  23. print(delFromGraph(g, [(2,1), (3,1)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement