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 619d1d5

Browse files
Three Hundred - Fifty-Two Commit: Implement DFS() function
1 parent 609c5ca commit 619d1d5

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,11 @@ def BFS(self, source_vertex):
115115
visited[j] = 1
116116
q.enqueue(j)
117117

118+
def DFS(self,source_vertex):
119+
if self._visited[source_vertex]==0:
120+
print(source_vertex,end=' ')
121+
self._visited[source_vertex]=1
122+
for j in range(self._vertices):
123+
if self._adjacent_matrix[source_vertex][j]==1 and self._visited[j]==0:
124+
self.DFS(j)
125+

‎Section_12(Graphs)/(6)_graph-breadth-first-search-demo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@
1616
G.insert_edge(5, 2)
1717
G.insert_edge(5, 3)
1818
G.insert_edge(6, 3)
19-
print('Edges:')
20-
G.edge_count()
2119
print('BFS:')
2220
G.BFS(0)

‎Section_12(Graphs)/graph.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@ def BFS(self, source_vertex):
6868
if self._adjacent_matrix[i][j] == 1 and visited[j] == 0:
6969
print(j, end=' ')
7070
visited[j] = 1
71-
q.enqueue(j)
71+
q.enqueue(j)
72+
73+
def DFS(self,source_vertex):
74+
if self._visited[source_vertex]==0:
75+
print(source_vertex,end=' ')
76+
self._visited[source_vertex]=1
77+
for j in range(self._vertices):
78+
if self._adjacent_matrix[source_vertex][j]==1 and self._visited[j]==0:
79+
self.DFS(j)

0 commit comments

Comments
(0)

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