Skip to content

Commit d859eb8

Browse files
committed
Add number of function evaluations for Floyd
1 parent ab4cbf9 commit d859eb8

1 file changed

Lines changed: 316 additions & 2 deletions

File tree

source_code/cycles.ipynb

Lines changed: 316 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
},
238238
{
239239
"cell_type": "code",
240-
"execution_count": 6,
240+
"execution_count": 10,
241241
"id": "92990251-8f03-46cb-9206-e71274ec87ba",
242242
"metadata": {},
243243
"outputs": [],
@@ -317,7 +317,7 @@
317317
},
318318
{
319319
"cell_type": "code",
320-
"execution_count": 9,
320+
"execution_count": 4,
321321
"id": "aa224337-7847-41da-8859-d285c994c528",
322322
"metadata": {},
323323
"outputs": [],
@@ -768,6 +768,320 @@
768768
"Experiments with still larger values of the offset and cycle lengths will convince you that the memory consumption of the dictionary is so large that the Jupyter kernel crashes, while Floyd's algorithm still finds the correct result."
769769
]
770770
},
771+
{
772+
"cell_type": "markdown",
773+
"id": "71569a31-3a3e-4bfb-b9e9-a80007f0b679",
774+
"metadata": {},
775+
"source": [
776+
"# Function evaluations"
777+
]
778+
},
779+
{
780+
"cell_type": "markdown",
781+
"id": "be9d4358-615b-43de-9d7f-448e0a38bcb7",
782+
"metadata": {},
783+
"source": [
784+
"It is clear that Floyd's algorithm trades memory usage for function evaluations. Both the naive and the dictionary method will evaluate the function for offset and cycle length. To check how many times Floyd's algorithm evaluates the function, you can create a class with a `__call__` method. An instance of that class can be called as if it were a function, but it can do bookkeeping as well."
785+
]
786+
},
787+
{
788+
"cell_type": "code",
789+
"execution_count": 6,
790+
"id": "b4e85177-119d-41aa-a26b-df0ba634183b",
791+
"metadata": {},
792+
"outputs": [],
793+
"source": [
794+
"class CountFunction:\n",
795+
"\n",
796+
" def __init__(self, offset, cycle_length):\n",
797+
" self._offset = offset\n",
798+
" self._cycle_length = cycle_length\n",
799+
" self._evaluations = 0\n",
800+
"\n",
801+
" def __call__(self, n):\n",
802+
" self._evaluations += 1\n",
803+
" if n < self._offset - 1:\n",
804+
" return n + 1\n",
805+
" return self._offset + ((n + 1 - self._offset) % self._cycle_length)\n",
806+
"\n",
807+
" @property\n",
808+
" def evaluations(self):\n",
809+
" return self._evaluations\n",
810+
"\n",
811+
" def reset(self):\n",
812+
" self._evaluations = 0"
813+
]
814+
},
815+
{
816+
"cell_type": "code",
817+
"execution_count": 7,
818+
"id": "14a13658-fb42-4e54-9ece-99945d6249b5",
819+
"metadata": {},
820+
"outputs": [],
821+
"source": [
822+
"f = CountFunction(100, 213)"
823+
]
824+
},
825+
{
826+
"cell_type": "code",
827+
"execution_count": 8,
828+
"id": "c5a2b685-3914-472c-8861-8e759143ea68",
829+
"metadata": {},
830+
"outputs": [
831+
{
832+
"data": {
833+
"text/plain": [
834+
"(100, 213)"
835+
]
836+
},
837+
"execution_count": 8,
838+
"metadata": {},
839+
"output_type": "execute_result"
840+
}
841+
],
842+
"source": [
843+
"find_cycles_floyd(f, 0, 0)"
844+
]
845+
},
846+
{
847+
"cell_type": "code",
848+
"execution_count": 9,
849+
"id": "e7e8fbfa-8c56-4d0d-9c89-03602b624c12",
850+
"metadata": {
851+
"scrolled": true
852+
},
853+
"outputs": [
854+
{
855+
"data": {
856+
"text/plain": [
857+
"1051"
858+
]
859+
},
860+
"execution_count": 9,
861+
"metadata": {},
862+
"output_type": "execute_result"
863+
}
864+
],
865+
"source": [
866+
"f.evaluations"
867+
]
868+
},
869+
{
870+
"cell_type": "code",
871+
"execution_count": 11,
872+
"id": "cce24ae6-8f22-4632-8ba5-04305c3a3dd8",
873+
"metadata": {},
874+
"outputs": [],
875+
"source": [
876+
"f.reset()"
877+
]
878+
},
879+
{
880+
"cell_type": "code",
881+
"execution_count": 12,
882+
"id": "235f5234-ee85-4680-85e9-fac1993a751c",
883+
"metadata": {},
884+
"outputs": [
885+
{
886+
"data": {
887+
"text/plain": [
888+
"(100, 213)"
889+
]
890+
},
891+
"execution_count": 12,
892+
"metadata": {},
893+
"output_type": "execute_result"
894+
}
895+
],
896+
"source": [
897+
"find_cycles_dict(f, 0, 500)"
898+
]
899+
},
900+
{
901+
"cell_type": "code",
902+
"execution_count": 13,
903+
"id": "426ae31e-c22e-4ce2-8bf6-1dd035923f87",
904+
"metadata": {},
905+
"outputs": [
906+
{
907+
"data": {
908+
"text/plain": [
909+
"313"
910+
]
911+
},
912+
"execution_count": 13,
913+
"metadata": {},
914+
"output_type": "execute_result"
915+
}
916+
],
917+
"source": [
918+
"f.evaluations"
919+
]
920+
},
921+
{
922+
"cell_type": "markdown",
923+
"id": "47b936b2-db14-42bb-8f1c-a610ac1514ac",
924+
"metadata": {},
925+
"source": [
926+
"It is indeed obvious that the number of times the function is evaluated when using Floyd's algorithm is significantly higher."
927+
]
928+
},
929+
{
930+
"cell_type": "code",
931+
"execution_count": 14,
932+
"id": "c2d8c18f-6f57-4a89-881b-bda95d9d2cd3",
933+
"metadata": {},
934+
"outputs": [],
935+
"source": [
936+
"f = CountFunction(1023, 4095)"
937+
]
938+
},
939+
{
940+
"cell_type": "code",
941+
"execution_count": 15,
942+
"id": "b2d0db59-9887-4900-8603-3b516a88e9cf",
943+
"metadata": {},
944+
"outputs": [
945+
{
946+
"data": {
947+
"text/plain": [
948+
"(1023, 4095)"
949+
]
950+
},
951+
"execution_count": 15,
952+
"metadata": {},
953+
"output_type": "execute_result"
954+
}
955+
],
956+
"source": [
957+
"find_cycles_floyd(f, 0, 0)"
958+
]
959+
},
960+
{
961+
"cell_type": "code",
962+
"execution_count": 16,
963+
"id": "b66e8003-70ca-446e-8bad-a6d49224689b",
964+
"metadata": {},
965+
"outputs": [
966+
{
967+
"data": {
968+
"text/plain": [
969+
"18425"
970+
]
971+
},
972+
"execution_count": 16,
973+
"metadata": {},
974+
"output_type": "execute_result"
975+
}
976+
],
977+
"source": [
978+
"f.evaluations"
979+
]
980+
},
981+
{
982+
"cell_type": "code",
983+
"execution_count": 17,
984+
"id": "27067bd1-7bad-46d9-9d17-adaf8a8f8c08",
985+
"metadata": {},
986+
"outputs": [],
987+
"source": [
988+
"f = CountFunction(1023, 3)"
989+
]
990+
},
991+
{
992+
"cell_type": "code",
993+
"execution_count": 18,
994+
"id": "f66b25b9-141f-43d0-abdc-a295432b973d",
995+
"metadata": {},
996+
"outputs": [
997+
{
998+
"data": {
999+
"text/plain": [
1000+
"(1023, 3)"
1001+
]
1002+
},
1003+
"execution_count": 18,
1004+
"metadata": {},
1005+
"output_type": "execute_result"
1006+
}
1007+
],
1008+
"source": [
1009+
"find_cycles_floyd(f, 0, 0)"
1010+
]
1011+
},
1012+
{
1013+
"cell_type": "code",
1014+
"execution_count": 19,
1015+
"id": "c5bfce04-58bd-4ff6-bb77-b0ae49ef4ecb",
1016+
"metadata": {},
1017+
"outputs": [
1018+
{
1019+
"data": {
1020+
"text/plain": [
1021+
"5117"
1022+
]
1023+
},
1024+
"execution_count": 19,
1025+
"metadata": {},
1026+
"output_type": "execute_result"
1027+
}
1028+
],
1029+
"source": [
1030+
"f.evaluations"
1031+
]
1032+
},
1033+
{
1034+
"cell_type": "code",
1035+
"execution_count": 20,
1036+
"id": "88a42d4b-02f9-47f8-a8f1-5c60ddc10c48",
1037+
"metadata": {},
1038+
"outputs": [],
1039+
"source": [
1040+
"f = CountFunction(10, 3)"
1041+
]
1042+
},
1043+
{
1044+
"cell_type": "code",
1045+
"execution_count": 21,
1046+
"id": "100f0e39-efed-4db4-b0c1-b2698305350b",
1047+
"metadata": {},
1048+
"outputs": [
1049+
{
1050+
"data": {
1051+
"text/plain": [
1052+
"(10, 3)"
1053+
]
1054+
},
1055+
"execution_count": 21,
1056+
"metadata": {},
1057+
"output_type": "execute_result"
1058+
}
1059+
],
1060+
"source": [
1061+
"find_cycles_floyd(f, 0, 0)"
1062+
]
1063+
},
1064+
{
1065+
"cell_type": "code",
1066+
"execution_count": 22,
1067+
"id": "5a7525f4-a60e-4557-8971-9bc5c6e58b3d",
1068+
"metadata": {},
1069+
"outputs": [
1070+
{
1071+
"data": {
1072+
"text/plain": [
1073+
"58"
1074+
]
1075+
},
1076+
"execution_count": 22,
1077+
"metadata": {},
1078+
"output_type": "execute_result"
1079+
}
1080+
],
1081+
"source": [
1082+
"f.evaluations"
1083+
]
1084+
},
7711085
{
7721086
"cell_type": "markdown",
7731087
"id": "e8131595-c098-44ea-aa68-e3f08440cb1f",

0 commit comments

Comments
 (0)