Skip to content

Instantly share code, notes, and snippets.

@auser
Created September 22, 2009 22:36
Show Gist options
  • Select an option

  • Save auser/191504 to your computer and use it in GitHub Desktop.

Select an option

Save auser/191504 to your computer and use it in GitHub Desktop.
require "rubygems"
require "gratr"
require "gratr/dot"
# 1 -> 2 -> 3
# |-> 6 -> |-> 4 -> 5 -> 2
# 2 -> 4 -> 5
dg = GRATR::Digraph[1,2, 2,3, 2,4, 4,5, 6,4, 1,6]
module GRATR
class Digraph
# Crappy n*n
def find_cycle(from=self)
return [] unless cyclic?
cyclic_cycle = []
forward_edge = Proc.new {|e| }
back_edge = Proc.new do |b|
cyclic_cycle = dfs_tree_from_vertex(b)
end
from.dfs({
:forward_edge => forward_edge,
:back_edge => back_edge
})
cyclic_cycle
end
end
end
# p dg.find_cycle
dep = 2
r = 5
existing_connections = dg.adjacent(dep)
p existing_connections
existing_connections.each {|c| dg.remove_edge!(r, c) }
dg.add_edge!(dep, r, dep)
existing_connections.each {|c| dg.add_edge!(dep, c) }
dg.write_to_graphic_file("png", "/tmp/graph")
`open /tmp/graph.png`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment