Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > PHP > PHP Forum > สอบถามวิธีการทำ topological sort หน่อยครับ (แบบที่มีการจัดเรียงโดยเอาตัวที่มัน depend มาขึ้นก่อนน่ะครับ)



 

สอบถามวิธีการทำ topological sort หน่อยครับ (แบบที่มีการจัดเรียงโดยเอาตัวที่มัน depend มาขึ้นก่อนน่ะครับ)

 



Topic : 127612



โพสกระทู้ ( 4,720 )
บทความ ( 8 )



สถานะออฟไลน์




ผมมีข้อมูล array แบบนี้

Code (PHP)
$test = [
    ['id' => 'one', 'deps' => ['two']],
    ['id' => 'two', 'deps' => ['three', 'four']],
    ['id' => 'three', 'deps' => []],
    ['id' => 'four', 'deps' => []],
    ['id' => 'five', 'deps' => ['four']],
    ['id' => 'six', 'deps' => []],
];


ผมอยากให้มันเรียงออกมาเป็น...
Code (PHP)
[
    ['id' => 'three', 'deps' => []],
    ['id' => 'four', 'deps' => []],
    ['id' => 'two', 'deps' => ['three', 'four']],
    ['id' => 'one', 'deps' => ['two']],
    ['id' => 'five', 'deps' => ['four']],
    ['id' => 'six', 'deps' => []],
];


ปัจจุบันมีโค้ดที่ลองไปแล้วหลายตัว ได้แก่
1 http://stackoverflow.com/questions/39711720/php-order-array-based-on-elements-dependency
2 http://stackoverflow.com/questions/15901159/how-can-i-rearrange-array-items-moving-dependencies-on-top
แต่ว่ายังไม่มีตัวไหนทำงานได้อย่างที่ต้องการ ที่ทำได้ใกล้เคียงที่สุดลำดับมันก็ค่อนข้างมั่วเลยครับ ไม่ให้ความสำคัญกับลำดับที่ปรากฏมาก่อนในต้นฉบับเลย
ยกตัวอย่างลิ้งค์แรกจะได้ three, four, five, six, two, one ซึ่งควรจะเป็น three, four, two, one, five, six.

ทีนี้ส่วนของกระผมที่ทำไปแล้วก็มีอยู่ แต่มันออกไม่หมด เพราะมันติดตรงส่งผ่านค่าที่เป็น sub ย่อยลงไปแล้วตัวแม่มันหาย -_-!
Code (PHP)
function topologicalSort(array $items, array &$sorted = [])
{
    foreach ($items as $indexKey => $item) {
        echo 'loop items: '.$item['id'].'<br>';// debug
        if (isset($item['deps']) && is_array($item['deps']) && !empty($item['deps'])) {
            echo ' &nbsp;&nbsp; '.$item['id'].' has depend.<br>';// debug
            foreach ($item['deps'] as $dependency_id) {
                echo ' &nbsp;&nbsp; searching key for depend '.$dependency_id.'<br>';// debug
                if (false !== $items_key = array_search($dependency_id, array_column($items, 'id'))) {
                    echo ' &nbsp;&nbsp; depend on '.$dependency_id.' => '.$items_key.'<br>';// debug
                    echo ' &nbsp;&nbsp; &nbsp; &nbsp; &gt;&gt; ';// debug
                    //$sorted[] = $item;
                    topologicalSort([$items_key => $items[$items_key]], $sorted);
                    echo ' &nbsp;&nbsp; after request on depend '.$dependency_id.' from '.$item['id'].'<br>';// debug
                }
            }// endforeach;
            unset($dependency_id);
            echo ' &nbsp;&nbsp; end load dependencies for '.$item['id'].'<br>';// debug
        } else {
            echo ' &nbsp;&nbsp; '.$item['id'].' doesn\'t have depend.<br>';// debug
            if (isset($item['id']) && false === $item_key = array_search($item['id'], array_column($sorted, 'id'))) {
                echo ' &nbsp;&nbsp; [['.$item['id'].' was set.]]<br>';// debug
                $sorted[] = $item;
            }
        }// endif; dependency.
    }// endforeach;
    unset($indexKey, $item);
}// topologicalSort




Tag : PHP









ประวัติการแก้ไข
2017-05-15 19:12:59
2017-05-15 19:13:31
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-05-15 19:09:46 By : mr.v View : 660 Reply : 1
 

 

No. 1



โพสกระทู้ ( 4,720 )
บทความ ( 8 )



สถานะออฟไลน์


ได้ละ ใช้ marcj/topsort
https://github.com/marcj/topsort.php
ติดตั้งผ่าน composer https://packagist.org/packages/marcj/topsort

Code (PHP)
<?php

$test = [
    ['id' => 'one', 'deps' => ['two']],
    ['id' => 'two', 'deps' => ['three', 'four']],
    ['id' => 'three', 'deps' => []],
    ['id' => 'four', 'deps' => []],
    ['id' => 'five', 'deps' => ['four']],
    ['id' => 'six', 'deps' => []],
    ['id' => 'seven', 'deps' => ['eight']],
    ['id' => 'eight', 'deps' => ['nine']],
    ['id' => 'nine', 'deps' => ['one']],
];

// the sorted should be three, four, two, one, five, six, nine, eight, seven

require 'vendor/autoload.php';

var_dump($test);
echo '<hr>';
echo '<div>the result should be &quot;three, four, two, one, five, six, nine, eight, seven&quot;</div>'."\n";


$Sorter = new \MJS\TopSort\Implementations\FixedArraySort();

foreach ($test as $item) {
    if (isset($item['id']) && isset($item['deps'])) {
        $Sorter->add($item['id'], $item['deps']);
    }
}// endforeach;

$result = $Sorter->sort();

var_dump($result);
unset($result, $Sorter);









ประวัติการแก้ไข
2017-05-16 14:03:12
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-05-16 13:42:44 By : mr.v
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามวิธีการทำ topological sort หน่อยครับ (แบบที่มีการจัดเรียงโดยเอาตัวที่มัน depend มาขึ้นก่อนน่ะครับ)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่