How to group route of journey (Set cover problem)

52 Views Asked by At

I have this issue need to solve:

Input:

  • List of route between city, all route will start from same origin
  • Set of all city

Output:

  • Group of path that can visit all city, if not found then return group cover the most of city with minimum of route.

Example:

Testcase 1:

Input

  • Route List:

    1.Newyork -> Washington -> Los Angeles -> Chicago
    2.Newyork -> Houston -> Alaska
    3.Newyork -> Dallas
    4.Newyork -> Houston -> Washington -> Chicago -> Los Angeles -> Alaska
    5.Newyork -> Alaska
    6.Newyork -> Chicago
    7.Newyork -> Los Angeles

  • City: Newyork, Washington, Los Angeles, Chicago, Alaska, Dallas, Houston

Output:

1.Newyork -> Houston -> Washington -> Chicago -> Los Angeles -> Alaska
2.Newyork -> Dallas

This is best group because with only 2 route it can visit all given cities

Testcase 2:

Input

  • Route List:

    1.Newyork -> Washington -> Los Angeles -> Chicago
    2.Newyork -> Houston -> Alaska
    3.Newyork -> Dallas
    4.Newyork -> Alaska
    5.Newyork -> Chicago
    6.Newyork -> Los Angeles

  • City: Newyork, Washington, Los Angeles, Chicago, Alaska, Dallas, Houston

Output:

1.Newyork -> Washington -> Los Angeles -> Chicago
2.Newyork -> Houston -> Alaska
3.Newyork -> Dallas

This is best group because with 3 routes it can visit all given cities

Testcase 3:

Input

  • Route List:

    2.Newyork -> Houston -> Alaska
    3.Newyork -> Dallas
    4.Newyork -> Alaska
    5.Newyork -> Chicago
    6.Newyork -> Los Angeles

  • City: Newyork, Washington, Los Angeles, Chicago, Alaska, Dallas, Houston

Output:

1.Newyork -> Houston -> Alaska
2.Newyork -> Chicago
3.Newyork -> Dallas
4.Newyork -> Los Angeles

This is best group because with 4 routes it can visit 6/7 cities (except Washington)

implementation

public static void main(String[] args){
    List<List<String>> allRoutes = new ArrayList<>();
    allRoutes.add(List.of("Newyork","Washington","Los Angeles","Chicago"));
    allRoutes.add(List.of("Newyork","Houston","Alaska"));
    allRoutes.add(List.of("Newyork","Dallas"));
    allRoutes.add(List.of("Newyork","Houston","Washington","Chicago","Los Angeles","Alaska"));
    allRoutes.add(List.of("Newyork","Alaska"));
    allRoutes.add(List.of("Newyork","Chicago"));
    allRoutes.add(List.of("Newyork","Los Angeles"));
    Set<String> cities = new HashSet<>(List.of("Newyork","Houston","Washington","Chicago","Los Angeles","Alaska"));

    List<List<String>> result = findBestRoute(allRoutes, cities);
    //expected allPaths:
    //`Newyork -> Houston -> Washington -> Chicago -> Los Angeles -> Alaska`\
    //`Newyork -> Dallas`

  }

private static void findBestRoute(List<List<String>> allRoutes, Set<String> cities){
   //implementation
}

So any advice for algorithm i can use?

0

There are 0 best solutions below