├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── Gemfile
├── Gemfile.lock
├── MIT-LICENSE
├── README.md
├── Rakefile
├── abuelo.gemspec
├── docs
├── index.html
├── index.md
└── support.md
├── index.html
├── javascripts
└── scale.fix.js
├── lib
├── abuelo.rb
└── abuelo
│ ├── algorithms
│ └── dijkstra.rb
│ ├── edge.rb
│ ├── exceptions
│ └── exceptions.rb
│ ├── graph.rb
│ ├── node.rb
│ └── version.rb
├── params.json
├── spec
├── algorithms
│ └── dijkstra_spec.rb
├── edge_spec.rb
├── graph_spec.rb
├── node_spec.rb
└── spec_helper.rb
└── stylesheets
├── github-light.css
├── normalize.css
├── styles.css
└── stylesheet.css
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | .bundle
3 | .yardoc
4 | doc/*
5 | pkg
6 | log
7 | test/tmp/*
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: ruby
2 | rvm:
3 | - 2.3.0
4 | - 2.2.4
5 | - 2.1.8
6 | - 2.0.0
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # 0.1.0 - 2016-02-24
2 | * implemented Dijkstra's algorithm
3 |
4 | # 0.0.2 - 2016-02-21
5 | * new possibility to build a graph: adjacency matrix
6 | * added code of conduct
7 | * refactoring to better meet community standards
8 | * dropped support for Ruby < 2.0.0
9 |
10 | # 0.0.1 - 2016-01-17
11 | hello world
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to making participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, gender identity and expression, level of experience,
9 | nationality, personal appearance, race, religion, or sexual identity and
10 | orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies both within project spaces and in public spaces
49 | when an individual is representing the project or its community. Examples of
50 | representing a project or community include using an official project e-mail
51 | address, posting via an official social media account, or acting as an appointed
52 | representative at an online or offline event. Representation of a project may be
53 | further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the project team at [cache.zero+abuelo@mailbox.org]. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at [http://contributor-covenant.org/version/1/4][version]
72 |
73 | [homepage]: http://contributor-covenant.org
74 | [version]: http://contributor-covenant.org/version/1/4/
75 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | # Specify your gem's dependencies in abuelo.gemspec
4 | gemspec
5 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | abuelo (0.1.0)
5 |
6 | GEM
7 | remote: https://rubygems.org/
8 | specs:
9 | coderay (1.1.0)
10 | diff-lcs (1.2.5)
11 | method_source (0.8.2)
12 | pry (0.10.1)
13 | coderay (~> 1.1.0)
14 | method_source (~> 0.8.1)
15 | slop (~> 3.4)
16 | rake (10.4.2)
17 | rspec (3.3.0)
18 | rspec-core (~> 3.3.0)
19 | rspec-expectations (~> 3.3.0)
20 | rspec-mocks (~> 3.3.0)
21 | rspec-core (3.3.2)
22 | rspec-support (~> 3.3.0)
23 | rspec-expectations (3.3.1)
24 | diff-lcs (>= 1.2.0, < 2.0)
25 | rspec-support (~> 3.3.0)
26 | rspec-mocks (3.3.2)
27 | diff-lcs (>= 1.2.0, < 2.0)
28 | rspec-support (~> 3.3.0)
29 | rspec-support (3.3.0)
30 | slop (3.6.0)
31 |
32 | PLATFORMS
33 | ruby
34 |
35 | DEPENDENCIES
36 | abuelo!
37 | bundler (~> 1.7)
38 | pry
39 | rake
40 | rspec
41 |
--------------------------------------------------------------------------------
/MIT-LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | Copyright (c) 2016 Dirk Holzapfel
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 |
6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 |
8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Abuelo
2 | [](https://travis-ci.org/dirkholzapfel/abuelo)
3 | [](https://gitter.im/dirkholzapfel/abuelo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4 |
5 | Abuelo is a graph theory library written in Ruby that allows you to build a representation of a graph.
6 |
7 | A graph consists of nodes (= vertices, points) and edges (= lines, arcs). The graph may be undirected or directed. For the sake of simplicity Abuelo sticks with the same vocabulary (nodes, edges) for directed and undirected graphs in contrast to theoretical graph theory.
8 |
9 | Abuelo supports Ruby >= 2.0.0
10 |
11 | ## Examples
12 | ### Undirected graph
13 | ```ruby
14 | graph = Abuelo::Graph.new
15 |
16 | node_1 = Abuelo::Node.new('node 1')
17 | node_2 = Abuelo::Node.new('node 2')
18 | node_3 = Abuelo::Node.new('node 3')
19 | edge_1 = Abuelo::Edge.new(node_1, node_2, 42)
20 | edge_2 = Abuelo::Edge.new(node_2, node_3, 23)
21 |
22 | graph.add_node(node_1)
23 | .add_node(node_2)
24 | .add_node(node_3)
25 | .add_edge(edge_1)
26 | .add_edge(edge_2)
27 |
28 | graph.order # => 3
29 | graph.size # => 2
30 | graph.nodes # => [node_1, node_2, node_3]
31 | graph.has_node?(node_1) # => true
32 | graph.has_node_with_name?('foo') # => false
33 | graph.find_node_by_name('node 1') # => node_1
34 | graph.edges # => [[edge_1, edge_1.symmetric], [edge_2, edge_2.symmetric]]
35 | graph.has_edge?(edge_1) # => true
36 | graph.has_edge?(edge_1.symmetric) # => true
37 | graph.find_edge(node_1, node_2) # => edge_1
38 | graph.find_edge(node_2, node_1) # => edge_1.symmetric
39 | graph.edges_for_node(node_2) # => [edge_1.symmetric, edge_2]
40 |
41 | node_1.edges # => [edge_1]
42 | node_1.neighbours # => [node_2]
43 | ```
44 |
45 | ### Directed graph
46 | ```ruby
47 | graph = Abuelo::Graph.new(directed: true)
48 |
49 | node_1 = Abuelo::Node.new('node 1')
50 | node_2 = Abuelo::Node.new('node 2')
51 | node_3 = Abuelo::Node.new('node 3')
52 | edge_1 = Abuelo::Edge.new(node_1, node_2, 42)
53 | edge_2 = Abuelo::Edge.new(node_2, node_3, 23)
54 |
55 | graph.add_node(node_1)
56 | .add_node(node_2)
57 | .add_node(node_3)
58 | .add_edge(edge_1)
59 | .add_edge(edge_2)
60 |
61 | graph.order # => 3
62 | graph.size # => 2
63 | graph.nodes # => [node_1, node_2, node_3]
64 | graph.has_node?(node_1) # => true
65 | graph.has_node_with_name?('foo') # => false
66 | graph.find_node_by_name('node 1') # => node_1
67 | graph.edges # => [edge_1, edge_2]
68 | graph.has_edge?(edge_1) # => true
69 | graph.has_edge?(edge_1.symmetric) # => false
70 | graph.find_edge(node_1, node_2) # => edge_1
71 | graph.find_edge(node_2, node_1) # => nil
72 | graph.edges_for_node(node_2) # => [edge_2]
73 |
74 | node_1.edges # => [edge_1]
75 | node_1.neighbours # => [node_2]
76 | ```
77 |
78 | ### Initialize a graph with an adjacency matrix
79 | The above, object oriented way to build graphs is the recommended way to work with this library.
80 | But you can also build a graph with an [adjacency matrix](https://en.wikipedia.org/wiki/Adjacency_matrix).
81 | That is a nice shortcut used in some tests and may be a good alternative if you use Abuelo in the console to play around.
82 |
83 | A zero indicates there is no edge between the nodes, an Integer indicates that there is an edge with the given weight between the nodes. The nodes are automatically named, starting with "node 1".
84 | The above example can be built like this:
85 |
86 | #### Undirected graphs
87 | Be aware that you have to provide all symmetric edges in an adjacency matrix for an undirected graph - the lib does not add them automatically as it happens with the `.add_edge` method.
88 |
89 | ```ruby
90 | adjacency_matrix = <<-matrix
91 | 0 42 0
92 | 42 0 23
93 | 0 23 0
94 | matrix
95 |
96 | # The above matrix corresponds to this internal representation
97 | #
98 | # | node 1 | node 2 | node 3 |
99 | # ------------------------------------
100 | # node 1 | 0 | 42 | 0 |
101 | # node 2 | 42 | 0 | 23 |
102 | # node 3 | 0 | 23 | 0 |
103 |
104 | graph = Abuelo::Graph.new(adjacency_matrix: adjacency_matrix)
105 | node_1 = graph.find_node_by_name('node 1')
106 | node_2 = graph.find_node_by_name('node 2')
107 | graph.find_edge(node_1, node_2).weight # => 42
108 | ```
109 | #### Directed graphs
110 | ```ruby
111 | adjacency_matrix = <<-matrix
112 | 0 42 0
113 | 0 0 23
114 | 0 0 0
115 | matrix
116 |
117 | # The above matrix corresponds to this internal representation
118 | #
119 | # | node 1 | node 2 | node 3 |
120 | # ------------------------------------
121 | # node 1 | 0 | 42 | 0 |
122 | # node 2 | 0 | 0 | 23 |
123 | # node 3 | 0 | 0 | 0 |
124 |
125 | graph = Abuelo::Graph.new(adjacency_matrix: adjacency_matrix, directed: true)
126 | ```
127 | ## Algorithms
128 | ### Dijkstra
129 | This is the example from https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
130 |
131 |
132 |
133 | ```ruby
134 | adjacency_matrix = <<-matrix
135 | 0 7 9 0 0 14
136 | 7 0 10 15 0 0
137 | 9 10 0 11 0 2
138 | 0 15 11 0 6 0
139 | 0 0 0 6 0 9
140 | 14 0 2 0 9 0
141 | matrix
142 |
143 | graph = Abuelo::Graph.new(adjacency_matrix: adjacency_matrix)
144 | start_node = graph.find_node_by_name('node 1')
145 | node_5 = graph.find_node_by_name('node 5')
146 |
147 | dijkstra = Abuelo::Algorithms::Dijkstra.new(graph, start_node)
148 | dijkstra.shortest_distance_to(node_5) # => 20
149 | dijkstra.shortest_path_to(node_5).map(&:to_s) # => ['node 1', 'node 3', 'node 6', 'node 5']
150 | ```
151 |
152 | ## Documentation
153 | [YARD](http://yardoc.org) documentation is available at [rubydoc](http://www.rubydoc.info/github/dirkholzapfel/abuelo).
154 |
155 | ## Future
156 | * Implement graph algorithms
157 | * [Kruskal's algorithm](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm)
158 | * [Prim's algorithm](https://en.wikipedia.org/wiki/Prim%27s_algorithm)
159 | * Implement visualization of graph
160 | * Add priority queue to Dijkstra's algorithm implementation
161 |
162 | ## Installation
163 | Abuelo is a gem which you can install with:
164 | ```
165 | gem install abuelo
166 | ```
167 |
168 | In Rails 3+, add the following to your ```Gemfile```:
169 | ```
170 | gem 'abuelo'
171 | ```
172 |
173 | ## Credits
174 | Dirk Holzapfel ([@cachezero](https://twitter.com/cachezero))
175 |
176 | [cachezero.net](http://cachezero.net)
177 |
178 | [Abuelo](http://www.ronabuelopanama.com)
179 |
180 | ### Contributors
181 | [dirkholzapfel](https://github.com/dirkholzapfel),
182 | [mbirman](https://github.com/mbirman),
183 | [sergey-kintsel](https://github.com/sergey-kintsel)
184 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'bundler/gem_tasks'
2 | require 'rspec/core/rake_task'
3 |
4 | task default: :tests
5 | task tests: [:spec]
6 |
7 | RSpec::Core::RakeTask.new(:spec)
8 |
--------------------------------------------------------------------------------
/abuelo.gemspec:
--------------------------------------------------------------------------------
1 | $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2 | require 'abuelo/version'
3 |
4 | Gem::Specification.new do |spec|
5 | spec.name = 'abuelo'
6 | spec.version = Abuelo::VERSION
7 | spec.summary = 'Abuelo'
8 | spec.description = 'Abuelo is a graph theory library.'
9 | spec.authors = ['Dirk Holzapfel']
10 | spec.email = 'cache.zero@mailbox.org'
11 | spec.homepage =
12 | 'http://github.com/dirkholzapfel/abuelo'
13 | spec.license = 'MIT'
14 |
15 | spec.files = `git ls-files`.split("\n")
16 | spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17 | spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18 | spec.require_paths = ['lib']
19 |
20 | # Development
21 | spec.add_development_dependency 'bundler', '~> 1.7'
22 | spec.add_development_dependency 'rake'
23 | spec.add_development_dependency 'pry'
24 |
25 | # Testing
26 | spec.add_development_dependency 'rspec'
27 | end
28 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
22 |
23 | Welcome to GitHub Pages.
24 |
25 | This automatic page generator is the easiest way to create beautiful pages for all of your projects. Author your page content here using GitHub Flavored Markdown, select a template crafted by a designer, and publish. After your page is generated, you can check out the new gh-pages
branch locally. If you’re using GitHub Desktop, simply sync your repository and you’ll see the new branch.
26 |
27 |
28 | Designer Templates
29 |
30 | We’ve crafted some handsome templates for you to use. Go ahead and click 'Continue to layouts' to browse through them. You can easily go back to edit your page before publishing. After publishing your page, you can revisit the page generator and switch to another theme. Your Page content will be preserved.
31 |
32 |
33 | Creating pages manually
34 |
35 | If you prefer to not use the automatic generator, push a branch named gh-pages
to your repository to create a page manually. In addition to supporting regular HTML content, GitHub Pages support Jekyll, a simple, blog aware static site generator. Jekyll makes it easy to create site-wide headers and footers without having to copy them across every page. It also offers intelligent blog support and other advanced templating features.
36 |
37 |
38 | Authors and Contributors
39 |
40 | You can @mention a GitHub username to generate a link to their profile. The resulting <a>
element will link to the contributor’s GitHub Profile. For example: In 2007, Chris Wanstrath (@defunkt), PJ Hyett (@pjhyett), and Tom Preston-Werner (@mojombo) founded GitHub.
41 |
42 |
43 | Support or Contact
44 |
45 | Having trouble with Pages? Check out our documentation or contact support and we’ll help you sort it out.
46 |
47 |
48 |
49 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/javascripts/scale.fix.js:
--------------------------------------------------------------------------------
1 | var metas = document.getElementsByTagName('meta');
2 | var i;
3 | if (navigator.userAgent.match(/iPhone/i)) {
4 | for (i=0; i