Guest User

Untitled

a guest
Apr 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. 0 1 4
  2. 0 2 3
  3. 1 4 7
  4. 5 3 8
  5.  
  6. class Node():
  7. def __init__(self, v, w, next=None):
  8. self.v = v
  9. self.w = w
  10. self.next = next
  11. ...
  12.  
  13. class LinkedList():
  14. def __init__(self, head=None)
  15. self.head = head
  16.  
  17. def add_node():
  18. pass
  19. ...
  20.  
  21. 0 -> [1:4]-> [2:3]
  22. 1 -> [4:7]
  23. 2 -> []
  24. 3 -> []
  25. 4 -> []
  26. 5 -> [3:8]
  27.  
  28. g = [LinkedList_for_0, LinkedList_for_1, ...]
  29.  
  30. g = [[] for v in range(number_of_vertex)]
  31. for f, t, w in edges:
  32. g[f].add_node(Node(t,w))
  33.  
  34. class Node():
  35. def __init__(self, v, w):
  36. self.v = v
  37. self.w = w
  38.  
  39. [Node(1,4), Node(2,3)]
  40.  
  41. [
  42. [Node(1,4), Node(2,3)],
  43. [Node(0,4), Node(4,7)],
  44. [Node(0,3)],
  45. [Node(5,8)],
  46. [Node(1,7)],
  47. [Node(3,8)]
  48. ]
  49.  
  50. g = [[] for v in range(number_of_vertex)]
  51. for f,t,w in edges:
  52. g[f].append(Node(t,w))
  53. g[t].append(Node(f,w))
  54.  
  55. adjacency_list = {}
  56. for line in open(file):
  57. from, to, weight = map(int, line.split())
  58. adjacency_list.setdefault(from, {})[to] = weight
  59. adjacency_list.setdefault(to, {})[from] = weight # for undircted graph add reverse edges
  60.  
  61. import networkx as nx
  62.  
  63. G=nx.Graph()
  64.  
  65. for line in file:
  66. a, b, w = map(int, line.strip().split(' '))
  67. G.add_edge(a, b, weight=w)
  68.  
  69. from collections import defaultdict
  70.  
  71. adj = defaultdict(list)
  72.  
  73. content = open('input.txt', 'r').readlines()
  74.  
  75. for line in content:
  76. u, v, w = map(int, line.strip().split(' '))
  77. # if edge is on-way
  78. adj[u].append((v, w))
  79. # otherwise,
  80. adj[u].append((v, w))
  81. adj[v].append((u, w))
Add Comment
Please, Sign In to add comment