In the below code, I have a Depth First Search with the pseudocode and the python code. My first question is how can I call this program in MATLAB?
I can use the following:
system('python DFS.py')
But I want to call it like:
py.DFS();
And store the data in MATLAB, but when I execute the code it says Unable to resolve the name py.DFS.
My second question is, how can I change the code in python to where it takes a MATLAB adjacency list and runs that in the code, instead of putting the adjacency list directly in the python code?
#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)
Make sure the folder containing the DFS.py file is added to your python search path. I think you need to call it in MATLAB as:
visited = py.DFS.dfs(graph, start);
You should initialize graph and start before call the function above. You can create a python list in MATLAB using:
graph = py.list({1.5,3.0,2.5})
Also, your dfs function return values can be accessed in MATLAB.
You can read more at https://uk.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html.