Skip to content

Update fork #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exclude_patterns:
- "html/"
- "test/"
- "docs/"
39 changes: 30 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
name: Ruby
name: Build

on: [push,pull_request]

jobs:
build:
runs-on: ubuntu-latest
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.2
bundler-cache: true
- name: Run the default task
run: bundle exec rake
- uses: actions/checkout@v2
- name: Install Dependencies
run: |
sudo sed -i 's/azure\.//' /etc/apt/sources.list
sudo apt-get update
sudo apt-get install -y libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.2
bundler-cache: true
- name: Install Codeclimate
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
- name: Before-build Codeclimate
run: ./cc-test-reporter before-build
- uses: actions/cache@v2
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Run the default task
run: bundle exec rake
- name: After-build Codeclimate
run: ./cc-test-reporter after-build
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in visual_graphs.gemspec
gemspec
gem 'ruby2d', '~> 0.9.2'
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GEM
docile (1.4.0)
minitest (5.14.4)
rake (13.0.6)
ruby2d (0.9.5)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -18,12 +19,14 @@ GEM

PLATFORMS
x64-mingw32
x86_64-linux

DEPENDENCIES
minitest (~> 5.0)
rake (~> 13.0)
ruby2d (~> 0.9.2)
simplecov (~> 0.3)
visual_graphs!

BUNDLED WITH
2.2.20
2.2.22
71 changes: 68 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,84 @@
[![Build](https://github.com/mmcs-ruby/visual_graphs/actions/workflows/main.yml/badge.svg)](https://github.com/mmcs-ruby/josephjoguts/visual_graphs/workflows/main.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/48549da79ca45b3cc525/maintainability)](https://codeclimate.com/github/mmcs-ruby/visual_graphs/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/48549da79ca45b3cc525/test_coverage)](https://codeclimate.com/github/mmcs-ruby/visual_graphs/test_coverage)

# VisualGraphs

## Usage

```ruby
graph = Graph.new(path_to_file)
#simple not weighted graph
# json import
graph = Graph.load_from_json(path)

#initialization from hash
hash_graph = {1 => [2, 3], 2 => [1, 4]}
graph = Graph.adjacency_list_init(hash_graph)

graph.vertices #return list of vertices
graph.edges #return list of edges
```
```ruby
#adding new vertex (vertex could be numbers,chars .etc)
# if vertex is already exists it will not be added
graph.insert_vertex(new_vertex)
```
```ruby
#adding new edge for simple Graph
# vertices will also be included in vertices list of graph if they are not there
# also add second_vertex to adjacency_list of first_vertex
graph.insert_edge([first_vertex, second_vertex])
```
```ruby
graph.output_to_standard_stream # prints graph(adjacency_list)
```
```ruby
#will create(overwrite) json_file and save adjacency_list in it
#json_file name must ends with .json
graph.dump_to_json(path_to_file)
```

```ruby
WeightedGraph - weighted_adjacency_list

# json import
# will raise error if
graph = WeightedGraph.load_from_json(path)

#initialization from hash
hash_graph = {1 => [[2, 3]], 2 => [[1, 4]]}
graph = WeightedGraph.adjacency_list_init(hash_graph)

graph.vertices #return list of vertices
graph.edges #return list of edges

#v1,v2 vertices,w - weight
graph.insert_edge([v1,[v2,w]])
```

## Data formats

```json
```
for simple not weighted Graph class
{
"1":[2],
"2":[3],
"3":[1]
"3":[1,4]
"4": []
}

hash = {1 => [2,3]}

for weightedGraph
{
"1":[[2,1]],
"2":[[3,4]],
"3":[[1,5],[4,6]]
"4": []
}

hash = {1 => [[2,3], [3,4]]}

```


Expand Down
7 changes: 7 additions & 0 deletions lib/visual_graphs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@
require_relative "visual_graphs/graph"
module VisualGraphs
class Error < StandardError; end

class InvalidJSONFileNameError < RuntimeError; end

class IncorrectArgumentsForGraphInit < StandardError;end
# error for wrong parameters in weighted graph initialization
class WrongParamsForWeightedGraphInit< StandardError;end

end
70 changes: 63 additions & 7 deletions lib/visual_graphs/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,80 @@

module VisualGraphs
class Graph
def initialize(path)
file = File.open(path)
attr_accessor :adjacency_list
attr_accessor :edges

def initialize
@adjacency_list = {}
@edges = []
end

def self.load_from_json(path)
graph = Graph.new
file = File.open(path)
temp = JSON.load(file)
temp.each do |k, v|
temp_k = k.to_i
@adjacency_list[temp_k] = v
v.each { |adj_vertex| @edges << [temp_k, adj_vertex] }
graph.adjacency_list[temp_k] = v
v.each { |adj_vertex| graph.edges << [temp_k, adj_vertex] }
end
file.close
graph
end


def self.adjacency_list_init(list)
graph = Graph.new
list.each do |pair|
graph.adjacency_list.store(pair[0], pair[1])
pair[1].each {|vertex| graph.edges << [pair[0], vertex] }
end
graph
end

def vertices()
def vertices
@adjacency_list.keys
end

def edges()
@edges

def insert_vertex(vertex)
unless @adjacency_list.keys.include? vertex
@adjacency_list[vertex] = []
return true
end
false
end

# edge is array [1,2] , where 1 and 2 are vertex
# also will insert vertexes unless they are not in adjecency_list
def insert_edge(edge)
unless @edges.include? edge
insert_vertex(edge[0])
insert_vertex(edge[1])
@adjacency_list[edge[0]] << edge[1]
@edges << edge
return true
end
false
end

def output_to_standard_stream
@adjacency_list.each do |vertex, adjacency_list|
puts "#{vertex} : #{adjacency_list}"
end
end

def correct_path?(path)
path.is_a? String and path.end_with? '.json'
end

# will create json file and dump graph to it
# throws No such file or directory error
def dump_to_json(path)
raise InvalidJSONFileNameError unless correct_path? path

File.open(path, 'w') { |f| f.write(@adjacency_list.to_json) }
end

end
end
54 changes: 54 additions & 0 deletions lib/visual_graphs/weighted_graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require_relative 'graph'

module VisualGraphs
class WeightedGraph < VisualGraphs::Graph
def initialize()
super()
end

def self.load_from_json(path)
graph = Graph.load_from_json(path)
weighted_graph = WeightedGraph.new
weighted_graph.edges = graph.edges
weighted_graph.adjacency_list = graph.adjacency_list
graph_is_weighted?(graph.edges)
weighted_graph
end

def self.adjacency_list_init(list)
graph = Graph.adjacency_list_init(list)
weighted_graph = WeightedGraph.new
weighted_graph.edges = graph.edges
weighted_graph.adjacency_list = graph.adjacency_list
graph_is_weighted?(graph.edges)
weighted_graph
end

#check that all edges are: [v1, [v2,w]]
# v1,v2 - vertices , w - weight
def self.graph_is_weighted?(edges)
edges.each do |edge|
unless correct_edge?(edge)
raise WrongParamsForWeightedGraphInit
end
end
end

def self.correct_edge?(edge)
edge[1].is_a? Array and edge[1].length == 2
end

def insert_edge(edge)
if (WeightedGraph.correct_edge? edge) && !(@edges.include? edge)
insert_vertex(edge[0])
insert_vertex(edge[1][0])
@adjacency_list[edge[0]] << edge[1]
@edges << edge
return true
end
false
end

end

end
Loading