Project: Dijkstra and a search tree
The idea: a 400×400 grid with random weights (fixed seed 42): the shortest path from corner to corner with Dijkstra and a min-heap. Then a binary search tree with 200 thousand insertions, its height, and freeing all of it.
The program
Section titled “The program”# خوارزميات وهياكل بيانات: أقصر مسار (Dijkstra بكومة ثنائية) على شبكة 400×400، وشجرة بحث ثنائية بمؤشراتاستورد عشوائياستورد نظام
ثابت ع_ص = 400ثابت لا_نهاية = 1000000000
هيكل عنصر_كومة: مسافة عدد64 عقدة عدد64
# ---- كومة صغرى فوق قائمة ----داله ادفع(ك قائمة[عنصر_كومة], ع عنصر_كومة) فراغ: اضف(ك, ع) ف = طول(ك) - 1 طالما ف > 0: اب = (ف - 1) / 2 اذا ك[اب].مسافة <= ك[ف].مسافة: توقف مؤقت = ك[اب] ك[اب] = ك[ف] ك[ف] = مؤقت ف = اب
داله اسحب(ك قائمة[عنصر_كومة]) عنصر_كومة: اعلى = ك[0] اخير = ك[طول(ك) - 1] احذف(ك, طول(ك) - 1) اذا طول(ك) == 0: ارجع اعلى ك[0] = اخير ف = 0 ن = طول(ك) طالما صواب: ي = 2 * ف + 1 اذا ي >= ن: توقف اذا ي + 1 < ن و ك[ي + 1].مسافة < ك[ي].مسافة: ي += 1 اذا ك[ف].مسافة <= ك[ي].مسافة: توقف مؤقت = ك[ف] ك[ف] = ك[ي] ك[ي] = مؤقت ف = ي ارجع اعلى
داله اقصر_مسار(وزن قائمة[عدد64]) عدد64: ن = ع_ص * ع_ص بعد قائمة[عدد64] = [] خاتمه حرر(بعد) لكل ع في مدى(0, ن): اضف(بعد, لا_نهاية) كومة قائمة[عنصر_كومة] = [] خاتمه حرر(كومة) بعد[0] = وزن[0] ادفع(كومة, عنصر_كومة(وزن[0], 0)) طالما طول(كومة) > 0: ا = اسحب(كومة) اذا ا.مسافة > بعد[ا.عقدة]: تجاوز ص = ا.عقدة / ع_ص س = ا.عقدة % ع_ص جيران مصفوفة[عدد64, 4] = [-1, -1, -1, -1] اذا ص > 0: جيران[0] = ا.عقدة - ع_ص اذا ص < ع_ص - 1: جيران[1] = ا.عقدة + ع_ص اذا س > 0: جيران[2] = ا.عقدة - 1 اذا س < ع_ص - 1: جيران[3] = ا.عقدة + 1 لكل ج في جيران: اذا ج < 0: تجاوز جديد_بعد = ا.مسافة + وزن[ج] اذا جديد_بعد < بعد[ج]: بعد[ج] = جديد_بعد ادفع(كومة, عنصر_كومة(جديد_بعد, ج)) ارجع بعد[ن - 1]
# ---- شجرة بحث ثنائية ----هيكل عقدة_شجرة: مفتاح عدد64 يسار مؤشر[عقدة_شجرة] يمين مؤشر[عقدة_شجرة]
داله ادرج_في(جذر مؤشر[عقدة_شجرة], مفتاح عدد64) مؤشر[عقدة_شجرة]: اذا جذر == لاشيء: ارجع جديد(عقدة_شجرة(مفتاح, لاشيء, لاشيء)) ع = محتوى(جذر) اذا مفتاح < ع.مفتاح: محتوى(جذر).يسار = ادرج_في(ع.يسار, مفتاح) غير ذلك اذا مفتاح > ع.مفتاح: محتوى(جذر).يمين = ادرج_في(ع.يمين, مفتاح) ارجع جذر
داله ارتفاع(جذر مؤشر[عقدة_شجرة]) عدد64: اذا جذر == لاشيء: ارجع 0 ي = ارتفاع(محتوى(جذر).يسار) م = ارتفاع(محتوى(جذر).يمين) اذا ي > م: ارجع ي + 1 ارجع م + 1
داله احص(جذر مؤشر[عقدة_شجرة]) عدد64: اذا جذر == لاشيء: ارجع 0 ارجع 1 + احص(محتوى(جذر).يسار) + احص(محتوى(جذر).يمين)
داله احرق(جذر مؤشر[عقدة_شجرة]) فراغ: # تحرير عميق يدوي: لا تحرير يتبع المؤشرات اذا جذر == لاشيء: ارجع احرق(محتوى(جذر).يسار) احرق(محتوى(جذر).يمين) حرر(جذر)
عشوائي.بذرة(42)وزن قائمة[عدد64] = []لكل ع في مدى(0, ع_ص * ع_ص): اضف(وزن, عشوائي.صحيح(1, 9))ت0 = نظام.مؤقت()اطبع("أقصر مسار: ", اقصر_مسار(وزن))ت1 = نظام.مؤقت()جذر مؤشر[عقدة_شجرة] = لاشيءلكل ع في مدى(0, 200000): جذر = ادرج_في(جذر, عشوائي.صحيح(0, 1000000000))اطبع("عقد الشجرة: ", احص(جذر), " ارتفاعها: ", ارتفاع(جذر))احرق(جذر)ت2 = نظام.مؤقت()اطبع("زمن المسار ", ت1 - ت0 > 0.0, "، زمن الشجرة ", ت2 - ت1 > 0.0)Also built with meem build when the site was built: a static file of 46.3 kB, run on its own, and its output matched the output above.
Step by step
Section titled “Step by step”- A min-heap on top of a list:
ادفع(push) adds at the end then raises the element until it settles, andاسحب(pop) takes the smallest and moves the last one down in its place. The elements areعنصر_كومةstructs, by value. - Dijkstra: a list
بعدof distances, a heap of candidates, and the four neighbours in a fixedمصفوفة[عدد64, 4](-1 for a missing one). - The tree: a node holds
مؤشر[عقدة_شجرة]pointers to the left and right.جديد(...)(new) allocates a node,محتوى(م)(content) reaches it, andلاشيء(nothing) for an empty one. - Deep freeing:
حررdoes not follow pointers, soاحرقwalks the tree and frees each node by hand. - Time: it prints «صواب» (true), not the seconds, so that the output stays the same.
What was hard
Section titled “What was hard”- No generic types: the heap works for
عنصر_كومةonly; for another type the code is copied (or the list holds indices into another list). Generic types come after 1.0. - Deep freeing is manual: nothing frees the tree except
احرق. - The
--debug-memreport attributed a list’s growth to a line in the standard library (عشوائي.م), not to a line of the program.
What we learned
Section titled “What we learned”- Performance on a par with C: in the report, 0.27–0.31 s for the built file, against 0.29 s for a C version built with
gcc -O2, and a peak of 8.6 MB. - Pointers in Meem are simple and need no
خطر:جديد,محتوىandحرر, and a check forلاشيءbefore access.