8000 added backtracking problem · RockLee444/Java-Algorithms@483e2c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 483e2c0

Browse files
committed
added backtracking problem
1 parent 9173120 commit 483e2c0

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

Backtracking/FlightItinerary.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package backtracking;
2+
3+
import java.util.HashSet;
4+
import java.util.Stack;
5+
6+
/**
7+
* given a few flights and a starting point - print if you can connect them all together into a journey
8+
* <p>
9+
* a Flight is just an object that has a Starting point and an Ending point
10+
*/
11+
12+
/**
13+
* Dynamic programming tips:
14+
*
15+
* Can I construct a mini problem with a solution
16+
* can I verify if that solution is valid or not
17+
* Can I say when the full problem is solved
18+
*/
19+
20+
// in this case:
21+
// mini problem is just using less flights
22+
// is_valid would check if I am at an airport that I can't fly off from and there are more flights I have yet to board
23+
// full problem is solved when I have used all flights
24+
public class FlightItinerary {
25+
26+
27+
public static void addFlights(String startPoint, HashSet<Flight> flights, Stack<Flight> itinerary) {
28+
if (flights.isEmpty()) {
29+
//yey we have made a journey with everything
30+
return;
31+
}
32+
33+
HashSet<Flight> expensiveSolution = new HashSet<>(flights);
34+
35+
for (Flight i : flights) {
36+
if (startPoint.equals(i.start)) {
37+
itinerary.push(i);
38+
expensiveSolution.remove(i);
39+
40+
//if valid so far try adding the next flight
41+
if (isValid(i.end, expensiveSolution)) {
42+
addFlights(i.end, expensiveSolution, itinerary);
43+
} else {
44+
45+
itinerary.pop();
46+
expensiveSolution.add(i);
47+
}
48+
}
49+
}
50+
}
51+
52+
public static boolean isValid(String startPoint, HashSet<Flight> flights) {
53+
if (flights.isEmpty())
54+
return true;
55+
for (Flight i : flights) {
56+
if (startPoint.equals(i.start)) {
57+
return true;
58+
}
59+
}
60+
return false;
61+
}
62+
63+
public static void main(String[] args) {
64+
65+
Flight f1 = new Flight("HNL", "AKL");
66+
Flight f2 = new Flight("YUL", "ORD");
67+
Flight f3 = new Flight("ORD", "SFO");
68+
Flight f4 = new Flight("SFO", "HNL");
69+
70+
71+
Flight f5 = new Flight("AKL", "ORD");
72+
Flight f6 = new Flight("ORD", "YUL");
73+
74+
String startingAirport = "YUL";
75+
76+
HashSet<Flight> flights = new HashSet<>();
77+
Stack<Flight> itinerary = new Stack<>();
78+
79+
flights.add(f1);
80+
flights.add(f2);
81+
flights.add(f3);
82+
flights.add(f4);
83+
84+
85+
flights.add(f5);
86+
flights.add(f6);
87+
88+
addFlights("YUL", flights, itinerary);
89+
90+
itinerary.stream().forEach(e -> System.out.print(e.start + " -> " + e.end + " "));
91+
}
92+
93+
private static class Flight {
94+
String start;
95+
String end;
96+
97+
public Flight(String start, String end) {
98+
this.start = start;
99+
this.end = end;
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)
0