Sha256: 73ebd84999c869ffa529b7bf06348212d76c690a9d1fd0c6e3da9effb389795f

Contents?: true

Size: 1.35 KB

Versions: 15

Compression:

Stored size: 1.35 KB

Contents

#!/usr/bin/env ruby
$: << File.dirname(__FILE__)+'/../../lib'
require 'wukong'

class Edge < Struct.new(:src, :dest)
end

class ASymmetricB < Edge
end

module Wukong::Streamer
  class EdgeStreamer < Wukong::Streamer::Base
    def recordize line
      rsrc, src, dest, *_ = super(line)
      [ASymmetricB.new(src.to_i, dest.to_i)]
    end
  end
end

#
# Find symmetric links
#
# Takes adjacency list for a directed graph and emits only edges where
#   A->B and B->A
#
# The output will list each such symmetric edge exactly once as
#    a_symmetric_b   node1    node2
# where node1 is lexicographically less than node2.
#
module FindSymmetricLinks

  class Mapper < Wukong::Streamer::EdgeStreamer
    def process edge
      yield edge.to_flat(false)
      yield ASymmetricB.new(edge.dest, edge.src).to_flat(false)
    end
  end

  #
  #
  class Reducer < Wukong::Streamer::Base
    def stream
      %x{/usr/bin/uniq -c}.split("\n").each do |line|
        key_count, rsrc, src, dest, data = line.chomp.strip.split(/\s+/, 4)
        next unless key_count.to_i == 2
        next unless src.to_i < dest.to_i
        emit [src, dest, data].compact
      end
    end
  end

  class Script < Wukong::Script
    def default_options
      super.merge :sort_fields => 3
    end
  end
end

# Execute the script
Wukong::Script.new(
  FindSymmetricLinks::Mapper,
  FindSymmetricLinks::Reducer
  ).run

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
wukong-2.0.0 examples/network_graph/gen_symmetric_links.rb
wukong-1.5.4 examples/network_graph/gen_symmetric_links.rb
wukong-1.5.3 examples/network_graph/gen_symmetric_links.rb
wukong-1.5.2 examples/network_graph/gen_symmetric_links.rb
wukong-1.5.1 examples/network_graph/gen_symmetric_links.rb
wukong-1.5.0 examples/network_graph/gen_symmetric_links.rb
wukong-1.4.12 examples/network_graph/gen_symmetric_links.rb
wukong-1.4.11 examples/network_graph/gen_symmetric_links.rb
wukong-1.4.10 examples/network_graph/gen_symmetric_links.rb
wukong-1.4.9 examples/network_graph/gen_symmetric_links.rb
wukong-1.4.7 examples/graph/gen_symmetric_links.rb
wukong-1.4.6 examples/graph/gen_symmetric_links.rb
wukong-1.4.5 examples/graph/gen_symmetric_links.rb
wukong-1.4.2 examples/graph/gen_symmetric_links.rb
wukong-1.4.1 examples/graph/gen_symmetric_links.rb