GithubHelp home page GithubHelp logo

marcelocg / plexus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chikamichi/plexus

0.0 2.0 0.0 1.85 MB

Graph Theory library for Ruby

Home Page: http://rubydoc.info/github/chikamichi/plexus/master/frames

License: Other

plexus's Introduction

Plexus (was Graphy). A framework for graph theory, graph data structures and associated algorithms.

Graph algorithms currently provided are:

  • Topological Sort
  • Strongly Connected Components
  • Transitive Closure
  • Rural Chinese Postman
  • Biconnected

These are based on more general algorithm patterns:

  • Breadth First Search
  • Depth First Search
  • A* Search
  • Floyd-Warshall
  • Best First Search
  • Djikstra's Algorithm
  • Lexicographic Search

A quick Tour

Arcs

There are two vertices bound classes, Plexus::Arc and Plexus::Edge. The former defines directional edges, the latter undirected edges.

Vertices

Vertices can be any Object.

Graph Types

There are a number of different graph types, each of which provide different features and constraints:

Plexus::Digraph and its alias Plexus::DirectedGraph:

  • Single directed edges (arcs) between vertices
  • Loops are forbidden

Plexus::DirectedPseudoGraph:

  • Multiple directed edges (arcs) between vertices
  • Loops are forbidden

Plexus::DirectedMultiGraph:

  • Multiple directed edges (arcs) between vertices
  • Loops on vertices

Plexus::UndirectedGraph, Plexus::UndirectedPseudoGraph, and Graph::UndirectedMultiGraph are similar but all edges are undirected.

Data Structures

In order to modelize data structures, make use of the Plexus::AdjacencyGraph module which provides a generalized adjacency list and an edge list adaptor.

The Plexus::Digraph class is the general purpose "swiss army knife" of graph classes, most of the other classes are just modifications to this class. It is optimized for efficient access to just the out-edges, fast vertex insertion and removal at the cost of extra space overhead, etc.

Example Usage

Using IRB, first require the library:

require 'rubygems' # only if you are using ruby 1.8.x
require 'plexus'

If you'd like to include all the classes in the current scope (so you don't have to prefix with Plexus::), just:

include Plexus

Let's play with the library a bit in IRB:

>> dg = Digraph[1,2, 2,3, 2,4, 4,5, 6,4, 1,6]
=> Plexus::Digraph[[2, 3], [1, 6], [2, 4], [4, 5], [1, 2], [6, 4]]

A few properties of the graph we just created:

>> dg.directed?
=> true
>> dg.vertex?(4)
=> true
>> dg.edge?(2,4)
=> true
>> dg.edge?(4,2)
=> false
>> dg.vertices
=> [1, 2, 3, 4, 5, 6]

Every object could be a vertex, even the class object Object:

>> dg.vertex?(Object)
=> false

>> UndirectedGraph.new(dg).edges.sort.to_s
=> "[Plexus::Edge[1,2,nil], Plexus::Edge[2,3,nil], Plexus::Edge[2,4,nil],
      Plexus::Edge[4,5,nil], Plexus::Edge[1,6,nil], Plexus::Edge[6,4,nil]]"

Add inverse edge (4-2) to directed graph:

>> dg.add_edge!(4,2)
=> Plexus::DirectedGraph[Plexus::Arc[1,2,nil], Plexus::Arc[1,6,nil], Plexus::Arc[2,3,nil],
                          Plexus::Arc[2,4,nil], Plexus::Arc[4,5,nil], Plexus::Arc[4,2,nil],
                          Plexus::Arc[6,4,nil]]

(4-2) == (2-4) in the undirected graph (4-2 doesn't show up):

    >> UndirectedGraph.new(dg).edges.sort.to_s
    => "[Plexus::Edge[1,2,nil], Plexus::Edge[2,3,nil], Plexus::Edge[2,4,nil],
         Plexus::Edge[4,5,nil], Plexus::Edge[1,6,nil], Plexus::Edge[6,4,nil]]"

(4-2) != (2-4) in directed graphs (both show up):

>> dg.edges.sort.to_s
=> "[Plexus::Arc[1,2,nil], Plexus::Arc[1,6,nil], Plexus::Arc[2,3,nil],
      Plexus::Arc[2,4,nil], Plexus::Arc[4,2,nil], Plexus::Arc[4,5,nil],
      Plexus::Arc[6,4,nil]]"

>> dg.remove_edge! 4,2
=> Plexus::DirectedGraph[Plexus::Arc[1,2,nil], Plexus::Arc[1,6,nil], Plexus::Arc[2,3,nil],
                          Plexus::Arc[2,4,nil], Plexus::Arc[4,5,nil], Plexus::Arc[6,4,nil]]

Topological sorting is realized with an iterator:

>> dg.topsort
=> [1, 6, 2, 4, 5, 3]
>> y = 0; dg.topsort { |v| y += v }; y
=> 21

You can use DOT to visualize the graph:

>> require 'plexus/dot'
>> dg.write_to_graphic_file('jpg','visualize')

Here's an example showing the module inheritance hierarchy:

>> module_graph = Digraph.new
>> ObjectSpace.each_object(Module) do |m|
>>   m.ancestors.each {|a| module_graph.add_edge!(m,a) if m != a}
>> end
>> gv = module_graph.vertices.select {|v| v.to_s.match(/Plexus/) }
>> module_graph.induced_subgraph(gv).write_to_graphic_file('jpg','module_graph')

Look for more in the examples directory.

History

This library is based on GRATR by Shawn Garbett (itself a fork of Horst Duchene's RGL library) which is heavily influenced by the Boost Graph Library (BGL).

This fork attempts to modernize and extend the API and tests.

References

For more information on Graph Theory, you may want to read:

Credits

See CREDITS.markdown

TODO

See TODO.markdown

CHANGELOG

See CHANGELOG.markdown

License

MIT License. See the LICENSE file.

plexus's People

Contributors

chikamichi avatar papipo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.