project2/p2q3-alt.rb
def find_way_all(adj_matrix, origin)
  q = PriorityQueue.new
  n = adj_matrix.size

  distance = Array.new(n, 999999) # best known distance to each node
  processed = Array.new(n, false) # if the node has been processed before
  previous = Array.new(n)         # previous node to node => the best known dist

  # all things start from the origin
  distance[origin] = 0
  processed[origin] = true
  q.push([0, origin])

  # keep processing until we're done (reach destination)
  while !q.empty?
    # pop off from PQ
    cost, node = q.pop
    processed[node] = true

    # examine all unvisited neighbours
    neighbours = adj_matrix[node]
    for d in 0...n
      if !processed[d]
        if !neighbours[d].nil?
          # calculate new cost via node to d
          new_cost = distance[node] + neighbours[d]

          # if we have a better path, update
          if new_cost < distance[d]
            distance[d] = new_cost
            previous[d] = node
            q.push([new_cost, d])
          end
        end
      end
    end
  end

  return distance
end
# find shortest path to all nodes from given origin
find shortest path to all nodes from given origin
find shortest path to all nodes from given origin
def find_way_all(adj_matrix, origin)
  q = PriorityQueue.new
  n = adj_matrix.size
  distance = Array.new(n, 999999) 
  processed = Array.new(n, false) 
  previous = Array.new(n)         
  distance[origin] = 0
  processed[origin] = true
  q.push([0, origin])
  while !q.empty?
    cost, node = q.pop
    processed[node] = true
    neighbours = adj_matrix[node]
    for d in 0...n
      if !processed[d]
        if !neighbours[d].nil?
          new_cost = distance[node] + neighbours[d]
          if new_cost < distance[d]
            distance[d] = new_cost
            previous[d] = node
            q.push([new_cost, d])
          end
        end
      end
    end
  end
  return distance
end
def find_way_all ( adj_matrix , origin ) q = PriorityQueue . new n = adj_matrix . size distance = Array . new ( n , 999999 ) processed = Array . new ( n , false ) previous = Array . new ( n ) distance [ origin ] = 0 processed [ origin ] = true q . push ( [ 0 , origin ] ) while ! q . empty? cost , node = q . pop processed [ node ] = true neighbours = adj_matrix [ node ] for d in 0 ... n if ! processed [ d ] if ! neighbours [ d ] . nil? new_cost = distance [ node ] + neighbours [ d ] if new_cost < distance [ d ] distance [ d ] = new_cost previous [ d ] = node q . push ( [ new_cost , d ] ) end end end end end return distance end
find shortest path to all nodes from given origin
find shortest path to all nodes from given origin
# best known distance to each node # if the node has been processed before # previous node to node => the best known dist # all things start from the origin # keep processing until we're done (reach destination) # pop off from PQ # examine all unvisited neighbours # calculate new cost via node to d # if we have a better path, update
adj_matrix
origin