Skip to content

Commit abdfceb

Browse files
authored
fix period (#90)
Thanks. I am merging this, although ideally the white space changes should have been a separate commit.
1 parent 25a935f commit abdfceb

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

src/connectivity.jl

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ julia> g = SimpleDiGraph(Edge.(edge_list))
207207
208208
julia> strongly_connected_components(g)
209209
4-element Array{Array{Int64,1},1}:
210-
[8, 9]
211-
[5, 6, 7]
210+
[8, 9]
211+
[5, 6, 7]
212212
[1, 2, 3, 4]
213-
[10, 11]
213+
[10, 11]
214214
215215
```
216216
"""
@@ -221,18 +221,18 @@ function strongly_connected_components end
221221
one_t = one(T)
222222
nvg = nv(g)
223223
count = one_t
224-
225-
224+
225+
226226
index = zeros(T, nvg) # first time in which vertex is discovered
227227
stack = Vector{T}() # stores vertices which have been discovered and not yet assigned to any component
228228
onstack = zeros(Bool, nvg) # false if a vertex is waiting in the stack to receive a component assignment
229229
lowlink = zeros(T, nvg) # lowest index vertex that it can reach through back edge (index array not vertex id number)
230230
parents = zeros(T, nvg) # parent of every vertex in dfs
231231
components = Vector{Vector{T}}() # maintains a list of scc (order is not guaranteed in API)
232232

233-
233+
234234
dfs_stack = Vector{T}()
235-
235+
236236
@inbounds for s in vertices(g)
237237
if index[s] == zero_t
238238
index[s] = count
@@ -243,8 +243,8 @@ function strongly_connected_components end
243243
count = count + one_t
244244

245245
# start dfs from 's'
246-
push!(dfs_stack, s)
247-
246+
push!(dfs_stack, s)
247+
248248
while !isempty(dfs_stack)
249249
v = dfs_stack[end] #end is the most recently added item
250250
u = zero_t
@@ -267,11 +267,11 @@ function strongly_connected_components end
267267
# time to start popping.
268268
popped = pop!(dfs_stack)
269269
lowlink[parents[popped]] = min(lowlink[parents[popped]], lowlink[popped])
270-
270+
271271
if index[v] == lowlink[v]
272272
# found a cycle in a completed dfs tree.
273273
component = Vector{T}()
274-
274+
275275
while !isempty(stack) #break when popped == v
276276
# drain stack until we see v.
277277
# everything on the stack until we see v is in the SCC rooted at v.
@@ -284,19 +284,19 @@ function strongly_connected_components end
284284
break
285285
end
286286
end
287-
287+
288288
reverse!(component)
289289
push!(components, component)
290290
end
291-
291+
292292
else #LABEL A
293293
# add unvisited neighbor to dfs
294294
index[u] = count
295295
lowlink[u] = count
296296
onstack[u] = true
297297
parents[u] = v
298298
count = count + one_t
299-
299+
300300
push!(stack, u)
301301
push!(dfs_stack, u)
302302
# next iteration of while loop will expand the DFS tree from u.
@@ -312,7 +312,7 @@ end
312312
"""
313313
strongly_connected_components_kosaraju(g)
314314
315-
Compute the strongly connected components of a directed graph `g` using Kosaraju's Algorithm.
315+
Compute the strongly connected components of a directed graph `g` using Kosaraju's Algorithm.
316316
(https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm).
317317
318318
Return an array of arrays, each of which is the entire connected component.
@@ -345,17 +345,17 @@ julia> g=SimpleDiGraph(11)
345345
346346
julia> edge_list=[(1,2),(2,3),(3,4),(4,1),(3,5),(5,6),(6,7),(7,5),(5,8),(8,9),(9,8),(10,11),(11,10)]
347347
13-element Array{Tuple{Int64,Int64},1}:
348-
(1, 2)
349-
(2, 3)
350-
(3, 4)
351-
(4, 1)
352-
(3, 5)
353-
(5, 6)
354-
(6, 7)
355-
(7, 5)
356-
(5, 8)
357-
(8, 9)
358-
(9, 8)
348+
(1, 2)
349+
(2, 3)
350+
(3, 4)
351+
(4, 1)
352+
(3, 5)
353+
(5, 6)
354+
(6, 7)
355+
(7, 5)
356+
(5, 8)
357+
(8, 9)
358+
(9, 8)
359359
(10, 11)
360360
(11, 10)
361361
@@ -364,98 +364,98 @@ julia> g = SimpleDiGraph(Edge.(edge_list))
364364
365365
julia> strongly_connected_components_kosaraju(g)
366366
4-element Array{Array{Int64,1},1}:
367-
[11, 10]
367+
[11, 10]
368368
[2, 3, 4, 1]
369-
[6, 7, 5]
370-
[9, 8]
369+
[6, 7, 5]
370+
[9, 8]
371371
372372
```
373373
"""
374374
function strongly_connected_components_kosaraju end
375375
@traitfn function strongly_connected_components_kosaraju(g::AG::IsDirected) where {T<:Integer, AG <: AbstractGraph{T}}
376-
377-
nvg = nv(g)
376+
377+
nvg = nv(g)
378378

379379
components = Vector{Vector{T}}() # Maintains a list of strongly connected components
380-
380+
381381
order = Vector{T}() # Vector which will store the order in which vertices are visited
382-
sizehint!(order, nvg)
383-
382+
sizehint!(order, nvg)
383+
384384
color = zeros(UInt8, nvg) # Vector used as for marking the colors during dfs
385-
385+
386386
dfs_stack = Vector{T}() # Stack used for dfs
387-
387+
388388
# dfs1
389389
@inbounds for v in vertices(g)
390-
391-
color[v] != 0 && continue
390+
391+
color[v] != 0 && continue
392392
color[v] = 1
393-
393+
394394
# Start dfs from v
395395
push!(dfs_stack, v) # Push v to the stack
396-
396+
397397
while !isempty(dfs_stack)
398398
u = dfs_stack[end]
399399
w = zero(T)
400-
400+
401401
for u_neighbor in outneighbors(g, u)
402402
if color[u_neighbor] == 0
403403
w = u_neighbor
404404
break
405405
end
406406
end
407-
407+
408408
if w != 0
409409
push!(dfs_stack, w)
410410
color[w] = 1
411411
else
412412
push!(order, u) #Push back in vector to store the order in which the traversal finishes(Reverse Topological Sort)
413413
color[u] = 2
414-
pop!(dfs_stack)
414+
pop!(dfs_stack)
415415
end
416416
end
417417
end
418-
418+
419419
@inbounds for i in vertices(g)
420420
color[i] = 0 # Marking all the vertices from 1 to n as unvisited for dfs2
421421
end
422-
422+
423423
# dfs2
424424
@inbounds for i in 1:nvg
425-
425+
426426
v = order[end-i+1] # Reading the order vector in the decreasing order of finish time
427-
color[v] != 0 && continue
427+
color[v] != 0 && continue
428428
color[v] = 1
429-
429+
430430
component=Vector{T}() # Vector used to store the vertices of one component temporarily
431-
431+
432432
# Start dfs from v
433433
push!(dfs_stack, v) # Push v to the stack
434-
434+
435435
while !isempty(dfs_stack)
436436
u = dfs_stack[end]
437437
w = zero(T)
438-
438+
439439
for u_neighbor in inneighbors(g, u)
440440
if color[u_neighbor] == 0
441441
w = u_neighbor
442442
break
443443
end
444444
end
445-
445+
446446
if w != 0
447447
push!(dfs_stack, w)
448448
color[w] = 1
449449
else
450450
color[u] = 2
451451
push!(component, u) # Push u to the vector component
452-
pop!(dfs_stack)
452+
pop!(dfs_stack)
453453
end
454454
end
455-
455+
456456
push!(components, component)
457457
end
458-
458+
459459
return components
460460
end
461461

@@ -500,11 +500,11 @@ function period end
500500

501501
g_bfs_tree = bfs_tree(g, 1)
502502
levels = gdistances(g_bfs_tree, 1)
503-
tree_diff = difference(g, g_bfs_tree)
504503
edge_values = Vector{T}()
505504

506505
divisor = 0
507-
for e in edges(tree_diff)
506+
for e in edges(g)
507+
has_edge(g_bfs_tree, src(e), dst(e)) && continue
508508
@inbounds value = levels[src(e)] - levels[dst(e)] + 1
509509
divisor = gcd(divisor, value)
510510
isequal(divisor, 1) && return 1

0 commit comments

Comments
 (0)