Skip to content

Commit

Permalink
Add PERT to GraphViz::Theory
Browse files Browse the repository at this point in the history
  • Loading branch information
glejeune committed Aug 6, 2010
1 parent a0dfb2c commit 1331b35
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 88 deletions.
47 changes: 47 additions & 0 deletions examples/theory/pert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
$:.unshift( "../../lib" )
require 'graphviz'
require 'graphviz/theory'

g = GraphViz.digraph( "G", :path => "/usr/local/bin", :use => "fdp" ) do |g|
g.node[:shape => "record"]

g.start[:label => "Start"]
g.task_1[:label => "Task #1 - Duration : 4d"]
g.task_2[:label => "Task #2 - Duration : 5.25d"]
g.task_3[:label => "Task #3 - Duration : 5.17d"]
g.task_4[:label => "Task #4 - Duration : 6.33d"]
g.task_5[:label => "Task #5 - Duration : 5.17d"]
g.task_6[:label => "Task #6 - Duration : 4.5d"]
g.task_7[:label => "Task #7 - Duration : 5.17d"]
g.finish[:label => "End"]

(g.start << g.task_1)[:weight => 0.0]
(g.start << g.task_2)[:weight => 0.0]
(g.task_1 << g.task_3)[:weight => 4.0]
(g.task_1 << g.task_4)[:weight => 4.0]
(g.task_2 << g.task_5)[:weight => 5.25]
(g.task_3 << g.task_5)[:weight => 5.17]
(g.task_4 << g.task_6)[:weight => 6.33]
(g.task_5 << g.task_7)[:weight => 5.17]
(g.task_6 << g.finish)[:weight => 4.5]
(g.task_7 << g.finish)[:weight => 5.17]
end
g.output( :png => "PERT.png" )

t = GraphViz::Theory.new( g )

print "Ranges : "
rr = t.range
p rr
puts "Your graph contains circuits" if rr.include?(nil)

puts "Critical path : "
rrr = t.critical_path
print "\tPath :"
_ = ""
rrr[:path].each do |i|
print _ + g.get_node_at_index(i-1).id
_ = " -> "
end
puts
puts "\tDistance : #{rrr[:distance]}"
45 changes: 22 additions & 23 deletions examples/theory/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
require 'graphviz'
require 'graphviz/theory'

require 'pp'

g = GraphViz.digraph( "G", :path => "/usr/local/bin" ) do |g|
g.a # 1
g.b # 2
g.c # 3
g.d # 4
g.e # 5
g.f # 6
g.a[:label => "1"]
g.b[:label => "2"]
g.c[:label => "3"]
g.d[:label => "4"]
g.e[:label => "5"]
g.f[:label => "6"]

g.a << g.a
# g.a << g.a
g.a << g.b
g.a << g.d
(g.a << g.f)[:weight => 6, :label => "6"]
Expand All @@ -22,14 +20,15 @@
g.c << g.d
(g.c << g.f)[:weight => 2, :label => "2"]
g.d << g.e
# g.e << g.c
end
g.output( :png => "matrix.png" )

t = GraphViz::Theory.new( g )

puts "Adjancy matrix : "
puts t.adjancy_matrix
# => [ 1 1 0 1 0 1]
# => [ 0 1 0 1 0 1]
# [ 0 0 1 1 1 0]
# [ 0 0 0 1 0 1]
# [ 0 0 0 0 1 0]
Expand All @@ -40,7 +39,7 @@

puts "Incidence matrix :"
puts t.incidence_matrix
# => [ 2 1 1 1 0 0 0 0 0 0]
# => [ 1 1 1 1 0 0 0 0 0 0]
# [ 0 -1 0 0 1 1 1 0 0 0]
# [ 0 0 0 0 -1 0 0 1 1 0]
# [ 0 0 -1 0 0 -1 0 -1 0 1]
Expand All @@ -51,16 +50,6 @@
puts "Degree of node `#{name}' = #{t.degree(node)}"
end

puts "Degree matrix : "
puts t.degree_matrix
# => [ 4 0 0 0 0 0]
# [ 0 4 0 0 0 0]
# [ 0 0 3 0 0 0]
# [ 0 0 0 4 0 0]
# [ 0 0 0 0 2 0]
# [ 0 0 0 0 0 2]


puts "Laplacian matrix :"
puts t.laplacian_matrix
# => [ 3 -1 0 -1 0 -1]
Expand All @@ -75,8 +64,18 @@
if r.nil?
puts "No way !"
else
print "Path : "; p r[:path]
print "Distance : #{r[:distance]}"
print "\tPath : "; p r[:path]
puts "\tDistance : #{r[:distance]}"
end
# => Path : ["a", "b", "c", "f"]
# Distance : 4.0

print "Ranges : "
rr = t.range
p rr
puts "Your graph contains circuits" if rr.include?(nil)

puts "Critical path : "
rrr = t.critical_path
print "\tPath "; p rrr[:path]
puts "\tDistance : #{rrr[:distance]}"
Loading

0 comments on commit 1331b35

Please sign in to comment.