Module: Graphos::Algorithm

Includes:
Containers
Defined in:
lib/graphos/algorithm/prim.rb,
lib/graphos/algorithm/dijkstra.rb

Class Method Summary collapse

Class Method Details

.dijkstra(graph, initial) ⇒ Object

Runs the dijstra algorithm on a given graph at a starting node This uses a Heap to get the lightest edge



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphos/algorithm/dijkstra.rb', line 10

def self.dijkstra graph, initial
  #OK? E o path?
  fathers = Array.new(graph.size)

  #os paf
  allPaths = Array.new(graph.size)

  #OK #dist[v] = infinito
  costs = Array.new(graph.size, Float::INFINITY)
  #dist[s] = 0
  costs[initial] = 0

  #OK
  heap = Heap.new{|x,y| (costs[x] <=> costs[y]) == -1}
  (0...graph.size).each{|i| heap.push(i)}

  update_cost = -> (idx,cost) do
    costs[idx] = cost
    heap.change_key(idx,idx)
  end


  #Para cada vértice v
  #enquanto heap (S-V) != 0
  while idx=heap.pop
    #Selecione u em V-S, tal que dist[u] é mínima
    u = graph[idx]
    distu = costs[idx]
    allPaths[idx] ||= Path.new
    #Para cada vizinho v (edge.to) de u faça
    u.edges.each do |edge|
      #Se dist[v] > dist[u] + w(u,v) então
      if costs[edge.to.index] > distu + edge.weight
        #dist[v] = dist[u] + w(u,v)
        fathers[edge.to.index] = u.index
        update_cost.call(edge.to.index, distu + edge.weight)
        #criar o Path entre root e v
        #se existe já, tem q atualizar. O novo é o do pai + ele msm
        allPaths[edge.to.index] = allPaths[u.index] + Path.new(edge)
      end
    end
  end

  allPaths
end

.prim(graph, initial) ⇒ Object

Runs the prim algorithm in order to find a MST for a given graph.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphos/algorithm/prim.rb', line 10

def self.prim graph, initial
  fathers = Array.new(graph.size)

  costs = Array.new(graph.size, Float::INFINITY)
  costs[initial] = 0

  heap = Heap.new{|x,y| (costs[x] <=> costs[y]) == -1}
  (0..graph.size-1).each{|i| heap.push(i)}

  update_cost = -> (idx,cost) do
    costs[idx] = cost
    heap.change_key(idx,idx)
  end

  while idx=heap.pop
    node = graph[idx]
    node.edges.each do |edge|
      if costs[edge.to.index] > edge.weight
        fathers[edge.to.index] = node.index
        update_cost.call(edge.to.index, edge.weight)
      end
    end
  end

  result = Weighted::Graph.new graph.size
  fathers.each_with_index do |f,c|
    if f
      result.add_edge(f, c, costs[c])
    end
  end
  result
end