forked from ArsalanKhairani/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstSearch(Recursive).py
More file actions
28 lines (24 loc) · 982 Bytes
/
DepthFirstSearch(Recursive).py
File metadata and controls
28 lines (24 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
################################################################################
## Copyright(c) Arsalan Khairani, 2014 ##
## Recursive Depth First Search ##
## ##
## Depth first graph searching algorithm. Built-in List is used as stack ##
################################################################################
# Function: Search all the nodes of the graph given the starting vertex s.
def DFS(graph, s):
global explored
explored[s] = True
for v in graph[s]:
print (str(s), str(v))
if not explored[v]:
DFS(graph, v)
file = open("graph3.txt")
graph = {}
for line in file:
Array = line.split()
node = int(Array[0])
edges = []
edges = [int(i) for i in Array[1:]]
graph[node] = edges
explored = { i:False for i in graph.keys() }
DFS(graph, 1)