Question

Is there a good C library for graph theoretic manipulations? I particularly need to calculate the strongly connected components of a directed graph. I have implemented Tarjan's algorithm in Ruby as follows:

    def strongly_connected_components graph
        @index, @stack, @indice, @lowlink, @scc = 0, [], {}, {}, []
        @graph = graph
        vertices(@graph).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

and it was working with small graphs, but as the graph grew big, it started to return "stack level too deep" errors due to recursive call of the method strong_connect. I guess I need a C library and access that from Ruby, in which the main program is written.

In addition to the library, any suggestion for using that in a Ruby library would be a help.

Was it helpful?

Solution

I came across the igraph library. It is written in C and has wrappers for Ruby, Python and R. For you, this means that you can enjoy the speed of C with the comfort of Ruby.

OTHER TIPS

Ruby Graph Library (RGL) (written in Ruby) is one option to consider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top