[英]Printing relations between members
我有一個大學項目,其中必須逐級打印不同班級的學生之間的關系。 這個想法是,如果我們讓John和Kris在同一個班級學習,他們是第一級的朋友,如果Kris在同一門課程上學習數學,那么John和Math是第二級的朋友。 我研究了這個問題,發現了像這樣的算法,但是我的主要問題是我將對象用作輸入數據:
<?php
class Student {
private $id = null;
private $classes = [];
public function __construct($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
public function getClasses() {
return $this->classes;
}
public function addClass(UClass $class) {
array_push($this->classes, $class);
}
}
class UClass {
private $id = null;
private $students= [];
public function __construct($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
public function getStudents() {
return $this->students;
}
public function addStudent(Student $student) {
array_push($this->students, $student);
$student->addClass($this);
}
}
function getRelations(Student $start_student, &$tree = array(), $level = 2, &$visited) {
foreach ($start_student>Classes() as $class) {
foreach ($class->Students() as $student) {
if($start_student->getId() != $student->getId() && !is_int(array_search($student->getId(), $visited))) {
$tree[$level][] = $student->getId();
array_push($visited, $student->getId());
getRelations($student, $tree, $level+1, $visited);
}
}
}
}
$class = new UClass(1);
$class2 = new UClass(2);
$class3 = new UClass(3);
$student = new Student(1);
$student2 = new Student(2);
$student3 = new Student(3);
$student4 = new Student(4);
$student5 = new Student(5);
$student6 = new Student(6);
$class->addStudent($student);
$class->addStudent($student2);
$class->addStudent($student4);
$class2->addStudentr($student2);
$class2->addStudent($student4);
$class2->addStudent($student5);
$class3->addStudent($student4);
$class3->addStudent($student5);
$class3->addStudent($student6);
$tree[1][] = $student->getId();
$visited = array($student->getId());
getRelations($student, $tree, 2, $visited);
print_r($tree);
我被困在編寫getRelations()函數,該函數應該創建類似於
Array ( [1] => Array ( [0] => 1 ) [2] => Array ( [0] => 2 [1] => 4 ) [3] => Array ( [0] => 5 [1] => 6 ) )
但是我無法獲得正確的遞歸(或者可能是整個算法)。 任何幫助將不勝感激。
我想出了那個功能(不確定這是否是最好的解決方案,但是它可以與類對象一起使用)
function print_students(Student $start_student, &$tree = array(), $lvl = 1) {
if (!$start_student) {
return;
}
$tree[$lvl][] = $start_student->getId();
$q = array();
array_push($q, $start_student);
$visited = array($start_student->getId());
while (count($q)) {
$lvl++;
$lvl_students = array();
foreach ($q as $current_student) {
foreach ($current_student->getClasses() as $class) {
foreach ($class->getStudents() as $student) {
if (!is_int(array_search($student->getId(), $visited))) {
array_push($lvl_students, $student);
array_push($visited, $student->getId());
$tree[$lvl][] = $student->getId();
}
}
}
}
$q = $lvl_students;
}
}
遞歸過程中的邏輯不正確。 例:
假設您輸入了某個級別A的程序,並且實際上有2個學生可以找到該級別的連接。
您先處理,分配正確的級別A,將他標記為“已訪問”。
然后,在進入第二名之前,您需要為第一個學生處理A + 1級。 在他的“鏈條”中的某個地方,您可能還會發現第二名正在等待被A級處理的學生。但是,現在他被分配了更高的A + n級,然后被標記為已訪問。
接下來,當student1的遞歸完成時,您將繼續第二次。 但是,他已經被“訪問”了……
(順便說一句,我不太了解(但是我的PHP很弱...)為什么您第一次調用GetRelations時指定的級別為2。)
無論如何,要使您的邏輯正確,就無需遞歸。
向每個學生添加一個屬性“級別”。 將所有學生也放在一個總體集合中。
然后,對於所選的“ startStudent”,給他自己級別= 0,所有其他學生級別= -1。
迭代級別並嘗試填寫友誼級別,直到無所事事為止。 我的php實際上不存在,因此我嘗試了一些偽代碼。
for(int level=0; ; level++) // no terminating condition here
{
int countHandled = 0;
for each (student in population.students)
{
if (student.level==level)
{
for each (class in student.classes)
{
for each (student in class.students)
{
if(student.level==-1)
{
student.level = level+1;
countHandled++;
}
}
}
}
}
if(countHandled==0)
break;
}
希望這可以幫助你。 當然,您仍然必須填寫樹/打印內容。 我的貢獻僅涉及正確分配級別的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.