Removed the (incorrectly named) redundant file graph_list.py and renamed graph.py to graph_list.py (#820)

This commit is contained in:
Anirudh Ajith 2019-05-21 11:36:05 +05:30 committed by John Law
parent c1130490d7
commit b5667e5ee9
2 changed files with 36 additions and 67 deletions

View File

@ -1,44 +0,0 @@
#!/usr/bin/python
# encoding=utf8
from __future__ import print_function
# Author: OMKAR PATHAK
# We can use Python's dictionary for constructing the graph.
class AdjacencyList(object):
def __init__(self):
self.List = {}
def addEdge(self, fromVertex, toVertex):
# check if vertex is already present
if fromVertex in self.List.keys():
self.List[fromVertex].append(toVertex)
else:
self.List[fromVertex] = [toVertex]
def printList(self):
for i in self.List:
print((i,'->',' -> '.join([str(j) for j in self.List[i]])))
if __name__ == '__main__':
al = AdjacencyList()
al.addEdge(0, 1)
al.addEdge(0, 4)
al.addEdge(4, 1)
al.addEdge(4, 3)
al.addEdge(1, 0)
al.addEdge(1, 4)
al.addEdge(1, 3)
al.addEdge(1, 2)
al.addEdge(2, 3)
al.addEdge(3, 4)
al.printList()
# OUTPUT:
# 0 -> 1 -> 4
# 1 -> 0 -> 4 -> 3 -> 2
# 2 -> 3
# 3 -> 4
# 4 -> 1 -> 3

View File

@ -1,31 +1,44 @@
#!/usr/bin/python
# encoding=utf8
from __future__ import print_function from __future__ import print_function
# Author: OMKAR PATHAK
# We can use Python's dictionary for constructing the graph.
class Graph: class AdjacencyList(object):
def __init__(self, vertex): def __init__(self):
self.vertex = vertex self.List = {}
self.graph = [[0] for i in range(vertex)]
def add_edge(self, u, v): def addEdge(self, fromVertex, toVertex):
self.graph[u - 1].append(v - 1) # check if vertex is already present
if fromVertex in self.List.keys():
self.List[fromVertex].append(toVertex)
else:
self.List[fromVertex] = [toVertex]
def show(self): def printList(self):
for i in range(self.vertex): for i in self.List:
print('%d: '% (i + 1), end=' ') print((i,'->',' -> '.join([str(j) for j in self.List[i]])))
for j in self.graph[i]:
print('%d-> '% (j + 1), end=' ')
print(' ')
if __name__ == '__main__':
al = AdjacencyList()
al.addEdge(0, 1)
al.addEdge(0, 4)
al.addEdge(4, 1)
al.addEdge(4, 3)
al.addEdge(1, 0)
al.addEdge(1, 4)
al.addEdge(1, 3)
al.addEdge(1, 2)
al.addEdge(2, 3)
al.addEdge(3, 4)
al.printList()
g = Graph(100) # OUTPUT:
# 0 -> 1 -> 4
g.add_edge(1,3) # 1 -> 0 -> 4 -> 3 -> 2
g.add_edge(2,3) # 2 -> 3
g.add_edge(3,4) # 3 -> 4
g.add_edge(3,5) # 4 -> 1 -> 3
g.add_edge(4,5)
g.show()