#DFS(G, u)
#u.visited = true
#for each v within G.adj[u]
#if v.visited == false
#DFS(G,v)
#init() {
#For each u within G
#u.visited == false
#For each u within G
#DFS(G,u)
#}
# DFS algorithm in Python
# DFS algorithm
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
graph = {'1': set(['2', '4', '5']),
'2': set(['1', '4', '3']),
'3': set(['2', '4', '6']),
'4': set(['1', '2', '3', '5', '6', '7']),
'5': set(['1', '4', '7', '8']),
'6': set(['3', '4', '7', '10']),
'7': set(['4', '5', '6', '8', '10']),
'8': set(['5', '7', '9']),
'9': set(['8', '10']),
'10': set(['6', '7', '9'])}
dfs(graph, '1')
n = len(graph)
s = pow(n, n-2) #number of spanning tree graphs
print("This is the number of spanning trees: ", s)
python
algorithm
matlab
graph-theory
depth-first-se