graphify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +18 -0
- data/bin/setup +8 -0
- data/graphify.gemspec +41 -0
- data/graphify.png +0 -0
- data/lib/graphify.rb +54 -0
- data/lib/graphify/graph.rb +296 -0
- data/lib/graphify/graph_list.rb +88 -0
- data/lib/graphify/graph_matrix.rb +95 -0
- data/lib/graphify/graph_matrix_db.rb +41 -0
- data/lib/graphify/version.rb +3 -0
- data/main_test.png +0 -0
- data/main_test_dfs_tree.png +0 -0
- data/main_test_shortest_path_tree.png +0 -0
- data/main_test_strongly_connected_components.png +0 -0
- data/main_test_undirected.png +0 -0
- data/main_test_undirected_dfs_tree.png +0 -0
- data/main_test_undirected_minimum_spanning_tree.png +0 -0
- data/main_test_undirected_shortest_path_tree.png +0 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 124c7b666e738e2ea886e0b9a804a05230daca15
|
4
|
+
data.tar.gz: dc91569f2e17087a22fe4f9f07417ddf839edbb0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfe2fb70be1c39bbc93eb9984ee44a7fe11cf9f97008c4d8f969a21e2db058ee161ecfed2fa38a8991914eb60e91195dec50a8e28cc48a0172a726b8f352881d
|
7
|
+
data.tar.gz: 24c4f04e43a9731798781e40cbdb877df045c0a75f2290d4133922a932619ab3c3661d6a70f7735568e901774cbeb74c055893844121de4f3db76cd8443c9966
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
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 rohan_bhide@hotmail.com. 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/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 brohan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Graphify
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/graphify`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'graphify'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install graphify
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphify. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "graphify"
|
5
|
+
require "./lib/graphify/graph.rb"
|
6
|
+
require "./lib/graphify/graph_matrix.rb"
|
7
|
+
require "./lib/graphify/graph_list.rb"
|
8
|
+
|
9
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
10
|
+
# with your gem easier. You can also use a different console, if you like.
|
11
|
+
|
12
|
+
|
13
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
14
|
+
# require "pry"
|
15
|
+
# Pry.start
|
16
|
+
|
17
|
+
require "irb"
|
18
|
+
IRB.start
|
data/bin/setup
ADDED
data/graphify.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'graphify/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "graphify"
|
8
|
+
spec.version = Graphify::VERSION
|
9
|
+
spec.authors = ["brohan"]
|
10
|
+
spec.email = ["brohan@seas.upenn.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{To generate random graphs and store it in your database}
|
13
|
+
spec.description = %q{To generate randomized graphs}
|
14
|
+
spec.homepage = "https://www.seas.upenn.edu/~cis196"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
spec.add_development_dependency "activerecord", "~> 4.2.4"
|
37
|
+
spec.add_development_dependency "sqlite3"
|
38
|
+
spec.add_development_dependency "ruby-graphviz"
|
39
|
+
spec.add_development_dependency "pry"
|
40
|
+
spec.add_development_dependency "simplecov"
|
41
|
+
end
|
data/graphify.png
ADDED
Binary file
|
data/lib/graphify.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "graphify/version"
|
2
|
+
|
3
|
+
module Graphify
|
4
|
+
|
5
|
+
def self.get_graph(name, num_vertices, num_edges, storage_type, type, weight_limit)
|
6
|
+
errors = check_params(name, num_vertices, num_edges, storage_type, type, weight_limit)
|
7
|
+
p errors if errors != {}
|
8
|
+
raise 'Invalid Parameters as above' if errors != {}
|
9
|
+
Graph.new({'name': name, 'num_vertices': num_vertices, 'num_edges': num_edges, 'storage_type': storage_type,
|
10
|
+
'type': type, 'weight_limit': weight_limit})
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def self.check_params(name, num_vertices, num_edges, storage_type, type, weight_limit)
|
16
|
+
errors = {}
|
17
|
+
errors['name'] = check_name(name) unless check_name(name).nil?
|
18
|
+
errors['num_vertices'] = check_num_vertices(num_vertices) unless check_num_vertices(num_vertices).nil?
|
19
|
+
errors['type'] = check_type(type) unless check_type(type).nil?
|
20
|
+
errors['weight_limit'] = check_weight_limit(weight_limit) unless check_weight_limit(weight_limit).nil?
|
21
|
+
errors['storage_type'] = check_storage_type(storage_type) unless check_storage_type(storage_type).nil?
|
22
|
+
errors['num_edges'] =
|
23
|
+
check_num_edges(num_edges, num_vertices, type) unless check_num_edges(num_edges, num_vertices, type).nil?
|
24
|
+
errors
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.check_name(name)
|
28
|
+
[] << 'Name cannot be empty or nil' if name == '' || name.nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.check_num_vertices(num_vertices)
|
32
|
+
[] << 'Number of vertices cannot be negative' if num_vertices < 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.check_num_edges(num_edges, num_vertices, type)
|
36
|
+
errors = []
|
37
|
+
errors << 'Number of edges cannot be negative' if num_edges < 0
|
38
|
+
edge_limit = type == 'directed' ? (num_vertices) * (num_vertices - 1) : ((num_vertices) * (num_vertices - 1) / 2)
|
39
|
+
errors << 'The number of edges is too large' if num_edges > edge_limit
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.check_storage_type(storage_type)
|
43
|
+
[] << 'Storage type is invalid' if storage_type != 'matrix' && storage_type != 'list'
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.check_type(type)
|
47
|
+
[] << 'Type of graph is invalid' if type != 'directed' && type != 'undirected'
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.check_weight_limit(weight_limit)
|
51
|
+
[] << 'Weight limit cannot be negative' if weight_limit < 0
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,296 @@
|
|
1
|
+
require 'ruby-graphviz'
|
2
|
+
require_relative 'graph_matrix'
|
3
|
+
require_relative 'graph_list'
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
def initialize(args)
|
7
|
+
@graph = if args[:storage_type] == 'matrix'
|
8
|
+
GraphMatrix.new(args)
|
9
|
+
elsif args[:storage_type] == 'list'
|
10
|
+
GraphList.new(args)
|
11
|
+
end
|
12
|
+
@type = args[:type] == 'directed' ? :digraph : :graph
|
13
|
+
@name = args[:name]
|
14
|
+
@storage_type = args[:storage_type]
|
15
|
+
end
|
16
|
+
|
17
|
+
def picture
|
18
|
+
g = GraphViz.new(:G, type: @type)
|
19
|
+
vertices = add_vertices_to_picture(g)
|
20
|
+
add_edges_to_picture(vertices, g)
|
21
|
+
g.output(png: @name + '.png')
|
22
|
+
end
|
23
|
+
|
24
|
+
def dfs_tree(vertex)
|
25
|
+
g = initialize_g
|
26
|
+
dfs_helper(vertex, [], g)
|
27
|
+
g
|
28
|
+
end
|
29
|
+
|
30
|
+
def edges
|
31
|
+
@graph.edges_with_weights
|
32
|
+
end
|
33
|
+
|
34
|
+
def dfs_tree_picture(vertex)
|
35
|
+
tree = dfs_tree(vertex)
|
36
|
+
if @storage_type == 'matrix'
|
37
|
+
produce_picture_from_matrix(tree, '_dfs_tree.png')
|
38
|
+
elsif @storage_type == 'list'
|
39
|
+
produce_picture_from_list(tree, '_dfs_tree.png')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def shortest_path_tree(vertex)
|
44
|
+
g = initialize_g
|
45
|
+
distances, predecessors = initialize_djikstras(vertex)
|
46
|
+
djikstras_helper(vertex, [], distances, predecessors, g)
|
47
|
+
g
|
48
|
+
end
|
49
|
+
|
50
|
+
def shortest_path_tree_picture(vertex)
|
51
|
+
tree = shortest_path_tree(vertex)
|
52
|
+
if @storage_type == 'matrix'
|
53
|
+
produce_picture_from_matrix(tree, '_shortest_path_tree.png')
|
54
|
+
elsif @storage_type == 'list'
|
55
|
+
produce_picture_from_list(tree, '_shortest_path_tree.png')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def scc_vertex_sets
|
60
|
+
scc_vertex_sets_helper
|
61
|
+
end
|
62
|
+
|
63
|
+
def scc_edge_set
|
64
|
+
construct_scc_edges(scc_vertex_sets)
|
65
|
+
end
|
66
|
+
|
67
|
+
def scc_picture
|
68
|
+
g = GraphViz.new(:G, type: @type)
|
69
|
+
picture_vertices = add_scc_vertices(scc_vertex_sets, g)
|
70
|
+
add_scc_edges(picture_vertices, scc_edge_set, g)
|
71
|
+
g.output(png: @name + '_strongly_connected_components.png')
|
72
|
+
end
|
73
|
+
|
74
|
+
def topological_sort
|
75
|
+
scc_total_ordering
|
76
|
+
end
|
77
|
+
|
78
|
+
def mst_picture
|
79
|
+
g = GraphViz.new(:G, type: @type)
|
80
|
+
picture_vertices = add_vertices_to_picture(g)
|
81
|
+
add_mst_edges(picture_vertices, g)
|
82
|
+
g.output(png: @name + '_minimum_spanning_tree.png')
|
83
|
+
end
|
84
|
+
|
85
|
+
def mst_edges
|
86
|
+
kruskals_edges
|
87
|
+
end
|
88
|
+
|
89
|
+
# Private helper functions
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
# Initialize the edge set for DFS, Djikstra's
|
94
|
+
|
95
|
+
def initialize_g
|
96
|
+
num_vertices = @graph.vertices.size
|
97
|
+
return Array.new(num_vertices) { Array.new(num_vertices) } if @storage_type == 'matrix'
|
98
|
+
g = {}
|
99
|
+
(0..num_vertices - 1).each do |num|
|
100
|
+
g[num] = {}
|
101
|
+
end
|
102
|
+
g
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
# Depth First Search Functions
|
107
|
+
|
108
|
+
def dfs_helper(vertex, seen, g)
|
109
|
+
seen << vertex
|
110
|
+
@graph.get_out_edges(vertex).each do |neighbour|
|
111
|
+
next if seen.include?(neighbour)
|
112
|
+
g[vertex][neighbour] = @graph.get_edge_weight(vertex, neighbour)
|
113
|
+
dfs_helper(neighbour, seen, g)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# Djikstra's Algorithm and Breadth First Search Functions
|
118
|
+
|
119
|
+
def initialize_djikstras(start)
|
120
|
+
dist = {}
|
121
|
+
predecessors = {}
|
122
|
+
@graph.vertices.each do |vertex|
|
123
|
+
dist[vertex] = vertex == start ? 0 : Float::INFINITY
|
124
|
+
predecessors[vertex] = vertex == start ? start : nil
|
125
|
+
end
|
126
|
+
return dist, predecessors
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
def djikstras_helper(vertex, seen, current_d, predecessors, g)
|
131
|
+
seen << vertex
|
132
|
+
add_predecessor(predecessors[vertex], vertex, g)
|
133
|
+
@graph.get_out_edges(vertex).each do |node|
|
134
|
+
next if seen.include?(node) || current_d[vertex] + @graph.get_edge_weight(vertex, node) > current_d[node]
|
135
|
+
current_d[node] = current_d[vertex] + @graph.get_edge_weight(vertex, node)
|
136
|
+
predecessors[node] = vertex
|
137
|
+
end
|
138
|
+
current_d.delete(vertex)
|
139
|
+
return if seen.size == predecessors.keys.size
|
140
|
+
djikstras_helper(current_d.key(current_d.values.min), seen, current_d, predecessors, g)
|
141
|
+
end
|
142
|
+
|
143
|
+
def add_predecessor(from, to, g)
|
144
|
+
return if to == from
|
145
|
+
g[from][to] = @graph.get_edge_weight(from, to)
|
146
|
+
end
|
147
|
+
|
148
|
+
def add_matrix_edge_to_picture(from, to, edges, vertices, picture)
|
149
|
+
return if edges[from][to] == nil
|
150
|
+
picture_edge = picture.add_edges(vertices[from], vertices[to])
|
151
|
+
picture_edge[label: edges[from][to].to_s]
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
# Strongly Connected Components functions
|
156
|
+
|
157
|
+
|
158
|
+
def scc_get_dfs_order(vertex, seen, finished)
|
159
|
+
seen << vertex
|
160
|
+
@graph.get_out_edges(vertex).each do |neighbour|
|
161
|
+
next if seen.include?(neighbour) || finished.include?(neighbour)
|
162
|
+
scc_get_dfs_order(neighbour, seen, finished)
|
163
|
+
end
|
164
|
+
finished.unshift(vertex)
|
165
|
+
end
|
166
|
+
|
167
|
+
def scc_transpose_dfs(vertex, seen, this_set)
|
168
|
+
seen << vertex unless seen.include?(vertex)
|
169
|
+
this_set << vertex unless this_set.include?(vertex)
|
170
|
+
@graph.get_in_edges(vertex).each do |neighbour|
|
171
|
+
scc_transpose_dfs(neighbour, seen, this_set) unless seen.include?(neighbour) || this_set.include?(neighbour)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def scc_total_ordering
|
176
|
+
finished = []
|
177
|
+
for i in (0.. @graph.vertices.size - 1)
|
178
|
+
scc_get_dfs_order(i, [], finished) unless finished.include?(i)
|
179
|
+
end
|
180
|
+
finished
|
181
|
+
end
|
182
|
+
|
183
|
+
def scc_vertex_sets_helper
|
184
|
+
dfs_sets, seen, counter = {}, [], 0
|
185
|
+
scc_total_ordering.each do |vertex|
|
186
|
+
next if seen.include?(vertex)
|
187
|
+
dfs_set = []
|
188
|
+
scc_transpose_dfs(vertex, seen, dfs_set)
|
189
|
+
dfs_sets[counter] = dfs_set
|
190
|
+
counter += 1
|
191
|
+
end
|
192
|
+
dfs_sets
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
def construct_scc_edges(scc_vertex_set)
|
197
|
+
edges = Array.new(scc_vertex_set.size) { Array.new(scc_vertex_set.size) }
|
198
|
+
@graph.edges_with_weights.each do |edge|
|
199
|
+
from = (scc_vertex_set.select { |k, value| value.include?(edge[0]) }).keys[0]
|
200
|
+
to = (scc_vertex_set.select { |k , value| value.include?(edge[1]) }).keys[0]
|
201
|
+
next if to == from || !edges[from][to].nil?
|
202
|
+
edges[from][to] = 1
|
203
|
+
end
|
204
|
+
edges
|
205
|
+
end
|
206
|
+
|
207
|
+
def add_scc_vertices(scc_vertex_set, g)
|
208
|
+
vertices = {}
|
209
|
+
scc_vertex_set.each do |key, value|
|
210
|
+
vertices[key] = g.add_nodes(value.reduce('') { |memo, vertex| memo + vertex.to_s + ', ' })
|
211
|
+
end
|
212
|
+
vertices
|
213
|
+
end
|
214
|
+
|
215
|
+
def add_scc_edges(scc_picture_vertices, scc_edge_set, g)
|
216
|
+
(0..scc_edge_set.size - 1).each do |from|
|
217
|
+
(0..scc_edge_set.size - 1).each do |to|
|
218
|
+
g.add_edges(scc_picture_vertices[from], scc_picture_vertices[to]) unless scc_edge_set[from][to].nil?
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
# Kruskal's and Minimum Spanning Trees
|
227
|
+
|
228
|
+
def kruskals_edges
|
229
|
+
num_vertices = @graph.vertices.size
|
230
|
+
sorted_edges = @graph.edges_with_weights.sort_by { |elem| elem[2] }
|
231
|
+
result = []
|
232
|
+
tree = []
|
233
|
+
sorted_edges.each do |edge|
|
234
|
+
break if tree.size == num_vertices
|
235
|
+
next if tree.include?(edge[0]) && tree.include?(edge[1])
|
236
|
+
tree << edge[0] unless tree.include?(edge[0])
|
237
|
+
tree << edge[1] unless tree.include?(edge[1])
|
238
|
+
result << edge
|
239
|
+
end
|
240
|
+
result
|
241
|
+
end
|
242
|
+
|
243
|
+
def add_mst_edges(vertices, g)
|
244
|
+
kruskals_edges.each do |edge|
|
245
|
+
picture_edge = g.add_edges(vertices[edge[0]], vertices[edge[1]])
|
246
|
+
picture_edge[label: edge[2].to_s]
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
# Functions to draw the main picture
|
253
|
+
|
254
|
+
|
255
|
+
def add_vertices_to_picture(g)
|
256
|
+
vertices = {}
|
257
|
+
@graph.vertices.each do |vertex|
|
258
|
+
vertices[vertex] = g.add_nodes(vertex.to_s)
|
259
|
+
end
|
260
|
+
vertices
|
261
|
+
end
|
262
|
+
|
263
|
+
def add_edges_to_picture(vertices, g)
|
264
|
+
@graph.edges_with_weights.each do |edge|
|
265
|
+
picture_edge = g.add_edges(vertices[edge[0]], vertices[edge[1]])
|
266
|
+
picture_edge[label: edge[2].to_s]
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def produce_picture_from_matrix(edges, type_name)
|
271
|
+
picture = GraphViz.new(:G, type: @type)
|
272
|
+
vertices = add_vertices_to_picture(picture)
|
273
|
+
(0..vertices.size - 1).each do |from|
|
274
|
+
(0..vertices.size - 1).each do |to|
|
275
|
+
add_matrix_edge_to_picture(from, to, edges, vertices, picture)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
picture.output(png: @name + type_name)
|
279
|
+
end
|
280
|
+
|
281
|
+
def produce_picture_from_list(edges, type_name)
|
282
|
+
picture = GraphViz.new(:G, type: @type)
|
283
|
+
vertices = add_vertices_to_picture(picture)
|
284
|
+
edges.keys.each do |key|
|
285
|
+
edges[key].keys.each do |s_key|
|
286
|
+
|
287
|
+
picture_edge = picture.add_edges(vertices[key], vertices[s_key])
|
288
|
+
picture_edge[label: edges[key][s_key].to_s]
|
289
|
+
end
|
290
|
+
end
|
291
|
+
picture.output(png: @name + type_name)
|
292
|
+
end
|
293
|
+
|
294
|
+
|
295
|
+
attr_accessor :graph
|
296
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class GraphList
|
2
|
+
|
3
|
+
def initialize(args)
|
4
|
+
@type = args[:type]
|
5
|
+
@weight_limit = args[:weight_limit]
|
6
|
+
@edge_lists = {}
|
7
|
+
if args.has_key?(:num_vertices)
|
8
|
+
(0..args[:num_vertices] - 1).each do |num|
|
9
|
+
@edge_lists[num] = {}
|
10
|
+
end
|
11
|
+
initialize_edges(args[:num_vertices], args[:num_edges])
|
12
|
+
else
|
13
|
+
@edge_lists = args[:edge_lists]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def get_out_edges(vertex)
|
19
|
+
@edge_lists[vertex].keys
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_in_edges(vertex)
|
23
|
+
@edge_lists.reduce([]) { |memo, (key, value)| value.has_key?(vertex) ? memo << key : memo }
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_edge_weight(to, from)
|
27
|
+
@edge_lists[to][from]
|
28
|
+
end
|
29
|
+
|
30
|
+
def vertices
|
31
|
+
result = []
|
32
|
+
(0..@edge_lists.keys.size - 1).each do |vertex|
|
33
|
+
result << vertex
|
34
|
+
end
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
def edges
|
39
|
+
result = []
|
40
|
+
@edge_lists.keys.each do |k|
|
41
|
+
@edge_lists[k].keys.each do |s_k|
|
42
|
+
next if @type == 'undirected' && s_k > k
|
43
|
+
result << [k, s_k]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
result
|
47
|
+
end
|
48
|
+
|
49
|
+
def edges_with_weights
|
50
|
+
result = []
|
51
|
+
@edge_lists.keys.each do |k|
|
52
|
+
@edge_lists[k].keys.each do |s_k|
|
53
|
+
next if @type == 'undirected' && s_k > k
|
54
|
+
result << [k, s_k, @edge_lists[k][s_k]]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
result
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def initialize_edges(num_vertices, num_edges)
|
63
|
+
counter = 0
|
64
|
+
while counter < num_edges
|
65
|
+
forward = rand(num_vertices)
|
66
|
+
to = rand(num_vertices)
|
67
|
+
next if forward == to
|
68
|
+
counter = add_edge_helper(to, forward, counter)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def add_edge_helper(to, from, counter)
|
73
|
+
return counter if @edge_lists[from].has_key?(to)
|
74
|
+
@edge_lists[from][to] = generate_edge_weight
|
75
|
+
@edge_lists[to][from] = @edge_lists[from][to] if @type == 'undirected'
|
76
|
+
counter + 1
|
77
|
+
end
|
78
|
+
|
79
|
+
def generate_edge_weight
|
80
|
+
if @weight_limit == 0
|
81
|
+
1
|
82
|
+
else
|
83
|
+
rand(@weight_limit)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
attr_accessor :edge_lists, :type, :weight_limit
|
88
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
class GraphMatrix
|
2
|
+
|
3
|
+
def initialize(args)
|
4
|
+
@type = args[:type]
|
5
|
+
@weight_limit = args[:weight_limit]
|
6
|
+
if args.has_key?(:num_vertices)
|
7
|
+
@edges = Array.new(args[:num_vertices]) { Array.new(args[:num_vertices]) }
|
8
|
+
initialize_helper(args[:num_vertices], args[:num_edges])
|
9
|
+
else
|
10
|
+
@edges = args[:edge_matrix]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_out_edges(vertex)
|
15
|
+
return if (vertex < 0 || vertex >= @edges.length)
|
16
|
+
vertices = []
|
17
|
+
(0..(@edges.length - 1)).each do |index|
|
18
|
+
vertices << index if @edges[vertex][index] != nil
|
19
|
+
end
|
20
|
+
vertices
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_in_edges(vertex)
|
24
|
+
return if (vertex < 0 || vertex >= @edges.length)
|
25
|
+
vertices = []
|
26
|
+
(0..(@edges.length - 1)).each do |index|
|
27
|
+
vertices << index if @edges[index][vertex] != nil
|
28
|
+
end
|
29
|
+
vertices
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_edge_weight(from, to)
|
33
|
+
@edges[from][to]
|
34
|
+
end
|
35
|
+
|
36
|
+
def vertices
|
37
|
+
vertices = []
|
38
|
+
(0..(@edges.length - 1)).each do |index|
|
39
|
+
vertices << index
|
40
|
+
end
|
41
|
+
vertices
|
42
|
+
end
|
43
|
+
|
44
|
+
def edges
|
45
|
+
result = []
|
46
|
+
(0..(@edges.length - 1)).each do |index|
|
47
|
+
(0..(@edges.length - 1)).each do |s_index|
|
48
|
+
next if @type == 'undirected' && s_index < index
|
49
|
+
result << [index, s_index] if @edges[index][s_index] != nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
def edges_with_weights
|
56
|
+
result = []
|
57
|
+
(0..(@edges.length - 1)).each do |index|
|
58
|
+
(0..(@edges.length - 1)).each do |s_index|
|
59
|
+
next if @type == 'undirected' && s_index < index
|
60
|
+
result << [index, s_index, @edges[index][s_index]] if @edges[index][s_index] != nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def initialize_helper(num_vertices, num_edges)
|
69
|
+
counter = 0
|
70
|
+
while counter < num_edges
|
71
|
+
from = rand(num_vertices)
|
72
|
+
to = rand(num_vertices)
|
73
|
+
next if from == to
|
74
|
+
counter = add_edge_helper(to, from, counter)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_edge_helper(to, from, counter)
|
79
|
+
return counter if @edges[from][to] != nil
|
80
|
+
@edges[from][to] = generate_edge_weight
|
81
|
+
@edges[to][from] = @edges[from][to] if @type == 'undirected'
|
82
|
+
counter + 1
|
83
|
+
end
|
84
|
+
|
85
|
+
def generate_edge_weight
|
86
|
+
if @weight_limit == 0
|
87
|
+
1
|
88
|
+
else
|
89
|
+
rand(@weight_limit)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
attr_writer :edges, :type, :weight_limit
|
95
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
class GraphMatrixDb < ActiveRecord::Base
|
3
|
+
validates edge_matrix, presence: true
|
4
|
+
|
5
|
+
def get_out_edge_matrix(vertex)
|
6
|
+
return if (vertex < 0 || vertex >= @edge_matrix.length)
|
7
|
+
vertices = []
|
8
|
+
(0..(@edge_matrix.length - 1)).each do |index|
|
9
|
+
vertices << index if @edge_matrix[vertex][index] == 1
|
10
|
+
end
|
11
|
+
vertices
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_in_edge_matrix(vertex)
|
15
|
+
return if (vertex < 0 || vertex >= @edge_matrix)
|
16
|
+
vertices = []
|
17
|
+
(0..(@edge_matrix.length - 1)).each do |index|
|
18
|
+
vertices << index if @edge_matrix[index][vertex] == 1
|
19
|
+
end
|
20
|
+
vertices
|
21
|
+
end
|
22
|
+
|
23
|
+
def vertices
|
24
|
+
vertices = []
|
25
|
+
(0..(@edge_matrix.length - 1)).each do |index|
|
26
|
+
vertices << index
|
27
|
+
end
|
28
|
+
vertices
|
29
|
+
end
|
30
|
+
|
31
|
+
def edges
|
32
|
+
result = []
|
33
|
+
(0..(@edge_matrix.length - 1)).each do |index|
|
34
|
+
(0..(@edge_matrix.length - 1)).each do |s_index|
|
35
|
+
result << [index, s_index] if @edge_matrix[index][s_index] == 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
result
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/main_test.png
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graphify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- brohan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.2.4
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.2.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: ruby-graphviz
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: To generate randomized graphs
|
126
|
+
email:
|
127
|
+
- brohan@seas.upenn.edu
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- graphify.gemspec
|
143
|
+
- graphify.png
|
144
|
+
- lib/graphify.rb
|
145
|
+
- lib/graphify/graph.rb
|
146
|
+
- lib/graphify/graph_list.rb
|
147
|
+
- lib/graphify/graph_matrix.rb
|
148
|
+
- lib/graphify/graph_matrix_db.rb
|
149
|
+
- lib/graphify/version.rb
|
150
|
+
- main_test.png
|
151
|
+
- main_test_dfs_tree.png
|
152
|
+
- main_test_shortest_path_tree.png
|
153
|
+
- main_test_strongly_connected_components.png
|
154
|
+
- main_test_undirected.png
|
155
|
+
- main_test_undirected_dfs_tree.png
|
156
|
+
- main_test_undirected_minimum_spanning_tree.png
|
157
|
+
- main_test_undirected_shortest_path_tree.png
|
158
|
+
homepage: https://www.seas.upenn.edu/~cis196
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata:
|
162
|
+
allowed_push_host: https://rubygems.org
|
163
|
+
post_install_message:
|
164
|
+
rdoc_options: []
|
165
|
+
require_paths:
|
166
|
+
- lib
|
167
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
requirements: []
|
178
|
+
rubyforge_project:
|
179
|
+
rubygems_version: 2.5.1
|
180
|
+
signing_key:
|
181
|
+
specification_version: 4
|
182
|
+
summary: To generate random graphs and store it in your database
|
183
|
+
test_files: []
|