3

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    Does it need to be c? I've been told the [Boost Graph Library](http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/index.html) is the way to go if you're ok with c++. – Michael Apr 14 '12 at 00:31
  • @Michael That's fine as long as it can be called from Ruby. I just am not familiar with extending Ruby using other languages. Do you have any idea how to call it from Ruby? – sawa Apr 14 '12 at 00:35
  • Sorry, I have no idea. I've barely used Ruby at all. – Michael Apr 14 '12 at 22:06

3 Answers3

2

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.

Yaakov Belch
  • 4,692
  • 33
  • 39
1

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

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
  • Do you have any idea about the scalability of this library? Since I also implemented the same algorithm in Ruby, and had a problem, I am concerned that this library is also written in Ruby. It could be that there is a way to avoid the problems that I had. I don't know. – sawa Apr 14 '12 at 00:40
  • @sawa: I don't know about its scalability. – Jeremiah Willcock Apr 14 '12 at 01:31
0

In C++ ther is CXXGraph Library, that is an header-only library for graph manipulation and algorithms.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35