Thursday, August 18, 2011

Path Finding in PHP

Lately create a path finding algorithm example using PHP for my junior to refer. Hopefully this code will also benefit others who look for same solution.



Scenario
Have bunch of records which is in the tree structure with one entry point. Need to find all the possible path from entry point node until the leave node.

Following is the tree structure.

          1     
        / | \
       /  |  \
      2   3   14
     /    |
    /     |
   4      5
  /      / \
 /      /   \
6      7     8 
      /       \      
     9         10
     |          /\
    11         /  \
   / | \      12   13
  /  |  \      |
 16  17  18   15



Expected Path:
ROUTE1:ROUTE2:ROUTE4:ROUTE6
ROUTE1:ROUTE3:ROUTE5:ROUTE7:ROUTE9:ROUTE11:ROUTE16
ROUTE1:ROUTE3:ROUTE5:ROUTE7:ROUTE9:ROUTE11:ROUTE17
ROUTE1:ROUTE3:ROUTE5:ROUTE7:ROUTE9:ROUTE11:ROUTE18
ROUTE1:ROUTE3:ROUTE5:ROUTE8:ROUTE10:ROUTE12:ROUTE15
ROUTE1:ROUTE3:ROUTE5:ROUTE8:ROUTE10:ROUTE13
ROUTE1:ROUTE14


Click here to Download. 

No comments:

Post a Comment