GithubHelp home page GithubHelp logo

cventeic / directed_graph Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrpowers/directed_graph

0.0 1.0 0.0 116 KB

Modeling directed acyclic graphs (DAG) for topological sorting, shortest path, longest path, etc.

License: MIT License

Ruby 99.16% Shell 0.84%

directed_graph's Introduction

directed_graph

The directed_graph gem is useful for modeling directed, acyclic, unweighted graphs. It provides methods to topologically sort graphs, find the shortest path between two vertices, and render the graphs and json for client-side graphing.

The following graph will be used to demonstrate the functionality of the directed_graph gem.

graph_example

The Graph class should be instantiated with an array of edges:

# create the vertices
@root = Vertex.new(name: "root")
@a = Vertex.new(name: "a")
@b = Vertex.new(name: "b")
@c = Vertex.new(name: "c")
@d = Vertex.new(name: "d")
@e = Vertex.new(name: "e")

# The first argument is the origin_vertex
# and the second is the destination_vertex
@ra = Edge.new(origin_vertex: @root, destination_vertex: @a)
@ab = Edge.new(origin_vertex: @a, destination_vertex: @b)
@bc = Edge.new(origin_vertex: @b, destination_vertex: @c)
@bd = Edge.new(origin_vertex: @b, destination_vertex: @d)
@ae = Edge.new(origin_vertex: @a, destination_vertex: @e)
@de = Edge.new(origin_vertex: @d, destination_vertex: @e)
@edges = [@ra, @ab, @bc, @bd, @ae, @de]
@graph = Graph.new(@edges)

The @graph object can be used to get a topological sorted array of the vertices:

@graph.sorted_vertices # [@root, @a, @b, @d, @e, @c]

Here is an awesome blog post on topological sorting in Ruby with the TSort module.

The @graph object can also be used to calculate the shortest path between two vertices:

@graph.shortest_path(@root, @e) # [@root, @a, @e]
@graph.shortest_path(@root, Vertex.new(name: "blah")) # nil because the "blah" vertex doesn't exist
@graph.shortest_path(@d, @a) # nil because the graph is directed and can't be traversed in the wrong direction

The @graph object can be used to calculate the longest path between two vertices:

@graph.longest_path(@root, @e) # [@root, @a, @b, @d, @e]
@graph.longest_path(@a, @c) # [@a, @b, @c]
@graph.longest_path(@d, @a) # returns [] when the path doesn't exist

The @graph object can be used to generate a graphml file

require 'color-generator'

generator = ColorGenerator.new saturation: 0.3, lightness: 0.75

# Set the color and label of each vertex
#
@graph.vertices.each do |vertex|
  color = generator.create_hex
  label = vertex.name

  vertex.data[:graphics]  = {:fill=>["##{color}"], :label=>[label], :group=>color}
end

# Generate a graphml file
# Import this file into program like yEd to size nodes and layout graph
File.open("output.graphml", "w+") { |f| f.write(@graph.to_graphml())}

Installation

Add this line to your application's Gemfile:

gem 'directed_graph'

Require the gem in your code:

require 'directed_graph'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/MrPowers/directed_graph.

License

The gem is available as open source under the terms of the MIT License.

directed_graph's People

Contributors

mrpowers avatar eclosson avatar

Watchers

Chris Venteicher 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.