Example #1
0
 /**
  * @covers Application\Math\Graph::getDistance
  * @covers Application\Math\Graph::solve
  */
 public function testGetDistance()
 {
     $this->object->addEdge(2100, 2101, 10);
     $this->object->addEdge(2101, 2102, 5);
     $this->object->addEdge(2101, 2100, 10);
     $this->object->addEdge(2102, 2101, 5);
     $this->object->setStartVertex(strtoupper(2100))->setEndVertex(strtoupper(2102));
     $this->assertSame(15, $this->object->getDistance());
 }
/**
 * 단어 목록으로 인접 리스트를 사용해서 그래프 생성
 *
 * @param string $word_file 단어파일
 * @return object Graph 그래프객체
 * @throws Exception
 */
function buildWordLadderGraph($word_file)
{
    $container = array();
    $graph = new Graph();
    $f = fopen($word_file, "r");
    if ($f) {
        while (($word = fgets($f)) !== false) {
            $container = setBucketContainer($container, trim($word));
        }
        fclose($f);
    } else {
        throw new Exception('파일을 읽을 수 없습니다.');
    }
    $bucket_keys = array_keys($container);
    foreach ($bucket_keys as $bucket) {
        foreach ($container[$bucket] as $word1) {
            foreach ($container[$bucket] as $word2) {
                if ($word1 != $word2) {
                    $graph->addEdge($word1, $word2);
                }
            }
        }
    }
    return $graph;
}
Example #3
0
 public function testCanNotAddRepeatedEdges()
 {
     $graph = new Graph(self::UNDIRECTED_GRAPH, self::NON_WEIGHTED_GRAPH);
     $graph->addEdge('node1', 'node2');
     $graph->addEdge('node1', 'node2');
     $this->assertCount(1, $graph->getEdges('node1'));
 }