Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a17cd74

Browse files
Added Graph operations
1 parent 61f1aef commit a17cd74

File tree

18 files changed

+207
-8
lines changed

18 files changed

+207
-8
lines changed

‎BinaryTrees/BinaryTree.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
sys.path.append(".")
3-
from Queues.queuesLL import QueueLL
3+
from Queue.queueLL import QueueLL
44

55
class _Node:
66
'''

‎Graphs/graphAM.py‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import numpy as np
2+
import sys
3+
4+
sys.path.append('.')
5+
from Queue.queue import Queue
6+
from Stack.stack import Stack
7+
8+
9+
class Graph:
10+
def __init__(self, vertices, graph_type='directed', weighted = False):
11+
self._vertices = vertices
12+
self._type = graph_type
13+
self._weighted = weighted
14+
self._adjMAT = np.zeros(shape=(vertices, vertices), dtype=np.int8)
15+
16+
self._visited = [False] * self._vertices
17+
18+
def insert_edge(self, u, v, weight=1):
19+
self._adjMAT[u][v] = weight
20+
if self._type == 'undirected':
21+
self._adjMAT[v][u] = weight
22+
23+
def remove_edge(self, u, v):
24+
self._adjMAT[u][v] = 0
25+
if self._type == 'undirected':
26+
self._adjMAT[v][u] = 0
27+
28+
def exist_edge(self, u, v):
29+
return self._adjMAT[u][v] != 0
30+
31+
def vertex_count(self):
32+
return self._vertices
33+
34+
def edge_count(self):
35+
count = 0
36+
for i in range(self._vertices):
37+
for j in range(self._vertices):
38+
if self._adjMAT[i][j] != 0:
39+
count += 1
40+
return count
41+
42+
def vertices(self):
43+
for i in range(self._vertices):
44+
print(i, end=' ')
45+
print()
46+
47+
def edges(self):
48+
for i in range(self._vertices):
49+
for j in range(self._vertices):
50+
if self._adjMAT[i][j] != 0 and self._weighted == True:
51+
print(f'{i} -- {j} = {self._adjMAT[i][j]}')
52+
elif self._adjMAT[i][j] != 0:
53+
print(f'{i} -- {j}')
54+
55+
def outdegree(self, v):
56+
count = 0
57+
for j in range(self._vertices):
58+
if self._adjMAT[v][j] != 0:
59+
count += 1
60+
return count
61+
62+
def indegree(self, v):
63+
count = 0
64+
for i in range(self._vertices):
65+
if self._adjMAT[i][v] != 0:
66+
count += 1
67+
return count
68+
69+
def BFS(self, start_vertext):
70+
i = start_vertext
71+
q = Queue()
72+
visited = [False] * self._vertices
73+
print(i, end=' ')
74+
visited[i] = True
75+
q.enqueue(i)
76+
77+
while not q.isempty():
78+
i = q.dequeue()
79+
for j in range(self._vertices):
80+
if self._adjMAT[i][j] > 0 and visited[j] == False:
81+
print(j, end=' ')
82+
visited[j] = True
83+
q.enqueue(j)
84+
85+
def DFS_iterative(self, start_vertex):
86+
i = start_vertex
87+
s = Stack()
88+
visited = [False] * self._vertices
89+
s.push(i)
90+
while not s.isempty():
91+
i = s.pop()
92+
if (not visited[i]):
93+
print(i, end=' ')
94+
visited[i] = True
95+
for j in range(self._vertices):
96+
if self._adjMAT[i][j] > 0:
97+
if (not visited[j]):
98+
s.push(j)
99+
100+
def DFS_recursive(self, start_vertex):
101+
if self._visited[start_vertex] == False:
102+
print(start_vertex, end=' ')
103+
self._visited[start_vertex] = True
104+
105+
for j in range(self._vertices):
106+
if self._adjMAT[start_vertex][j] > 0 and self._visited[j] == False:
107+
self.DFS_recursive(j)
108+
109+
def display(self):
110+
n_edges = self.edge_count()
111+
112+
if self._type == 'undirected':
113+
n_edges = int(n_edges / 2)
114+
115+
print(self._adjMAT)
116+
print("Vertices: ", self.vertex_count())
117+
print("Edges: ", n_edges)
118+
119+
120+
g = Graph(7, graph_type='directed', weighted = True)
121+
122+
g.insert_edge(0, 1, 20)
123+
g.insert_edge(0, 5, 20)
124+
g.insert_edge(0, 6, 20)
125+
126+
g.insert_edge(1, 0, 40)
127+
g.insert_edge(1, 2, 40)
128+
g.insert_edge(1, 5, 40)
129+
g.insert_edge(1, 6, 40)
130+
131+
g.insert_edge(2, 3, 30)
132+
g.insert_edge(2, 4, 30)
133+
g.insert_edge(2, 6, 30)
134+
135+
g.insert_edge(3, 4, 10)
136+
137+
g.insert_edge(4, 2, 50)
138+
g.insert_edge(4, 5, 50)
139+
140+
g.insert_edge(5, 2, 60)
141+
g.insert_edge(5, 3, 60)
142+
143+
g.insert_edge(6, 3, 70)
144+
145+
g.display()
146+
g.edges()
147+
print("DFS Iterative:")
148+
g.DFS_iterative(0)
149+
print("\nDFS Iterative:")
150+
g.DFS_recursive(0)

‎Graphs/tempCodeRunnerFile.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
# g.insert_edge(2, 1, 10)

‎Heap/__pycache__/heap.cpython-38.pyc‎

3.56 KB
Binary file not shown.
File renamed without changes.
2.71 KB
Binary file not shown.
2.71 KB
Binary file not shown.

‎Queues/queues.py‎ renamed to ‎Queue/queue.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def isempty(self):
1515
'''
1616
Returns True if Queue is empty, otherwise False.
1717
'''
18-
return self.__len__ == 0
18+
return len(self._data) == 0
1919

2020
def enqueue(self, e):
2121
'''

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /