Cycles in directed graph
up vote
3
down vote
favorite
I am wiring a function to check whether a graph contains a cycle.
It is represented as a list of lists of all indexes of nodes each node is connected to. Nodes are enumerated from 1 (task requirement).
While checking the graph [[2, 3], , [4], ], the program enters the first listed node correctly, yet in the second iteration, it is assumed that adjlist[node-1] is an int of value 3 rather than an array (or int = 2 at very least)
What am I missing?
The code:
def is_acyclic(adjlist: List, visited: List, path: List) -> bool:
'''
:param adjlist: list representation of a graph; eg: [[2, 3], , [4], ]
:param visited: visited nodes
:param path: visited nodes in current iteration
:return: the graph does not contain a cycle
'''
for node in range(1, len(adjlist)+1):
if node not in visited:
visited.append(node)
path.append(node)
for child in adjlist[node-1]:
if child in path:
return False
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path) is False:
return False
path.remove(node)
return True
python python-3.x graph-theory
add a comment |
up vote
3
down vote
favorite
I am wiring a function to check whether a graph contains a cycle.
It is represented as a list of lists of all indexes of nodes each node is connected to. Nodes are enumerated from 1 (task requirement).
While checking the graph [[2, 3], , [4], ], the program enters the first listed node correctly, yet in the second iteration, it is assumed that adjlist[node-1] is an int of value 3 rather than an array (or int = 2 at very least)
What am I missing?
The code:
def is_acyclic(adjlist: List, visited: List, path: List) -> bool:
'''
:param adjlist: list representation of a graph; eg: [[2, 3], , [4], ]
:param visited: visited nodes
:param path: visited nodes in current iteration
:return: the graph does not contain a cycle
'''
for node in range(1, len(adjlist)+1):
if node not in visited:
visited.append(node)
path.append(node)
for child in adjlist[node-1]:
if child in path:
return False
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path) is False:
return False
path.remove(node)
return True
python python-3.x graph-theory
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am wiring a function to check whether a graph contains a cycle.
It is represented as a list of lists of all indexes of nodes each node is connected to. Nodes are enumerated from 1 (task requirement).
While checking the graph [[2, 3], , [4], ], the program enters the first listed node correctly, yet in the second iteration, it is assumed that adjlist[node-1] is an int of value 3 rather than an array (or int = 2 at very least)
What am I missing?
The code:
def is_acyclic(adjlist: List, visited: List, path: List) -> bool:
'''
:param adjlist: list representation of a graph; eg: [[2, 3], , [4], ]
:param visited: visited nodes
:param path: visited nodes in current iteration
:return: the graph does not contain a cycle
'''
for node in range(1, len(adjlist)+1):
if node not in visited:
visited.append(node)
path.append(node)
for child in adjlist[node-1]:
if child in path:
return False
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path) is False:
return False
path.remove(node)
return True
python python-3.x graph-theory
I am wiring a function to check whether a graph contains a cycle.
It is represented as a list of lists of all indexes of nodes each node is connected to. Nodes are enumerated from 1 (task requirement).
While checking the graph [[2, 3], , [4], ], the program enters the first listed node correctly, yet in the second iteration, it is assumed that adjlist[node-1] is an int of value 3 rather than an array (or int = 2 at very least)
What am I missing?
The code:
def is_acyclic(adjlist: List, visited: List, path: List) -> bool:
'''
:param adjlist: list representation of a graph; eg: [[2, 3], , [4], ]
:param visited: visited nodes
:param path: visited nodes in current iteration
:return: the graph does not contain a cycle
'''
for node in range(1, len(adjlist)+1):
if node not in visited:
visited.append(node)
path.append(node)
for child in adjlist[node-1]:
if child in path:
return False
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path) is False:
return False
path.remove(node)
return True
python python-3.x graph-theory
python python-3.x graph-theory
edited Nov 8 at 23:13
Dominique Fortin
1,622816
1,622816
asked Oct 23 at 22:41
Filip Frey
182
182
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
This is due to the fact that the function is being called recursively. This part of your code keeps trimming the graph adjacency list:
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path, level=level + 1) is False:
return False
The first time adjacency list is:
[[2, 3], , [4], ]
and adjlist[node-1] is [2, 3]
the second time around adjacency list is:
[2, 3]
and adjlist[node-1] is 3
since 2 is already in visited, node actually gets incremented to 2. Hence, you see:
adjlist[node-1] == adjlist[2-1] == adjlist[1] == 3
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This is due to the fact that the function is being called recursively. This part of your code keeps trimming the graph adjacency list:
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path, level=level + 1) is False:
return False
The first time adjacency list is:
[[2, 3], , [4], ]
and adjlist[node-1] is [2, 3]
the second time around adjacency list is:
[2, 3]
and adjlist[node-1] is 3
since 2 is already in visited, node actually gets incremented to 2. Hence, you see:
adjlist[node-1] == adjlist[2-1] == adjlist[1] == 3
add a comment |
up vote
0
down vote
This is due to the fact that the function is being called recursively. This part of your code keeps trimming the graph adjacency list:
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path, level=level + 1) is False:
return False
The first time adjacency list is:
[[2, 3], , [4], ]
and adjlist[node-1] is [2, 3]
the second time around adjacency list is:
[2, 3]
and adjlist[node-1] is 3
since 2 is already in visited, node actually gets incremented to 2. Hence, you see:
adjlist[node-1] == adjlist[2-1] == adjlist[1] == 3
add a comment |
up vote
0
down vote
up vote
0
down vote
This is due to the fact that the function is being called recursively. This part of your code keeps trimming the graph adjacency list:
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path, level=level + 1) is False:
return False
The first time adjacency list is:
[[2, 3], , [4], ]
and adjlist[node-1] is [2, 3]
the second time around adjacency list is:
[2, 3]
and adjlist[node-1] is 3
since 2 is already in visited, node actually gets incremented to 2. Hence, you see:
adjlist[node-1] == adjlist[2-1] == adjlist[1] == 3
This is due to the fact that the function is being called recursively. This part of your code keeps trimming the graph adjacency list:
elif child not in visited:
if is_acyclic(adjlist[node-1], visited, path, level=level + 1) is False:
return False
The first time adjacency list is:
[[2, 3], , [4], ]
and adjlist[node-1] is [2, 3]
the second time around adjacency list is:
[2, 3]
and adjlist[node-1] is 3
since 2 is already in visited, node actually gets incremented to 2. Hence, you see:
adjlist[node-1] == adjlist[2-1] == adjlist[1] == 3
answered Oct 23 at 22:58
LeKhan9
921112
921112
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52958765%2fcycles-in-directed-graph%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown