Advertisement
AvA64

eccentricity

May 6th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.25 KB | None | 0 0
  1. require 'rubygems'
  2. require 'json'
  3.  
  4. class Graph
  5.     def initialize(data=nil)
  6.         @graph = data || get_json_data
  7.         @eccentricities = []
  8.     end
  9.  
  10.     def get_json_data
  11.         file = File.read("graph.json")
  12.         JSON.parse(file)
  13.     end
  14.  
  15.     def graph_nodes_count
  16.         @graph.keys
  17.     end
  18.  
  19.     def find_shortest_path(start, finish, travel_path=[])
  20.         travel_path = Array.new(travel_path + start.split)
  21.         return travel_path if (start == finish)
  22.         return nil         if !(@graph.include? start)
  23.         shortest_path = nil
  24.         for node in @graph[start] do
  25.             if (!travel_path.include? node)
  26.                 if (new_path = find_shortest_path(node, finish, travel_path))
  27.                     shortest_path = new_path if (!shortest_path || new_path.length < shortest_path.length)
  28.                 end
  29.             end
  30.         end
  31.         return shortest_path
  32.     end
  33.  
  34.     def node_eccentricity(eccentricity, path)
  35.         @eccentricities << eccentricity
  36.         puts "ECCENTRICITY OF PATH: #{path} IS #{eccentricity}"
  37.     end
  38.  
  39.     def get_radius
  40.         radius = 9999
  41.         for eccentricity in @eccentricities do
  42.             if radius > eccentricity then
  43.                 radius = eccentricity
  44.             end
  45.         end
  46.         radius
  47.     end
  48.  
  49.     def get_diameter
  50.         diameter = 0
  51.         for eccentricity in @eccentricities do
  52.             if diameter < eccentricity then
  53.                 diameter = eccentricity
  54.             end
  55.         end
  56.         diameter
  57.     end
  58.  
  59.     def show_all_eccentricities
  60.         nodes = graph_nodes_count
  61.         for start in nodes do
  62.             max = 0
  63.             max_path = []
  64.             for finish in nodes do
  65.                 next if (start == finish)
  66.                 path         = find_shortest_path(start, finish)
  67.                 eccentricity = path.length
  68.  
  69.                 total = nodes.count + ((nodes.count - 1) * 2) + 2 # ":"
  70.                 subtotal = eccentricity + ((eccentricity - 1) * 2)
  71.                 tab = " " * (total - subtotal)             
  72.  
  73.                 eccentricity -= 1
  74.  
  75.                 for point in path do
  76.                     (path.last == point) ? print("#{point}:#{tab}") : print("#{point}->") # Paths list
  77.                 end
  78.                 if (path && max < eccentricity) then
  79.                     max      = eccentricity
  80.                     max_path = path
  81.                 end
  82.                 puts max
  83.             end
  84.             puts
  85.             # puts max # and steps count
  86.             node_eccentricity(max, max_path)
  87.         end
  88.     end
  89. end
  90.  
  91. data = {'A'=> ['B', 'C'],
  92.         'B'=> ['A', 'C', 'D'],
  93.         'C'=> ['A', 'B', 'D', 'F'],
  94.         'D'=> ['B', 'C'],
  95.         'E'=> ['C', 'F'],
  96.         'F'=> ['C', 'E']
  97. }
  98.  
  99. graph = Graph.new()
  100. graph.show_all_eccentricities
  101. p graph.get_radius
  102. p graph.get_diameter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement