Skip to content

Commit 6d5842e

Browse files
committed
Update Graph.py
1 parent bee0a35 commit 6d5842e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Graphs/Graph.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,19 @@ def insert_edge(self, u, v, x = None):
121121
"""Inser and return a new Edge from u to v with auxiliary element x"""
122122
e = self.Edge(u, v, x)
123123
self._outgoing[u][v] = e
124-
self.incident_edges[v][u] = e
124+
self.incident_edges[v][u] = e
125+
126+
"""DFS Implementation"""
127+
def DFS(g, u, discovered):
128+
"""Perform DFS on the undiscovered portion of the graph g starting at vertex u
129+
130+
discovered is a dictionary mapping each vertex to the edge that was used to
131+
discover it during thr DFS.
132+
(u should be "discovered' prior to the call.)
133+
Newly discovered vertices will be added to the dictionary as a result"""
134+
135+
for e in g.incident_edges(u): # for every outgoing edge from u
136+
v= e.opposite(u)
137+
if v not in discovered: # v is an unvisted vertex
138+
discovered[v] = e # e is the tree edge that discovered v
139+
DFS(g, v, discovered) #recursively explore from v

0 commit comments

Comments
 (0)