U E D R , A S I H C RSS

Von Neumann Airport/Leonardong

Trafficν•˜κ³  Configuration을 각각 2차원 ν–‰λ ¬λ‘œ ν‘œν˜„ν–ˆλ‹€. Traffic은 ( origin, destination )에 λ”°λ₯Έ traffic양이고, Configuration은 originμ—μ„œ destination κΉŒμ§€ 떨어진 κ±°λ¦¬λΌ μ €μž₯ν•œ 행렬이닀. 전체 νŠΈλž˜ν”½μ€ ν–‰λ ¬μ—μ„œ 같은 μœ„μΉ˜μ— μžˆλŠ” μ›μ†ŒλΌλ¦¬ κ³±ν•˜λ„λ‘ λ˜μ–΄μžˆλ‹€. μž…μΆœλ ₯ 뢀뢄은 μ œμ™Έν•˜κ³  전체 νŠΈλž˜ν”½ κ΅¬ν•˜λŠ” κΈ°λŠ₯κΉŒμ§€λ§Œ κ΅¬ν˜„ν–ˆλ‹€.

맀우 데이터에 의쑴적인 ν”„λ‘œκ·Έλž¨μ΄λΌλŠ” 생각이 λ“ λ‹€. λ§Œμ•½ μ„μ²œμ΄ν˜• μƒκ°λŒ€λ‘œ μš”κ΅¬μ‚¬ν•­μ΄ 바뀐닀면 μ§€κΈˆ ν”„λ‘œκ·Έλž¨μ€ 감당해낼 수 μžˆμ„κΉŒ?

λ¦¬νŒ©ν† λ§ ν•  것.
ν˜„μž¬ μš”κ΅¬μ‚¬ν•­μ— λ§žμΆ”λ”λΌλ„ 행렬에 ν•΄λ‹Ήν•˜λŠ” κΈ°λŠ₯을 μ •λ¦¬ν•˜κ³ , νŠΈλž˜ν”½μ΄λ‚˜ 거리 κ³„μ‚°μ—λŠ” 행렬을 μ΄μš©ν•˜λŠ” 식이 λ˜μ–΄μ•Ό ν•  것이닀. μ§€κΈˆμ²˜λŸΌ 상속받을 κΉŒλ‹­μ΄ μ—†λ‹€.
ν–‰λ ¬ κ³±μ…ˆ 연산을 μ •μ˜ν•˜λ©΄ μ΄λΌ μ΄μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” νŠΈλž˜ν”½μ΄λ‚˜ 거리 ν–‰λ ¬ 쀑 μ–΄λŠ ν•œ μͺ½μ€ μ „μΉ˜ν–‰λ ¬μ΄ λ˜μ–΄μ•Ό ν•œλ‹€. 그러렀면 μ „μΉ˜ ν–‰λ ¬ 연산도 μžˆμ–΄μ•Όκ² λ‹€.
κ±°λ¦¬λΌ κ³„μ‚°ν•˜λŠ” 더 쒋은 μ•Œκ³ λ¦¬μ¦˜μ΄ μžˆμ„κΉŒ? μ§€κΈˆμ€ O(num of city^2).


~cpp
#1h 48m
import unittest

class Matrix:
    def __init__(self, numofGates):
        self.matrix = []
        for i in range( numofGates ):
            self.matrix.append([0]*numofGates)
    def getElement( self, origin, destination ):
        return self.matrix[origin-1][destination-1]
    
class DistanceMatrix(Matrix):
    def __init__(self, numofGates):
        Matrix.__init__(self, numofGates)
    def construct( self, origins, destinations ):
        for o in origins:
            for d in destinations:
                self.matrix[o-1][d-1] = abs( origins.index(o)
                                             - destinations.index(d) ) + 1
    def getDistance( self, origin, destination ):
        return self.getElement( origin, destination )

class TrafficMatrix(Matrix):
    def __init__(self, numofGates):
        Matrix.__init__(self, numofGates)
    def construct( self, origin, traffics ):
        for traffic in traffics:
            self.matrix[origin-1][traffic.destination-1] = traffic.load
    def getLoad( self, origin, destination ):
        return self.getElement( origin, destination )

class Traffic:
    def __init__(self, destination, load):
        self.destination = destination
        self.load = load

def total( traffic, distance ):
    result = 0
    for origin in range( 1, MAX+1 ):
        for dest in range( 1, MAX+1 ):
            result += distance.getDistance(origin, dest)\
                      * traffic.getLoad(origin, dest)
    return result
MAX = 25
class TestDistance(unittest.TestCase):
    def setUp(self):    
        self.distMatrix = DistanceMatrix(MAX)
        self.traffic = TrafficMatrix(MAX)
    def testOneToOneDistance(self):
        self.distMatrix.construct( origins     = range(1,MAX+1),
                                   destinations= range(1,MAX+1) )
        for i in range( 1, MAX+1 ):
            self.assertEquals( 1, self.distMatrix.getDistance(i, i) )
    def testReversalDistance(self):
        self.distMatrix.construct( origins     = range(1,MAX+1),
                                   destinations= range(MAX,0,-1) )
        self.assertEquals( 1, self.distMatrix.getDistance( 1, MAX ) )
        self.assertEquals( MAX, self.distMatrix.getDistance( 1, 1 ) )
        self.assertEquals( 1, self.distMatrix.getDistance( MAX/2+1,# middle
                                                           MAX/2+1 ) )
    def testTraffic(self):
        self.traffic.construct( origin   = 1,
                                traffics = [Traffic(1,100),
                                            Traffic(2,50)] )
        self.assertEquals( 100, self.traffic.getLoad( origin     = 1,
                                                      destination= 1 ) )
        self.assertEquals( 50, self.traffic.getLoad( origin     = 1,
                                                     destination= 2 ) )
    def testTotal(self):
        self.traffic.construct( origin   = 1,
                           traffics = [Traffic(2,100)])
        self.traffic.construct( origin   = 2,
                           traffics = [Traffic(1,200)])
        self.distMatrix.construct( origins      = [1,2],
                                   destinations = [1,2] )
        self.assertEquals( 600, total( self.traffic, self.distMatrix ) )
        self.distMatrix.construct( origins      = [1,2],
                                   destinations = [2,1] )
        self.assertEquals( 300, total( self.traffic, self.distMatrix ) )
    def testReal(self):
        self.traffic.construct( origin   = 1,
                   traffics = [Traffic(2,10), Traffic(3,15)])
        self.traffic.construct( origin   = 2,
                   traffics = [Traffic(3,10)])
        self.traffic.construct( origin   = 3,
                   traffics = [Traffic(1,12), Traffic(2,20)])
        self.distMatrix.construct( origins      = [1,2,3],
                                   destinations = [2,3,1] )
        self.assertEquals( 122, total( self.traffic, self.distMatrix ) )
        self.distMatrix.construct( origins      = [2,3,1],
                                   destinations = [3,2,1] )
        self.assertEquals( 119, total( self.traffic, self.distMatrix ) )
if __name__ == "__main__":
    unittest.main()

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:23
Processing time 0.0350 sec