Home > OS >  recursive function return all the possible paths
recursive function return all the possible paths

Time:01-17

I try to write recursive function which gets an array of flights and returns all the possible paths from source country to dest country.

The function signature in Java:

List<List<String>> printAllPathsUtil(String src, String d,List<Flight> localPathList)

every object of type Flight contain 2 features from and to.

I succeed to write the below code and its run successfuly,but I sent to the function one more parameter : String help -who store the source country.

public List<List<String>> printAllPathsUtil(**String help**,String src, String d,List<Flight> localPathList)

I would appreciate it if someone could please find a way how to use the function without the extra parameter.

Note:I implemented the classes in a minimal way just to have parameters to send to the function, therefore there is no get and set in the classes.

import java.util.ArrayList;
import java.util.List;
public class flligth {
    public static void main(String args[]) {
        
    Flight f1 = new Flight("ISRAEL","ROMANIA");
    Flight f2 = new Flight("ISRAEL","HOLAND");
    Flight f3 = new Flight("ISRAEL","LONDON");
    Flight f4 = new Flight("ISRAEL","U.S.A");
    Flight f5 = new Flight("HOLAND","LONDON");
    Flight f6 = new Flight("LONDON","ROMANIA"); 
      
    all_flight all_f=new all_flight(6);
    all_f.addEdge(f1);f1.print();
    all_f.addEdge(f2);f2.print();
    all_f.addEdge(f3);f3.print();
    all_f.addEdge(f4);f4.print();
    all_f.addEdge(f5);f5.print();
    all_f.addEdge(f6);f6.print();
    List <Flight> localPathList=new ArrayList<>();
    all_f.printAllPathsUtil("ISRAEL","ISRAEL","ROMANIA",localPathList);
    }
    
}
class Flight{
    String from;
    String to;
    public Flight(String from ,String to){
       this.from=from;
       this.to=to;
    }

    public void print()
    {
     System.out.print(from " ");
     System.out.println(to);
    }
    
}
class all_flight{
    static int current=0;
    // adjacency list
    public ArrayList<Flight> f;
    int index;
    // Constructor
    public all_flight(int index){
    this.index=index;
    initFlight();
    }
    // utility method to initialise
    // f list
    private void initFlight()
    {
        f = new ArrayList<Flight>();
        
    }
    // add edge from u to v
    public void addEdge( Flight path)
    {
        // Add to list.
        f.add(path);
    }
    public List<List<String>> printAllPathsUtil(String help,String src, String d,List<Flight> localPathList)
    {
       Flight now = f.stream()
       .filter(a -> a.from.equals(src)).findFirst()
      .orElse(new Flight("no from","no to"));
       if(now.from.equals("no from")){ 
           f.remove(0);
           localPathList.clear();
           if(!(f.isEmpty())){
              return printAllPathsUtil(help,f.get(0).from,d,localPathList);  
           }
           return null;
       }
      
         localPathList.add(now);
        
       if(localPathList.get(localPathList.size()-1).to.equals(d) 
         && localPathList.get(0).from.equals(help)){
               
           
        System.out.println("the path is :");
        printPath(localPathList);
        }
      return printAllPathsUtil(help,now.to,d,localPathList);    
     }


    private static void printPath(List<Flight> path)
    {
        for(Flight v : path)
        {
            v.print();
        }
        System.out.println();
    }
    
   
}

CodePudding user response:

Whenever you invoke the printAllPathsUtil() function, you're passing the same value of help each time. Since the property stays constant for each recursive call, you can simply remove it from the parameters.

Instead, you can create a class attribute origin, and replace the usage of help in the function with self.origin.

CodePudding user response:

If localPathList is empty, then help is src. Otherwise, it's localPathList.get(0).from.

  • Related