Skip to content

Commit b5667e5

Browse files
anirudhajithpoyea
authored andcommitted
Removed the (incorrectly named) redundant file graph_list.py and renamed graph.py to graph_list.py (TheAlgorithms#820)
1 parent c113049 commit b5667e5

File tree

2 files changed

+43
-74
lines changed

2 files changed

+43
-74
lines changed

graphs/graph.py

-44
This file was deleted.

graphs/graph_list.py

+43-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
from __future__ import print_function
2-
3-
4-
class Graph:
5-
def __init__(self, vertex):
6-
self.vertex = vertex
7-
self.graph = [[0] for i in range(vertex)]
8-
9-
def add_edge(self, u, v):
10-
self.graph[u - 1].append(v - 1)
11-
12-
def show(self):
13-
for i in range(self.vertex):
14-
print('%d: '% (i + 1), end=' ')
15-
for j in self.graph[i]:
16-
print('%d-> '% (j + 1), end=' ')
17-
print(' ')
18-
19-
20-
21-
g = Graph(100)
22-
23-
g.add_edge(1,3)
24-
g.add_edge(2,3)
25-
g.add_edge(3,4)
26-
g.add_edge(3,5)
27-
g.add_edge(4,5)
28-
29-
30-
g.show()
1+
#!/usr/bin/python
2+
# encoding=utf8
313

4+
from __future__ import print_function
5+
# Author: OMKAR PATHAK
6+
7+
# We can use Python's dictionary for constructing the graph.
8+
9+
class AdjacencyList(object):
10+
def __init__(self):
11+
self.List = {}
12+
13+
def addEdge(self, fromVertex, toVertex):
14+
# check if vertex is already present
15+
if fromVertex in self.List.keys():
16+
self.List[fromVertex].append(toVertex)
17+
else:
18+
self.List[fromVertex] = [toVertex]
19+
20+
def printList(self):
21+
for i in self.List:
22+
print((i,'->',' -> '.join([str(j) for j in self.List[i]])))
23+
24+
if __name__ == '__main__':
25+
al = AdjacencyList()
26+
al.addEdge(0, 1)
27+
al.addEdge(0, 4)
28+
al.addEdge(4, 1)
29+
al.addEdge(4, 3)
30+
al.addEdge(1, 0)
31+
al.addEdge(1, 4)
32+
al.addEdge(1, 3)
33+
al.addEdge(1, 2)
34+
al.addEdge(2, 3)
35+
al.addEdge(3, 4)
36+
37+
al.printList()
38+
39+
# OUTPUT:
40+
# 0 -> 1 -> 4
41+
# 1 -> 0 -> 4 -> 3 -> 2
42+
# 2 -> 3
43+
# 3 -> 4
44+
# 4 -> 1 -> 3

0 commit comments

Comments
 (0)