Created
January 15, 2017 10:03
-
-
Save devpruthvi/ba2d9cd7887892e6fb45b5169c441136 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
class Vertex: | |
def __init__(self, node): | |
self.id = node | |
self.adjacent = {} | |
def __str__(self): | |
return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent]) | |
def add_neighbor(self, neighbor, weight=0): | |
self.adjacent[neighbor] = weight | |
def get_connections(self): | |
return self.adjacent.keys() | |
def get_id(self): | |
return self.id | |
def get_weight(self, neighbor): | |
return self.adjacent[neighbor] | |
class Graph: | |
def __init__(self): | |
self.vert_dict = {} | |
self.num_vertices = 0 | |
def __iter__(self): | |
return iter(self.vert_dict.values()) | |
def add_vertex(self, node): | |
self.num_vertices = self.num_vertices + 1 | |
new_vertex = Vertex(node) | |
self.vert_dict[node] = new_vertex | |
return new_vertex | |
def get_vertex(self, n): | |
if n in self.vert_dict: | |
return self.vert_dict[n] | |
else: | |
return None | |
def add_edge(self, frm, to, cost=0): | |
if frm not in self.vert_dict: | |
self.add_vertex(frm) | |
if to not in self.vert_dict: | |
self.add_vertex(to) | |
self.vert_dict[frm].add_neighbor(self.vert_dict[to], cost) | |
self.vert_dict[to].add_neighbor(self.vert_dict[frm], cost) | |
def get_vertices(self): | |
return self.vert_dict.keys() | |
numTests = int(input()) + 1 | |
for testcase in range(1, numTests): | |
print(" ---------------- ", testcase , " ----------------- ") | |
n, m, k = [int(x) for x in input().split()] | |
families_to_serve = [] | |
G = Graph() | |
graph = [] | |
for each in range(m): | |
a, b, g = [int(x) for x in input().split()] | |
G.add_edge(str(a), str(b), cost=g) | |
for each in range(k): | |
si, di = [int(x) for x in input().split()] | |
families_to_serve.append([si, di]) | |
print(families_to_serve) | |
for v in G: | |
for w in v.get_connections(): | |
vid = v.get_id() | |
wid = w.get_id() | |
print('( %s , %s, %3d)' % (vid, wid, v.get_weight(w))) | |
for v in G: | |
print('g.vert_dict[%s]=%s' % (v.get_id(), G.vert_dict[v.get_id()])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment