Skip to content
Permalink
Browse files
Create UnweightGraph.py
Q6) Implement an unweighted data graph structure.
  • Loading branch information
raib10 committed Apr 7, 2021
1 parent f34b02c commit 49e75f0b51cf90cc8e24df80b501c3634d8bbe6e
Showing 1 changed file with 28 additions and 0 deletions.
@@ -0,0 +1,28 @@
class Node:
def __init__(self, n):
self.name = n

class Graph:
vertices = {}
edges = []
edge_indices = {}

def add_node(self, node):
if isinstance(node, Node) and node.name not in self.vertices:
self.vertices[node.name] = node
for row in self.edges:
row.append(0)
self.edges.append([0] * (len(self.edges)+1))
self.edge_indices[node.name] = len(self.edge_indices)
return True
else:
return False

def add_edge(self, x, y, weight=1):
if x in self.vertices and y in self.vertices:
self.edges[self.edge_indices[x]][self.edge_indices[y]] = weight
self.edges[self.edge_indices[y]][self.edge_indices[x]] = weight
return True
else:
return False

0 comments on commit 49e75f0

Please sign in to comment.