Skip to content

Repository files navigation

BuildMaintainabilityTest Coverage

VisualGraphs

Usage

#simple not weighted graph# json importgraph=Graph.load_from_json(path)#initialization from hashhash_graph={1=>[2,3],2=>[1,4]}graph=Graph.adjacency_list_init(hash_graph)graph.vertices#return list of verticesgraph.edges#return list of edges
#USAGE of Vertex Class# initialization:vertex=Vertex.new(:name=>"name",:value=>10,:color=>"black")# default name is empty string (""), default color is "black"
# print Vertex info to output:puts`vertex = #{vertex}`# it will be formatted like:# "vertex = Vertex <key: 0 value: 10 color: red name: name>"
#adding new vertex (vertex could be numbers,chars .etc)# if vertex is already exists it will not be addedgraph.insert_vertex(new_vertex)
#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])
graph.output_to_standard_stream# prints graph(adjacency_list)
#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)
WeightedGraph - weighted_adjacency_list# json import# will raise error if graph=WeightedGraph.load_from_json(path)#initialization from hashhash_graph={1=>[[2,3]],2=>[[1,4]]}graph=WeightedGraph.adjacency_list_init(hash_graph)graph.vertices#return list of verticesgraph.edges#return list of edges#v1,v2 vertices,w - weightgraph.insert_edge([v1,[v2,w]])
MinHeap - min_heap# used for implementation of Prim's algorithm# initializes empty heapmin_heap=MinHeap.new# insertion of new vertexmin_heap << Vertex.new(key: 2,value: 3)# delete heap element with key of elem_to_deleteelem_to_delete=Vertex.new(key: 12)deleted=min_heap.delete_element(elem_to_delete)# count amount of actual elements in heapcnt=min_heap.count# get the minimum key from the heapvert=min_heap.peek_min# iteration of heap elementsmin_heap.elements.each_with_indexdo |element,index|
puts"#{index} element: #{element}"end# return the actual elements and re-heapify the minheapvert=min_heap.extract_min# check if heap contains elementmin_heap.contains_element(element)# print heap element.to_s for each heap element line by linemin_heap.print_heap# Graph based on adjacency matrix# creating a graph using default constructorgraph=AdjMatrixGraph.new# loading a graph from JSON-filegraph.load_from_json(path)# unloading a graph into JSON-filegraph.dump_to_json(path)# loading a graph from an object of Array typeobject=[[2,1],[3,4]]# adjacency matrix as Array objectgraph.load_from_array(object)# getting arrays of vertices and edges from graphgraph.verticesgraph.edges# adding vertices and edges to the graphgraph.add_vertex(8)graph.add_edge(3,5,4)# edge from vertex 3 to vertex 5 with weight 4# prints a graph to console in a nice wayputsgraph

Convert adjacency list based graph to adjacency matrix based graph return adjacency matrix based graph

graphList=Graph.adjacency_list_init({1=>[2],2=>[1]})graphMatrix=graphList.to_adj_matrix_graphgraphMatrix.to_s# => "Adjacency matrix:\n0 1 \n1 0 \n"

Data formats

for simple not weighted Graph class
{
"1":[2],
"2":[3],
"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]]}

Installation

Add this line to your application's Gemfile:

gem'visual_graphs'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install visual_graphs

Usage

TODO: Write usage instructions here

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/visual_graphs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the VisualGraphs project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages