/**
 * Get the buses that are common between the two stops
 * @param $stopName1
 * @param $stopName2
 */
function getBusesCommonBetweenTwoStops($stopName1, $stopName2)
{
    //echo $stopName1.",".$stopName2;
    //could also use
    /*
    *
    * select * from newBusdetails as B,newBusDetails as A where (A.BusNumber=B.BusNumber) and (A.stopName='Vidyapeetha Circle' and
    B.stopName='Kamakya (Depot 13)')
    *
    */
    //get all the buses for stopName1
    $startBuses = getBusesForStopWithFrequency($stopName1);
    // echo "start buses";
    //print_r($startBuses);
    //echo "<br/>";
    $startBusesArray = explode(",", $startBuses);
    //get all the buses for stopName2
    $endBuses = getBusesForStopWithFrequency($stopName2);
    //echo "End buses";
    //print_r($endBuses);
    //echo "<br/>";
    $endBusesArray = explode(",", $endBuses);
    //get the intersection
    $commonBusesArrayTemp = array_intersect($startBusesArray, $endBusesArray);
    $commonBusesArray = array();
    // echo "common buses between".$stopName1."..and..".$stopName2;
    //print_r($commonBuses);
    $sizeofCommonBuses = sizeof($commonBusesArrayTemp);
    //required to reset the keys from 0
    foreach ($commonBusesArrayTemp as $key => $value) {
        array_push($commonBusesArray, $value);
    }
    //print_r($commonBusesArray);
    //echo "<br/>";
    /*$commonBusesString='';
        for($j=0;$j<$sizeofCommonBuses;$j++)
        {
    
            if($j==$sizeofCommonBuses-1)
                $commonBusesString = $commonBusesString.$commonBuses[$j];
            else
                $commonBusesString = $commonBusesString.$commonBuses[$j].",";
        }
        */
    //array_intersect_key()
    //need to fix the indexes of the array after the intersection
    //echo implode(",",sortBasedOnBusFrequency($commonBusesArray));
    //print_r($commonBusesArray);
    return sortBasedOnBusFrequency($commonBusesArray);
    //echo $commonBusesString;
    //return $commonBusesString;
}
Beispiel #2
0
/**
*getCommonBuses: function to get the buses for the start and teh end point
* the function will check what should be the stopover point for the buses
* means what is the combination of the bus to be used and also where to change the bus.
* Try to avoid the starting points of the bus as the junctions.
* need to find out which are the most common junctions and try to get the buses for that
* Also in case multiple buses fulfilling the combination is found then list them all. FOr e.g. take 505, 500K, 500C to Marathatlli and then 
* change to 400, 335E for the Final destination

* Changes DOne : 26/Dec/2011: Added the conditional Indirect route. So in case you get only one or two direct routes we can offer the 
* user an option to check for the indirect buses also.
* Comment added : 09/01/2012: Frequency data obtained for few buses. That data can be used to sort the arrays. We can have the sorting algo whihc 
* will take care of the frequency based sorting of the final array.
* We are taking care of only the distance that is required to walk. Along with that we need to put some weight on the frequency. I feel we should perform some kind 
* of the weighted evealuation of the sorting.
* In case of the direct bus it is straight forward
* IN caese of the indirect bus we need to check if both the buses are frequent or not
* so basically we will check busA+busB. 
* first sort it on the basis of the distances whihc is already done
* now we will sort them on the basis of sum of the combination frequency. So best number is 4+4=8 and worst is 1+1=2
* need to add teh frequency data for the missing buses
* I think the frequecny wise sorting can also be done in the indirectJunction function
**/

function getCommonBuses($startBuses,$endBuses,$showOnlyIndirectBuses)
{
	// actually the arrays have the last entry as null so 1 less
	$arrSameBus=array();
	for($ii=0;$ii<sizeof($startBuses)-1;$ii++)
	{
		$tempStart=$startBuses[$ii];
		/*for($jj=0;$jj<sizeof($endBuses);$jj++)
		{
			if(strcmp($tempStart,$end
		}*/
		// this checks is the same bus is available in the start buses and end buses.
		if(in_array($tempStart,$endBuses))
		{			
			//return $tempStart.":".$tempStart;
			$element=$tempStart.":".$tempStart;
			array_push($arrSameBus,$element);
		}			

	}

	
	if(sizeof($arrSameBus)>0 && $showOnlyIndirectBuses==0)
	{
		/*echo "before sort <br/>";
		print_r($arrSameBus);
		echo "after sorting <br/>";
		$sortedArray= sortBasedOnBusFrequency($arrSameBus);
		print_r($sortedArray);*/
		return sortBasedOnBusFrequency($arrSameBus); // at this point we can sort it out based on the frequency of the buses and also let the know the meaning of the frequency
	}
	
	// in case you need to get the indirect buses forcefully then remove the common buses from the array of the $startBuses and $endBuses

	for($i=0;$i<sizeof($arrSameBus);$i++)
	{
		$temp=split(":",$arrSameBus[$i]);
		$key=array_search($temp[0],$startBuses);
		unset($startBuses[$key]);
		$startBuses=array_values($startBuses);
		
	}
	
	for($i=0;$i<sizeof($arrSameBus);$i++)
	{
		$temp=split(":",$arrSameBus[$i]);
		$key=array_search($temp[0],$endBuses);
		unset($endBuses[$key]);
		$endBuses=array_values($endBuses);
		
	}

	//return $startBuses;
	
	// if the common bus is not found
	//echo "no comon bus found";
	// in case the common bus is not found we need to see what is the distance that the user has to walk and the 
	// stop from where he can get the bus. That will also determin the choice of the bus. If the distance to walk is more then 
	//consider other bus.
	//echo "jjj";
	$indirectBusDistances=array();
	for($i=0;$i<sizeof($startBuses)-1;$i++)
	{
		$firstBus=$startBuses[$i];
		$minimum=10000;
		for($j=0;$j<sizeof($endBuses)-1;$j++)
		{
			$secondBus=$endBuses[$j];
			list($firstBusNumber,$firstRouteNumber,$firstBusStop,$secondBusNumber,$secondRouteNumber,$secondBusStop,$distance)=split(":",getIntermediateStopsAndDistance($firstBus,$secondBus));			
			if($distance<=$minimum)
			{
				$minimum=$distance;
				$element=new IndirectBusStructure($firstBus,$secondBus,$distance);
				array_push($indirectBusDistances,$element);
			}
			
			
		}

	}

	$sortedIndirectBuses=BubbleSort($indirectBusDistances,sizeof($indirectBusDistances));
	//print_r($sortedIndirectBuses);
	// create an array that will contain the various combinations as found
	$arrBuses=array();
	$arrBIASBuses=array();	// array to collect the BIAS BIAS combination
	// try to avoid the BIAS and BIAS combination.

	for($k=0;$k<sizeof($sortedIndirectBuses);$k++)
	{
		//echo $sortedIndirectBuses[$k]->getFirstBus();
		$pos1=strpos($sortedIndirectBuses[$k]->getFirstBus(),'BIAS');
		$pos2=strpos($sortedIndirectBuses[$k]->getSecondBus(),'BIAS');
		//echo $pos1.$pos2;
		if($pos1!==false && $pos2!==false)
		{
			$element=$sortedIndirectBuses[$k]->getFirstBus().":".$sortedIndirectBuses[$k]->getSecondBus();
			//echo $element;
			array_push($arrBIASBuses,$element);
			
		}//continue;
		else
		{	
			//return $sortedIndirectBuses[$k]->getFirstBus().":".$sortedIndirectBuses[$k]->getSecondBus();
			//echo "dskdjsjd";
			$element=$sortedIndirectBuses[$k]->getFirstBus().":".$sortedIndirectBuses[$k]->getSecondBus();
			//echo $element;
			array_push($arrBuses,$element);
			
		}
	}
	// if BIAS and BIAS is the only combination then go for it
	//return $startBuses[0].":".$endBuses[0];
	 if(sizeof($arrBuses)>0)
	{
		 //print_r($arrBuses);
		 return sortBasedOnBusFrequency($arrBuses);
	}
	else
	{
		//return $sortedIndirectBuses[0]->getFirstBus().":".$sortedIndirectBuses[0]->getSecondBus();
		//print_r($arrBIASBuses);
		return sortBasedOnBusFrequency($arrBIASBuses);
	}
		
}