You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, if you keep the input size constant, you can notice the difference between an efficient algorithm and a slow one. An excellent sorting algorithm is `mergesort` for instance, and inefficient algorithm for large inputs is `bubble sort` .
64
64
Organizing 1 million elements with merge sort takes 20 seconds while bubble sort takes 12 days, ouch!
65
65
The amazing thing is that both programs are measured on the same hardware with the same data!
<4> Test if the current change is a solution: reached the end of the string.
73
72
<5> Revert back the change (backtracking): Undo swap from step 2
74
73
75
-
As you can see, we iterate through each letter and swap with the following letters until we reach the end of the string. Then, we rollback the change and try another path.
74
+
As you can see, we iterate through each element and swap with the following letters until we reach the end of the string. Then, we roll back the change and try another path.
76
75
77
-
In the following tree, you can visualize how the backtracking algorithm is swaping the letters.
76
+
In the following tree, you can visualize how the backtracking algorithm is swapping the letters. We are taking the `art` as an example.
78
77
79
78
[graphviz, Words Permutations, svg]
80
79
....
@@ -116,12 +115,12 @@ digraph g {
116
115
....
117
116
118
117
.Legend:
119
-
- The [red]#red# words are the iterations added to the solution array.
118
+
- The asterisk (`*`) indicates `start` index.
120
119
- *Black* arrows indicate the `swap` operations.
121
120
- *Grey* arrows indicate the _backtracking_ operation (undo swap).
122
-
- The asterisk (`*`) indicates `start` index.
121
+
- The [red]#red# words are the iterations added to the solution array.
123
122
124
-
Most of backtracking algorihtms do something similar. What changes is the test function to determine if a current iteration is a solution or not.
123
+
Most of the backtracking algorithms do something similar. What changes is the test function or base case to determine if a current iteration is a solution or not.
Copy file name to clipboardExpand all lines: book/chapters/greedy-algorithms--knapsack-problem.adoc
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ const items = [
19
19
const maxWeight = 7;
20
20
----
21
21
22
-
So, we have four items that we can choose from. We can't take them all because the total weight is `13` and the maximum we can carry is `7`. We can't just take the first one because with value `1` because is not the best profit.
22
+
So, we have four items that we can choose from. We can't take them all because the total weight is `13` and the maximum we can carry is `7`. We can't just take the first one because with value `1` because it is not the best profit.
0 commit comments