How could I change this Python code to Matlab?

This is python code. I want to write this code in matlab. is there any tools for that?
# Python program to detect cycle
# in a graph
from collections import defaultdict
class Graph():
def __init__(self,vertices):
self.graph = defaultdict(list)
self.V = vertices
def addEdge(self,u,v):
self.graph[u].append(v)
def isCyclicUtil(self, v, visited, recStack):
# Mark current node as visited and
# adds to recursion stack
visited[v] = True
recStack[v] = True
# Recur for all neighbours
# if any neighbour is visited and in
# recStack then graph is cyclic
for neighbour in self.graph[v]:
if visited[neighbour] == False:
if self.isCyclicUtil(neighbour, visited, recStack) == True:
return True
elif recStack[neighbour] == True:
return True
# The node needs to be poped from
# recursion stack before function ends
recStack[v] = False
return False
# Returns true if graph is cyclic else false
def isCyclic(self):
visited = [False] * self.V
recStack = [False] * self.V
for node in range(self.V):
if visited[node] == False:
if self.isCyclicUtil(node,visited,recStack) == True:
return True
return False
g = Graph(4)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
if g.isCyclic() == 1:
print "Graph has a cycle"
else:
print "Graph has no cycle"
# Thanks to Divyanshu Mehta for contributing this code

1 Comment

I don't know of any automatic tools to convert Python to Matlab, but you can call Python from Matlab and the reverse.

Sign in to comment.

Answers (1)

As Rik says there's no tool to convert python code into matlab so you'd have to do that manually, first by understanding what the python code is doing, then by writing the equivalent in matlab.
Of course, if you don't know what the python code is doing you have a problem. (But why are you using code your don't understand?)
Saying that, the code you show is trivially converted into matlab since matlab has built-in classes for graphs. The particular code shown is for a directed graph, digraph in matlab, so:
g = digraph;
g = addedge(g, 0, 1);
g = addedge(g, 0, 2);
g = addedge(g, 1, 2);
g = addedge(g, 2, 0);
g = addedge(g, 2, 3);
g = addedge(g, 3, 3);
g.isdag %is graph directly acyclic or not?
edit: stupid autocorrect inserted the wrong words

7 Comments

In my case I only have edge of graph.
E=[1 2;1 5;2 3;2 4;2 5;3 4;4 5;4 7;4 9;5 6;6 11;6 12;6 13; 7 8;7 9;9 10;9 14;10 11;12 13;13 14];
I want to find cycle of graph using DFS. I know how DFS works.
As matlab does not have built-in DFS code. I found thw above code in Python.
So I think if I can change it to matlab code, I can find a cycle
"As matlab does not have built-in DFS"
If you'd followed the link to digraph I've posted in my answer, you would have seen in the list of functions dfsearch.
With your E matrix the graph can be trivially constructed:
g = digraph(E(:, 1), E(:, 2))
This is the right answer. One of the key reasons to use MATLAB is for MathWorks developed and tested software. There's no reason to reinvent the wheel here unless this is a homework assignment.
NA
NA on 1 Apr 2019
Edited: NA on 1 Apr 2019
I mean ''There are no built-in algorithms in matlab to find cycles in graphs''.
I also find this DFS but do not know how to change it to find cycle
So, I tried to write my own code but have a problem. Finally I found this Python code, but do not understand it.
As I said if you want to know if a directed graph has cycles, use isdag. It will return true if there's no cycle and false otherwise. No need to write your own code.
NA
NA on 1 Apr 2019
Edited: NA on 1 Apr 2019
Sorry, I mean I want to find all cycle in graph using DFS. Not only reture, true or fulse
in my case I want to print out , [1,2,5],[1,2,4,5],[2,3,4],....
This has actually been answered recently, See this post for a simple solution.

Sign in to comment.

Asked:

NA
on 1 Apr 2019

Commented:

on 1 Apr 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!