Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted by STom ( 13 years ago )
<?php

function findMinConnections($array) {
 $count = 0;
 $foundKey = 0;

 foreach($array as $key => $value) {

  if($count == 0) {
   $count = count($value);
   $foundKey = $key;
  }

  if($count > count($value) && count($value) != 0) {
   $count = count($value);
   $foundKey = $key;
  }
 }

 return array($foundKey, $count); 
}

$array = array(
 '1' => array(
   2, 3, 4
  ),
 '2' => array(
   1, 3, 4
  ),
 '3' => array(
   1, 2, 4
  ),
 '4' => array(
   1, 2, 3
  ),
 '5' => array(
   6
  ),
 '6' => array(
   5, 7
  ),
 '7' => array(
   6
  )
 );


$photos = array();


while(true) {
 $min = findMinConnections($array);

 if($min[1] == 0) {
  break;
 }

 $count = -1;
 $minKey = 0;

 foreach($array[$min[0]] as $key => $value) {
  
  if($count == -1) {
   $count = count($array[$value]);
   $minKey = $value;
  }

  if(count($array[$value]) < $count) {
   $count = count($array[$value]);
   $minKey = $value;
  }
 }

 $photos[] = array($min[0], $minKey);

 unset($array[$min[0]]);
 unset($array[$minKey]);

 foreach($array as $keys => $values) {
  foreach($values as $key => $value) {
   if($value == $min[0] || $value == $minKey) {
    unset($array[$keys][$key]);
   }
  }
 }
}

print_r($photos);

?>

 

Revise this Paste

Your Name: Code Language: