Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0 1 4
- 0 2 3
- 1 4 7
- 5 3 8
- class Node():
- def __init__(self, v, w, next=None):
- self.v = v
- self.w = w
- self.next = next
- ...
- class LinkedList():
- def __init__(self, head=None)
- self.head = head
- def add_node():
- pass
- ...
- 0 -> [1:4]-> [2:3]
- 1 -> [4:7]
- 2 -> []
- 3 -> []
- 4 -> []
- 5 -> [3:8]
- g = [LinkedList_for_0, LinkedList_for_1, ...]
- g = [[] for v in range(number_of_vertex)]
- for f, t, w in edges:
- g[f].add_node(Node(t,w))
- class Node():
- def __init__(self, v, w):
- self.v = v
- self.w = w
- [Node(1,4), Node(2,3)]
- [
- [Node(1,4), Node(2,3)],
- [Node(0,4), Node(4,7)],
- [Node(0,3)],
- [Node(5,8)],
- [Node(1,7)],
- [Node(3,8)]
- ]
- g = [[] for v in range(number_of_vertex)]
- for f,t,w in edges:
- g[f].append(Node(t,w))
- g[t].append(Node(f,w))
- adjacency_list = {}
- for line in open(file):
- from, to, weight = map(int, line.split())
- adjacency_list.setdefault(from, {})[to] = weight
- adjacency_list.setdefault(to, {})[from] = weight # for undircted graph add reverse edges
- import networkx as nx
- G=nx.Graph()
- for line in file:
- a, b, w = map(int, line.strip().split(' '))
- G.add_edge(a, b, weight=w)
- from collections import defaultdict
- adj = defaultdict(list)
- content = open('input.txt', 'r').readlines()
- for line in content:
- u, v, w = map(int, line.strip().split(' '))
- # if edge is on-way
- adj[u].append((v, w))
- # otherwise,
- adj[u].append((v, w))
- adj[v].append((u, w))
Add Comment
Please, Sign In to add comment