Python/graphs/graph_matrix.py

25 lines
481 B
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
class Graph:
def __init__(self, vertex):
self.vertex = vertex
2019-10-05 05:14:13 +00:00
self.graph = [[0] * vertex for i in range(vertex)]
2018-10-19 12:48:28 +00:00
def add_edge(self, u, v):
self.graph[u - 1][v - 1] = 1
self.graph[v - 1][u - 1] = 1
def show(self):
for i in self.graph:
for j in i:
2019-10-05 05:14:13 +00:00
print(j, end=" ")
print(" ")
2018-10-19 12:48:28 +00:00
g = Graph(100)
2019-10-05 05:14:13 +00:00
g.add_edge(1, 4)
g.add_edge(4, 2)
g.add_edge(4, 5)
g.add_edge(2, 5)
g.add_edge(5, 3)
2018-10-19 12:48:28 +00:00
g.show()