secret
anonymous / rohitsasikumar.rb
Last active

  • Download Gist
rohitsasikumar.rb
Ruby
1 2 3 4 5 6 7 8 9 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
require 'test/unit'
 
=begin
 
Assumptions: Paths given as input should always indicate the flow of current.
For eg: [A, B, 50] should mean current flowing from A to B in circuit. If the above is
indicated by [B, A, 50] then the program wont give the desired output.
=end
 
# this function takes as input an array of paths, a source node and a destination node
def find_redundant_resistors(input, src, dest)
nodes = []
weight_nodes = Hash.new(1<<32)
prev_node = {}
# convert the input to form {"node1"=>{"neighbour1"=>7, "neighbour2"=>9}, "node2"=>{"neighbour1"=>5, "neighbour2"=>7,}}
graph = input.inject({}) do |hash, path|
hash[path[0]] ||= Hash.new
hash[path[0]][path[1]] = path[2]
hash
end
nodes = graph.keys
# follow the dijkstra algorithm and find least weight and best predecessor of all the nodes
weight_nodes[src] = 0
until (nodes.empty?)
# find the node with the minimum weight
min = nil
nodes.each do |node|
min = node if weight_nodes[node] < weight_nodes[min]
end
break if min.nil?
# update the weight and predecessor of nodes
nodes = nodes - [min]
graph[min] and graph[min].keys.each do |v|
total = weight_nodes[min] + graph[min][v]
if total < weight_nodes[v]
weight_nodes[v] = total
prev_node[v] = min
end
end
end
# find the shortest path and create an array of arrays similar to the format of input
path = []
until prev_node[dest].nil?
path << [prev_node[dest], dest, weight_nodes[dest] - weight_nodes[prev_node[dest]]]
dest = prev_node[dest]
end
# return the paths which do not form the shortest path
input - path
end
 
# Some testcases
class TestRedundantResistors < Test::Unit::TestCase
def test_redundant_resistors
assert_equal(
find_redundant_resistors(
[['A','B',50]],
'A','B'),
[])
assert_equal(
find_redundant_resistors(
[['A','B',50],
['B', 'C', 50],
['A', 'C', 110]],
'A','C'),
[['A', 'C', 110]] )
assert_equal(
find_redundant_resistors(
[[ 'A', 'B', 50],
[ 'A', 'D', 150],
[ 'B', 'C', 250],
[ 'B', 'E', 250],
[ 'C', 'E', 350],
[ 'C', 'F', 100],
[ 'D', 'C', 50],
[ 'D', 'F', 400],
[ 'E', 'G', 200],
[ 'F', 'G', 100]],
'A', 'G' ),
[["A", "B", 50],
["B", "C", 250],
["B", "E", 250],
["C", "E", 350],
["D", "F", 400],
["E", "G", 200]] )
assert_equal(
find_redundant_resistors(
[[ 'A', 'B', 7],
[ 'A', 'C', 9],
[ 'A', 'F', 14],
[ 'B', 'C', 10],
[ 'B', 'D', 15],
[ 'C', 'D', 11],
[ 'C', 'F', 2],
[ 'D', 'E', 6],
[ 'F', 'E', 9]],
'A', 'E' ),
[[ 'A', 'B', 7],
[ 'A', 'F', 14],
[ 'B', 'C', 10],
[ 'B', 'D', 15],
[ 'C', 'D', 11],
[ 'D', 'E', 6]] )
assert_equal(
find_redundant_resistors(
[[ 'A', 'B', 1],
[ 'A', 'H', 50],
[ 'B', 'I', 60],
[ 'B', 'C', 7],
[ 'C', 'I', 100],
[ 'C', 'D', 2],
[ 'D', 'I', 4],
[ 'D', 'E', 3],
[ 'E', 'I', 10],
[ 'E', 'F', 200],
[ 'F', 'I', 50],
[ 'F', 'G', 80],
[ 'G', 'I', 70],
[ 'G', 'H', 90],
[ 'I', 'H', 5],
[ 'I', 'A', 15]],
'A', 'H' ),
[["A", "H", 50],
["B", "I", 60],
["C", "I", 100],
["D", "E", 3],
["E", "I", 10],
["E", "F", 200],
["F", "I", 50],
["F", "G", 80],
["G", "I", 70],
["G", "H", 90],
["I", "A", 15]])
 
assert_equal(
find_redundant_resistors(
[[ 'A', 'B', 50],
[ 'D', 'A', 150],
[ 'B', 'C', 250],
[ 'B', 'E', 250],
[ 'C', 'E', 350],
[ 'C', 'F', 100],
[ 'D', 'C', 50],
[ 'D', 'F', 400],
[ 'G', 'E', 200],
[ 'F', 'G', 100]],
'D', 'E' ),
[["A", "B", 50],
["D", "A", 150],
["B", "C", 250],
["B", "E", 250],
["C", "F", 100],
["D", "F", 400],
["G", "E", 200],
["F", "G", 100]])
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.