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 8bf43eb

Browse files
Three Hundred - Fifty-Three Commit: Create graph-depth-first-search-demo.py
1 parent 619d1d5 commit 8bf43eb

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

‎Section_12(Graphs)/(1)_graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Graph:
5050
def __init__(self, vertices):
5151
self._vertices = vertices
5252
self._adjacent_matrix = np.zeros((vertices, vertices))
53+
self._visited = [0] * self._vertices
5354

5455
def insert_edge(self, u, v, weight_edge = 1):
5556
self._adjacent_matrix[u][v] = weight_edge
@@ -103,16 +104,15 @@ def display_adjacent_matrix(self):
103104
def BFS(self, source_vertex):
104105
i = source_vertex
105106
q = LinkedListQueue()
106-
visited = [0] * self._vertices
107107
print(i, end=' ')
108-
visited[i] = 1
108+
self._visited[i] = 1
109109
q.enqueue(i)
110110
while not q.is_empty():
111111
i = q.dequeue()
112112
for j in range(self._vertices):
113-
if self._adjacent_matrix[i][j] == 1 and visited[j] == 0:
113+
if self._adjacent_matrix[i][j] == 1 and self._visited[j] == 0:
114114
print(j, end=' ')
115-
visited[j] = 1
115+
self._visited[j] = 1
116116
q.enqueue(j)
117117

118118
def DFS(self,source_vertex):

‎Section_12(Graphs)/(7)_graph-depth-first-search-demo.py

Whitespace-only changes.

‎Section_12(Graphs)/graph.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Graph:
55
def __init__(self, vertices):
66
self._vertices = vertices
77
self._adjacent_matrix = np.zeros((vertices, vertices))
8+
self._visited = [0] * self._vertices
89

910
def insert_edge(self, u, v, weight_edge = 1):
1011
self._adjacent_matrix[u][v] = weight_edge
@@ -58,16 +59,16 @@ def display_adjacent_matrix(self):
5859
def BFS(self, source_vertex):
5960
i = source_vertex
6061
q = LinkedListQueue()
61-
visited= [0] *self._vertices
62+
6263
print(i, end=' ')
63-
visited[i] = 1
64+
self._visited[i] = 1
6465
q.enqueue(i)
6566
while not q.is_empty():
6667
i = q.dequeue()
6768
for j in range(self._vertices):
68-
if self._adjacent_matrix[i][j] == 1 and visited[j] == 0:
69+
if self._adjacent_matrix[i][j] == 1 and self._visited[j] == 0:
6970
print(j, end=' ')
70-
visited[j] = 1
71+
self._visited[j] = 1
7172
q.enqueue(j)
7273

7374
def DFS(self,source_vertex):

0 commit comments

Comments
(0)

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