Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:57:05+00:00 2026-05-31T10:57:05+00:00

I want an algorithm that gives one instance of a cycle in a directed

  • 0

I want an algorithm that gives one instance of a cycle in a directed graph if there is any. Can anyone show me a direction? In pseudo-code, or preferably, in Ruby?

I previously asked a similar question, and following the suggestions there, I implemented Kahn’s algorithm in Ruby that detects if a graph has a cycle, but I want not only whether it has a cycle, but also one possible instance of such cycle.

example_graph = [[1, 2], [2, 3], [3, 4], [3, 5], [3, 6], [6, 2]]

Kahn’s algorithm

def cyclic? graph
  ## The set of edges that have not been examined
  graph = graph.dup
  n, m = graph.transpose
  ## The set of nodes that are the supremum in the graph
  sup = (n - m).uniq
  while sup_old = sup.pop do
    sup_old = graph.select{|n, _| n == sup_old}
    graph -= sup_old
    sup_old.each {|_, ssup| sup.push(ssup) unless graph.any?{|_, n| n == ssup}}
  end
  !graph.empty?
end

The above algorithm tells whether a graph has a cycle:

cyclic?(example_graph) #=> true

but I want not only that but an example of a cycle like this:

#=> [[2, 3], [3, 6], [6, 2]]

If I were to output the variable graph in the above code at the end of examination, it will give:

#=> [[2, 3], [3, 4], [3, 5], [3, 6], [6, 2]]

which includes the cycle I want, but it also includes extra edges that are irrelevant to the cycle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:57:06+00:00Added an answer on May 31, 2026 at 10:57 am

    I asked the same question in the math stackexchange site, and got an answer. It turned out that Tarjan’s algorithm is good for solving this problem. I implemented it in Ruby as follows:

    module DirectedGraph; module_function
        ## Tarjan's algorithm
        def strongly_connected_components graph
            @index, @stack, @indice, @lowlink, @scc = 0, [], {}, {}, []
            @graph = graph
            @graph.flatten(1).uniq.each{|v| strong_connect(v) unless @indice[v]}
            @scc
        end
        def strong_connect v
            @indice[v] = @index
            @lowlink[v] = @index
            @index += 1
            @stack.push(v)
            @graph.each do |vv, w|
                next unless vv == v
                if !@indice[w]
                    strong_connect(w)
                    @lowlink[v] = [@lowlink[v], @lowlink[w]].min
                elsif @stack.include?(w)
                    @lowlink[v] = [@lowlink[v], @indice[w]].min
                end
            end
            if @lowlink[v] == @indice[v]
                i = @stack.index(v)
                @scc.push(@stack[i..-1])
                @stack = @stack[0...i]
            end
        end
    end
    

    So if I apply it to the example above, I get a list of strongly connected components of the graph:

    example_graph = [[1, 2], [2, 3], [3, 4], [3, 5], [3, 6], [6, 2]]
    DirectedGraph.strongly_connected_components(example_graph)
    #=> [[4], [5], [2, 3, 6], [1]]
    

    By selecting those components that are longer than one, I get the cycles:

    DirectedGraph.strongly_connected_components(example_graph)
    .select{|a| a.length > 1}
    #=> [[2, 3, 6]]
    

    And further if I select from the graph the edges whose both vertices are included in the components, I get the crucial edges that constitute the cycles:

    DirectedGraph.strongly_connected_components(example_graph)
    .select{|a| a.length > 1}
    .map{|a| example_graph.select{|v, w| a.include?(v) and a.include?(w)}}
    #=> [[[2, 3], [3, 6], [6, 2]]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this algorithm that I want to implement on VB6. Sub Main() dim
I want to implement a searching algorithm for sequence of words. For that i
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
I want to work on the Rijndael algorithm using C#. Can anybody help me
As a self-development exercise, I want to develop a simple classification algorithm that, given
What is a good algorithm, or class of algorithms that can be used to
I want to design an algorithm that determines whether an array A is plural,
I have the following (imperative) algorithm that I want to implement in Haskell: Given
I am looking for an algorithm that will allow me to visually separate any
I want an efficient algorithm to fill polygon with an Image, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.