Kosaraju: Difference between revisions

m
Combine Python entries with appropriate works with verbiage
(Insert a working python3 version of the python code)
m (Combine Python entries with appropriate works with verbiage)
Line 1,217:
</pre>
 
=={{header|Python3Python}}==
Works with Python 2
<syntaxhighlight lang="python3">def kosaraju(g):
<syntaxhighlight lang="python">def kosaraju(g):
class nonlocal: pass
 
# 1. For each vertex u of the graph, mark u as unvisited. Let l be empty.
size = len(g)
 
vis = [False] * size
lvis = [0False] * size # vertexes that have been visited
xl = [0]*size
tnonlocal.x = [[] for _ in range(size)]
t = [[]]*size # transpose graph
 
def visit(u):
nonlocal x
if not vis[u]:
vis[u] = True
for v in g[u]:
visit(v)
t[v].append( = t[v] + [u)]
nonlocal.x -= nonlocal.x - 1
l[nonlocal.x] = u
 
# 2. For each vertex u of the graph do visit(u)
for u in range(size):
for u in range(len(g)):
visit(u)
c = [0] * size
 
def assign(u, root):
Line 1,246 ⟶ 1,251:
assign(v, root)
 
# 3: For each element u of l in order, do assign(u, u)
for u in l:
assign(u, u)
Line 1,251 ⟶ 1,257:
return c
 
g = [[1], [2], [0], [1,2,4], [3,5], [2,6], [5], [4,6,7]]
 
print kosaraju(g)</syntaxhighlight>
g = [[1], [2], [0], [1, 2, 4], [3, 5], [2, 6], [5], [4, 6, 7]]
print(kosaraju(g))</syntaxhighlight>
 
{{out}}
<pre>[0, 0, 0, 3, 3, 5, 5, 7]</pre>
 
Syntax requirements have changed. This version works with Python 3.
 
<syntaxhighlight lang="python3">def kosaraju(g):
 
=={{header|Python}}==
<syntaxhighlight lang="python">def kosaraju(g):
class nonlocal: pass
 
# 1. For each vertex u of the graph, mark u as unvisited. Let l be empty.
size = len(g)
vis = [False] * size
 
visl = [False0] * size # vertexes that have been visited
lx = [0]*size
nonlocal.xt = [[] for _ in range(size)]
t = [[]]*size # transpose graph
 
def visit(u):
nonlocal x
if not vis[u]:
vis[u] = True
for v in g[u]:
visit(v)
t[v] = t[v] + [.append(u])
nonlocal.x -= nonlocal.x - 1
l[nonlocal.x] = u
 
for u in range(size):
# 2. For each vertex u of the graph do visit(u)
for u in range(len(g)):
visit(u)
c = [0] * size
 
def assign(u, root):
Line 1,293 ⟶ 1,291:
assign(v, root)
 
# 3: For each element u of l in order, do assign(u, u)
for u in l:
assign(u, u)
Line 1,299 ⟶ 1,296:
return c
 
 
g = [[1], [2], [0], [1,2,4], [3,5], [2,6], [5], [4,6,7]]
g = [[1], [2], [0], [1, 2, 4], [3, 5], [2, 6], [5], [4, 6, 7]]
print kosaraju(g)</syntaxhighlight>
print(kosaraju(g))</syntaxhighlight>
 
{{out}}
<pre>[0, 0, 0, 3, 3, 5, 5, 7]</pre>
 
 
=={{header|Racket}}==
10,333

edits