Skip to content

Commit 4c3aea8

Browse files
committed
graph implementations in Python, and updated LinkedLists
1 parent eeaf173 commit 4c3aea8

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

LinkedLists.py

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def find (self, d):
6161
myList.add(5)
6262
myList.add(8)
6363
myList.add(12)
64+
print("size="+str(myList.get_size()))
6465
myList.remove(8)
66+
print("size="+str(myList.get_size()))
6567
print(myList.remove(12))
68+
print("size="+str(myList.get_size()))
6669
print(myList.find(5))

graph_adjacency-list.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Vertex:
2+
def __init__(self, n):
3+
self.name = n
4+
self.neighbors = list()
5+
6+
def add_neighbor(self, v):
7+
if v not in self.neighbors:
8+
self.neighbors.append(v)
9+
self.neighbors.sort()
10+
11+
class Graph:
12+
vertices = {}
13+
14+
def add_vertex(self, vertex):
15+
if isinstance(vertex, Vertex) and vertex.name not in self.vertices:
16+
self.vertices[vertex.name] = vertex
17+
return True
18+
else:
19+
return False
20+
21+
def add_edge(self, u, v):
22+
if u in self.vertices and v in self.vertices:
23+
for key, value in self.vertices.items():
24+
if key == u:
25+
value.add_neighbor(v)
26+
if key == v:
27+
value.add_neighbor(u)
28+
return True
29+
else:
30+
return False
31+
32+
def print_graph(self):
33+
for key in sorted(list(self.vertices.keys())):
34+
print(key + str(self.vertices[key].neighbors))
35+
36+
g = Graph()
37+
# print(str(len(g.vertices)))
38+
a = Vertex('A')
39+
g.add_vertex(a)
40+
g.add_vertex(Vertex('B'))
41+
for i in range(ord('A'), ord('K')):
42+
g.add_vertex(Vertex(chr(i)))
43+
44+
edges = ['AB', 'AE', 'BF', 'CG', 'DE', 'DH', 'EH', 'FG', 'FI', 'FJ', 'GJ', 'HI']
45+
for edge in edges:
46+
g.add_edge(edge[:1], edge[1:])
47+
48+
g.print_graph()

graph_adjacency-matrix.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# implementation of an undirected graph using Adjacency Matrix, with weighted or unweighted edges
2+
class Vertex:
3+
def __init__(self, n):
4+
self.name = n
5+
6+
class Graph:
7+
vertices = {}
8+
edges = []
9+
edge_indices = {}
10+
11+
def add_vertex(self, vertex):
12+
if isinstance(vertex, Vertex) and vertex.name not in self.vertices:
13+
self.vertices[vertex.name] = vertex
14+
for row in self.edges:
15+
row.append(0)
16+
self.edges.append([0] * (len(self.edges)+1))
17+
self.edge_indices[vertex.name] = len(self.edge_indices)
18+
return True
19+
else:
20+
return False
21+
22+
def add_edge(self, u, v, weight=1):
23+
if u in self.vertices and v in self.vertices:
24+
self.edges[self.edge_indices[u]][self.edge_indices[v]] = weight
25+
self.edges[self.edge_indices[v]][self.edge_indices[u]] = weight
26+
return True
27+
else:
28+
return False
29+
30+
def print_graph(self):
31+
for v, i in sorted(self.edge_indices.items()):
32+
print(v + ' ', end='')
33+
for j in range(len(self.edges)):
34+
print(self.edges[i][j], end='')
35+
print(' ')
36+
37+
g = Graph()
38+
# print(str(len(g.vertices)))
39+
a = Vertex('A')
40+
g.add_vertex(a)
41+
g.add_vertex(Vertex('B'))
42+
for i in range(ord('A'), ord('K')):
43+
g.add_vertex(Vertex(chr(i)))
44+
45+
edges = ['AB', 'AE', 'BF', 'CG', 'DE', 'DH', 'EH', 'FG', 'FI', 'FJ', 'GJ', 'HI']
46+
for edge in edges:
47+
g.add_edge(edge[:1], edge[1:])
48+
49+
g.print_graph()

0 commit comments

Comments
 (0)